ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/applications/nanoparticleBuilder/nanorodBuilderCmd.cpp
Revision: 1701
Committed: Thu Apr 5 19:37:58 2012 UTC (13 years ago) by kstocke1
File size: 33032 byte(s)
Log Message:
Added files for fcc and pentagonal nanorod builders. Updated runMe in samples/builders to include nanorod builders.

File Contents

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