home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR23 / TOUCH2.ZIP / DTTM.C < prev    next >
C/C++ Source or Header  |  1993-08-14  |  6KB  |  309 lines

  1. /*
  2.  * dttm.c - manage date and time
  3.  *
  4.  * V. Abell
  5.  */
  6.  
  7. /*
  8.  * Copyright 1993 Victor A. Abell, Lafayette, Indiana  47906.  All rights
  9.  * reserved.
  10.  *
  11.  * Written by Victor A. Abell.
  12.  *
  13.  * Permission is granted to anyone to use this software for any purpose on
  14.  * any computer system, and to alter it and redistribute it freely, subject
  15.  * to the following restrictions:
  16.  *
  17.  * 1. Victor A. Abell is not responsible for any consequences of the use of
  18.  * this software.
  19.  *
  20.  * 2. The origin of this software must not be misrepresented, either by
  21.  *    explicit claim or by omission.  Credit to Victor A. Abell must
  22.  *    appear in documentation and sources.
  23.  *
  24.  * 3. Altered versions must be plainly marked as such, and must not be
  25.  *    misrepresented as being the original software.
  26.  *
  27.  * 4. This notice may not be removed or altered.
  28.  */
  29. #ifndef lint
  30. static char copyright[] =
  31. "@(#) Copyright 1993 Victor A. Abell.\nAll rights reserved.\n";
  32. #endif
  33.  
  34. #include "touch2.h"
  35. #include <ctype.h>
  36.  
  37.  
  38. struct menu MainDT[] = {
  39.     {  2, 21, "Read and set the meter's date and time." },
  40.     {  9, 21, "R - read date and time" },
  41.     { 11, 21, "S - set date and time" },
  42.     { 13, 21, "X - eXit" },
  43. #define    PROMPTLN    15
  44. #define    PROMPTCOL    11
  45.     {  0,  0, NULL },
  46. };
  47.  
  48. struct menu ReadWt[] = {
  49.     { 12, 26, "Waiting to read date and time" },
  50.     {  0,  0, NULL },
  51. };
  52.  
  53. struct menu DateErr[] = {
  54.     { 12, 33, "Date read/set error" },
  55.     {  0,  0, NULL },
  56. };
  57.  
  58. short month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  59.  
  60.  
  61. static char *AsmNum(char *p, int *n);
  62. static void ReadDtTm();
  63. static void SetDtTm();
  64.  
  65.  
  66. /*
  67.  * AsmNum() - assemble number
  68.  */
  69.  
  70. static char *
  71. AsmNum(p, n)
  72.     char *p;            /* pointer to number string */
  73.  
  74. #if    defined(UNIX)
  75.     int *n;                /* pointer to result */
  76. #else
  77.     short *n;            /* pointer to result */
  78. #endif
  79.  
  80. {
  81.     for (*n = 0; *p; p++) {
  82.         if ( ! isdigit(*p))
  83.             break;
  84.         *n = (*n * 10) + *p - '0';
  85.     }
  86.     return(p);
  87. }
  88.  
  89.  
  90. /*
  91.  * DateTime() - read and set date and time
  92.  */
  93.  
  94. void
  95. DateTime()
  96. {
  97.     int ch;
  98.  
  99.     DispMenu(MainDT, NULL);
  100.     for (;;) {
  101.         if ( !kbhit()) {
  102.             reset_buffer();
  103.             continue;
  104.         }
  105.         if ((ch = getch()) == 0)
  106.             ch = getch();
  107.         switch (ch) {
  108.  
  109.         case ESC:
  110.         case 'x':
  111.         case 'X':
  112.             return;
  113.  
  114.         case 'r':
  115.         case 'R':
  116.             ReadDtTm();
  117.             break;
  118.  
  119.         case 's':
  120.         case 'S':
  121.             SetDtTm();
  122.             break;
  123.  
  124.         default:
  125.             putch(BELL);
  126.         }
  127.         DispMenu(MainDT, NULL);
  128.     }
  129. }
  130.  
  131.  
  132. /*
  133.  * ReadDtTm() - read the date and time
  134.  */
  135.  
  136. static void
  137. ReadDtTm()
  138. {
  139.     char b[64], ch, *cp;
  140.     int err, i;
  141.  
  142.     for (;;) {
  143.         if (WaitRdy() == 0)
  144.             return;
  145.         DispMenu(ReadWt, NULL);
  146.         if (WaitCmd("DMF", 'F'))
  147.             break;
  148.         DispMenu(DateErr,
  149.             "Press ESC to exit; any other key to retry.");
  150.         if ((char)WaitAnyKey() == ESC)
  151.             return;
  152.     }
  153.     GetDataLn(DumpLine, DUMPLL);
  154.     cp = DumpLine;
  155.     if (*cp == 'F') {
  156.         cp++;
  157.         if (*cp == ' ')
  158.             cp++;
  159.     }
  160.     for (i = 0; i < sizeof(b) - 1; cp++) {
  161.         if (*cp == '"')
  162.             continue;
  163.         b[i++] = *cp;
  164.     }
  165.     b[i] = 0;
  166.     ClearRow(12, 1);
  167.     _settextposition(12, 22);
  168.     _outtext(b);
  169.     PromptMsg("Press any key to exit.");
  170.     (void) WaitAnyKey();
  171. }
  172.  
  173.  
  174. /*
  175.  * SetDtTm() - set the date and time
  176.  */
  177.  
  178. static void
  179. SetDtTm()
  180. {
  181.     char buf[19], ch, cmd[32], *cp;
  182.     int err;
  183.  
  184. #if    defined(UNIX)
  185.     int day, hr, min, mo, sec, yr;
  186. #else
  187.     short day, hr, min, mo, sec, yr;
  188. #endif
  189.     int nc;
  190.  
  191.     for (buf[0] = '\0', err = 0;;) {
  192.         ClearRow(PROMPTLN, PROMPTCOL);
  193.         if (err) {
  194.             putch(BELL);
  195.             err = 0;
  196.         }
  197.         buf[17] = '\0';
  198.         nc = GetInp(PROMPTLN, PROMPTCOL,
  199.             "Enter date/time (mm/dd/yy hh:mm[:ss) ? ", buf, buf,
  200.             sizeof(buf));
  201.         if (nc == 0)
  202.             return;
  203.         err++;
  204.         cp = AsmNum(buf, &mo);
  205.         if (*cp++ != '/' || mo < 1 || mo > 12)
  206.             continue;
  207.         cp = AsmNum(cp, &day);
  208.         if (*cp++ != '/' || day < 1 || day > 31)
  209.             continue;
  210.         cp = AsmNum(cp, &yr);
  211.         if (*cp++ != ' ' || (yr > 14 && yr < 84))
  212.             continue;
  213.         if (mo == 2) {
  214.             nc = (yr >= 84) ? yr + 1900 : yr + 2000;
  215.             if ((((nc % 4) == 0) && ((nc % 100) != 0))
  216.             || ((nc % 400) == 0))
  217.                 month_days[1] = 29;
  218.             else
  219.                 month_days[1] = 28;
  220.         }
  221.         if (day > month_days[mo - 1])
  222.             continue;
  223.         cp = AsmNum(cp, &hr);
  224.         if (*cp++ != ':' || hr < 0 || hr > 23)
  225.             continue;
  226.         cp = AsmNum(cp, &min);
  227.         if (min < 0 || min > 59)
  228.             continue;
  229.         if (*cp == ':') {
  230.             cp = AsmNum(++cp, &sec);
  231.             if (sec < 0 || sec > 59)
  232.                 continue;
  233.         } else
  234.             sec = 0;
  235.         if (*cp != '\0')
  236.             continue;
  237.         (void) sprintf(cmd, "DMT%02d/%02d/%02d %02d:%02d:%02d",
  238.             mo, day, yr, hr, min, sec);
  239.         for (;;) {
  240.             if (WaitRdy() == 0)
  241.                 return;
  242.             DispMenu(ReadWt, NULL);
  243.             if (WaitCmd(cmd, 'T')) {
  244.                 GetDataLn(DumpLine, DUMPLL);
  245.                 if (DumpLine[0] == ' ' && DumpLine[1] == '"')
  246.                     break;
  247.             }
  248.             DispMenu(DateErr,
  249.                 "Press ESC to exit; any other key to retry.");
  250.             _settextposition(14, 1);
  251.             _outtext("Command response:");
  252.             _settextposition(14, 19);
  253.             _outtext(DumpLine);
  254.             if ((char)WaitAnyKey() == ESC)
  255.                 return;
  256.             continue;
  257.         }
  258.         return;
  259.     }
  260. }
  261.  
  262.  
  263. /*
  264.  * WaitCmd() - issue command and wait for reply
  265.  */
  266.  
  267. int
  268. WaitCmd(c, e)
  269.     char *c;                        /* command text */
  270.  
  271. #if    defined(UNIX)
  272.     int e;                /* command echo character */
  273. #else
  274.     char e;                /* command echo character */
  275. #endif
  276.  
  277. {
  278.     char ch;
  279.     int err;
  280.  
  281.     (void) reset_buffer();
  282.     (void) writeln_com(c, &err);
  283.     if (err) {
  284.         (void) sprintf(DumpLine, "Meter transmission error %d", err);
  285.         DumpErr[0].col = (short)(((Vc.numtextcols
  286.                    - strlen(DumpLine))/2) + 1);
  287.         DispMenu(DumpErr, "Press any key to continue.");
  288.         (void) WaitAnyKey();
  289.         return(0);
  290.     }
  291.     do {
  292.         if (CheckESC()) {
  293.             err = -1;
  294.             break;
  295.         }
  296.         (void) check_com(&ch, &err);
  297.     } while (err == 6 || (err == 0 && ch != e));
  298.     if ( ! err)
  299.         return(1);
  300.     if (err == -1)
  301.         return(0);
  302.     (void) sprintf(DumpLine, "Error reading command reply: %d", err);
  303.     DumpErr[0].col = (short)(((Vc.numtextcols - strlen(DumpLine))/2) + 1);
  304.     DispMenu(DumpErr, "Press any key to continue.");
  305.     (void) WaitAnyKey();
  306.     return(0);
  307.  
  308. }
  309.