home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / AZTEC-C / COMND004.ARK / CMDPFD.C < prev    next >
C/C++ Source or Header  |  1986-06-17  |  9KB  |  344 lines

  1. /*    cmdpfd.c    COMND module; Date/time function parse.
  2.  
  3.     Copyright (C) 1985 Mark E. Mallett
  4.  
  5.     Permission is hereby granted to distribute this file indiscriminately.
  6.  
  7.     This file contains the code for the date/time parsing function.
  8.  
  9. Edit history
  10.  
  11. When    Who    What
  12. ------    ---    --------------------------------
  13. 84xxxx    MEM    Create file.
  14.  
  15.     Routines included:
  16.  
  17.         CFPdat        Parse date and/or time
  18.  
  19. */
  20.  
  21.  
  22. #include "stdio.h"            /* Standard system defs */
  23. #include "comnd.h"            /* COMND interface definitions */
  24. #include "comndi.h"            /* COMND internal definitions */
  25.  
  26.  
  27. typedef                    /* Structure for "special" dates */
  28.   struct {
  29.     int        _mo;            /* Month (1 - 12) */
  30.     int        _da;            /* Day of month (1 - 31) */
  31.   } SMD;
  32.  
  33.  
  34. /* External routines */
  35.  
  36.  
  37. /* External data */
  38.  
  39. extern    int    CMDbel;            /* Beep request flag */
  40.  
  41. /* Internal (public) routines */
  42.  
  43.  
  44.  
  45. /* Internal (public) data */
  46.  
  47.  
  48. /* Local (static) data */
  49.  
  50.     /* Tables for date and time parsing.  */
  51.  
  52. static    char    *Mnmtbl[] = {        /* Month names */
  53.             "January",    "February",    "March",
  54.             "April",    "May",        "June",
  55.             "July",        "August",    "September",
  56.             "October",    "November",    "December",
  57.             NULL};
  58.  
  59. static    char    *Snmtbl[] = {        /* Table of special names */
  60.             "April-Fools-Day",
  61.             "Christmas",
  62.             "Erics-Birthday",
  63.             "Fourth-of-July",
  64.             "Halloween",
  65.             "New-Years-Day",
  66.             "Valentines-day",
  67.             NULL
  68.                 };
  69.  
  70. static    SMD    Sndtbl[] = {        /* month/day for special names */
  71.             4, 1,        /* April fools */
  72.             12, 25,        /* Christmas */
  73.             8, 19,        /* Eric's birthday */
  74.             7, 4,        /* Fourth of July */
  75.             10, 31,        /* Halloween */
  76.             1, 1,        /* New Year's Day */
  77.             2, 14,        /* Valentine's day */
  78.             0, 0};        /*  end */
  79.  
  80.  
  81. static    char    *Dowtbl[] = {        /* Day-of-week table */
  82.             "Sunday", "Monday", "Tuesday", "Wednesday",
  83.             "Thursday", "Friday", "Saturday", NULL
  84.                 };
  85.  
  86.  
  87. static    char    *Todtbl[] = {        /* Time-of-day table */
  88.             "noon",
  89.             NULL
  90.                 };
  91.  
  92.  
  93. static    CFB    Timcfb = {_CMNUM, _CFHPP|_CFSDH, 0, 10,
  94.             "Time in the form hh:mm:ss", 0};
  95. static    CFB    Todcfb = {_CMKEY, _CFHPP, &Timcfb, &Todtbl,
  96.             "Time of day, ", 0};
  97. static    CFB    Plscfb = {_CMTOK, _CFHPP|_CFSDH, 0, "+",
  98.               "+ for future offset from now", 0};
  99. static    CFB    Mincfb = {_CMTOK, _CFHPP|_CFSDH, &Plscfb, "-",
  100.               "- for past offset from now", 0};
  101. static    CFB    Adncfb = {_CMNUM, _CFHPP|_CFSDH, &Mincfb, 10,
  102.             "Decimal month number", 0};
  103. static    CFB    Dowcfb = {_CMKEY, _CFHPP, &Adncfb, &Dowtbl,
  104.             "Day of week, ", 0};
  105. static    CFB    Snmcfb = {_CMKEY, _CFHPP, &Dowcfb, &Snmtbl,
  106.             "Special date, ", 0};
  107. static    CFB    Mnmcfb = {_CMKEY, _CFHPP, &Snmcfb, &Mnmtbl,
  108.             "Month name, ", 0};
  109.  
  110. static    CFB    Clncfb = {_CMTOK, 0, 0, ":", 0, 0};
  111. static    CFB    Cmycfb = {_CMTOK, _CFHPP|_CFSDH, 0, ",",
  112.             "Comma and year", 0};
  113. static    CFB    Domcfb = {_CMNUM, _CFHPP|_CFSDH, 0, 10, "Day of month", 0};
  114. static    CFB    Yrcfb =  {_CMNUM, _CFHPP|_CFSDH, 0, 10, "Year", 0};
  115.  
  116. /*
  117.  
  118. *//* CFPdat (CSBptr, CFBptr, ccptr)
  119.  
  120.     Function parse for type=_CMDAT, date and/or time
  121.  
  122. This function parses a date and/or time.  It does this by calling
  123. COMND recursively, with special date/time parsing function blocks.
  124.  
  125.  
  126. Accepts :
  127.  
  128.     CSBptr        Address of command state block
  129.     CFBptr        Address of command function block
  130.     ccptr        Address of CC table (N/A here)
  131.  
  132.  
  133. Returns :
  134.  
  135.     <value>        Parse status, _CPxxx as defined in comndi.h.
  136.  
  137. */
  138.  
  139. CFPdat (CSBptr, CFBptr, ccptr)
  140.  
  141. CSB        *CSBptr;        /* Addr of command state block */
  142. CFB        *CFBptr;        /* Addr of command function block */
  143. WORD        *ccptr;            /* Addr of CC table */
  144.  
  145. {
  146. IND    int    i,j,t;            /* Scratch */
  147. IND    int    id,it;            /* Internal date and time */
  148. IND    int    *rslptr;        /* Result pointer */
  149.  
  150. rslptr = CSBptr -> CSB_ABF;        /* Get pointer to result buffer */
  151. curdtm (&id, &it);            /* Get today's date/time */
  152. if (CFBptr -> CFB_FLG & _CFDTD)        /* If date wanted */
  153.     {
  154.     i = CFPdtd (CSBptr, CFBptr);    /* Try for date */
  155.     if (!CFBptr -> CFB_FLG & _CFDTT)    /* If time not wanted */
  156.     return (i);            /* Just return the result */
  157.  
  158.     switch (i)                /* Go by parse result of date */
  159.     {
  160.     case _CPABT:            /* If abort... */
  161.         return (_CPABT);        /*  return abort code */
  162.  
  163.     case _CPSUCC:            /* Success(!) */
  164.         id = rslptr[0];        /* Remember date */
  165.         j = COMND (CSBptr, &Clncfb); /* Try for colon for time */
  166.         if (j == _CRNOP)        /* If not parsed */
  167.         {
  168.         rslptr[0] = id;        /* Put date back */
  169.         rslptr[1] = it;        /* Use current time */
  170.         return (_CPSUCC);    /* Return ok */
  171.         }
  172.         else if (j != _CROK)    /* If not OK */
  173.         return (_CPABT);    /*  stop now */
  174.         break;            /* Continue with getting time! */
  175.  
  176.     case _CPAGN:            /* Try again later.. */
  177.     case _CPGVH:            /* Gave help */
  178.     case _CPNOP:            /* Could not parse.. */
  179.         rslptr[0] = id;        /* Store current date */
  180.         break;            /* Quit and continue */
  181.  
  182.     }
  183.     }
  184.  
  185. /* Try for time, if wanted */
  186.  
  187. if (!(CFBptr -> CFB_FLG & _CFDTT))    /* If time not wanted */
  188.     return (i);                /*  return result of date parse */
  189.  
  190. id = rslptr[0];                /* Remember date */
  191. t = CFPdtt(CSBptr, CFBptr);        /* Try to parse time */
  192. rslptr[0] = id;                /* Pass back date, if any */
  193.  
  194. if (!(CFBptr -> CFB_FLG & _CFDTD))    /* If we did not parse a date */
  195.     return (t);                /*  return result of time attempt. */
  196.  
  197. if (i == _CPGVH)            /* If date gave help */
  198.     return (_CPGVH);            /* then we HAVE to return that code */
  199.  
  200. return (t);                /* Otherwise return what time said. */
  201. }
  202. /*
  203.  
  204. *//* CFBdtd (CSBptr, CFBptr)
  205.  
  206.     Parse date portion of date/time
  207.  
  208. This function parses a date.  It does this by calling COMND
  209. recursively.
  210.  
  211.  
  212. Accepts :
  213.  
  214.     CSBptr        Address of command state block
  215.     CFBptr        Address of command function block
  216.  
  217.  
  218. Returns :
  219.  
  220.     <value>        Parse status, _CPxxx as defined in comndi.h.
  221.  
  222. */
  223.  
  224. CFPdtd (CSBptr, CFBptr)
  225.  
  226. CSB        *CSBptr;        /* Addr of command state block */
  227. CFB        *CFBptr;        /* Addr of command function block */
  228.  
  229. {
  230. IND    CFB    *CFBmat;        /* Matching CFB address */
  231. IND    int    m,d,y,id,it;        /* Date values */
  232. IND    int    *rslptr;        /* Result pointer */
  233. IND    char    **kptr;            /* Keyword ptr ptr */
  234. IND    int    i;            /* Scratch */
  235. IND    int    prem;            /* Remembers parse point */
  236.  
  237. prem = CSBptr -> CSB_PRS;        /* Remember the current parse point */
  238. rslptr = CSBptr -> CSB_ABF;        /* Point to result area */
  239. i = COMNDr (CSBptr, &Mnmcfb, 1);    /* Try parsing the date. */
  240.  
  241. if ((i != _CPSUCC) && (i != _CPCPE))    /* If not successful parse */
  242.     return (i);                /*  then return whatever it was */
  243.  
  244. CFBmat = CSBptr -> CSB_CFB;        /* Get addr of matching CFB */
  245.  
  246. if (CFBmat == &Plscfb)            /* Plus date/time */
  247.     {
  248.     fprintf (stderr, "Relative date/time not supported\n");
  249.     return (_CPABT);
  250.     }
  251.  
  252. else if (CFBmat == &Mincfb)        /* Minus date/time */
  253.     {
  254.     fprintf (stderr, "Relative date/time not supported\n");
  255.     return (_CPABT);
  256.     }
  257.  
  258. else if ((CFBmat == &Mnmcfb)  || (CFBmat == &Snmcfb))
  259.                     /* Month or special date in year */
  260.     {
  261.     kptr = (char **) (CSBptr -> CSB_RVL._ADR);
  262.                      /* Get the table entry address */
  263.     if (CFBmat == &Mnmcfb)
  264.     {
  265.     m = (kptr - &Mnmtbl[0]) +1;    /* Get month number */
  266.  
  267.     /* Get day of month */
  268.  
  269.     i = COMNDr (CSBptr, &Domcfb, 2); /* Try for day of month */
  270.     if ((i != _CPSUCC) && (i != _CPCPE)) /* Check not success */
  271.         {
  272.         CSBptr -> CSB_PRS = prem;    /* Reset parse pointer */
  273.         return (i);            /*  return */
  274.         }
  275.     d = CSBptr -> CSB_RVL._INT;    /* Get day of month */
  276.     }
  277.     else                /* Special... */
  278.     {
  279.     i = (kptr - &Snmtbl[0]);    /* Get index */
  280.     m = Sndtbl[i]._mo;        /* Get month */
  281.     d = Sndtbl[i]._da;        /*  and day */
  282.     }
  283.  
  284.     i = COMNDr (CSBptr, &Cmycfb, 2);    /* Try for comma and year */
  285.     if ((i != _CPSUCC) && (i != _CPCPE)) /* Check not success */
  286.     {
  287.     CSBptr -> CSB_PRS = prem;    /* Reset parse pointer */
  288.     return (i);
  289.     }
  290.     i = COMNDr (CSBptr, &Yrcfb, 2);    /* Now try for year */
  291.     if ((i != _CPSUCC) && (i != _CPCPE)) /* Check not success */
  292.     {
  293.     CSBptr -> CSB_PRS = prem;    /* Reset parse pointer */
  294.     return (i);
  295.     }
  296.  
  297.     y = CSBptr -> CSB_RVL._INT;        /* Get year */
  298.     i = cvedid (rslptr, &it, m, d, y, 0, 0);
  299.     if (i != 0)
  300.     {
  301.     fprintf (stderr, "Bad component in date: %d\n", i);
  302.     fprintf (stderr, "Inputs were %d/%d/%d\n", m, d, y);
  303.     return (_CPNOP);        /* Bad parse */
  304.     }
  305.     return (_CPSUCC);            /* Got it. */
  306.     }
  307.  
  308. else
  309.     return (_CPNOP);            /* Nope. */
  310. }
  311. /*
  312.  
  313. *//* CFBdtt (CSBptr, CFBptr)
  314.  
  315.     Parse time portion of date/time
  316.  
  317. This function parses a time.  It does this by calling COMND
  318. recursively.
  319.  
  320.  
  321. Accepts :
  322.  
  323.     CSBptr        Address of command state block
  324.     CFBptr        Address of command function block
  325.  
  326.  
  327. Returns :
  328.  
  329.     <value>        Parse status, _CPxxx as defined in comndi.h.
  330.  
  331. */
  332.  
  333. CFPdtt (CSBptr, CFBptr)
  334.  
  335. CSB        *CSBptr;        /* Addr of command state block */
  336. CFB        *CFBptr;        /* Addr of command function block */
  337.  
  338. {
  339. IND    int    i;            /* Scratch */
  340.  
  341. i = COMNDr (CSBptr, &Todcfb, 2);    /* Try parsing the time */
  342. return (i);                /* Return now. */
  343. }
  344.