home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - INTEGER.CPP
- // COPYRIGHT (C) 1990, 1991. All Rights Reserved.
- // Zinc Software Incorporated. Pleasant Grove, Utah USA
-
- #include "ui_win.hpp"
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- UIW_NUMBER::UIW_NUMBER(int left, int top, int width, char *a_value,
- char *a_range, USHORT flags, USHORT woFlags,
- int (*validate)(void *object, int ccode)) :
- UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS)
- {
- type = NUM_CHAR;
- Constructor(a_value, width, flags, a_range,
- UIW_NUMBER::WholeToAscii, UIW_NUMBER::AsciiToWhole,
- UIW_NUMBER::ValidWholeNumber, validate);
- }
-
- UIW_NUMBER::UIW_NUMBER(int left, int top, int width, unsigned char *a_value,
- char *a_range, USHORT flags, USHORT woFlags,
- int (*validate)(void *object, int ccode)) :
- UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS)
- {
- type = NUM_CHAR;
- Constructor(a_value, width, flags | NMF_UNSIGNED, a_range,
- UIW_NUMBER::WholeToAscii, UIW_NUMBER::AsciiToWhole,
- UIW_NUMBER::ValidWholeNumber, validate);
- }
-
- UIW_NUMBER::UIW_NUMBER(int left, int top, int width, int *a_value,
- char *a_range, USHORT flags, USHORT woFlags,
- int (*validate)(void *object, int ccode)) :
- UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS)
- {
- type = NUM_SHORT;
- Constructor(a_value, width, flags, a_range,
- UIW_NUMBER::WholeToAscii, UIW_NUMBER::AsciiToWhole,
- UIW_NUMBER::ValidWholeNumber, validate);
- }
-
- UIW_NUMBER::UIW_NUMBER(int left, int top, int width, unsigned int *a_value,
- char *a_range, USHORT flags, USHORT woFlags,
- int (*validate)(void *object, int ccode)) :
- UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS)
- {
- type = NUM_SHORT;
- Constructor(a_value, width, flags | NMF_UNSIGNED, a_range,
- UIW_NUMBER::WholeToAscii, UIW_NUMBER::AsciiToWhole,
- UIW_NUMBER::ValidWholeNumber, validate);
- }
-
- UIW_NUMBER::UIW_NUMBER(int left, int top, int width, short *a_value,
- char *a_range, USHORT flags, USHORT woFlags,
- int (*validate)(void *object, int ccode)) :
- UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS)
- {
- type = NUM_SHORT;
- Constructor(a_value, width, flags, a_range,
- UIW_NUMBER::WholeToAscii, UIW_NUMBER::AsciiToWhole,
- UIW_NUMBER::ValidWholeNumber, validate);
- }
-
- UIW_NUMBER::UIW_NUMBER(int left, int top, int width, unsigned short *a_value,
- char *a_range, USHORT flags, USHORT woFlags,
- int (*validate)(void *object, int ccode)) :
- UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS)
- {
- type = NUM_SHORT;
- Constructor(a_value, width, flags | NMF_UNSIGNED, a_range,
- UIW_NUMBER::WholeToAscii, UIW_NUMBER::AsciiToWhole,
- UIW_NUMBER::ValidWholeNumber, validate);
- }
-
- UIW_NUMBER::UIW_NUMBER(int left, int top, int width, long *a_value,
- char *a_range, USHORT flags, USHORT woFlags,
- int (*validate)(void *object, int ccode)) :
- UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS)
- {
- type = NUM_LONG;
- Constructor(a_value, width, flags, a_range,
- UIW_NUMBER::WholeToAscii, UIW_NUMBER::AsciiToWhole,
- UIW_NUMBER::ValidWholeNumber, validate);
- }
-
- UIW_NUMBER::UIW_NUMBER(int left, int top, int width, unsigned long *a_value,
- char *a_range, USHORT flags, USHORT woFlags,
- int (*validate)(void *object, int ccode)) :
- UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS)
- {
- type = NUM_LONG;
- Constructor(a_value, width, flags | NMF_UNSIGNED, a_range,
- UIW_NUMBER::WholeToAscii, UIW_NUMBER::AsciiToWhole,
- UIW_NUMBER::ValidWholeNumber, validate);
- }
-
- void UIW_NUMBER::WholeToAscii()
- {
- switch (type)
- {
- case NUM_CHAR:
- if (FlagSet(woStatus, WOS_UNANSWERED))
- *((char *)value) = 0;
- sprintf(state.text, "%d", nmFlags & NMF_UNSIGNED ?
- *((unsigned char *)value) : *((char *)value));
- break;
-
- case NUM_SHORT:
- if (FlagSet(woStatus, WOS_UNANSWERED))
- *((short *)value) = 0;
- sprintf(state.text, nmFlags & NMF_UNSIGNED ? "%u" : "%d",
- nmFlags & NMF_UNSIGNED ? *((unsigned short *)value) :
- *((short *)value));
- break;
-
- case NUM_LONG:
- if (FlagSet(woStatus, WOS_UNANSWERED))
- *((long *)value) = 0;
- sprintf(state.text, nmFlags & NMF_UNSIGNED ? "%lu" : "%ld",
- nmFlags & NMF_UNSIGNED ? *((unsigned long *)value) :
- *((long *)value));
- break;
- }
- }
-
- void UIW_NUMBER::AsciiToWhole()
- {
- switch (type)
- {
- case NUM_CHAR:
- if (FlagSet(nmFlags, NMF_UNSIGNED))
- *((unsigned char *)value) = atoi(state.text);
- else
- {
- *((char *)value) = atoi(state.text);
- if (state.isNegative)
- *((char *)value) = - *((char *)value);
- }
- break;
-
- case NUM_SHORT:
- if (FlagSet(nmFlags, NMF_UNSIGNED))
- *((unsigned short *)value) = (unsigned short) atol(state.text);
- else
- {
- *((short *)value) = atoi(state.text);
- if (state.isNegative)
- *((short *)value) = - *((short *)value);
- }
- break;
-
- case NUM_LONG:
- if (FlagSet(nmFlags, NMF_UNSIGNED))
- *((unsigned long *)value) = strtoul(state.text, 0, 10);
- else
- {
- *((long *)value) = atol(state.text);
- if (state.isNegative)
- *((long *)value) = - *((long *)value);
- }
- break;
- }
- }
-
- int UIW_NUMBER::ValidWholeBetween(char *minValue, char *maxValue)
- {
- int minNegative;
- int maxNegative;
- int minLength;
- int maxLength;
- int valLength;
- char *valPtr = state.text;
- int valNegative = state.isNegative;
-
- minNegative = (minValue[0] == '-') ? TRUE : FALSE;
- if (minNegative)
- *minValue++;
- maxNegative = (maxValue[0] == '-') ? TRUE : FALSE;
- if (maxNegative)
- *maxValue++;
- if (!minNegative && valNegative || maxNegative && !valNegative)
- return(FALSE);
-
- // Take out leading zeroes.
- while (*minValue == '0' && *(minValue + 1))
- minValue++;
- while (*maxValue == '0' && *(maxValue + 1))
- maxValue++;
- while (*valPtr == '0' && *(valPtr + 1))
- valPtr++;
-
- valLength = (valPtr) ? strlen(valPtr) : 0;
- minLength = (minValue) ? strlen(minValue) : 0;
- if ((!minNegative &&
- !valNegative &&
- (minLength > valLength ||
- (minLength == valLength && strcmp(minValue, valPtr) > 0))) ||
- (minNegative &&
- valNegative &&
- (minLength < valLength ||
- (minLength == valLength && strcmp(minValue, valPtr) < 0))))
- return(FALSE);
-
- /* Compare the value against the maximum */
- maxLength = (maxValue) ? strlen(maxValue) : 0;
- if ((!maxNegative &&
- !valNegative &&
- (maxLength < valLength ||
- (maxLength == valLength && strcmp(maxValue, valPtr) < 0))) ||
- (maxNegative &&
- valNegative &&
- (maxLength > valLength ||
- (maxLength == valLength && strcmp(maxValue, valPtr) > 0))))
- return(FALSE);
-
- /* The value falls within the minimum and maximum values */
- return(TRUE);
- }
-
- int UIW_NUMBER::ValidWholeInRange(char *a_range)
- {
- /* See if a range exists */
- if (!a_range || a_range[0] == '\0')
- return(TRUE);
-
- /* See if the value is in the specified range */
- char minValue[20];
- char maxValue[20];
- int validNumber = FALSE;
- int offset = 0;
- int rangeLength = (a_range) ? strlen(a_range) : 0;
- while (!validNumber && offset < rangeLength)
- {
- offset = ParseRange(a_range, offset, minValue, maxValue);
- validNumber = ValidWholeBetween(minValue, maxValue);
- }
- return(validNumber);
- }
-
- int UIW_NUMBER::ValidWholeNumber()
- {
- char *absoluteRange;
-
- switch (type)
- {
- case NUM_CHAR:
- absoluteRange = (FlagSet(nmFlags, NMF_UNSIGNED)) ?
- "0..255" : "-128..127";
- break;
-
- case NUM_SHORT:
- absoluteRange = (FlagSet(nmFlags, NMF_UNSIGNED)) ?
- "0..65535" : "-32768..32767";
- break;
-
- case NUM_LONG:
- absoluteRange = (FlagSet(nmFlags, NMF_UNSIGNED)) ?
- "0..4294967295" : "-2147483648..2147483647";
- break;
- }
-
- if (range && !ValidWholeInRange(range))
- {
- RangeError(range);
- return (FALSE);
- }
- else if (!ValidWholeInRange(absoluteRange))
- {
- RangeError(absoluteRange);
- return (FALSE);
- }
-
- return (TRUE);
- }
-
- #ifdef ZIL_LOAD
- UIW_INTEGER::UIW_INTEGER(const char *name, UI_STORAGE *file, USHORT loadFlags) :
- UIW_NUMBER(name, file, loadFlags | L_SUB_LEVEL)
- {
- windowID[0] = ID_NUMBER;
- windowID[1] = ID_STRING;
-
- if (!file)
- file = _storage;
- file->Load(&type);
- file->Load(&nmFlags);
- value = new double;
- file->Load(value, sizeof(double));
- UIW_NUMBER::Constructor(value, relative.right - relative.left + 1,
- nmFlags, 0, UIW_NUMBER::WholeToAscii, UIW_NUMBER::AsciiToWhole,
- UIW_NUMBER::ValidWholeNumber, NULL);
- file->Load(&range);
- if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
- delete file;
- }
- #endif
-
- #ifdef ZIL_STORE
- void UIW_INTEGER::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
- {
- search.type = ID_INTEGER;
- UIW_NUMBER::Store(name, file, storeFlags | S_SUB_LEVEL);
- file->Store(type);
- file->Store(nmFlags);
- file->Store(value, sizeof(double));
- file->Store(range);
- if (!FlagSet(storeFlags, S_SUB_LEVEL))
- file->ObjectSize(name, search);
- }
- #endif
-