home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_5.ZIP / WINSRC.ZIP / NUMBER1.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  6.8 KB  |  248 lines

  1. //    Zinc Interface Library - NUMBER1.CPP
  2. //    COPYRIGHT (C) 1990, 1991.  All Rights Reserved.
  3. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  4.  
  5. #include "ui_win.hpp"
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. static void StripDecimal(char *buffer)
  12. {
  13.     char *dpl = strchr(buffer, _countryInfo.co_desep[0]);
  14.     if (dpl)
  15.         memmove(dpl, dpl + 1, strlen(dpl));
  16. }
  17.  
  18. int UIW_NUMBER::ParseRange(char *buffer, int offset, char *minValue, char *maxValue)
  19. {
  20.     // Get the minimum value.
  21.     int position = 0;
  22.     while (buffer[offset] != '\0' &&
  23.         (buffer[offset] != '.' || buffer[offset+1] != '.') &&
  24.         buffer[offset] != '/')
  25.         minValue[position++] = buffer[offset++];
  26.     minValue[position] = '\0';
  27.  
  28.     // See if it is a standalone value.
  29.     if (buffer[offset] == '/' || buffer[offset] == '\0')
  30.     {
  31.         strcpy(maxValue, minValue);
  32.         return(++offset);
  33.     }
  34.  
  35.     // Increment the offset.
  36.     while (buffer[offset] != '\0' &&
  37.         buffer[offset] == '.')
  38.         offset++;
  39.  
  40.     // Get the maximum value.
  41.     position = 0;
  42.     while (buffer[offset] != '\0' &&
  43.         buffer[offset] != '/')
  44.         maxValue[position++] = buffer[offset++];
  45.     maxValue[position] = '\0';
  46.     if (decimal && decimal != 0xFF && type != NUM_FLOAT && type != NUM_DOUBLE)
  47.     {
  48.         StripDecimal(minValue);
  49.         StripDecimal(maxValue);
  50.     }
  51.  
  52.     // Return the offset.
  53.     return(++offset);
  54. }
  55.  
  56. void UIW_NUMBER::FormatNumericBuffer(char *buffer, UCHAR maximumLength)
  57. {
  58.     int i;
  59.     char decimalPoint = _countryInfo.co_desep[0];
  60.  
  61.     // Get the buffer length.
  62.     int bufferLength = (buffer) ? strlen(buffer) : 0;
  63.     if (bufferLength == 0)
  64.     {
  65.         strcpy(buffer, "0");
  66.         bufferLength++;
  67.     }
  68.  
  69.     // Replace '.' with country specific decimal separator..
  70.     ui_strrepc(buffer, '.', decimalPoint);
  71.  
  72.     // See if the buffer is free format.
  73.     if (decimal == 0xFF || type == NUM_FLOAT || type == NUM_DOUBLE)
  74.     {
  75.         if (bufferLength >= maximumLength)
  76.             bufferLength = maximumLength - 1;
  77.         buffer[bufferLength] = '\0';
  78.         return;
  79.     }
  80.  
  81.     // Remove the '-' sign.
  82.     int negativeValue = (buffer[0] == '-') ? TRUE : FALSE;
  83.     if (negativeValue)
  84.         memmove(&buffer[0], &buffer[1], bufferLength--);
  85.  
  86.     // Try to find a decimal point.
  87.     int position = 0;
  88.     while (position < bufferLength && buffer[position] != decimalPoint)
  89.         position++;
  90.  
  91.     // Insert the decimal point.
  92.     if (decimal == 0)
  93.          bufferLength = position;
  94.     else if (decimal != 0xFF && position == bufferLength)
  95.     {
  96.         // Make sure the buffer is long enough.
  97.         while (bufferLength <= decimal)
  98.         {
  99.             memmove(&buffer[1], &buffer[0], bufferLength++);
  100.             buffer[0] = '0';
  101.         }
  102.  
  103.         // Insert the decimal.
  104.         position = bufferLength - decimal;
  105.         memmove(&buffer[position+1], &buffer[position],    bufferLength - position);
  106.         buffer[position] = decimalPoint;
  107.         bufferLength++;
  108.     }
  109.     else if (decimal != 0xFF)
  110.     {
  111.         // Make sure the buffer is long enough.
  112.         for (i = 1; i <= decimal; i++)
  113.             if (position + i >= bufferLength)
  114.                 buffer[bufferLength++] = '0';
  115.  
  116.         // Get the new buffer length.
  117.         bufferLength = position + decimal + 1;
  118.     }
  119.  
  120.     // Insert the commas.
  121.     if (nmFlags & NMF_COMMAS)
  122.     {
  123.         for (i = position - 3; i > 0; i = i - 3)
  124.         {
  125.             memmove(&buffer[i+1], &buffer[i], bufferLength - i);
  126.             buffer[i] = _countryInfo.co_thsep[0];
  127.             bufferLength++;
  128.         }
  129.     }
  130.  
  131.     // Insert the currency symbol.
  132.     if (nmFlags & NMF_CURRENCY)
  133.     {
  134.         i = (_countryInfo.co_curr) ? strlen(_countryInfo.co_curr) : 0;
  135.         memmove(&buffer[i], &buffer[0], bufferLength);
  136.         memmove(&buffer[0], _countryInfo.co_curr, i);
  137.         bufferLength += i;
  138.     }
  139.  
  140.     // Insert the '%' sign.
  141.     if (nmFlags & NMF_PERCENT)
  142.         buffer[bufferLength++] = '%';
  143.  
  144.     // Insert the '-', '(' and ')', or ' ' characters (for negative value).
  145.     if (negativeValue && (nmFlags & NMF_CREDIT))
  146.     {
  147.         memmove(&buffer[1], &buffer[0], bufferLength++);
  148.         buffer[0] = '(';
  149.         buffer[bufferLength++] = ')';
  150.     }
  151.     else if (negativeValue)
  152.     {
  153.         memmove(&buffer[1], &buffer[0], bufferLength++);
  154.         buffer[0] = '-';
  155.         if (buffer[bufferLength-1] != '%')
  156.             buffer[bufferLength++] = ' ';
  157.     }
  158.     else if (buffer[bufferLength-1] != '%')
  159.         buffer[bufferLength++] = ' ';
  160.  
  161.     // Finish the buffer.
  162.     if (bufferLength > maximumLength)
  163.     {
  164.         buffer[maximumLength-1] = (buffer[bufferLength-1] == ')') ? ')' : ' ';
  165.         bufferLength = maximumLength;
  166.     }
  167.     buffer[bufferLength] = '\0';
  168.     if (bufferLength && buffer[bufferLength - 1] == ' ')
  169.         buffer[bufferLength - 1] = '\0';
  170. }
  171.  
  172. int UIW_NUMBER::RangeError(char *a_range)
  173. {
  174.     // Declaration of local variables.
  175.     char formattedMin[40];
  176.     char formattedMax[40];
  177.     char formattedString[256];
  178.     char tempString[80];
  179.     static char throughString[] = "through";
  180.     static char orString[] = "or";
  181.     int position;
  182.     int offset = 0;
  183.     int count = 0;
  184.  
  185.     // Set up the initial variables.
  186.     int rangeLength = (a_range) ? strlen(a_range) : 0;
  187.     char separator = ',';
  188.     int cursorColumn;
  189.     Expand(formattedString, &cursorColumn);
  190.     strcat(formattedString, " is not valid.  The value must be in the range ");
  191.     while (offset < rangeLength)
  192.     {
  193.         // Format the current minimum and maximum values.
  194.         if (count == 1)
  195.         {
  196.             if (!strcmp(formattedMin, formattedMax))
  197.                 strcpy(tempString, formattedMin);
  198.             else
  199.                 sprintf(tempString, "%s %s %s", formattedMin, throughString,
  200.                     formattedMax);
  201.             strcat(formattedString, tempString);
  202.         }
  203.         else if (count != 0)
  204.         {
  205.             if (!strcmp(formattedMin, formattedMax))
  206.                 sprintf(tempString, "%c %s", separator, formattedMin);
  207.             else
  208.                 sprintf(tempString, "%c %s %s %s", separator,
  209.                     formattedMin, throughString, formattedMax);
  210.             strcat(formattedString, tempString);
  211.         }
  212.  
  213.         // Get a new set of minimum and maximum values.
  214.         count++;
  215.         offset = ParseRange(a_range, offset, formattedMin, formattedMax);
  216.         FormatNumericBuffer(formattedMin, sizeof(formattedMin));
  217.         FormatNumericBuffer(formattedMax, sizeof(formattedMax));
  218.     }
  219.  
  220.     // Append the final minimum and maximum values on the string.
  221.     if (count > 2 && !strcmp(formattedMin, formattedMax))
  222.         sprintf(tempString, "%c %s %s.", separator, orString, formattedMin);
  223.     else if (count > 2)
  224.         sprintf(tempString, "%c %s %s %s %s.", separator, orString,
  225.             formattedMin, throughString, formattedMax);
  226.     else if (count == 2 && !strcmp(formattedMin, formattedMax))
  227.         sprintf(tempString, " %s %s.", orString, formattedMin);
  228.     else if (count == 2)
  229.         sprintf(tempString, " %s %s %s %s.", orString, formattedMin,
  230.             throughString, formattedMax);
  231.     else if (count > 0)
  232.         sprintf(tempString, "%s %s %s.", formattedMin, throughString,
  233.             formattedMax);
  234.     else
  235.     {
  236.         position = 0;        
  237.         while (formattedString[position] != '\0' &&
  238.             formattedString[position] != '.')
  239.             position++;
  240.         formattedString[++position] = '\0';
  241.     }
  242.     strcat(formattedString, tempString);
  243.     _errorSystem->ReportError(windowManager,
  244.         woFlags & (WOF_NO_UNANSWERED | WOF_NO_INVALID), "%s", formattedString);
  245.     return (TRUE);
  246. }
  247.  
  248.