home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / cd / CDTest / args.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  7.7 KB  |  356 lines

  1. /*
  2. **  cd.device Test
  3. **  Written by John J. Szucs
  4. **  Copyright © 1993-1999 Amiga, Inc.
  5. **  All Rights Reserved
  6. */
  7.  
  8. /*
  9. **  ANSI includes
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15. #include <string.h>
  16. #include <math.h>
  17.  
  18. /*
  19. **  System includes
  20. */
  21.  
  22. #include <exec/types.h>
  23. #include <exec/io.h>
  24.  
  25. #include <dos/dos.h>
  26. #include <dos/dostags.h>
  27.  
  28. #include <devices/cd.h>
  29.  
  30. #include <rexx/rxslib.h>
  31. #include <rexx/storage.h>
  32. #include <rexx/errors.h>
  33.  
  34. #include <clib/exec_protos.h>
  35. #include <clib/dos_protos.h>
  36. #include <clib/rexxsyslib_protos.h>
  37. #include <clib/alib_protos.h>
  38.  
  39. /*
  40. **  Local includes
  41. */
  42.  
  43. #include "simplerexx.h"
  44. #include "cdtest.h"
  45.  
  46. /****** cd/obtainArgs() ******************************************
  47. *
  48. *   NAME
  49. *       obtainArgs  -   parse ARexx command arguments
  50. *
  51. *   SYNOPSIS
  52. *       rdArgs=obtainArgs(string,template,options,optionsSize);
  53. *
  54. *       struct RDArgs *obtainArgs(STRPTR string,STRPTR template,
  55. *           void *options,ULONG optionsSize);
  56. *
  57. *   FUNCTION
  58. *       Parse ARexx command arguments.
  59. *
  60. *   INPUTS
  61. *       string      -   argument string
  62. *       template    -   command template (in dos.library/ReadArgs() format)
  63. *       options     -   options array/structure (as for dos.library/ReadArgs())
  64. *       optionsSize -   size of options array/structure
  65. *
  66. *   RESULT
  67. *       rdArgs      -   dos.library/ReadArgs() control structure;
  68. *                       NULL if error
  69. *
  70. *   EXAMPLE
  71. *
  72. *   NOTES
  73. *       A new-line is appended to the input string by this function
  74. *       if one is not already present. There must be sufficent space in the
  75. *       string for this.
  76. *
  77. *   BUGS
  78. *
  79. *   SEE ALSO
  80. *       releaseArgs()
  81. *
  82. ******************************************************************************
  83. *
  84. */
  85.  
  86. struct RDArgs *obtainArgs(STRPTR string,STRPTR template,
  87.     void *options,ULONG optionsSize)
  88. {
  89.  
  90.     struct RDArgs *rdArgs;
  91.  
  92.     /* Clear options array */
  93.     memset(options,0,optionsSize);
  94.  
  95.     /* Append new-line to argument string */
  96.     if (string[strlen(string)-1]!='\n') {
  97.         strcat(string,"\n");
  98.     }
  99.  
  100.     /* Allocate RDArgs control structure */
  101.     rdArgs=AllocDosObjectTags(DOS_RDARGS,
  102.         TAG_DONE, NULL);
  103.     if (!rdArgs) {
  104.         Printf("%s: Error allocating RDArgs control structure\n",
  105.             PROGRAM_NAME);
  106.         return(NULL);
  107.     }
  108.  
  109.     /* Initialize RDArgs control structure */
  110.     rdArgs->RDA_Source.CS_Buffer=string;            /* Source is string */
  111.     rdArgs->RDA_Source.CS_Length=strlen(string);    /* Length is string length */
  112.     rdArgs->RDA_Source.CS_CurChr=0L;                /* Current character is start */
  113.     rdArgs->RDA_Buffer=NULL;                        /* Use dos.library-allocated buffer */
  114.     rdArgs->RDA_BufSiz=0L;                          /* Same */
  115.     rdArgs->RDA_ExtHelp=NULL;                       /* No extended help */
  116.     rdArgs->RDA_Flags=RDAF_NOPROMPT;                /* No prompted input */
  117.  
  118.     /* Parse string */
  119.     if (!ReadArgs(template,options,rdArgs)) {
  120.         PrintFault(IoErr(),PROGRAM_NAME);
  121.         FreeArgs(rdArgs);
  122.         return(NULL);
  123.     }
  124.  
  125.     /* Return RDArgs control structure */
  126.     return(rdArgs);
  127.  
  128. }
  129.  
  130. /****** cd/releaseArgs() ******************************************
  131. *
  132. *   NAME
  133. *       releaseArgs     -   release argument parsing control structure
  134. *
  135. *   SYNOPSIS
  136. *       releaseArgs(rdArgs);
  137. *
  138. *       void releaseArgs(struct RDArgs *rdArgs);
  139. *
  140. *   FUNCTION
  141. *       Release dos.library/ReadArgs() control structure returned by
  142. *       obtainArgs().
  143. *
  144. *   INPUTS
  145. *       rdArgs      -   dos.library/ReadArgs() control structure
  146. *
  147. *   RESULT
  148. *       None
  149. *
  150. *   EXAMPLE
  151. *
  152. *   NOTES
  153. *
  154. *   BUGS
  155. *       This should be implemented as a macro.
  156. *
  157. *   SEE ALSO
  158. *
  159. ******************************************************************************
  160. *
  161. */
  162.  
  163. void releaseArgs(struct RDArgs *rdArgs)
  164. {
  165.     FreeArgs(rdArgs);
  166. }
  167.  
  168. /****** cd/strToMSF ******************************************
  169. *
  170. *   NAME
  171. *       strToMSF    -   convert string to MSF
  172. *
  173. *   SYNOPSIS
  174. *       msf=strToMSF(string);
  175. *
  176. *       ULONG strToMSF(STRPTR string);
  177. *
  178. *   FUNCTION
  179. *       Convert string (in mm:ss:ff format) to MSF value.
  180. *
  181. *   INPUTS
  182. *       string -- string to convert
  183. *
  184. *   RESULT
  185. *       msf -- MSF value
  186. *
  187. *   EXAMPLE
  188. *
  189. *   NOTES
  190. *
  191. *   BUGS
  192. *
  193. *   SEE ALSO
  194. *
  195. ******************************************************************************
  196. *
  197. */
  198.  
  199. ULONG strToMSF(STRPTR string)
  200. {
  201.  
  202.     ULONG minutes, seconds, frames;
  203.     ULONG msf;
  204.  
  205.     /* Parse minutes, seconds, and frames */
  206.     sscanf(string,"%ld:%ld:%ld",&minutes,&seconds,&frames);
  207.  
  208.     /* Pack MSF value */
  209.     msf=(minutes<<16)|(seconds<<8)|frames;
  210.  
  211.     /* Return MSF value */
  212.     return(msf);
  213.  
  214. }
  215.  
  216. /****** cd/setStemVarInt ******************************************
  217. *
  218. *   NAME
  219. *       setStemVarInt -- set integer stem variable
  220. *
  221. *   SYNOPSIS
  222. *       status=setStemVarInt(rxMsg,base,stem,value);
  223. *
  224. *       LONG setStemVarInt(struct RexxMsg *rxMsg,STRPTR base,
  225. *           STRPTR stem,LONG value);
  226. *
  227. *   FUNCTION
  228. *       Set integer stem variable.
  229. *
  230. *   INPUTS
  231. *       rxMsg   -   valid ARexx message
  232. *       base    -   base name of stem; do not include leading .
  233. *       stem    -   stem
  234. *       value   -   value to set
  235. *
  236. *   RESULT
  237. *       status  -   0 if success; !=0 if failure
  238. *
  239. *   EXAMPLE
  240. *       To set foo.bar.gak to 42, ignoring errors:
  241. *
  242. *           setStemVarInt(rxMsg,"foo","bar.gak",42);
  243. *
  244. *   NOTES
  245. *       The base and stem are converted to all upper-case,
  246. *       since ARexx requires all variable names to be in
  247. *       upper-case.
  248. *
  249. *   BUGS
  250. *
  251. *   SEE ALSO
  252. *
  253. ****************************************************************
  254. *
  255. */
  256.  
  257. LONG setStemVarInt(struct RexxMsg *rxMsg,STRPTR base,STRPTR stem,LONG value)
  258. {
  259.     static UBYTE name[REXX_STEM_BUFFER];
  260.     static UBYTE buffer[REXX_RESULT_BUFFER];
  261.  
  262.     LONG result;
  263.     char *cp;
  264.  
  265.     /* Construct value string */
  266.     bsprintf(buffer,"%ld",value);
  267.  
  268.     /* Construct stem variable name */
  269.     bsprintf(name,"%s.%s",base,stem);
  270.  
  271.     /* Convert variable name to upper-case */
  272.     for (cp=name;*cp;cp++) {
  273.         if (islower(*cp)) {
  274.             *cp=toupper(*cp);
  275.         }
  276.     }
  277.  
  278.     /* Set variable */
  279.     result=SetRexxVar(rxMsg,name,buffer,strlen(buffer));
  280.  
  281.     /* Return result */
  282.     return(result);
  283.  
  284. }
  285.  
  286. /****** cd/setStemVarIntArray ******************************************
  287. *
  288. *   NAME
  289. *       setStemVarIntArray -- set integer stem array variable
  290. *
  291. *   SYNOPSIS
  292. *       status=setStemVarIntArray(rxMsg,base,stem,index,value);
  293. *
  294. *       LONG setStemVarIntArray(struct RexxMsg *rxMsg,STRPTR base,
  295. *           STRPTR stem,LONG index,LONG value);
  296. *
  297. *   FUNCTION
  298. *       Set integer stem variable in array.
  299. *
  300. *   INPUTS
  301. *       rxMsg   -   valid ARexx message
  302. *       base    -   base name of stem; do not include leading .
  303. *       stem    -   stem
  304. *       index   -   index
  305. *       value   -   value to set
  306. *
  307. *   RESULT
  308. *       status  -   0 if success; !=0 if failure
  309. *
  310. *   EXAMPLE
  311. *       To set foo.17.gak to 42, ignoring errors:
  312. *
  313. *           setStemVarIntArray(rxMsg,"foo",17,"gak",42);
  314. *
  315. *   NOTES
  316. *       The base and stem are converted to all upper-case,
  317. *       since ARexx requires all variable names to be in
  318. *       upper-case.
  319. *
  320. *   BUGS
  321. *
  322. *   SEE ALSO
  323. *
  324. ****************************************************************
  325. *
  326. */
  327.  
  328. LONG setStemVarIntArray(struct RexxMsg *rxMsg,STRPTR base,LONG index,STRPTR stem,LONG value)
  329. {
  330.     static UBYTE name[REXX_STEM_BUFFER];
  331.     static UBYTE buffer[REXX_RESULT_BUFFER];
  332.     char *cp;
  333.  
  334.     LONG result;
  335.  
  336.     /* Construct value string */
  337.     bsprintf(buffer,"%ld",value);
  338.  
  339.     /* Construct stem variable name */
  340.     bsprintf(name,"%s.%ld.%s",base,index,stem);
  341.  
  342.     /* Convert variable name to upper-case */
  343.     for (cp=name;*cp;cp++) {
  344.         if (islower(*cp)) {
  345.             *cp=toupper(*cp);
  346.         }
  347.     }
  348.  
  349.     /* Set variable */
  350.     result=SetRexxVar(rxMsg,name,buffer,strlen(buffer));
  351.  
  352.     /* Return result */
  353.     return(result);
  354.  
  355. }
  356.