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