home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0904.lha / Fill / PModules / commandLineArgs.e next >
Text File  |  1993-08-26  |  2KB  |  80 lines

  1. PMODULE 'PMODULES:skipWhite'
  2. PMODULE 'PMODULES:skipNonWhite'
  3.  
  4.  
  5.  
  6. PROC getArg (theArg,  /* PTR TO STRING       */
  7.              index)   /* Argument id, 1 .. n */
  8.   DEF startPos, endPos, i, length
  9.  
  10.   /* This routine is intended for KS1.3 programmers who don't have access */
  11.   /* to taglists.  To get the first command-line argument pass in a       */
  12.   /* string large enough to hold arg (s := String (StrLen (arg)) ), and   */
  13.   /* a long int where 1 = first argument, 2 = second argument...  If      */
  14.   /* the requested argument doesn't exist (3 is requested when only 2     */
  15.   /* were entered on the command line) the function returns -1.  This     */
  16.   /* function does not recognize quoted arguments with embedded spaces.   */
  17.  
  18.   IF arg [] <= 0
  19.     StrCopy (theArg, '', ALL)
  20.     RETURN FALSE
  21.   ENDIF
  22.  
  23.   length := StrLen (arg)
  24.   startPos := 0
  25.  
  26.   FOR i := 2 TO index
  27.     startPos := skipNonWhite (arg, startPos)  /* Find next space. */
  28.     startPos := skipWhite (arg, startPos)     /* Find start of next arg. */
  29.     IF (startPos = length)
  30.       /* Access beyond end of string, and we haven't reached requested arg. */
  31.       StrCopy (theArg, '', ALL)
  32.       RETURN FALSE
  33.     ENDIF
  34.   ENDFOR
  35.  
  36.   endPos := skipNonWhite (arg, startPos)    /* Find end of arg. */
  37.   MidStr (theArg, arg, startPos, (endPos - startPos))
  38.  
  39. ENDPROC  TRUE
  40.   /* getArg */
  41.  
  42.  
  43.  
  44. PROC main () HANDLE
  45.   ENUM ER_NONE,
  46.        ER_USAGE,
  47.        ER_MEM
  48.   DEF anArg,
  49.       i = 1
  50.  
  51.   /* This main procedure demonstrates the usage of getArg () */
  52.  
  53.   IF arg [] <= 0 THEN Raise (ER_USAGE)
  54.  
  55.   anArg := String (StrLen (arg))
  56.   IF anArg = NIL THEN Raise (ER_MEM)
  57.  
  58.   WHILE getArg (anArg, i++)
  59.     WriteF ('Arg = \a\s\a\n', anArg)  /* In the using program you can check */
  60.                                       /* the value of anArg (ot whatever    */
  61.                                       /* you call it) and copy it to the    */
  62.                                       /* appropriate variable.              */
  63.     /*INC i*/
  64.   ENDWHILE
  65.  
  66.   CleanUp (0)
  67.  
  68. EXCEPT
  69.  
  70.   SELECT exception
  71.     CASE ER_USAGE;   WriteF ('Illegal usage.\n')
  72.     CASE ER_MEM;     WriteF ('Get more memory!\n')
  73.   ENDSELECT
  74.  
  75.   CleanUp (exception)
  76.  
  77. ENDPROC
  78.   /* getArg */
  79.  
  80.