home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 326.lha / DateRequester / rxdatereq.c < prev    next >
C/C++ Source or Header  |  1989-12-27  |  14KB  |  531 lines

  1. #include <intuition/intuition.h>
  2. #include <libraries/arpbase.h>
  3. #include <functions.h>
  4. #include <rexx/errors.h>
  5.  
  6. #include "MRDateReq.h"
  7. #include "minrexx.h"
  8.  
  9. int             Dispatch();
  10. void            RexxExit(), 
  11.                 RexxGetDate(), 
  12.                 RexxGetFullDate(),
  13.                 RexxGetDayName(), 
  14.                 RexxGetFormat(), 
  15.                 RexxGetTime(), 
  16.                 RexxRequest(),
  17.                 RexxSetFullDate(),
  18.                 RexxSetFormat(),
  19.                 RexxSetPrompt();
  20.  
  21. void            StrUpper();
  22.  
  23. int             commandError;
  24. MRDatePacket    *datePacket;
  25. char            firstCommand[256];
  26. int             keepGoing = 1;
  27. static char     prompt[256] = "Enter the date: ";
  28. long            rexxBit;                /* rexx signal bit */
  29.  
  30. static char *formatNames[FORMAT_MAX + 1] = {
  31.         "DOS", "International", "USA", "Canadian"
  32.     };
  33.  
  34. struct NewWindow newWindow = {
  35.     20,20,320,140,0,1,
  36.  
  37. /* IDCMP Flags */
  38.  
  39.     MENUVERIFY | RMBTRAP | GADGETUP | GADGETDOWN, 
  40.  
  41. /* Flags */
  42.     WINDOWDRAG,
  43.  
  44.     NULL,                           /* First gadget */
  45.     NULL,                           /* Checkmark */
  46.     (UBYTE *)"ARexx Date Requester",/* Window title */
  47.     NULL,                           /* No custom streen */
  48.     NULL,                           /* Not a super bitmap window */
  49.     0,0,640,200,                    /* Not used, but set up anyway */
  50.     WBENCHSCREEN
  51. };
  52.  
  53. struct rexxCommandList rcl[] = {
  54.     { "exit",           (APTR) &RexxExit            },
  55.     { "getdate",        (APTR) &RexxGetDate         },
  56.     { "getfulldate",    (APTR) &RexxGetFullDate     },
  57.     { "getdayname",     (APTR) &RexxGetDayName      },
  58.     { "getformat",      (APTR) &RexxGetFormat       },
  59.     { "gettime",        (APTR) &RexxGetTime         },
  60.     { "request",        (APTR) &RexxRequest         },
  61.     { "setfulldate",    (APTR) &RexxSetFullDate     },
  62.     { "setformat",      (APTR) &RexxSetFormat,      },
  63.     { "setprompt",      (APTR) &RexxSetPrompt       },
  64.     { NULL,             NULL }
  65.     };
  66.  
  67. struct ArpBase          *ArpBase;
  68. struct GfxBase          *GfxBase;
  69. struct IntuitionBase    *IntuitionBase;
  70.  
  71. main(argc, argv)
  72.     int     argc;
  73.     char    **argv;
  74. {
  75. static char *arpNotOpen = 
  76.     "The ARP library must be installed in the LIBS: directory!";
  77.  
  78.     ULONG                   class;
  79.     int                     i;
  80.     char                    *portName;
  81.     struct  IntuiMessage    *wMsg;
  82.  
  83.     ArpBase = (struct ArpBase *) OpenLibrary(ArpName, ArpVersion);
  84.     if (ArpBase == NULL) {
  85.         Write(Output(), arpNotOpen , (long) sizeof(arpNotOpen));
  86.         goto done;
  87.     }
  88.     GfxBase = (struct GfxBase *) ArpBase->GfxBase;
  89.     IntuitionBase = (struct IntuitionBase *) ArpBase->IntuiBase;
  90.  
  91.     /* Create and initialize date packet. */
  92.  
  93.     datePacket = CreateMRDatePacket(NULL, FORMAT_USA, 1);
  94.     if (argc == 2)
  95.         portName = argv[1];
  96.     else
  97.         portName = "mrdatereq";
  98.  
  99.     rexxBit = upRexxPort(portName, rcl, NULL, &Dispatch);
  100.  
  101.     while (keepGoing) {
  102.         Wait(rexxBit);
  103.         dispRexxPort();
  104.     }
  105.  
  106. done:
  107.     dnRexxPort();               /* Dispose of the rexx port. */
  108. }
  109.  
  110. /*  FUNCTION
  111.         Dispatch - dispatch rexx command.
  112.  
  113.     SYNOPSIS
  114.         int Dispatch(msg, cmd, parameters)
  115.                      struct RexxMsg         *msg;
  116.                      struct rexxCommandList *cmd;
  117.                      char                   *parameters;
  118.  
  119.     DESCRIPTION
  120.         Dispatch() invokes the appropriate function for the command
  121.         described by <cmd>, passing it the <msg> and <parameters>.
  122.         It then replies to the <msg> with a result code indicating the
  123.         success or failure of the command.
  124. */
  125.  
  126. int
  127. Dispatch(msg, cmd, params)
  128.     struct RexxMsg          *msg;
  129.     struct rexxCommandList  *cmd;
  130.     char                    *params;
  131. {
  132.     commandError = 0;
  133.     while (*params == ' ') ++params;
  134.     /* Dispatch to the user's function. */
  135.     ( (int (*)() )(cmd->userdata))(msg, params);
  136.     if (commandError) {             /* We got an error. */
  137.         replyRexxCmd(msg, (long) commandError, 0L, NULL);
  138.     }
  139.     return 0;                       /* Return value isn't used! */
  140. }
  141.  
  142. /*  FUNCTION
  143.         RexxExit - process an exit command.
  144.  
  145.     SYNOPSIS
  146.         void RexxExit(msg, p)
  147.                       struct RexxMsg    *msg;
  148.                       char              *p;
  149.  
  150.     DESCRIPTION
  151.         RexxExit() is called when the macro sends an 'exit' command.
  152.         This function simply clears the keepGoing flag, terminating
  153.         this program.
  154. */
  155.  
  156. void
  157. RexxExit(msg, p)
  158.     struct RexxMsg  *msg;
  159.     char            *p;
  160. {
  161.     keepGoing = 0;
  162. }
  163.  
  164. /*  FUNCTION
  165.         RexxGetDate - retrieve the formatted date string.
  166.  
  167.     SYNOPSIS
  168.         void RexxGetDate(msg, p)
  169.                          struct RexxMsg *msg;
  170.                          char           *p;
  171.  
  172.     DESCRIPTION
  173.         RexxGetDate is invoked with the macro issues a 'getdate' command.
  174.         This is normally done after a 'request' command. The text string
  175.         representing the date component (not time) is returned via the
  176.         result variable. 
  177. */
  178.  
  179. void
  180. RexxGetDate(msg, p)
  181. {
  182.     replyRexxCmd(msg, 0L, 0L,
  183.         datePacket->ARPDatePacket.dat_StrDate);
  184. }
  185.  
  186. /*  FUNCTION
  187.         RexxGetFullDate - define a compound symbol with all date components.
  188.  
  189.     SYNOPSIS
  190.         void RexxGetFullDate(msg, p)
  191.                              struct RexxMsg *msg;
  192.                              char           *p;
  193.  
  194.     DESCRIPTION
  195.         RexxGetFullDate is invoked when the macro program issues a
  196.         'getfulldate' command. The string pointed to by <p> is used as
  197.         the stem for a compound variable. The components of this variable
  198.         are:
  199.                 1 - Year    (19XX)
  200.                 2 - Month   (1 - 12)
  201.                 3 - Day     (1 - 31)
  202.                 4 - Hour    (00 - 59)
  203.                 5 - Minute  (00 - 59)
  204.                 6 - Second  (00 - 59)
  205.                 7 - Weekday (0 - 6, 0 => Sunday)
  206.  
  207.         For example, assuming a stem variable name of "mydate" and a
  208.         date selection of 5-Oct-1989, 
  209.             mydate.1 == 1989
  210.             mydate.2 == 10
  211.             mydate.3 == 5
  212.             ...and so on.
  213.  
  214.         If a failure is detected, the result variable will contain a
  215.         descriptive message on exit.
  216. */
  217.  
  218. void
  219. RexxGetFullDate(msg, p)
  220.     struct RexxMsg  *msg;
  221.     char            *p;
  222. {
  223.     LONG        error;
  224.     int         i;
  225.     int         nValue;
  226.     char        fullSym[256];
  227.     char        value[81];
  228.  
  229.     for (i = 1; i <= 7; ++i) {
  230.         switch (i) {
  231.         case 1:
  232.             nValue = datePacket->newDate.Dyear;
  233.             break;
  234.         case 2:
  235.             nValue = datePacket->newDate.Dmonth;
  236.             break;
  237.  
  238.         case 3:
  239.             nValue = datePacket->newDate.Dday;
  240.             break;
  241.  
  242.         case 4:
  243.             nValue = datePacket->newDate.Dhour;
  244.             break;
  245.  
  246.         case 5:
  247.             nValue = datePacket->newDate.Dminute;
  248.             break;
  249.  
  250.         case 6:
  251.             nValue = datePacket->newDate.Dsecond;
  252.             break;
  253.  
  254.         case 7:
  255.             nValue = datePacket->newDate.Dweekday;
  256.         }
  257.  
  258.         sprintf(fullSym, "%s.%d", p, i);
  259.         StrUpper(fullSym);
  260.         sprintf(value, "%d", nValue);
  261.         if (error = SetRexxVar(msg, fullSym, value, strlen(value)) ) {
  262.             sprintf(value, "Failed to set date component '%s'", fullSym);
  263.             replyRexxCmd(msg, error, 0L, value);
  264.             break;
  265.         }
  266.     }         
  267. }
  268.  
  269. /*  FUNCTION
  270.         RexxGetDayName - get the day name for the selected date.
  271.  
  272.     SYNOPSIS
  273.         void RexxGetDayName(msg, p)
  274.                             struct RexxMsg  *msg;
  275.                             char            *p;
  276.  
  277.     DESCRIPTION
  278.         RexxGetDayName is invoked when the macro program issues a
  279.         'getdayname' command. This is normally done after the 'request'
  280.         command has been issued. The full name of the day (Monday, Tuesday,
  281.         etc.) is returned via the result variable.
  282. */
  283.  
  284. void
  285. RexxGetDayName(msg, p)
  286.     struct RexxMsg  *msg;
  287.     char            *p;
  288. {
  289.     replyRexxCmd(msg, 0L, 0L,
  290.         datePacket->ARPDatePacket.dat_StrDay);
  291. }
  292.  
  293. /*  FUNCTION
  294.         RexxGetFormat - get the date format name string.
  295.  
  296.     SYNOPSIS
  297.         void RexxGetFormat(msg, p)
  298.                            struct RexxMsg   *msg;
  299.                            char             *p;
  300.  
  301.     DESCRIPTION
  302.         RexxGetFormat is called when the macro program issues a
  303.         'getdateformat' command. A name string describing the date
  304.         format is returned via the result variable. The current format
  305.         names are:
  306.  
  307.                 "DOS"           - DD-MMM-YYYY
  308.                 "International" - YY/MM/DD
  309.                 "USA"           - MM/DD/YY 
  310.                 "Canadian"      - DD/MM/YY
  311. */            
  312. void
  313. RexxGetFormat(msg, p)
  314.     struct RexxMsg  *msg;
  315.     char            *p;
  316. {
  317.     replyRexxCmd(msg, 0L, 0L, 
  318.         formatNames[datePacket->ARPDatePacket.dat_Format]);
  319. }
  320.  
  321. /*  FUNCTION
  322.         RexxGetTime - get the time component of the selected date.
  323.  
  324.     SYNOPSIS
  325.         void RexxGetTime(msg, p)
  326.                          struct RexxMsg *msg;
  327.                          char           *p;
  328.  
  329.     DESCRIPTION
  330.         RexxGetTime is invoked when the macro program issues a
  331.         'gettime' command, normally after a 'request' command.
  332.         The time component of the date is returned via the result
  333.         variable formatted as "HH:MM:DD".
  334. */
  335.  
  336. void
  337. RexxGetTime(msg, p)
  338.     struct RexxMsg  *msg;
  339.     char            *p;
  340. {
  341.     replyRexxCmd(msg, 0L, 0L,
  342.         datePacket->ARPDatePacket.dat_StrTime);
  343. }
  344.  
  345. /*  FUNCTION
  346.         RexxRequest - pop up the date requester and wait for user input.
  347.  
  348.     SYNOPSIS
  349.         void RexxDateRequest(msg, p)
  350.                              struct RexxMsg *msg;
  351.                              char           *p;
  352.  
  353.     DESCRIPTION
  354.         RexxDateRequest is invoked when the macro program issues a 'request'
  355.         command. A new window is first opened, then the date requester is
  356.         displayed in the window. The result code variable, rc, and the
  357.         result string, result, will contain information if the request
  358.         fails.
  359. */
  360.  
  361. void
  362. RexxRequest(msg, p)
  363.     struct RexxMsg  *msg;
  364.     char            *p;
  365. {
  366.     long result = 0;
  367.     struct Window           *window;
  368.  
  369.     window = OpenWindow(&newWindow);
  370.     if (! window )
  371.         result = RC_FATAL;
  372.     else {
  373.         datePacket->window = window;
  374.         datePacket->prompt = prompt;
  375.  
  376.         if (MRDateRequest(datePacket) )
  377.             result = RC_WARN;
  378.         CloseWindowSafely(window, FALSE);
  379.         datePacket->window = NULL;
  380.     }
  381.     if (result)
  382.         replyRexxCmd(msg, result, 0L, "Date request failed!");
  383. }
  384.  
  385. /*  FUNCTION
  386.         RexxSetFullDate - set all date components.
  387.  
  388.     SYNOPSIS
  389.         void RexxSetFullDate(msg, p)
  390.                              struct RexxMsg *msg;
  391.     DESCRIPTION
  392.         RexxSetFullDate() is invoked when the macro program issues a
  393.         'setfulldate' command. The parameter, <p>, is expected to point
  394.         to the base name of a compound variable. The elements of this
  395.         variable are expected to conform to the layout expected by
  396.         RexxGetFullDate(). Only elements 1-6 are used, since the day of
  397.         the week is implicit in the other components.
  398. */
  399.  
  400. void
  401. RexxSetFullDate(msg, p)
  402.     struct RexxMsg  *msg;
  403.     char            *p;
  404. {
  405.     LONG        error;
  406.     int         i;
  407.     int         nValue;
  408.     char        result[81];
  409.     char        *symValue;
  410.     char        fullSym[81];
  411.  
  412.     for (i = 1; i <= 6; ++i) {
  413.         sprintf(fullSym, "%s.%d", p, i);
  414.         StrUpper(fullSym);
  415.         if (error = GetRexxVar(msg, fullSym, symValue)) {
  416.             sprintf(result, "Failed to get date component '%s'", fullSym);
  417.             replyRexxCmd(msg, error, 0L, result);
  418.             break;
  419.         }
  420.         nValue = Atol(symValue);
  421.         switch (i) {
  422.         case 1:
  423.             datePacket->newDate.Dyear = nValue;
  424.             break;
  425.         case 2:
  426.             datePacket->newDate.Dmonth = nValue;
  427.             break;
  428.  
  429.         case 3:
  430.             datePacket->newDate.Dday = nValue;
  431.             break;
  432.  
  433.         case 4:
  434.             datePacket->newDate.Dhour = nValue;
  435.             break;
  436.  
  437.         case 5:
  438.             datePacket->newDate.Dminute = nValue;
  439.             break;
  440.  
  441.         case 6:
  442.             datePacket->newDate.Dsecond = nValue;
  443.             break;
  444.         }
  445.     }         
  446. }
  447.  
  448. /*  FUNCTION
  449.         RexxSetFormat - set the desired date format.
  450.  
  451.     SYNOPSIS
  452.         void RexxSetFormat(msg, p)
  453.                            struct RexxMsg   *msg;
  454.                            char             *p;
  455.  
  456.     DESCRIPTION
  457.         RexxSetFormat is invoked when the macro program issues a
  458.         'setformat' command. The string parameter, <p>, is expected to
  459.         point to a format name string (or unique prefix of one), as
  460.         described under RexxGetFormat. Example:
  461.  
  462.             'setformat' "Int"
  463.  
  464.         causes the date format to be set to "International".
  465. */
  466.  
  467. void
  468. RexxSetFormat(msg, p)
  469.     struct RexxMsg  *msg;
  470.     char            *p;
  471. {
  472.     long    length;
  473.     int     result = RC_ERROR;
  474.  
  475.     int     i;
  476.  
  477.     while (*p == ' ') ++p;          /* Throw away leading blanks. */
  478.     length = strlen(p);
  479.     for (i = 0; i <= FORMAT_MAX; ++i) {
  480.         if (! Strncmp(p, formatNames[i], length)) {
  481.             result = 0;
  482.             datePacket->ARPDatePacket.dat_Format = i;
  483.             break;
  484.         }
  485.     }
  486.     commandError = result;
  487. }
  488.  
  489. /*  FUNCTION
  490.         RexxSetPrompt - set date requester prompt string.
  491.  
  492.     SYNOPSIS
  493.         void RexxSetPrompt(msg, p)
  494.                            struct RexxMsg   *msg;
  495.                            char             *p;
  496.  
  497.     DESCRIPTION
  498.         RexxSetPrompt() copies the text pointed to by <p> into the package
  499.         prompt variable. The next time RexxRequestDate is called, the prompt
  500.         will be displayed in the date requester.
  501. */
  502. void
  503. RexxSetPrompt(msg, p)
  504.               struct RexxMsg    *msg;
  505.               char              *p;
  506. {
  507.     if (p) strcpy(prompt, p);
  508.     else *prompt = '\0';
  509. }
  510.  
  511. /*  FUNCTION
  512.         StrUpper - convert string to upper case.
  513.  
  514.     SYNOPSIS
  515.         void StrUpper(string)
  516.                       char *string;
  517.  
  518.     DESCRIPTION
  519.         StrUpper converts all lower case characters in <string> to
  520.         upper case.  The conversion is done in-place.
  521. */
  522.  
  523. void
  524. StrUpper(string)
  525.     char *string;
  526. {
  527.     char    *p;
  528.  
  529.     for (p = string; *p; ++p) *p = toupper(*p);
  530. }
  531.