ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/applications/randomBuilder/randomBuilderCmd.cpp
(Generate patch)

Comparing branches/development/src/applications/randomBuilder/randomBuilderCmd.c (file contents):
Revision 1465 by chuckv, Fri Jul 9 23:08:25 2010 UTC vs.
Revision 1630 by gezelter, Wed Sep 14 21:40:12 2011 UTC

# Line 1 | Line 1
1   /*
2 <  File autogenerated by gengetopt version 2.16
2 >  File autogenerated by gengetopt version 2.22.4
3    generated with the following command:
4 <  gengetopt -F randomBuilderCmd --unamed-opts
4 >  gengetopt -F randomBuilderCmd -u
5  
6    The developers of gengetopt consider the fixed text that goes in all
7    gengetopt output files to be in the public domain:
# Line 17 | Line 17
17   #include <stdlib.h>
18   #include <string.h>
19  
20 < #include "getopt.h"
20 > #ifndef FIX_UNUSED
21 > #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 > #endif
23  
24 + #include <getopt.h>
25 +
26   #include "randomBuilderCmd.h"
27  
28 + const char *gengetopt_args_info_purpose = "";
29 +
30 + const char *gengetopt_args_info_usage = "Usage: randomBuilder [OPTIONS]... [FILES]...";
31 +
32 + const char *gengetopt_args_info_description = "";
33 +
34 + const char *gengetopt_args_info_help[] = {
35 +  "  -h, --help                Print help and exit",
36 +  "  -V, --version             Print version and exit",
37 +  "  -o, --output=STRING       Output file name",
38 +  "      --density=DOUBLE      density (g/cm^3)",
39 +  "      --nx=INT              number of unit cells in x",
40 +  "      --ny=INT              number of unit cells in y",
41 +  "      --nz=INT              number of unit cells in z",
42 +  "      --molFraction=DOUBLE  (Default) Builds a multi-component random mixed \n                              nanoparticle. Mole Fraction must be specified for \n                              each componet > 1 in MD file.",
43 +    0
44 + };
45 +
46 + typedef enum {ARG_NO
47 +  , ARG_STRING
48 +  , ARG_INT
49 +  , ARG_DOUBLE
50 + } cmdline_parser_arg_type;
51 +
52   static
53   void clear_given (struct gengetopt_args_info *args_info);
54   static
55   void clear_args (struct gengetopt_args_info *args_info);
56  
57   static int
58 < cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error);
58 > cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
59 >                        struct cmdline_parser_params *params, const char *additional_error);
60  
61   static int
62   cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
63  
35 struct molFraction_list
36 {
37  double molFraction_arg;
38  char *molFraction_orig;
39  struct molFraction_list * next;
40 };
41
64   static char *
65   gengetopt_strdup (const char *s);
66  
# Line 58 | Line 80 | void clear_args (struct gengetopt_args_info *args_info
80   static
81   void clear_args (struct gengetopt_args_info *args_info)
82   {
83 +  FIX_UNUSED (args_info);
84    args_info->output_arg = NULL;
85    args_info->output_orig = NULL;
86    args_info->density_orig = NULL;
# Line 69 | Line 92 | void clear_args (struct gengetopt_args_info *args_info
92    
93   }
94  
95 + static
96 + void init_args_info(struct gengetopt_args_info *args_info)
97 + {
98 +
99 +
100 +  args_info->help_help = gengetopt_args_info_help[0] ;
101 +  args_info->version_help = gengetopt_args_info_help[1] ;
102 +  args_info->output_help = gengetopt_args_info_help[2] ;
103 +  args_info->density_help = gengetopt_args_info_help[3] ;
104 +  args_info->nx_help = gengetopt_args_info_help[4] ;
105 +  args_info->ny_help = gengetopt_args_info_help[5] ;
106 +  args_info->nz_help = gengetopt_args_info_help[6] ;
107 +  args_info->molFraction_help = gengetopt_args_info_help[7] ;
108 +  args_info->molFraction_min = 0;
109 +  args_info->molFraction_max = 0;
110 +  
111 + }
112 +
113   void
114   cmdline_parser_print_version (void)
115   {
116 <  printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
116 >  printf ("%s %s\n",
117 >     (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
118 >     CMDLINE_PARSER_VERSION);
119   }
120  
121 + static void print_help_common(void) {
122 +  cmdline_parser_print_version ();
123 +
124 +  if (strlen(gengetopt_args_info_purpose) > 0)
125 +    printf("\n%s\n", gengetopt_args_info_purpose);
126 +
127 +  if (strlen(gengetopt_args_info_usage) > 0)
128 +    printf("\n%s\n", gengetopt_args_info_usage);
129 +
130 +  printf("\n");
131 +
132 +  if (strlen(gengetopt_args_info_description) > 0)
133 +    printf("%s\n\n", gengetopt_args_info_description);
134 + }
135 +
136   void
137   cmdline_parser_print_help (void)
138   {
139 <  cmdline_parser_print_version ();
140 <  printf("\nUsage: randomBuilder [OPTIONS]... [FILES]...\n\n");
141 <  printf("%s\n","  -h, --help                Print help and exit");
142 <  printf("%s\n","  -V, --version             Print version and exit");
85 <  printf("%s\n","  -o, --output=STRING       Output file name");
86 <  printf("%s\n","      --density=DOUBLE      density (g/cm^3)");
87 <  printf("%s\n","      --nx=INT              number of unit cells in x");
88 <  printf("%s\n","      --ny=INT              number of unit cells in y");
89 <  printf("%s\n","      --nz=INT              number of unit cells in z");
90 <  printf("%s\n","      --molFraction=DOUBLE  (Default) Builds a multi-component random mixed \n                              nanoparticle. Mole Fraction must be specified for \n                              each componet > 1 in MD file.");
91 <  
139 >  int i = 0;
140 >  print_help_common();
141 >  while (gengetopt_args_info_help[i])
142 >    printf("%s\n", gengetopt_args_info_help[i++]);
143   }
144  
145   void
# Line 96 | Line 147 | cmdline_parser_init (struct gengetopt_args_info *args_
147   {
148    clear_given (args_info);
149    clear_args (args_info);
150 +  init_args_info (args_info);
151  
152 <  args_info->inputs = NULL;
152 >  args_info->inputs = 0;
153    args_info->inputs_num = 0;
154   }
155  
156 + void
157 + cmdline_parser_params_init(struct cmdline_parser_params *params)
158 + {
159 +  if (params)
160 +    {
161 +      params->override = 0;
162 +      params->initialize = 1;
163 +      params->check_required = 1;
164 +      params->check_ambiguity = 0;
165 +      params->print_errors = 1;
166 +    }
167 + }
168 +
169 + struct cmdline_parser_params *
170 + cmdline_parser_params_create(void)
171 + {
172 +  struct cmdline_parser_params *params =
173 +    (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
174 +  cmdline_parser_params_init(params);  
175 +  return params;
176 + }
177 +
178   static void
179 < cmdline_parser_release (struct gengetopt_args_info *args_info)
179 > free_string_field (char **s)
180   {
181 <  
108 <  unsigned int i;
109 <  if (args_info->output_arg)
181 >  if (*s)
182      {
183 <      free (args_info->output_arg); /* free previous argument */
184 <      args_info->output_arg = 0;
183 >      free (*s);
184 >      *s = 0;
185      }
186 <  if (args_info->output_orig)
187 <    {
188 <      free (args_info->output_orig); /* free previous argument */
189 <      args_info->output_orig = 0;
190 <    }
191 <  if (args_info->density_orig)
192 <    {
193 <      free (args_info->density_orig); /* free previous argument */
194 <      args_info->density_orig = 0;
195 <    }
196 <  if (args_info->nx_orig)
197 <    {
198 <      free (args_info->nx_orig); /* free previous argument */
199 <      args_info->nx_orig = 0;
200 <    }
201 <  if (args_info->ny_orig)
202 <    {
203 <      free (args_info->ny_orig); /* free previous argument */
204 <      args_info->ny_orig = 0;
205 <    }
206 <  if (args_info->nz_orig)
207 <    {
208 <      free (args_info->nz_orig); /* free previous argument */
209 <      args_info->nz_orig = 0;
210 <    }
211 <  if (args_info->molFraction_arg)
212 <    {
213 <      for (i = 0; i < args_info->molFraction_given; ++i)
214 <        {
215 <          if (args_info->molFraction_orig [i])
216 <            {
217 <              free (args_info->molFraction_orig [i]); /* free previous argument */
218 <              args_info->molFraction_orig [i] = 0;
219 <            }
220 <        }
221 <      free (args_info->molFraction_arg); /* free previous argument */
222 <      args_info->molFraction_arg = 0;
223 <      free (args_info->molFraction_orig); /* free previous argument */
224 <      args_info->molFraction_orig = 0;
225 <    }
186 > }
187 >
188 > /** @brief generic value variable */
189 > union generic_value {
190 >    int int_arg;
191 >    double double_arg;
192 >    char *string_arg;
193 >    const char *default_string_arg;
194 > };
195 >
196 > /** @brief holds temporary values for multiple options */
197 > struct generic_list
198 > {
199 >  union generic_value arg;
200 >  char *orig;
201 >  struct generic_list *next;
202 > };
203 >
204 > /**
205 > * @brief add a node at the head of the list
206 > */
207 > static void add_node(struct generic_list **list) {
208 >  struct generic_list *new_node = (struct generic_list *) malloc (sizeof (struct generic_list));
209 >  new_node->next = *list;
210 >  *list = new_node;
211 >  new_node->arg.string_arg = 0;
212 >  new_node->orig = 0;
213 > }
214 >
215 > /**
216 > * The passed arg parameter is NOT set to 0 from this function
217 > */
218 > static void
219 > free_multiple_field(unsigned int len, void *arg, char ***orig)
220 > {
221 >  unsigned int i;
222 >  if (arg) {
223 >    for (i = 0; i < len; ++i)
224 >      {
225 >        free_string_field(&((*orig)[i]));
226 >      }
227 >
228 >    free (arg);
229 >    free (*orig);
230 >    *orig = 0;
231 >  }
232 > }
233 >
234 >
235 > static void
236 > cmdline_parser_release (struct gengetopt_args_info *args_info)
237 > {
238 >  unsigned int i;
239 >  free_string_field (&(args_info->output_arg));
240 >  free_string_field (&(args_info->output_orig));
241 >  free_string_field (&(args_info->density_orig));
242 >  free_string_field (&(args_info->nx_orig));
243 >  free_string_field (&(args_info->ny_orig));
244 >  free_string_field (&(args_info->nz_orig));
245 >  free_multiple_field (args_info->molFraction_given, (void *)(args_info->molFraction_arg), &(args_info->molFraction_orig));
246 >  args_info->molFraction_arg = 0;
247    
248 +  
249    for (i = 0; i < args_info->inputs_num; ++i)
250      free (args_info->inputs [i]);
251 <  
251 >
252    if (args_info->inputs_num)
253      free (args_info->inputs);
254 <  
254 >
255    clear_given (args_info);
256 + }
257 +
258 +
259 + static void
260 + write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
261 + {
262 +  FIX_UNUSED (values);
263 +  if (arg) {
264 +    fprintf(outfile, "%s=\"%s\"\n", opt, arg);
265 +  } else {
266 +    fprintf(outfile, "%s\n", opt);
267 +  }
268 + }
269 +
270 + static void
271 + write_multiple_into_file(FILE *outfile, int len, const char *opt, char **arg, const char *values[])
272 + {
273 +  int i;
274 +  
275 +  for (i = 0; i < len; ++i)
276 +    write_into_file(outfile, opt, (arg ? arg[i] : 0), values);
277 + }
278 +
279 + int
280 + cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
281 + {
282 +  int i = 0;
283 +
284 +  if (!outfile)
285 +    {
286 +      fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
287 +      return EXIT_FAILURE;
288 +    }
289 +
290 +  if (args_info->help_given)
291 +    write_into_file(outfile, "help", 0, 0 );
292 +  if (args_info->version_given)
293 +    write_into_file(outfile, "version", 0, 0 );
294 +  if (args_info->output_given)
295 +    write_into_file(outfile, "output", args_info->output_orig, 0);
296 +  if (args_info->density_given)
297 +    write_into_file(outfile, "density", args_info->density_orig, 0);
298 +  if (args_info->nx_given)
299 +    write_into_file(outfile, "nx", args_info->nx_orig, 0);
300 +  if (args_info->ny_given)
301 +    write_into_file(outfile, "ny", args_info->ny_orig, 0);
302 +  if (args_info->nz_given)
303 +    write_into_file(outfile, "nz", args_info->nz_orig, 0);
304 +  write_multiple_into_file(outfile, args_info->molFraction_given, "molFraction", args_info->molFraction_orig, 0);
305 +  
306 +
307 +  i = EXIT_SUCCESS;
308 +  return i;
309   }
310  
311   int
# Line 175 | Line 322 | cmdline_parser_file_save(const char *filename, struct
322        return EXIT_FAILURE;
323      }
324  
325 <  if (args_info->help_given) {
179 <    fprintf(outfile, "%s\n", "help");
180 <  }
181 <  if (args_info->version_given) {
182 <    fprintf(outfile, "%s\n", "version");
183 <  }
184 <  if (args_info->output_given) {
185 <    if (args_info->output_orig) {
186 <      fprintf(outfile, "%s=\"%s\"\n", "output", args_info->output_orig);
187 <    } else {
188 <      fprintf(outfile, "%s\n", "output");
189 <    }
190 <  }
191 <  if (args_info->density_given) {
192 <    if (args_info->density_orig) {
193 <      fprintf(outfile, "%s=\"%s\"\n", "density", args_info->density_orig);
194 <    } else {
195 <      fprintf(outfile, "%s\n", "density");
196 <    }
197 <  }
198 <  if (args_info->nx_given) {
199 <    if (args_info->nx_orig) {
200 <      fprintf(outfile, "%s=\"%s\"\n", "nx", args_info->nx_orig);
201 <    } else {
202 <      fprintf(outfile, "%s\n", "nx");
203 <    }
204 <  }
205 <  if (args_info->ny_given) {
206 <    if (args_info->ny_orig) {
207 <      fprintf(outfile, "%s=\"%s\"\n", "ny", args_info->ny_orig);
208 <    } else {
209 <      fprintf(outfile, "%s\n", "ny");
210 <    }
211 <  }
212 <  if (args_info->nz_given) {
213 <    if (args_info->nz_orig) {
214 <      fprintf(outfile, "%s=\"%s\"\n", "nz", args_info->nz_orig);
215 <    } else {
216 <      fprintf(outfile, "%s\n", "nz");
217 <    }
218 <  }
219 <  if (args_info->molFraction_orig)
220 <    {
221 <      for (i = 0; i < args_info->molFraction_given; ++i)
222 <        {
223 <          if (args_info->molFraction_orig [i])
224 <            {
225 <              fprintf(outfile, "%s=\"%s\"\n", "molFraction", args_info->molFraction_orig [i]);
226 <            }
227 <        }
228 <    }
229 <  
325 >  i = cmdline_parser_dump(outfile, args_info);
326    fclose (outfile);
327  
232  i = EXIT_SUCCESS;
328    return i;
329   }
330  
# Line 239 | Line 334 | cmdline_parser_free (struct gengetopt_args_info *args_
334    cmdline_parser_release (args_info);
335   }
336  
337 <
243 < /* gengetopt_strdup() */
244 < /* strdup.c replacement of strdup, which is not standard */
337 > /** @brief replacement of strdup, which is not standard */
338   char *
339   gengetopt_strdup (const char *s)
340   {
341 <  char *result = NULL;
341 >  char *result = 0;
342    if (!s)
343      return result;
344  
# Line 259 | Line 352 | get_multiple_arg_token(const char *arg)
352   static char *
353   get_multiple_arg_token(const char *arg)
354   {
355 <  char *tok, *ret;
356 <  size_t len;
355 >  const char *tok;
356 >  char *ret;
357 >  size_t len, num_of_escape, i, j;
358  
359    if (!arg)
360 <    return NULL;
360 >    return 0;
361  
362    tok = strchr (arg, ',');
363 +  num_of_escape = 0;
364  
365 +  /* make sure it is not escaped */
366 +  while (tok)
367 +    {
368 +      if (*(tok-1) == '\\')
369 +        {
370 +          /* find the next one */
371 +          tok = strchr (tok+1, ',');
372 +          ++num_of_escape;
373 +        }
374 +      else
375 +        break;
376 +    }
377 +
378    if (tok)
379      len = (size_t)(tok - arg + 1);
380    else
381      len = strlen (arg) + 1;
382  
383 +  len -= num_of_escape;
384 +
385    ret = (char *) malloc (len);
386 <  strncpy (ret, arg, len-1);
386 >
387 >  i = 0;
388 >  j = 0;
389 >  while (arg[i] && (j < len-1))
390 >    {
391 >      if (arg[i] == '\\' &&
392 >          arg[ i + 1 ] &&
393 >          arg[ i + 1 ] == ',')
394 >        ++i;
395 >
396 >      ret[j++] = arg[i++];
397 >    }
398 >
399    ret[len-1] = '\0';
400  
401    return ret;
402   }
403  
404 < static char *
404 > static const char *
405   get_multiple_arg_token_next(const char *arg)
406   {
407 <  char *tok;
407 >  const char *tok;
408  
409    if (!arg)
410 <    return NULL;
410 >    return 0;
411  
412    tok = strchr (arg, ',');
413  
414 +  /* make sure it is not escaped */
415 +  while (tok)
416 +    {
417 +      if (*(tok-1) == '\\')
418 +        {
419 +          /* find the next one */
420 +          tok = strchr (tok+1, ',');
421 +        }
422 +      else
423 +        break;
424 +    }
425 +
426    if (! tok || strlen(tok) == 1)
427      return 0;
428  
429    return tok+1;
430   }
431  
432 + static int
433 + check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc);
434 +
435   int
436 < cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
436 > check_multiple_option_occurrences(const char *prog_name, unsigned int option_given, unsigned int min, unsigned int max, const char *option_desc)
437   {
438 +  int error = 0;
439 +
440 +  if (option_given && (min > 0 || max > 0))
441 +    {
442 +      if (min > 0 && max > 0)
443 +        {
444 +          if (min == max)
445 +            {
446 +              /* specific occurrences */
447 +              if (option_given != (unsigned int) min)
448 +                {
449 +                  fprintf (stderr, "%s: %s option occurrences must be %d\n",
450 +                    prog_name, option_desc, min);
451 +                  error = 1;
452 +                }
453 +            }
454 +          else if (option_given < (unsigned int) min
455 +                || option_given > (unsigned int) max)
456 +            {
457 +              /* range occurrences */
458 +              fprintf (stderr, "%s: %s option occurrences must be between %d and %d\n",
459 +                prog_name, option_desc, min, max);
460 +              error = 1;
461 +            }
462 +        }
463 +      else if (min > 0)
464 +        {
465 +          /* at least check */
466 +          if (option_given < min)
467 +            {
468 +              fprintf (stderr, "%s: %s option occurrences must be at least %d\n",
469 +                prog_name, option_desc, min);
470 +              error = 1;
471 +            }
472 +        }
473 +      else if (max > 0)
474 +        {
475 +          /* at most check */
476 +          if (option_given > max)
477 +            {
478 +              fprintf (stderr, "%s: %s option occurrences must be at most %d\n",
479 +                prog_name, option_desc, max);
480 +              error = 1;
481 +            }
482 +        }
483 +    }
484 +    
485 +  return error;
486 + }
487 + int
488 + cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
489 + {
490    return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
491   }
492  
493   int
494 < cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
494 > cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
495 >                   struct cmdline_parser_params *params)
496   {
497    int result;
498 +  result = cmdline_parser_internal (argc, argv, args_info, params, 0);
499  
500 <  result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
500 >  if (result == EXIT_FAILURE)
501 >    {
502 >      cmdline_parser_free (args_info);
503 >      exit (EXIT_FAILURE);
504 >    }
505 >  
506 >  return result;
507 > }
508  
509 + int
510 + cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
511 + {
512 +  int result;
513 +  struct cmdline_parser_params params;
514 +  
515 +  params.override = override;
516 +  params.initialize = initialize;
517 +  params.check_required = check_required;
518 +  params.check_ambiguity = 0;
519 +  params.print_errors = 1;
520 +
521 +  result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
522 +
523    if (result == EXIT_FAILURE)
524      {
525        cmdline_parser_free (args_info);
# Line 322 | Line 534 | cmdline_parser_required (struct gengetopt_args_info *a
534   {
535    int result = EXIT_SUCCESS;
536  
537 <  if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
537 >  if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
538      result = EXIT_FAILURE;
539  
540    if (result == EXIT_FAILURE)
# Line 338 | Line 550 | cmdline_parser_required2 (struct gengetopt_args_info *
550   cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
551   {
552    int error = 0;
553 +  FIX_UNUSED (additional_error);
554  
555 +  /* checks for required options */
556    if (! args_info->output_given)
557      {
558        fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
559        error = 1;
560      }
561 +  
562    if (! args_info->density_given)
563      {
564        fprintf (stderr, "%s: '--density' option required%s\n", prog_name, (additional_error ? additional_error : ""));
565        error = 1;
566      }
567 +  
568    if (! args_info->nx_given)
569      {
570        fprintf (stderr, "%s: '--nx' option required%s\n", prog_name, (additional_error ? additional_error : ""));
571        error = 1;
572      }
573 +  
574    if (! args_info->ny_given)
575      {
576        fprintf (stderr, "%s: '--ny' option required%s\n", prog_name, (additional_error ? additional_error : ""));
577        error = 1;
578      }
579 +  
580    if (! args_info->nz_given)
581      {
582        fprintf (stderr, "%s: '--nz' option required%s\n", prog_name, (additional_error ? additional_error : ""));
583        error = 1;
584      }
585 +  
586 +  if (check_multiple_option_occurrences(prog_name, args_info->molFraction_given, args_info->molFraction_min, args_info->molFraction_max, "'--molFraction'"))
587 +     error = 1;
588 +  
589 +  
590 +  /* checks for dependences among options */
591  
592    return error;
593   }
594  
595 < int
596 < cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error)
595 >
596 > static char *package_name = 0;
597 >
598 > /**
599 > * @brief updates an option
600 > * @param field the generic pointer to the field to update
601 > * @param orig_field the pointer to the orig field
602 > * @param field_given the pointer to the number of occurrence of this option
603 > * @param prev_given the pointer to the number of occurrence already seen
604 > * @param value the argument for this option (if null no arg was specified)
605 > * @param possible_values the possible values for this option (if specified)
606 > * @param default_value the default value (in case the option only accepts fixed values)
607 > * @param arg_type the type of this option
608 > * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
609 > * @param override @see cmdline_parser_params.override
610 > * @param no_free whether to free a possible previous value
611 > * @param multiple_option whether this is a multiple option
612 > * @param long_opt the corresponding long option
613 > * @param short_opt the corresponding short option (or '-' if none)
614 > * @param additional_error possible further error specification
615 > */
616 > static
617 > int update_arg(void *field, char **orig_field,
618 >               unsigned int *field_given, unsigned int *prev_given,
619 >               char *value, const char *possible_values[],
620 >               const char *default_value,
621 >               cmdline_parser_arg_type arg_type,
622 >               int check_ambiguity, int override,
623 >               int no_free, int multiple_option,
624 >               const char *long_opt, char short_opt,
625 >               const char *additional_error)
626   {
627 <  int c;        /* Character of the parsed option.  */
628 <  char *multi_token, *multi_next; /* for multiple options */
627 >  char *stop_char = 0;
628 >  const char *val = value;
629 >  int found;
630 >  char **string_field;
631 >  FIX_UNUSED (field);
632  
633 <  int i;        /* Counter */
633 >  stop_char = 0;
634 >  found = 0;
635  
636 <  struct molFraction_list * molFraction_list = NULL,* molFraction_new = NULL;
636 >  if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
637 >    {
638 >      if (short_opt != '-')
639 >        fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
640 >               package_name, long_opt, short_opt,
641 >               (additional_error ? additional_error : ""));
642 >      else
643 >        fprintf (stderr, "%s: `--%s' option given more than once%s\n",
644 >               package_name, long_opt,
645 >               (additional_error ? additional_error : ""));
646 >      return 1; /* failure */
647 >    }
648 >
649 >  FIX_UNUSED (default_value);
650 >    
651 >  if (field_given && *field_given && ! override)
652 >    return 0;
653 >  if (prev_given)
654 >    (*prev_given)++;
655 >  if (field_given)
656 >    (*field_given)++;
657 >  if (possible_values)
658 >    val = possible_values[found];
659 >
660 >  switch(arg_type) {
661 >  case ARG_INT:
662 >    if (val) *((int *)field) = strtol (val, &stop_char, 0);
663 >    break;
664 >  case ARG_DOUBLE:
665 >    if (val) *((double *)field) = strtod (val, &stop_char);
666 >    break;
667 >  case ARG_STRING:
668 >    if (val) {
669 >      string_field = (char **)field;
670 >      if (!no_free && *string_field)
671 >        free (*string_field); /* free previous string */
672 >      *string_field = gengetopt_strdup (val);
673 >    }
674 >    break;
675 >  default:
676 >    break;
677 >  };
678 >
679 >  /* check numeric conversion */
680 >  switch(arg_type) {
681 >  case ARG_INT:
682 >  case ARG_DOUBLE:
683 >    if (val && !(stop_char && *stop_char == '\0')) {
684 >      fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
685 >      return 1; /* failure */
686 >    }
687 >    break;
688 >  default:
689 >    ;
690 >  };
691 >
692 >  /* store the original value */
693 >  switch(arg_type) {
694 >  case ARG_NO:
695 >    break;
696 >  default:
697 >    if (value && orig_field) {
698 >      if (no_free) {
699 >        *orig_field = value;
700 >      } else {
701 >        if (*orig_field)
702 >          free (*orig_field); /* free previous string */
703 >        *orig_field = gengetopt_strdup (value);
704 >      }
705 >    }
706 >  };
707 >
708 >  return 0; /* OK */
709 > }
710 >
711 > /**
712 > * @brief store information about a multiple option in a temporary list
713 > * @param list where to (temporarily) store multiple options
714 > */
715 > static
716 > int update_multiple_arg_temp(struct generic_list **list,
717 >               unsigned int *prev_given, const char *val,
718 >               const char *possible_values[], const char *default_value,
719 >               cmdline_parser_arg_type arg_type,
720 >               const char *long_opt, char short_opt,
721 >               const char *additional_error)
722 > {
723 >  /* store single arguments */
724 >  char *multi_token;
725 >  const char *multi_next;
726 >
727 >  if (arg_type == ARG_NO) {
728 >    (*prev_given)++;
729 >    return 0; /* OK */
730 >  }
731 >
732 >  multi_token = get_multiple_arg_token(val);
733 >  multi_next = get_multiple_arg_token_next (val);
734 >
735 >  while (1)
736 >    {
737 >      add_node (list);
738 >      if (update_arg((void *)&((*list)->arg), &((*list)->orig), 0,
739 >          prev_given, multi_token, possible_values, default_value,
740 >          arg_type, 0, 1, 1, 1, long_opt, short_opt, additional_error)) {
741 >        if (multi_token) free(multi_token);
742 >        return 1; /* failure */
743 >      }
744 >
745 >      if (multi_next)
746 >        {
747 >          multi_token = get_multiple_arg_token(multi_next);
748 >          multi_next = get_multiple_arg_token_next (multi_next);
749 >        }
750 >      else
751 >        break;
752 >    }
753 >
754 >  return 0; /* OK */
755 > }
756 >
757 > /**
758 > * @brief free the passed list (including possible string argument)
759 > */
760 > static
761 > void free_list(struct generic_list *list, short string_arg)
762 > {
763 >  if (list) {
764 >    struct generic_list *tmp;
765 >    while (list)
766 >      {
767 >        tmp = list;
768 >        if (string_arg && list->arg.string_arg)
769 >          free (list->arg.string_arg);
770 >        if (list->orig)
771 >          free (list->orig);
772 >        list = list->next;
773 >        free (tmp);
774 >      }
775 >  }
776 > }
777 >
778 > /**
779 > * @brief updates a multiple option starting from the passed list
780 > */
781 > static
782 > void update_multiple_arg(void *field, char ***orig_field,
783 >               unsigned int field_given, unsigned int prev_given, union generic_value *default_value,
784 >               cmdline_parser_arg_type arg_type,
785 >               struct generic_list *list)
786 > {
787 >  int i;
788 >  struct generic_list *tmp;
789 >
790 >  if (prev_given && list) {
791 >    *orig_field = (char **) realloc (*orig_field, (field_given + prev_given) * sizeof (char *));
792 >
793 >    switch(arg_type) {
794 >    case ARG_INT:
795 >      *((int **)field) = (int *)realloc (*((int **)field), (field_given + prev_given) * sizeof (int)); break;
796 >    case ARG_DOUBLE:
797 >      *((double **)field) = (double *)realloc (*((double **)field), (field_given + prev_given) * sizeof (double)); break;
798 >    case ARG_STRING:
799 >      *((char ***)field) = (char **)realloc (*((char ***)field), (field_given + prev_given) * sizeof (char *)); break;
800 >    default:
801 >      break;
802 >    };
803 >    
804 >    for (i = (prev_given - 1); i >= 0; --i)
805 >      {
806 >        tmp = list;
807 >        
808 >        switch(arg_type) {
809 >        case ARG_INT:
810 >          (*((int **)field))[i + field_given] = tmp->arg.int_arg; break;
811 >        case ARG_DOUBLE:
812 >          (*((double **)field))[i + field_given] = tmp->arg.double_arg; break;
813 >        case ARG_STRING:
814 >          (*((char ***)field))[i + field_given] = tmp->arg.string_arg; break;
815 >        default:
816 >          break;
817 >        }        
818 >        (*orig_field) [i + field_given] = list->orig;
819 >        list = list->next;
820 >        free (tmp);
821 >      }
822 >  } else { /* set the default value */
823 >    if (default_value && ! field_given) {
824 >      switch(arg_type) {
825 >      case ARG_INT:
826 >        if (! *((int **)field)) {
827 >          *((int **)field) = (int *)malloc (sizeof (int));
828 >          (*((int **)field))[0] = default_value->int_arg;
829 >        }
830 >        break;
831 >      case ARG_DOUBLE:
832 >        if (! *((double **)field)) {
833 >          *((double **)field) = (double *)malloc (sizeof (double));
834 >          (*((double **)field))[0] = default_value->double_arg;
835 >        }
836 >        break;
837 >      case ARG_STRING:
838 >        if (! *((char ***)field)) {
839 >          *((char ***)field) = (char **)malloc (sizeof (char *));
840 >          (*((char ***)field))[0] = gengetopt_strdup(default_value->string_arg);
841 >        }
842 >        break;
843 >      default: break;
844 >      }
845 >      if (!(*orig_field)) {
846 >        *orig_field = (char **) malloc (sizeof (char *));
847 >        (*orig_field)[0] = 0;
848 >      }
849 >    }
850 >  }
851 > }
852 >
853 > int
854 > cmdline_parser_internal (
855 >  int argc, char **argv, struct gengetopt_args_info *args_info,
856 >                        struct cmdline_parser_params *params, const char *additional_error)
857 > {
858 >  int c;        /* Character of the parsed option.  */
859 >
860 >  struct generic_list * molFraction_list = NULL;
861    int error = 0;
862    struct gengetopt_args_info local_args_info;
863 +  
864 +  int override;
865 +  int initialize;
866 +  int check_required;
867 +  int check_ambiguity;
868 +  
869 +  package_name = argv[0];
870 +  
871 +  override = params->override;
872 +  initialize = params->initialize;
873 +  check_required = params->check_required;
874 +  check_ambiguity = params->check_ambiguity;
875  
876    if (initialize)
877      cmdline_parser_init (args_info);
# Line 387 | Line 880 | cmdline_parser_internal (int argc, char * const *argv,
880  
881    optarg = 0;
882    optind = 0;
883 <  opterr = 1;
883 >  opterr = params->print_errors;
884    optopt = '?';
885  
886    while (1)
887      {
888        int option_index = 0;
396      char *stop_char;
889  
890        static struct option long_options[] = {
891          { "help",       0, NULL, 'h' },
# Line 404 | Line 896 | cmdline_parser_internal (int argc, char * const *argv,
896          { "ny", 1, NULL, 0 },
897          { "nz", 1, NULL, 0 },
898          { "molFraction",        1, NULL, 0 },
899 <        { NULL, 0, NULL, 0 }
899 >        { 0,  0, 0, 0 }
900        };
901  
410      stop_char = 0;
902        c = getopt_long (argc, argv, "hVo:", long_options, &option_index);
903  
904        if (c == -1) break;       /* Exit from `while (1)' loop.  */
# Line 425 | Line 916 | cmdline_parser_internal (int argc, char * const *argv,
916            exit (EXIT_SUCCESS);
917  
918          case 'o':       /* Output file name.  */
919 <          if (local_args_info.output_given)
920 <            {
921 <              fprintf (stderr, "%s: `--output' (`-o') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
922 <              goto failure;
923 <            }
924 <          if (args_info->output_given && ! override)
925 <            continue;
926 <          local_args_info.output_given = 1;
927 <          args_info->output_given = 1;
928 <          if (args_info->output_arg)
438 <            free (args_info->output_arg); /* free previous string */
439 <          args_info->output_arg = gengetopt_strdup (optarg);
440 <          if (args_info->output_orig)
441 <            free (args_info->output_orig); /* free previous string */
442 <          args_info->output_orig = gengetopt_strdup (optarg);
919 >        
920 >        
921 >          if (update_arg( (void *)&(args_info->output_arg),
922 >               &(args_info->output_orig), &(args_info->output_given),
923 >              &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
924 >              check_ambiguity, override, 0, 0,
925 >              "output", 'o',
926 >              additional_error))
927 >            goto failure;
928 >        
929            break;
930  
445
931          case 0: /* Long option with no short option */
932            /* density (g/cm^3).  */
933            if (strcmp (long_options[option_index].name, "density") == 0)
934            {
935 <            if (local_args_info.density_given)
936 <              {
937 <                fprintf (stderr, "%s: `--density' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
938 <                goto failure;
939 <              }
940 <            if (args_info->density_given && ! override)
941 <              continue;
942 <            local_args_info.density_given = 1;
458 <            args_info->density_given = 1;
459 <            args_info->density_arg = strtod (optarg, &stop_char);
460 <            if (!(stop_char && *stop_char == '\0')) {
461 <              fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
935 >          
936 >          
937 >            if (update_arg( (void *)&(args_info->density_arg),
938 >                 &(args_info->density_orig), &(args_info->density_given),
939 >                &(local_args_info.density_given), optarg, 0, 0, ARG_DOUBLE,
940 >                check_ambiguity, override, 0, 0,
941 >                "density", '-',
942 >                additional_error))
943                goto failure;
944 <            }
464 <            if (args_info->density_orig)
465 <              free (args_info->density_orig); /* free previous string */
466 <            args_info->density_orig = gengetopt_strdup (optarg);
944 >          
945            }
946            /* number of unit cells in x.  */
947            else if (strcmp (long_options[option_index].name, "nx") == 0)
948            {
949 <            if (local_args_info.nx_given)
950 <              {
951 <                fprintf (stderr, "%s: `--nx' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
952 <                goto failure;
953 <              }
954 <            if (args_info->nx_given && ! override)
955 <              continue;
956 <            local_args_info.nx_given = 1;
479 <            args_info->nx_given = 1;
480 <            args_info->nx_arg = strtol (optarg, &stop_char, 0);
481 <            if (!(stop_char && *stop_char == '\0')) {
482 <              fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
949 >          
950 >          
951 >            if (update_arg( (void *)&(args_info->nx_arg),
952 >                 &(args_info->nx_orig), &(args_info->nx_given),
953 >                &(local_args_info.nx_given), optarg, 0, 0, ARG_INT,
954 >                check_ambiguity, override, 0, 0,
955 >                "nx", '-',
956 >                additional_error))
957                goto failure;
958 <            }
485 <            if (args_info->nx_orig)
486 <              free (args_info->nx_orig); /* free previous string */
487 <            args_info->nx_orig = gengetopt_strdup (optarg);
958 >          
959            }
960            /* number of unit cells in y.  */
961            else if (strcmp (long_options[option_index].name, "ny") == 0)
962            {
963 <            if (local_args_info.ny_given)
964 <              {
965 <                fprintf (stderr, "%s: `--ny' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
966 <                goto failure;
967 <              }
968 <            if (args_info->ny_given && ! override)
969 <              continue;
970 <            local_args_info.ny_given = 1;
500 <            args_info->ny_given = 1;
501 <            args_info->ny_arg = strtol (optarg, &stop_char, 0);
502 <            if (!(stop_char && *stop_char == '\0')) {
503 <              fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
963 >          
964 >          
965 >            if (update_arg( (void *)&(args_info->ny_arg),
966 >                 &(args_info->ny_orig), &(args_info->ny_given),
967 >                &(local_args_info.ny_given), optarg, 0, 0, ARG_INT,
968 >                check_ambiguity, override, 0, 0,
969 >                "ny", '-',
970 >                additional_error))
971                goto failure;
972 <            }
506 <            if (args_info->ny_orig)
507 <              free (args_info->ny_orig); /* free previous string */
508 <            args_info->ny_orig = gengetopt_strdup (optarg);
972 >          
973            }
974            /* number of unit cells in z.  */
975            else if (strcmp (long_options[option_index].name, "nz") == 0)
976            {
977 <            if (local_args_info.nz_given)
978 <              {
979 <                fprintf (stderr, "%s: `--nz' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
980 <                goto failure;
981 <              }
982 <            if (args_info->nz_given && ! override)
983 <              continue;
984 <            local_args_info.nz_given = 1;
521 <            args_info->nz_given = 1;
522 <            args_info->nz_arg = strtol (optarg, &stop_char, 0);
523 <            if (!(stop_char && *stop_char == '\0')) {
524 <              fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
977 >          
978 >          
979 >            if (update_arg( (void *)&(args_info->nz_arg),
980 >                 &(args_info->nz_orig), &(args_info->nz_given),
981 >                &(local_args_info.nz_given), optarg, 0, 0, ARG_INT,
982 >                check_ambiguity, override, 0, 0,
983 >                "nz", '-',
984 >                additional_error))
985                goto failure;
986 <            }
527 <            if (args_info->nz_orig)
528 <              free (args_info->nz_orig); /* free previous string */
529 <            args_info->nz_orig = gengetopt_strdup (optarg);
986 >          
987            }
988            /* (Default) Builds a multi-component random mixed nanoparticle. Mole Fraction must be specified for each componet > 1 in MD file..  */
989            else if (strcmp (long_options[option_index].name, "molFraction") == 0)
990            {
534            local_args_info.molFraction_given++;
991            
992 <            multi_token = get_multiple_arg_token(optarg);
993 <            multi_next = get_multiple_arg_token_next (optarg);
992 >            if (update_multiple_arg_temp(&molFraction_list,
993 >                &(local_args_info.molFraction_given), optarg, 0, 0, ARG_DOUBLE,
994 >                "molFraction", '-',
995 >                additional_error))
996 >              goto failure;
997            
539            while (1)
540              {
541                molFraction_new = (struct molFraction_list *) malloc (sizeof (struct molFraction_list));
542                molFraction_new->next = molFraction_list;
543                molFraction_list = molFraction_new;
544                molFraction_new->molFraction_arg = strtod (multi_token, &stop_char);
545                if (!(stop_char && *stop_char == '\0')) {
546                  fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], multi_token);
547                  goto failure;
548                }
549                molFraction_new->molFraction_orig = multi_token;
550          
551                if (multi_next)
552                  {
553                    multi_token = get_multiple_arg_token(multi_next);
554                    multi_next = get_multiple_arg_token_next (multi_next);
555                    local_args_info.molFraction_given++;
556                  }
557                else
558                  break;
559              }
560            break;
998            }
999            
1000            break;
# Line 572 | Line 1009 | cmdline_parser_internal (int argc, char * const *argv,
1009      } /* while */
1010  
1011  
1012 <  if (local_args_info.molFraction_given && molFraction_list)
1013 <    {
1014 <      struct molFraction_list *tmp;
1015 <      args_info->molFraction_arg = (double *) realloc (args_info->molFraction_arg, (args_info->molFraction_given + local_args_info.molFraction_given) * sizeof (double));
579 <      args_info->molFraction_orig = (char **) realloc (args_info->molFraction_orig, (args_info->molFraction_given + local_args_info.molFraction_given) * sizeof (char *));
580 <      for (i = (local_args_info.molFraction_given - 1); i >= 0; --i)
581 <        {
582 <          tmp = molFraction_list;
583 <          args_info->molFraction_arg [i + args_info->molFraction_given] = molFraction_list->molFraction_arg;
584 <          args_info->molFraction_orig [i + args_info->molFraction_given] = molFraction_list->molFraction_orig;
585 <          molFraction_list = molFraction_list->next;
586 <          free (tmp);
587 <        }
588 <    }
589 <  
1012 >  update_multiple_arg((void *)&(args_info->molFraction_arg),
1013 >    &(args_info->molFraction_orig), args_info->molFraction_given,
1014 >    local_args_info.molFraction_given, 0,
1015 >    ARG_DOUBLE, molFraction_list);
1016  
1017    args_info->molFraction_given += local_args_info.molFraction_given;
1018    local_args_info.molFraction_given = 0;
# Line 604 | Line 1030 | cmdline_parser_internal (int argc, char * const *argv,
1030    if (optind < argc)
1031      {
1032        int i = 0 ;
1033 +      int found_prog_name = 0;
1034 +      /* whether program name, i.e., argv[0], is in the remaining args
1035 +         (this may happen with some implementations of getopt,
1036 +          but surely not with the one included by gengetopt) */
1037  
1038 <      args_info->inputs_num = argc - optind ;
1038 >      i = optind;
1039 >      while (i < argc)
1040 >        if (argv[i++] == argv[0]) {
1041 >          found_prog_name = 1;
1042 >          break;
1043 >        }
1044 >      i = 0;
1045 >
1046 >      args_info->inputs_num = argc - optind - found_prog_name;
1047        args_info->inputs =
1048          (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
1049        while (optind < argc)
1050 <        args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
1050 >        if (argv[optind++] != argv[0])
1051 >          args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind-1]) ;
1052      }
1053  
1054    return 0;
1055  
1056   failure:
1057 <  if (molFraction_list)
619 <    {
620 <      struct molFraction_list *tmp;
621 <      while (molFraction_list)
622 <        {
623 <          tmp = molFraction_list;
624 <          free (molFraction_list->molFraction_orig);
625 <          molFraction_list = molFraction_list->next;
626 <          free (tmp);
627 <        }
628 <    }
1057 >  free_list (molFraction_list, 0 );
1058    
1059    cmdline_parser_release (&local_args_info);
1060    return (EXIT_FAILURE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines