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 / TIMEWIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  6.7 KB  |  279 lines

  1. //    Zinc Interface Library - TIMEWIN.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 <dos.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. UIW_TIME::UIW_TIME(int left, int top, int width, UI_TIME *_time,
  12.     const char *_range, USHORT _tmFlags, USHORT _woFlags,
  13.     int (*_validate)(void *object, int ccode)) :
  14.     UIW_STRING(left, top, width, NULL, 64, STF_NO_FLAGS, _woFlags, _validate),
  15.     tmFlags(_tmFlags)
  16. {
  17.     windowID[0] = ID_TIME;
  18.     windowID[1] = ID_STRING;
  19.     search.type = ID_TIME;
  20.  
  21.     if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  22.     {
  23.         text = new char[64];
  24.         text[0] = '\0';
  25.         textTail = text + 63;
  26.         *textTail = '\0';
  27.     }
  28.  
  29.     if (!_time)
  30.     {
  31.         time = new UI_TIME();
  32.         woFlags &= ~WOF_NO_ALLOCATE_DATA;
  33.     }
  34.     else if (FlagSet(_woFlags, WOF_NO_ALLOCATE_DATA))
  35.         time = _time;
  36.     else 
  37.         time = new UI_TIME(*_time);
  38.     range = ui_strdup(_range);
  39. }
  40.  
  41. UIW_TIME::~UIW_TIME(void)
  42. {
  43.     if (range)
  44.         delete range;
  45.     if (text && FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  46.     {
  47.         delete text;
  48.         text = NULL;
  49.     }
  50. }
  51.  
  52. void UIW_TIME::DataSet(UI_TIME *newTime)
  53. {
  54.     if (newTime)
  55.     {
  56.         if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  57.             time = newTime;
  58.         else
  59.             *time = *newTime;
  60.     }
  61.     time->Export(text, tmFlags);
  62.     UI_WINDOW_OBJECT::Redisplay(FALSE);
  63. }
  64.  
  65. int UIW_TIME::Event(const UI_EVENT &event)
  66. {
  67.     // Switch on the event type.
  68.     int needValidate = NeedsValidation();
  69.     int ccode = event.type;
  70.     if (ccode == S_CREATE)
  71.     {
  72.         if (FlagSet(woStatus, WOS_UNANSWERED))
  73.         {
  74.             time->Import(0, 0, 0, 0);
  75.             *text = '\0';
  76.         }
  77.         else
  78.             time->Export(text, tmFlags);
  79.     }
  80.     else if (ccode == S_CURRENT)
  81.     {
  82.         if (needValidate)
  83.             (*Validate)(this, ccode);
  84.         woStatus &= ~WOS_UNANSWERED;
  85.     }
  86.     else if (ccode == S_NON_CURRENT)
  87.     {
  88.         int result = time->Import(text, tmFlags);
  89.         if (result == TMI_OK)
  90.             result = TimeRangeCheck();
  91.         if (result != TMI_OK)
  92.         {
  93.             TimeError(result);
  94.             ccode = S_ERROR;
  95.         }
  96.         else
  97.         {
  98.             if (needValidate && (*Validate)(this, ccode) != 0)
  99.                 ccode = S_ERROR;
  100.             else
  101.             {
  102.                 if (*text == '\0' && !FlagSet(tmFlags, TMF_SYSTEM))
  103.                     woStatus |= WOS_UNANSWERED;        // Leave unanswered.
  104.                 time->Export(text, tmFlags);
  105.                 if (FlagSet(woStatus, WOS_UNANSWERED))
  106.                     *text = '\0';
  107.             }
  108.         }
  109.     }
  110.     if (ccode != S_ERROR)
  111.     {
  112.         woStatus &= ~WOS_INVALID;
  113.         ccode = UIW_STRING::Event(event);
  114.     }
  115.     else
  116.         woStatus |= WOS_INVALID;
  117.     return(ccode);
  118. }
  119.  
  120. void UIW_TIME::TimeError(int errorCode)
  121. {
  122.     static struct
  123.     {
  124.         int code;
  125.         char *msg;
  126.     } errorTable[] =
  127.     {
  128.         { TMI_INVALID,            "The time %s, is in an invalid format."        },
  129.         { TMI_VALUE_MISSING,    "A time value must be entered."                },
  130.         { TMI_OK,                0                                            }
  131.     };
  132.     char formattedString[512];
  133.  
  134.     if (errorCode == TMI_OUT_OF_RANGE)
  135.         TimeRangeError(formattedString);
  136.     else
  137.     {
  138.         strcpy(formattedString, "An unknown time error was received.");
  139.         for (int i = 0; errorTable[i].msg; i++)
  140.             if (errorTable[i].code == errorCode)
  141.             {
  142.                 sprintf(formattedString, errorTable[i].msg, text);
  143.                 break;
  144.             }
  145.     }
  146.     _errorSystem->ReportError(windowManager,
  147.         woFlags & (WOF_NO_UNANSWERED | WOF_NO_INVALID), formattedString);
  148. }
  149.  
  150. int UIW_TIME::TimeRangeCheck()
  151. {
  152.     char minValue[20];
  153.     char maxValue[20];
  154.     int errorCode = TMI_OK;
  155.  
  156.     /* See if a range exists */
  157.     if (!range || range[0] == '\0')
  158.         return (TMI_OK);
  159.  
  160.     int offset = 0;
  161.     int rangeLength = (range) ? strlen(range) : 0;
  162.     while (offset < rangeLength)
  163.     {
  164.         offset = ui_parse_range(range, offset, minValue, maxValue);
  165.         UI_TIME minTime;
  166.         UI_TIME maxTime;
  167.         int minValid = minTime.Import(minValue, tmFlags);
  168.         int maxValid = maxTime.Import(maxValue, tmFlags);
  169.         if (minValid != TMI_OK || maxValid != TMI_OK ||
  170.             *time < minTime || *time > maxTime)
  171.             errorCode = TMI_OUT_OF_RANGE;
  172.         else
  173.         {
  174.             errorCode = TMI_OK;
  175.             break;
  176.         }
  177.     }
  178.     return (errorCode);
  179. }
  180.  
  181. void UIW_TIME::TimeRangeError(char *formattedString)
  182. {
  183.     char formattedMin[40];
  184.     char formattedMax[40];
  185.     char tempString[80];
  186.     static char throughString[] = "through";
  187.     static char orString[] = "or";
  188.     int position;
  189.     int offset = 0;
  190.     int count = 0;
  191.     char badText[64];
  192.  
  193.     /* Set up the initial variables */
  194.     int rangeLength = (range) ? strlen(range) : 0;
  195.     char separator = ',';
  196.  
  197.     time->Export(badText, tmFlags);
  198.     sprintf(formattedString,
  199.         "%s is not valid.  The time must be in the range ", badText);
  200.     while (offset < rangeLength)
  201.     {
  202.         /* Format the current minimum and maximum values */
  203.         if (count == 1)
  204.         {
  205.             if (!strcmp(formattedMin, formattedMax))
  206.                 strcpy(tempString, formattedMin);
  207.             else
  208.                 sprintf(tempString, "%s %s %s", formattedMin, throughString,
  209.                 formattedMax);
  210.             strcat(formattedString, tempString);
  211.         }
  212.         else if (count != 0)
  213.         {
  214.             if (!strcmp(formattedMin, formattedMax))
  215.                 sprintf(tempString, "%c %s", separator, formattedMin);
  216.             else
  217.                 sprintf(tempString, "%c %s %s %s", separator,
  218.                     formattedMin, throughString, formattedMax);
  219.             strcat(formattedString, tempString);
  220.         }
  221.  
  222.         /* Get a new set of minimum and maximum values */
  223.         count++;
  224.         offset = ui_parse_range(range, offset, formattedMin, formattedMax);
  225.     }
  226.  
  227.     /* Append the final minimum and maximum values on the string */
  228.     if (count > 2 && !strcmp(formattedMin, formattedMax))
  229.         sprintf(tempString, "%c %s %s.", separator, orString, formattedMin);
  230.     else if (count > 2)
  231.         sprintf(tempString, "%c %s %s %s %s.", separator, orString,
  232.             formattedMin, throughString, formattedMax);
  233.     else if (count == 2 && !strcmp(formattedMin, formattedMax))
  234.         sprintf(tempString, " %s %s.", orString, formattedMin);
  235.     else if (count == 2)
  236.         sprintf(tempString, " %s %s %s %s.", orString, formattedMin,
  237.             throughString, formattedMax);
  238.     else if (count > 0)
  239.         sprintf(tempString, "%s %s %s.", formattedMin, throughString,
  240.             formattedMax);
  241.     else
  242.     {
  243.         position = 0;        
  244.         while (formattedString[position] != '\0' &&
  245.             formattedString[position] != '.')
  246.             position++;
  247.         formattedString[++position] = '\0';
  248.     }
  249.     strcat(formattedString, tempString);
  250. }
  251.  
  252. #ifdef ZIL_LOAD
  253. UIW_TIME::UIW_TIME(const char *name, UI_STORAGE *file, USHORT loadFlags) :
  254.     UIW_STRING(name, file, loadFlags | L_SUB_LEVEL)
  255. {
  256.     windowID[0] = ID_TIME;
  257.     windowID[1] = ID_STRING;
  258.  
  259.     if (!file)
  260.         file = _storage;
  261.     file->Load(&tmFlags);
  262.     file->Load(&range);
  263.     time = new UI_TIME(text, tmFlags);
  264.     if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
  265.         delete file;
  266. }
  267. #endif
  268.  
  269. #ifdef ZIL_STORE
  270. void UIW_TIME::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
  271. {
  272.     UIW_STRING::Store(name, file, storeFlags | S_SUB_LEVEL);
  273.     file->Store(tmFlags);
  274.     file->Store(range);
  275.     if (!FlagSet(storeFlags, S_SUB_LEVEL))
  276.         file->ObjectSize(name, search);
  277. }
  278. #endif
  279.