?login_element?

Subversion Repositories NedoOS

Rev

Rev 389 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #include "args.h"
  6. #include "global.h"
  7.  
  8.  
  9. struct argstruct
  10. {
  11.         const char * match;
  12.         int * const value_ptr;
  13.         const int is_int;
  14. };
  15.  
  16. struct argstruct args_info[] =
  17. {
  18.         {
  19.                 .match     = "--prebuf",
  20.                 .value_ptr = &g.prebuf,
  21.                 .is_int    = 1
  22.         },
  23.  
  24.         {
  25.                 .match     = "--syncchk",
  26.                 .value_ptr = &g.syncchk,
  27.                 .is_int    = 0
  28.         },
  29.  
  30.         {
  31.                 .match     = "--framechk",
  32.                 .value_ptr = &g.framechk,
  33.                 .is_int    = 0
  34.         },
  35.  
  36.         {
  37.                 .match     = NULL,
  38.                 .value_ptr = NULL,
  39.                 .is_int    = 0
  40.         }
  41. };
  42.  
  43.  
  44.  
  45. int parse_args(int cur_arg, int argc, char ** argv)
  46. {
  47.         if( cur_arg>argc ) return 0;
  48.  
  49.         struct argstruct * m = NULL;
  50.  
  51.         int read_value = 0;
  52.  
  53.  
  54.  
  55.         while( cur_arg < argc )
  56.         {
  57.                 if( read_value )
  58.                 {      
  59.                         if( 1!=sscanf(argv[cur_arg++],"%d",m->value_ptr) ) return 0;
  60.  
  61.                         read_value = 0;
  62.                         m = NULL;
  63.  
  64.                         continue;
  65.                 }
  66.  
  67.                
  68.                 for(m=args_info; m->match!=NULL; m++)
  69.                 {
  70.                         if( !strcmp(m->match,argv[cur_arg]) )
  71.                         {
  72.                                 if( !m->is_int )
  73.                                         *(m->value_ptr) = 1;
  74.                                 else
  75.                                         read_value = 1;
  76.  
  77.                                 cur_arg++;
  78.                                
  79.                                 break;
  80.                         }
  81.                 }
  82.  
  83.                 if( !m->match ) return 0;
  84.         }
  85.  
  86.         if( read_value ) return 0;
  87.  
  88.  
  89.  
  90.         return 1;
  91. }
  92.  
  93.  
  94.