Pol  Revision:dd09c1c
clib/cmdargs.cpp
Go to the documentation of this file.
00001 /*
00002 History
00003 =======
00004 
00005 Notes
00006 =======
00007 
00008 */
00009 
00010 #include <stdlib.h>
00011 #include <string.h>
00012 
00013 #ifdef _MSC_VER
00014 #   define strnicmp _strnicmp
00015 #endif
00016 
00017 #include "cmdargs.h"
00018 #include "clib.h"
00019 
00020 #ifdef __BORLANDC__
00021 #include <dos.h>
00022 #define argc _argc
00023 #define argv _argv
00024 #else
00025 static int argc;
00026 static char **argv;
00027 #endif
00028 namespace Pol {
00029   namespace Clib {
00030     void StoreCmdArgs( int in_argc, char **in_argv )
00031     {
00032 #ifdef __BORLANDC__
00033 #pragma argsused
00034 #else
00035       argc = in_argc;
00036       argv = in_argv;
00037 #endif
00038     }
00039 
00040 
00041     /*
00042         Search for a command of type "-cARG" or "/cARG" where {c} == tag
00043         Return the ARG portion.
00044         Example:    Tag="F", parm = "/Ffilename"
00045         Returns: pointer to "filename"
00046         */
00047     const char *FindArg( const char *tag )
00048     {
00049       int i;
00050       size_t taglen = strlen( tag );
00051       for( i = 1; i < argc; i++ )
00052       {
00053         const char *parm = argv[i];
00054         switch( parm[0] )
00055         {
00056           case '/': case '-':
00057             if( strnicmp( tag, parm + 1, taglen ) == 0 )
00058               return parm + 1 + taglen;
00059             break;
00060         }
00061       }
00062       return NULL;
00063     }
00064 
00065     /*
00066         FindArg2: Given the entire tag, return the default or the tag specified.
00067         Used for arguments of type "tag=value"
00068         For example: alltag="cmd=", parm="cmd=a.exe"
00069         Returns: "a.exe"
00070         */
00071     const char *FindArg2( const char *alltag, const char *dflt )
00072     {
00073       int i;
00074       size_t taglen = strlen( alltag );
00075       for( i = 1; i < argc; i++ )
00076       {
00077         const char *parm = argv[i];
00078         if( strnicmp( alltag, parm, taglen ) == 0 )
00079         {
00080           return parm + taglen;
00081         }
00082       }
00083       return dflt;
00084     }
00085     const char *FindArg2( const char *alltag )
00086     {
00087       int i;
00088       size_t taglen = strlen( alltag );
00089       for( i = 1; i < argc; i++ )
00090       {
00091         const char *parm = argv[i];
00092         if( strnicmp( alltag, parm, taglen ) == 0 )
00093         {
00094           return parm + taglen;
00095         }
00096       }
00097       return NULL;
00098     }
00099 
00100     /*
00101         IntArg, LongArg: Find parameters of the form "/TAGvalue" and return the value
00102         portion, or the default.
00103         */
00104     int IntArg( const char *tag, int deflt )
00105     {
00106       const char *parm = FindArg( tag );
00107       if( parm )
00108         return atoi( parm );
00109       else
00110         return deflt;
00111     }
00112 
00113     int LongArg( const char *tag, int deflt )
00114     {
00115       const char *parm = FindArg( tag );
00116       if( parm )
00117         return atol( parm );
00118       else
00119         return deflt;
00120     }
00121 
00122     int LongArg2( const char* alltag, int dflt )
00123     {
00124       int i;
00125       size_t taglen = strlen( alltag );
00126       for( i = 1; i < argc; i++ )
00127       {
00128         const char *parm = argv[i];
00129         if( strnicmp( alltag, parm, taglen ) == 0 )
00130         {
00131           return atol( parm + taglen );
00132         }
00133       }
00134       return dflt;
00135     }
00136 
00137     /* assume parameter following tag is hexadecimal (0x unnecessary) */
00138     int LongHexArg( const char *tag, int deflt )
00139     {
00140       const char *parm = FindArg( tag );
00141       if( parm )
00142         return strtoul( parm, NULL, 16 );
00143       else
00144         return deflt;
00145     }
00146 
00147     int LongHexArg2( const char *tag, int deflt )
00148     {
00149       const char *parm = FindArg2( tag );
00150       if( parm )
00151         return strtoul( parm, NULL, 16 );
00152       else
00153         return deflt;
00154     }
00155   }
00156 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines