home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ioctlapi.zip / appsrc.zip / parse.c < prev    next >
C/C++ Source or Header  |  1999-11-16  |  4KB  |  158 lines

  1. //-----------------------------------------------------------------------------
  2. // Freeware.  This file may be used freely to promote the ioctl90 mixer API.
  3. //-----------------------------------------------------------------------------
  4.  
  5. // Parse.c - Parse command line parameters
  6.  
  7. #include <stdio.h>
  8. #include <string.h>                     // memset
  9. #include <ctype.h>                      // topupper
  10.  
  11. #include <os2.h>                        // typedefs
  12.  
  13. #include "data.h"
  14. #include "parse.h"
  15. #include "asciinum.h"
  16.  
  17. // Store parsed token here to make easy to see in debugger
  18. PARSEDTOKEN Token;
  19.  
  20. // Given token which is first element of an ASCII-Z string,
  21. // parse the token to separate the parts left and right of the colon.
  22. // This can be useful for parsing config.sys command line options.
  23. // Numeric parameters are converted as HEX.
  24.  
  25. void SplitToken (char *pszLine, PPARSEDTOKEN pToken)
  26. {
  27.    char   Ch;
  28.    int    I;
  29.    int    iRC;
  30.  
  31.    memset (pToken, '\0', sizeof (PARSEDTOKEN));
  32.  
  33.    if ( *pszLine == '-' || *pszLine == '/' )
  34.    {
  35.       pToken->fLeadingDashFound = TRUE;
  36.       pszLine++;
  37.    }
  38.  
  39.    // Copy all characters before the colon
  40.    Ch = toupper (*pszLine);
  41.    I = 0;
  42.    while ( Ch > ' ' && Ch != ':' && I < MAXTOKENSIZE )
  43.    {
  44.       pToken->szKeyword[I] = Ch;          // Copy to output string
  45.       pszLine++;
  46.       Ch = toupper (*pszLine);
  47.       I++;
  48.  
  49.       // For specifying options where the value is a number, the
  50.       // separator (colon) can be omitted if the parser can determine
  51.       // that the value is concatenated next to the keywork (keychar).
  52.       if ( pToken->fLeadingDashFound && isdigit (Ch) && I == 1)
  53.       {
  54.          pToken->fSeparatorFound = TRUE; // Implied separator
  55.          break;
  56.       }
  57.    }
  58.  
  59.    if (*pszLine == ':')
  60.    {
  61.       pToken->fSeparatorFound = TRUE;
  62.       pszLine++;
  63.    }
  64.  
  65.    iRC = AsciiToUlong (pszLine, &pToken->ulValue, BASE16);
  66.    pToken->fNumericFound = (iRC == 0);
  67.  
  68.    // Copy the characters right of the colon
  69.    Ch = toupper (*pszLine);
  70.    I = 0;
  71.    while (Ch > ' ' && I < MAXTOKENSIZE)
  72.    {
  73.       pToken->szValue[I] = Ch;        // Copy to output string
  74.       pszLine++;
  75.       Ch = toupper (*pszLine);
  76.       I++;
  77.    }
  78.  
  79.    // If token did not follow keyword:value format and if the
  80.    // token represents an ascii number then determine, the numeric value.
  81.    if (! pToken->fLeadingDashFound && ! pToken->fSeparatorFound)
  82.    {
  83.       iRC = AsciiToUlong (pToken->szKeyword, &pToken->ulValue, BASE16);
  84.       pToken->fNumericFound = (iRC == 0);
  85.    }
  86. }
  87.  
  88.  
  89. char *BoolTicValue (BOOL fValue)
  90. {
  91.    if ( fValue )
  92.       return ("TRUE");
  93.    else
  94.       return ("FALSE");
  95. }
  96.  
  97.  
  98. int ParseToken (char *Parm)
  99. {
  100.    memset (&Token, '\0', sizeof (Token));
  101.    SplitToken (Parm, &Token);
  102.  
  103.    if (Token.fLeadingDashFound)
  104.    {
  105.       if (strlen (Token.szKeyword) == 1)
  106.       {
  107.          switch (Token.szKeyword[0])
  108.          {
  109.          case 'D':
  110.             Options.Debug = TRUE;
  111.             break;
  112.  
  113.          case 'V':
  114.             Options.Verbose = TRUE;
  115.             break;
  116.  
  117.          case 'H':
  118.          case '?':
  119.             Options.Help = TRUE;
  120.             break;
  121.          }
  122.       }
  123.       else if (strcmp (Token.szKeyword, "HELP") == 0)
  124.       {
  125.          Options.Help         = TRUE;
  126.          Options.ExtendedHelp = TRUE;
  127.       }
  128.    }
  129.  
  130.  
  131.    if (Options.Debug)
  132.    {
  133.       printf ("\nToken: \"%s\"\n", Parm);
  134.       printf ("   fLeadingDashFound %s\n", BoolTicValue (Token.fLeadingDashFound));
  135.       printf ("   fSeparatorFound   %s\n", BoolTicValue (Token.fSeparatorFound));
  136.       printf ("   fNumericFound     %s\n", BoolTicValue (Token.fNumericFound));
  137.       printf ("   ulValue (hex)     %x\n", Token.ulValue);
  138.       printf ("   szKeyword         %s\n", Token.szKeyword);
  139.       printf ("   szValue           %s\n", Token.szValue);
  140.    }
  141.  
  142.    return (0);
  143. }
  144.  
  145.  
  146. int ParseCommandLine (int argc, char *argv[])
  147. {
  148.    int iRC = 0;
  149.    int I;
  150.  
  151.    for (I=1; I < argc; I++)
  152.    {
  153.       iRC = ParseToken (argv[I]);
  154.    }
  155.  
  156.    return (iRC);
  157. }
  158.