File: args.c

package info (click to toggle)
paraview 4.0.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 526,572 kB
  • sloc: cpp: 2,284,430; ansic: 816,374; python: 239,936; xml: 70,162; tcl: 48,295; fortran: 39,116; yacc: 5,466; java: 3,518; perl: 3,107; lex: 1,620; sh: 1,555; makefile: 932; asm: 471; pascal: 228
file content (221 lines) | stat: -rw-r--r-- 7,054 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* $Id: args.c,v 1.19 2008/11/03 23:48:35 rcook Exp $ */
#include "args.h"
#include "debugutil.h"
#include <stdio.h>
#include <string.h> 
static int gNumArgs = 0;
static argt *gArgs = NULL;
static int gIgnoreBadArgs = 0; 
static int gArgDebug = 0; 

/*------------------------------------------------------------------*/
void debug_args(int iYesNo ){
  gArgDebug = iYesNo; 
}
/*------------------------------------------------------------------*/
void print_args(int argc, char *argv[] ){
  int i = 0 ;
  printf("Program called as: "); 
  while (i<argc) {
    printf("%s ", argv[i]); 
    ++i; 
  }
  printf("\n");
  return ;
}

/*------------------------------------------------------------------*/
void arg_ignore_bad_args(int iYesNo) { gIgnoreBadArgs = iYesNo; }

/*------------------------------------------------------------------*/
void arg_expect_args(argt args[], int numargs) {
  gNumArgs = numargs;
  if (gArgs) free(gArgs);
  gArgs = (argt*)malloc(sizeof(argt)* gNumArgs);
  memcpy(gArgs, args, sizeof(argt)*gNumArgs);
  return;
}

/*------------------------------------------------------------------*/
argt *arg_find_entry(char *flag){
  int argnum = 0;
  while (argnum < gNumArgs){
    if (!strcmp(gArgs[argnum]._flag, flag)) 
      return &gArgs[argnum];
    ++argnum;
  }
  
  return NULL;
}

/*------------------------------------------------------------------*/
void print_arg_entry(argt *iEntry) {
  fprintf(stderr, "arg entry: \n"); 
  fprintf(stderr, " iEntry->_type = %d\n", iEntry->_type); 
  fprintf(stderr, " iEntry->_flag = %s\n", iEntry->_flag); 
  fprintf(stderr, " iEntry->_numvals = %d\n", iEntry->_numvals); 
  switch(iEntry->_type) {
  case   BOOL_TYPE:  /* must be type long**/
  case   INT_TYPE:   /* must be type int* */
    if (!iEntry->_values) fprintf(stderr, "(empty)\n"); 
    vectprint(1, ((int*)iEntry->_values), iEntry->_numvals); 
    break;
  case   LONG_TYPE:   /* must be type long* */
  case   LONG_ARRAY_TYPE:   /* must be type long* */
    if (!iEntry->_values) fprintf(stderr, "(empty)\n"); 
    vectprint(1, ((long*)iEntry->_values), iEntry->_numvals); 
    break;
  case   FLOAT_TYPE:   /* must be type long* */
  case   FLOAT_ARRAY_TYPE:   /* must be type long* */
    if (!iEntry->_values) fprintf(stderr, "(empty)\n"); 
    vectprint(1, ((float*)iEntry->_values), iEntry->_numvals); 
    break;
  case   DOUBLE_TYPE: /* must be type double* */
  case   DOUBLE_ARRAY_TYPE: /* must be type double* */
    if (!iEntry->_values) fprintf(stderr, "(empty)\n"); 
    vectprint(1, ((long*)iEntry->_values), iEntry->_numvals); 
    break;
  case   STRING_TYPE:  /* must be type (char *) */
    fprintf(stderr, " iEntry->_values = %s\n", (char*)(iEntry->_values)); 
    break;
  }
  fprintf(stderr, "\n"); 
  return; 
}

/*! 
   "consume" the requested arg in argv by 
   shifting all later args one to the left 
*/ 
void ConsumeArg(int *argc, char *argv[], int position) {
  /*  fprintf(stderr, "Before ConsumeArg(%d, argv, %d): ", argc, position); 
      printargv(argc, argv); */
  if (position == 0 && *argc == 1) {
    argv[0] = NULL; 
  } else {
    while (position < *argc) {
      argv[position] = argv[position+1];
      ++position; 
    }
  }
  (*argc)--; 
  /* fprintf(stderr, "After ConsumeArg: "); 
     printargv(argc, argv); */
  return; 
}
  
