home *** CD-ROM | disk | FTP | other *** search
/ HomeWare 14 / HOMEWARE14.bin / os2 / cenv2_19.arj / OPTPARMS.LIB < prev    next >
Text File  |  1994-03-08  |  2KB  |  70 lines

  1. // OptParms.lib - Library for the OptionalParameter() routine: to
  2. // ver.1          parse optional paramters from the input to a
  3. //                Cmm program.
  4. //
  5. // Arguments are case-insentitive and can take any of these forms:
  6. //   Program /SETTING VALUE
  7. //   Program /SETTING=VALUE
  8. //   Program -SETTING VALUE
  9. //   Program -SETTING=VALUE
  10. //   Program /BOOL
  11. //   Program -BOOL
  12. //
  13. //
  14. // SYNTAX: bool OptionalParameter(argc,argv,Setting[,Value])
  15. // WHERE: argc: argument count, as in main(argc,argv)
  16. //        argv: input arguments, as in main(argc,argv)
  17. //        Setting: case-insensitive string to match arguments
  18. //        Value: optional input to get value following Setting.
  19. //               If Value is not supplied then Settings is a boolean
  20. // RETURN: TRUE if Setting[Value] found, and sets Value and adjusts
  21. //         argc and argv; else returns FALSE and no arguments changed
  22. //
  23.  
  24. OptionalParameter(iArgC,iArgV,iSetting,iValue)
  25. {
  26.    BoolSetting = ( va_arg() < 4 );
  27.    RemoveArgCount = 1; // default
  28.    SettingLen = strlen(iSetting);
  29.    for ( _i = 1; _i < iArgC; _i++ ) {
  30.       if ( strchr("/-",iArgV[_i][0]) ) {
  31.          _Setting = iArgV[_i] + 1;
  32.          if ( !strnicmp(iSetting,_Setting,SettingLen) ) {
  33.             switch ( _Setting[SettingLen] ) {
  34.                case 0:
  35.                   if ( BoolSetting ) {
  36.                      RemoveMainArg(iArgC,iArgV,_i);
  37.                      return( TRUE );
  38.                   }
  39.                   if ( _i < (iArgC - 1) ) {
  40.                      // Next argument is value for this setting
  41.                      RemoveMainArg(iArgC,iArgV,_i);
  42.                      strcpy(iValue,iArgV[_i]);
  43.                      RemoveMainArg(iArgC,iArgV,_i);
  44.                      return(TRUE);
  45.                   }
  46.                   break;
  47.                case '=':
  48.                   if ( !BoolSetting ) {
  49.                      strcpy(iValue,_Setting + SettingLen + 1);
  50.                      RemoveMainArg(iArgC,iArgV,_i);
  51.                      return(TRUE);
  52.                   }
  53.                   break;
  54.             }
  55.          }
  56.       }
  57.    }
  58.    return( FALSE );
  59. }
  60.  
  61.  
  62. RemoveMainArg(iArgC,iArgV,iIndex) // remove iIndex indexed arg
  63. {
  64.    for ( _i = iIndex; _i < iArgC; _i++ ) {
  65.       iArgV[_i] = iArgV[_i+1];
  66.    }
  67.    iArgC--;
  68. }
  69.  
  70.