home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_07 / 8n07047a < prev    next >
Text File  |  1990-06-19  |  2KB  |  98 lines

  1.  
  2.  
  3. ------------------------------------------------------
  4. LISTING 3
  5.  
  6.  
  7. /*        Options.c
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdarg.h>
  12. #include "Options.h"
  13. struct tOPTIONS{
  14.     int argc, next;
  15.     char    **argv;
  16.     boolean  *used;
  17.     };
  18. typedef struct tOPTIONS OPTIONS;
  19. #define this ((OPTIONS *)options)
  20.     static void
  21. ErrorExit( char *format, ...){
  22.         va_list ap;
  23.     va_start( ap, format);
  24.     fprintf( stderr, "Error:  ");
  25.     vfprintf( stderr, format, ap);
  26.     fprintf( stderr, "!\nAborting.\n");
  27.     exit(1);
  28.     va_end( ap );
  29.     }
  30.     boolean
  31. IsSwitch( Options options, const char switch_character){
  32.         int k =0;
  33.     for( ;k < this->argc; k++ )
  34.         if( this->argv[k][0] == '-' &&
  35.             this->argv[k][1] == switch_character
  36.             )return (this->used[k] =TRUE);
  37.     return FALSE;
  38.     }
  39.     char *
  40. GetParameter( Options options, const char switch_character){
  41.         int k =1;
  42.     for( ;k < this->argc; k++ )
  43.         if( this->argv[k][0] == '-' &&
  44.             this->argv[k][1] == switch_character
  45.             ){
  46.             this->used[k++] =TRUE;
  47.             this->used[k]   =TRUE;
  48.             return this->argv[k];
  49.             }
  50.     return NULL;
  51.     }
  52.     boolean
  53. IsMoreSwitches( Options options ){
  54.         int k =0;
  55.     for(; k < this->argc; k++)
  56.         if( !this->used[k] && this->argv[k][0] == '-' )
  57.         return TRUE;
  58.     return FALSE;
  59.     }
  60.     char *
  61. GetNextOption( Options options ){
  62.     for( ;this->next < this->argc; this->next++)
  63.         if(    !this->used[this->next] && 
  64.          this->argv[this->next][0] != '-'
  65.         )return( this->argv[this->next++] );
  66.     return NULL;
  67.     }
  68.     Options
  69. CreateOptions(void){
  70.         OPTIONS *opt;
  71.     if( (opt =calloc( 1, sizeof(OPTIONS))) == NULL )
  72.         ErrorExit("Out of memory in CreateOptions()");
  73.     opt->argc =0;
  74.     opt->argv =NULL;
  75.     opt->next =0;
  76.     opt->used =NULL;
  77.     return (Options)opt;
  78.     }
  79.     void
  80. PutArgs( Options options, const int argc, char **argv){
  81.     this->argc =argc -1;/* ignore the program name */
  82.     this->argv =argv +1;/* in argv[0]              */
  83.     this->next =0;
  84.     if( (this->used =calloc(this->argc,sizeof(boolean)))
  85.         == NULL )ErrorExit("Out of memory in PutArgs()");
  86.         {     int k=0; 
  87.             while( k < this->argc )
  88.                 this->used[k++] =FALSE;
  89.         }
  90.     }
  91.     void
  92. DestroyOptions( Options options){
  93.     if( this->used )free( this->used );
  94.     free( this );
  95.     }
  96.  
  97.  
  98.