/*------------------------------------------------------------------*/
/*!
  arg_parse_args
   return 0 on error, 1 if everything's OK.  
   parse argv, looking for flags set using arg_expect_args, starting at argv[1].
   Continue until no more valid args are found.  
   Removes all parsed args and options from argv and decrements numargs approprately.  Bad args are ignored, unless gIgnoreBadArgs is false; 
*/
int arg_parse_args(int *argc, char *argv[]){ 
  int argnum=1;
  argt*entry=NULL;
  if (!gArgs) {
    dbprintf(1, "Error parsing args:  arg_parse_args called without calling  arg_expect_args\n"); 
    return 0; 
  }
  
  while (argnum < *argc) {
    char *theArg = argv[argnum];     
    if (theArg[0] != '-') {
      argnum++;
      continue;  
    }
    entry = arg_find_entry(theArg);
    if (!entry){
      if (gIgnoreBadArgs) {
        argnum++;
        continue;   
      }
      else {
        fprintf(stderr, "Error parsing args:  bad arg: \"%s\"\n", theArg); 
        return 0;
      }
    } /* end bad flag code */ 
    else {
      ConsumeArg(argc, argv, argnum); 
      if (entry->_type != BOOL_TYPE && argnum == *argc) {
        fprintf(stderr, "ERROR -- arg_parse_args -- missing argument to %s\n", entry->_flag); 
        return 0; 
      }
      if (gArgDebug) fprintf(stderr, "entry type %d\n", entry->_type); 
      switch (entry->_type){
      case BOOL_TYPE:    
        *((long*)entry->_values)=1;
        break;
      case INT_TYPE:
        {
          int *values = (int*)entry->_values; 
          int valnum=entry->_numvals; while (valnum--) {
            if (argnum == *argc) {
              fprintf(stderr, "ERROR -- arg_parse_args -- missing argument to %s\n", entry->_flag); 
              return 0; 
            }
            *values++ = atoi(argv[argnum]);
            ConsumeArg(argc, argv, argnum); 
          }    
        }    
        break;        
      case LONG_TYPE:    
      case LONG_ARRAY_TYPE:
        {
          long *values = (long*)entry->_values; 
          int valnum=entry->_numvals; while (valnum--) {
            if (argnum == *argc) {
              fprintf(stderr, "ERROR -- arg_parse_args -- missing argument to %s\n", entry->_flag); 
              return 0; 
            }
            *values++ = strtol(argv[argnum], NULL, 10);
            ConsumeArg(argc, argv, argnum); 
          }    
        }    
        break;
      case FLOAT_TYPE:    
      case FLOAT_ARRAY_TYPE:
        {
          float *values = (float*)entry->_values; 
          int valnum=entry->_numvals; while (valnum--) {
            if (argnum == *argc) {
              fprintf(stderr, "ERROR -- arg_parse_args -- missing argument to %s\n", entry->_flag); 
              return 0; 
            }
            *values++ = strtod(argv[argnum], NULL);
            ConsumeArg(argc, argv, argnum); 
          }    
        }    
        break;
      case DOUBLE_TYPE:
      case DOUBLE_ARRAY_TYPE:
        {
          double *values = (double*)entry->_values; 
          int valnum=entry->_numvals; while (--valnum) {
            if (argnum == *argc) {
              fprintf(stderr, "ERROR -- arg_parse_args -- missing argument to %s\n", entry->_flag); 
              return 0; 
            }
            *values++ = strtod(argv[argnum], NULL);
            ConsumeArg(argc, argv, argnum); 
          }    
        }    
        break;
      case STRING_TYPE:
        strcpy((char*)entry->_values, argv[argnum]);
        ConsumeArg(argc, argv, argnum); 
        break;
      default:
        fprintf(stderr, "ERROR -- args.c: Can't identify my own type (%d)!\n", 
                (int)entry->_type);
        return 0;
        break;
      }
      if (gArgDebug) print_arg_entry(entry);  
    }
  }
  return 1; 
}