home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - NUMBER1.CPP
- // COPYRIGHT (C) 1990, 1991. All Rights Reserved.
- // Zinc Software Incorporated. Pleasant Grove, Utah USA
-
- #include "ui_win.hpp"
- #include <ctype.h>
- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- static void StripDecimal(char *buffer)
- {
- char *dpl = strchr(buffer, _countryInfo.co_desep[0]);
- if (dpl)
- memmove(dpl, dpl + 1, strlen(dpl));
- }
-
- int UIW_NUMBER::ParseRange(char *buffer, int offset, char *minValue, char *maxValue)
- {
- /* Get the minimum value */
- int position = 0;
- while (buffer[offset] != '\0' &&
- (buffer[offset] != '.' || buffer[offset+1] != '.') &&
- buffer[offset] != '/')
- minValue[position++] = buffer[offset++];
- minValue[position] = '\0';
-
- /* See if it is a standalone value */
- if (buffer[offset] == '/' || buffer[offset] == '\0')
- {
- strcpy(maxValue, minValue);
- return(++offset);
- }
-
- /* Increment the offset */
- while (buffer[offset] != '\0' &&
- buffer[offset] == '.')
- offset++;
-
- /* Get the maximum value */
- position = 0;
- while (buffer[offset] != '\0' &&
- buffer[offset] != '/')
- maxValue[position++] = buffer[offset++];
- maxValue[position] = '\0';
- if (decimal && decimal != 0xFF && type != NUM_FLOAT && type != NUM_DOUBLE)
- {
- StripDecimal(minValue);
- StripDecimal(maxValue);
- }
-
- /* Return the offset */
- return(++offset);
- }
-
- void UIW_NUMBER::FormatNumericBuffer(char *buffer, UCHAR maximumLength)
- {
- int i;
- char decimalPoint = _countryInfo.co_desep[0];
-
- /* Get the buffer length */
- int bufferLength = (buffer) ? strlen(buffer) : 0;
- if (bufferLength == 0)
- {
- strcpy(buffer, "0");
- bufferLength++;
- }
-
- /* Replace '.' with country specific decimal separator. */
- ui_strrepc(buffer, '.', decimalPoint);
-
- /* See if the buffer is free format */
- if (decimal == 0xFF || type == NUM_FLOAT || type == NUM_DOUBLE)
- {
- if (bufferLength >= maximumLength)
- bufferLength = maximumLength - 1;
- buffer[bufferLength] = '\0';
- return;
- }
-
- /* Remove the '-' sign */
- int negativeValue = (buffer[0] == '-') ? TRUE : FALSE;
- if (negativeValue)
- memmove(&buffer[0], &buffer[1], bufferLength--);
-
- /* Try to find a decimal point */
- int position = 0;
- while (position < bufferLength && buffer[position] != decimalPoint)
- position++;
-
- /* Insert the decimal point */
- if (decimal == 0)
- bufferLength = position;
- else if (decimal != 0xFF && position == bufferLength)
- {
- /* Make sure the buffer is long enough */
- while (bufferLength <= decimal)
- {
- memmove(&buffer[1], &buffer[0], bufferLength++);
- buffer[0] = '0';
- }
-
- /* Insert the decimal */
- position = bufferLength - decimal;
- memmove(&buffer[position+1], &buffer[position], bufferLength - position);
- buffer[position] = decimalPoint;
- bufferLength++;
- }
- else if (decimal != 0xFF)
- {
- /* Make sure the buffer is long enough */
- for (i = 1; i <= decimal; i++)
- if (position + i >= bufferLength)
- buffer[bufferLength++] = '0';
-
- /* Get the new buffer length */
- bufferLength = position + decimal + 1;
- }
-
- /* Insert the commas */
- if (nmFlags & NMF_COMMAS)
- {
- for (i = position - 3; i > 0; i = i - 3)
- {
- memmove(&buffer[i+1], &buffer[i], bufferLength - i);
- buffer[i] = _countryInfo.co_thsep[0];
- bufferLength++;
- }
- }
-
- /* Insert the currency symbol */
- if (nmFlags & NMF_CURRENCY)
- {
- i = (_countryInfo.co_curr) ? strlen(_countryInfo.co_curr) : 0;
- memmove(&buffer[i], &buffer[0], bufferLength);
- memmove(&buffer[0], _countryInfo.co_curr, i);
- bufferLength += i;
- }
-
- /* Insert the '%' sign */
- if (nmFlags & NMF_PERCENT)
- buffer[bufferLength++] = '%';
-
- /* Insert the '-', '(' and ')', or ' ' characters (for negative value) */
- if (negativeValue && (nmFlags & NMF_CREDIT))
- {
- memmove(&buffer[1], &buffer[0], bufferLength++);
- buffer[0] = '(';
- buffer[bufferLength++] = ')';
- }
- else if (negativeValue)
- {
- memmove(&buffer[1], &buffer[0], bufferLength++);
- buffer[0] = '-';
- if (buffer[bufferLength-1] != '%')
- buffer[bufferLength++] = ' ';
- }
- else if (buffer[bufferLength-1] != '%')
- buffer[bufferLength++] = ' ';
-
- /* Finish the buffer */
- if (bufferLength > maximumLength)
- {
- buffer[maximumLength-1] = (buffer[bufferLength-1] == ')') ? ')' : ' ';
- bufferLength = maximumLength;
- }
- buffer[bufferLength] = '\0';
- if (bufferLength && buffer[bufferLength - 1] == ' ')
- buffer[bufferLength - 1] = '\0';
- }
-
- int UIW_NUMBER::RangeError(char *a_range)
- {
- /* Declaration of local variables */
- char formattedMin[40];
- char formattedMax[40];
- char formattedString[256];
- char tempString[80];
- static char throughString[] = "through";
- static char orString[] = "or";
- int position;
- int offset = 0;
- int count = 0;
-
- /* Set up the initial variables */
- int rangeLength = (a_range) ? strlen(a_range) : 0;
- char separator = ',';
- int cursorColumn;
- Expand(formattedString, &cursorColumn);
- strcat(formattedString, " is not valid. The value must be in the range ");
- while (offset < rangeLength)
- {
- /* Format the current minimum and maximum values */
- if (count == 1)
- {
- if (!strcmp(formattedMin, formattedMax))
- strcpy(tempString, formattedMin);
- else
- sprintf(tempString, "%s %s %s", formattedMin, throughString,
- formattedMax);
- strcat(formattedString, tempString);
- }
- else if (count != 0)
- {
- if (!strcmp(formattedMin, formattedMax))
- sprintf(tempString, "%c %s", separator, formattedMin);
- else
- sprintf(tempString, "%c %s %s %s", separator,
- formattedMin, throughString, formattedMax);
- strcat(formattedString, tempString);
- }
-
- /* Get a new set of minimum and maximum values */
- count++;
- offset = ParseRange(a_range, offset, formattedMin, formattedMax);
- FormatNumericBuffer(formattedMin, sizeof(formattedMin));
- FormatNumericBuffer(formattedMax, sizeof(formattedMax));
- }
-
- /* Append the final minimum and maximum values on the string */
- if (count > 2 && !strcmp(formattedMin, formattedMax))
- sprintf(tempString, "%c %s %s.", separator, orString, formattedMin);
- else if (count > 2)
- sprintf(tempString, "%c %s %s %s %s.", separator, orString,
- formattedMin, throughString, formattedMax);
- else if (count == 2 && !strcmp(formattedMin, formattedMax))
- sprintf(tempString, " %s %s.", orString, formattedMin);
- else if (count == 2)
- sprintf(tempString, " %s %s %s %s.", orString, formattedMin,
- throughString, formattedMax);
- else if (count > 0)
- sprintf(tempString, "%s %s %s.", formattedMin, throughString,
- formattedMax);
- else
- {
- position = 0;
- while (formattedString[position] != '\0' &&
- formattedString[position] != '.')
- position++;
- formattedString[++position] = '\0';
- }
- strcat(formattedString, tempString);
- _errorSystem->ReportError(windowManager,
- woFlags & (WOF_NO_UNANSWERED | WOF_NO_INVALID), "%s", formattedString);
- return (TRUE);
- }
-
-