Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

options.h File Reference

Go to the source code of this file.

Compounds

struct  _option

Typedefs

typedef _option option

Enumerations

enum  TProgramOptions { optionBool, optionLong, optionString }

Functions

int get_options (int argc, char **argv, option *opttable)


Typedef Documentation

typedef struct _option option
 


Enumeration Type Documentation

enum TProgramOptions
 

Enumeration values:
optionBool 
optionLong 
optionString 

Definition at line 28 of file options.h.


Function Documentation

int get_options int    argc,
char **    argv,
option   opttable
 

Definition at line 43 of file options.cpp.

References _option::names, optionBool, optionLong, optionString, _option::type, and _option::val.

Referenced by ReadOptions().

00044 {
00045   option *opt;
00046   char *p, *next_char;
00047   // prima non opzione e inizio indice opzioni
00048   int optind, optno, optfirstind;
00049   optno = optind = 1;
00050   for ( ; optind<argc; )
00051   {
00052     // se non e' un'opzione ritorna
00053     while( argv[optind][0] != '-' )
00054       if ( ++optind == argc )
00055         return optno;
00056 
00057     optfirstind = optind;
00058 
00059     if ( argv[optind][1] == '-' )
00060     {
00061       // caso '--' ritorna indica successivo elemento
00062       if (argv[optind][2] == '\0' )
00063         return optind+1;
00064       for(opt = opttable; ; ++opt)
00065       {
00066         if ( opt->names == NULL )
00067             return -optind;
00068         p = opt->names;
00069 
00070         // caso parametri lunghi
00071         for( ;*(p=p+strlen(p)+1); )
00072         {
00073           int l = strlen(p);
00074           if ( strncmp(p,argv[optind]+2,l) == 0 )
00075           {
00076             next_char = &argv[optind][2+l];
00077             if ( opt->type != optionBool && *next_char == '=' )
00078             {
00079               // --arg=value
00080               p = next_char+1;
00081               goto param_value;
00082             }
00083             // prova con un'altra opzione
00084             if (*next_char != '\0')
00085               break;
00086             if ( opt->type == optionBool)
00087               goto param_value;
00088             // --arg value
00089             goto param_found;
00090           }
00091         }
00092       }
00093       // non raggiunge mai questo punto
00094     }
00095     else
00096     {
00097       for(p = argv[optind]+1; *p != '\0' ;++p)
00098       {
00099         for( opt = opttable; ; ++opt)
00100         {
00101           if ( opt->names == NULL )
00102             return -optind;
00103 
00104           // caso parametri a caratteri singoli
00105           if ( strchr(opt->names,*p) != NULL )
00106           {
00107             if ( opt->type != optionBool )
00108             {
00109               // -a value
00110               next_char = p+1;
00111               goto param_found;
00112             }
00113             *(bool*)(opt->val) = true;
00114             break;
00115           }
00116         }
00117       }
00118       goto option_found;
00119     }
00120 
00121 param_found:
00122     // il valore dell'opzione e' il successivo parametro
00123     if (*next_char != '\0' || ++optind == argc)
00124       return -optind;
00125     p = argv[optind];
00126 param_value:
00127     // scrivi dati in output
00128     switch(opt->type)
00129     {
00130       case optionBool:
00131         *(bool*)(opt->val) = true;
00132         break;
00133       case optionString:
00134         *(const char**)(opt->val) = p;
00135         break;
00136       case optionLong:
00137         {
00138         char *pend;
00139         if ( !isdigit(*p) )
00140           return -optind;
00141         *(long*)(opt->val) = strtol(p,&pend,0);
00142         if ( *pend != '\0' )
00143           return -optind;
00144         }
00145       break;
00146     }
00147 
00148 option_found:
00149     ++optind;
00150     // sposta in fondo gli argomenti non flag
00151     if (optfirstind != optno)
00152     {
00153       // shifta num volte opzioni all'inizio
00154       for (;optind != optfirstind;)
00155       {
00156         int n = optfirstind;
00157         char *temp = argv[n];
00158         for(;--n >= optno;)
00159           argv[n+1] = argv[n];
00160         argv[optno] = temp;
00161         ++optno;
00162         ++optfirstind;
00163       }
00164     }
00165     else
00166       optno = optind;
00167 
00168   }
00169   return optno;
00170 }


Generated on Mon Jan 13 22:20:35 2003 for perdr by doxygen1.2.15