home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / sql / dblib / c / textcopy / getopt.c next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  4.6 KB  |  147 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FILE: getopt.c
  4. //
  5. //      GetOption function
  6. //
  7. //  FUNCTIONS:
  8. //
  9. //      GetOption() - Get next command line option and parameter
  10. //
  11. //  COMMENTS:
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14.  
  15. #include <stddef.h>
  16. #include <ctype.h>
  17. #include <string.h>
  18. #include "getopt.h"
  19.  
  20. ///////////////////////////////////////////////////////////////////////////////
  21. //
  22. //  FUNCTION: GetOption()
  23. //
  24. //      Get next command line option and parameter
  25. //
  26. //  PARAMETERS:
  27. //
  28. //      argc - count of command line arguments
  29. //      argv - array of command line argument strings
  30. //      pszValidOpts - string of valid, case-sensitive option characters,
  31. //                     a colon ':' following a given character means that
  32. //                     option can take a parameter
  33. //      ppszParam - pointer to a pointer to a string for output
  34. //
  35. //  RETURNS:
  36. //
  37. //      If valid option is found, the character value of that option
  38. //          is returned, and *ppszParam points to the parameter if given,
  39. //          or is NULL if no param
  40. //      If standalone parameter (with no option) is found, 1 is returned,
  41. //          and *ppszParam points to the standalone parameter
  42. //      If option is found, but it is not in the list of valid options,
  43. //          -1 is returned, and *ppszParam points to the invalid argument
  44. //      When end of argument list is reached, 0 is returned, and
  45. //          *ppszParam is NULL
  46. //
  47. //  COMMENTS:
  48. //
  49. ///////////////////////////////////////////////////////////////////////////////
  50.  
  51. int GetOption (
  52.     int argc,
  53.     char** argv,
  54.     char* pszValidOpts,
  55.     char** ppszParam)
  56. {
  57.     static int iArg = 1;
  58.     char chOpt;
  59.     char* psz = NULL;
  60.     char* pszParam = NULL;
  61.  
  62.     if (iArg < argc)
  63.     {
  64.         psz = &(argv[iArg][0]);
  65.         if (*psz == '-' || *psz == '/')
  66.         {
  67.             // we have an option specifier
  68.             chOpt = argv[iArg][1];
  69.             if (isalnum(chOpt) || ispunct(chOpt))
  70.             {
  71.                 // we have an option character
  72.                 psz = strchr(pszValidOpts, chOpt);
  73.                 if (psz != NULL)
  74.                 {
  75.                     // option is valid, we want to return chOpt
  76.                     if (psz[1] == ':')
  77.                     {
  78.                         // option can have a parameter
  79.                         psz = &(argv[iArg][2]);
  80.                         if (*psz == '\0')
  81.                         {
  82.                             // must look at next argv for param
  83.                             if (iArg+1 < argc)
  84.                             {
  85.                                 psz = &(argv[iArg+1][0]);
  86.                                 if (*psz == '-' || *psz == '/')
  87.                                 {
  88.                                     // next argv is a new option, so param
  89.                                     // not given for current option
  90.                                 }
  91.                                 else
  92.                                 {
  93.                                     // next argv is the param
  94.                                     iArg++;
  95.                                     pszParam = psz;
  96.                                 }
  97.                             }
  98.                             else
  99.                             {
  100.                                 // reached end of args looking for param
  101.                             }
  102.  
  103.                         }
  104.                         else
  105.                         {
  106.                             // param is attached to option
  107.                             pszParam = psz;
  108.                         }
  109.                     }
  110.                     else
  111.                     {
  112.                         // option is alone, has no parameter
  113.                     }
  114.                 }
  115.                 else
  116.                 {
  117.                     // option specified is not in list of valid options
  118.                     chOpt = -1;
  119.                     pszParam = &(argv[iArg][0]);
  120.                 }
  121.             }
  122.             else
  123.             {
  124.                 // though option specifier was given, option character
  125.                 // is not alpha or was was not specified
  126.                 chOpt = -1;
  127.                 pszParam = &(argv[iArg][0]);
  128.             }
  129.         }
  130.         else
  131.         {
  132.             // standalone arg given with no option specifier
  133.             chOpt = 1;
  134.             pszParam = &(argv[iArg][0]);
  135.         }
  136.     }
  137.     else
  138.     {
  139.         // end of argument list
  140.         chOpt = 0;
  141.     }
  142.  
  143.     iArg++;
  144.     *ppszParam = pszParam;
  145.     return (chOpt);
  146. }
  147.