ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/randomBuilder/randomBuilderCmd.c
Revision: 2738
Committed: Tue Apr 25 22:59:27 2006 UTC (18 years, 2 months ago) by chuckv
Content type: text/plain
File size: 20828 byte(s)
Log Message:
Added random builder to cvs.

File Contents

# User Rev Content
1 chuckv 2738 /*
2     File autogenerated by gengetopt version 2.15
3     generated with the following command:
4     gengetopt --file-name=randomBuilderCmd --unamed-opts
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     #include "getopt.h"
21    
22     #include "randomBuilderCmd.h"
23    
24     static
25     void clear_given (struct gengetopt_args_info *args_info);
26     static
27     void clear_args (struct gengetopt_args_info *args_info);
28    
29     static int
30     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);
31    
32     static int
33     cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
34    
35     struct molFraction_list
36     {
37     double molFraction_arg;
38     char *molFraction_orig;
39     struct molFraction_list * next;
40     };
41    
42     static char *
43     gengetopt_strdup (const char *s);
44    
45     static
46     void clear_given (struct gengetopt_args_info *args_info)
47     {
48     args_info->help_given = 0 ;
49     args_info->version_given = 0 ;
50     args_info->output_given = 0 ;
51     args_info->latticetype_given = 0 ;
52     args_info->density_given = 0 ;
53     args_info->nx_given = 0 ;
54     args_info->ny_given = 0 ;
55     args_info->nz_given = 0 ;
56     args_info->molFraction_given = 0 ;
57     }
58    
59     static
60     void clear_args (struct gengetopt_args_info *args_info)
61     {
62     args_info->output_arg = NULL;
63     args_info->output_orig = NULL;
64     args_info->latticetype_arg = gengetopt_strdup ("fcc");
65     args_info->latticetype_orig = NULL;
66     args_info->density_orig = NULL;
67     args_info->nx_orig = NULL;
68     args_info->ny_orig = NULL;
69     args_info->nz_orig = NULL;
70     args_info->molFraction_arg = NULL;
71     args_info->molFraction_orig = NULL;
72    
73     }
74    
75     void
76     cmdline_parser_print_version (void)
77     {
78     printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
79     }
80    
81     void
82     cmdline_parser_print_help (void)
83     {
84     cmdline_parser_print_version ();
85     printf("\nUsage: simpleBuilder [OPTIONS]... [FILES]...\n\n");
86     printf("%s\n"," -h, --help Print help and exit");
87     printf("%s\n"," -V, --version Print version and exit");
88     printf("%s\n"," -o, --output=STRING Output file name");
89     printf("%s\n"," --latticetype=STRING Lattice type string. Valid types are fcc,hcp,bcc \n and hcp-water. (default=`fcc')");
90     printf("%s\n"," --density=DOUBLE density (g/cm^3)");
91     printf("%s\n"," --nx=INT number of unit cells in x");
92     printf("%s\n"," --ny=INT number of unit cells in y");
93     printf("%s\n"," --nz=INT number of unit cells in z");
94     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.");
95    
96     }
97    
98     void
99     cmdline_parser_init (struct gengetopt_args_info *args_info)
100     {
101     clear_given (args_info);
102     clear_args (args_info);
103    
104     args_info->inputs = NULL;
105     args_info->inputs_num = 0;
106     }
107    
108     static void
109     cmdline_parser_release (struct gengetopt_args_info *args_info)
110     {
111    
112     unsigned int i;
113     if (args_info->output_arg)
114     {
115     free (args_info->output_arg); /* free previous argument */
116     args_info->output_arg = 0;
117     }
118     if (args_info->output_orig)
119     {
120     free (args_info->output_orig); /* free previous argument */
121     args_info->output_orig = 0;
122     }
123     if (args_info->latticetype_arg)
124     {
125     free (args_info->latticetype_arg); /* free previous argument */
126     args_info->latticetype_arg = 0;
127     }
128     if (args_info->latticetype_orig)
129     {
130     free (args_info->latticetype_orig); /* free previous argument */
131     args_info->latticetype_orig = 0;
132     }
133     if (args_info->density_orig)
134     {
135     free (args_info->density_orig); /* free previous argument */
136     args_info->density_orig = 0;
137     }
138     if (args_info->nx_orig)
139     {
140     free (args_info->nx_orig); /* free previous argument */
141     args_info->nx_orig = 0;
142     }
143     if (args_info->ny_orig)
144     {
145     free (args_info->ny_orig); /* free previous argument */
146     args_info->ny_orig = 0;
147     }
148     if (args_info->nz_orig)
149     {
150     free (args_info->nz_orig); /* free previous argument */
151     args_info->nz_orig = 0;
152     }
153     if (args_info->molFraction_arg)
154     {
155     for (i = 0; i < args_info->molFraction_given; ++i)
156     {
157     if (args_info->molFraction_orig [i])
158     {
159     free (args_info->molFraction_orig [i]); /* free previous argument */
160     args_info->molFraction_orig [i] = 0;
161     }
162     }
163     free (args_info->molFraction_arg); /* free previous argument */
164     args_info->molFraction_arg = 0;
165     free (args_info->molFraction_orig); /* free previous argument */
166     args_info->molFraction_orig = 0;
167     }
168    
169     for (i = 0; i < args_info->inputs_num; ++i)
170     free (args_info->inputs [i]);
171    
172     if (args_info->inputs_num)
173     free (args_info->inputs);
174    
175     clear_given (args_info);
176     }
177    
178     int
179     cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
180     {
181     FILE *outfile;
182     int i = 0;
183    
184     outfile = fopen(filename, "w");
185    
186     if (!outfile)
187     {
188     fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
189     return EXIT_FAILURE;
190     }
191    
192     if (args_info->help_given) {
193     fprintf(outfile, "%s\n", "help");
194     }
195     if (args_info->version_given) {
196     fprintf(outfile, "%s\n", "version");
197     }
198     if (args_info->output_given) {
199     if (args_info->output_orig) {
200     fprintf(outfile, "%s=\"%s\"\n", "output", args_info->output_orig);
201     } else {
202     fprintf(outfile, "%s\n", "output");
203     }
204     }
205     if (args_info->latticetype_given) {
206     if (args_info->latticetype_orig) {
207     fprintf(outfile, "%s=\"%s\"\n", "latticetype", args_info->latticetype_orig);
208     } else {
209     fprintf(outfile, "%s\n", "latticetype");
210     }
211     }
212     if (args_info->density_given) {
213     if (args_info->density_orig) {
214     fprintf(outfile, "%s=\"%s\"\n", "density", args_info->density_orig);
215     } else {
216     fprintf(outfile, "%s\n", "density");
217     }
218     }
219     if (args_info->nx_given) {
220     if (args_info->nx_orig) {
221     fprintf(outfile, "%s=\"%s\"\n", "nx", args_info->nx_orig);
222     } else {
223     fprintf(outfile, "%s\n", "nx");
224     }
225     }
226     if (args_info->ny_given) {
227     if (args_info->ny_orig) {
228     fprintf(outfile, "%s=\"%s\"\n", "ny", args_info->ny_orig);
229     } else {
230     fprintf(outfile, "%s\n", "ny");
231     }
232     }
233     if (args_info->nz_given) {
234     if (args_info->nz_orig) {
235     fprintf(outfile, "%s=\"%s\"\n", "nz", args_info->nz_orig);
236     } else {
237     fprintf(outfile, "%s\n", "nz");
238     }
239     }
240     if (args_info->molFraction_orig)
241     {
242     for (i = 0; i < args_info->molFraction_given; ++i)
243     {
244     if (args_info->molFraction_orig [i])
245     {
246     fprintf(outfile, "%s=\"%s\"\n", "molFraction", args_info->molFraction_orig [i]);
247     }
248     }
249     }
250    
251     fclose (outfile);
252    
253     i = EXIT_SUCCESS;
254     return i;
255     }
256    
257     void
258     cmdline_parser_free (struct gengetopt_args_info *args_info)
259     {
260     cmdline_parser_release (args_info);
261     }
262    
263    
264     /* gengetopt_strdup() */
265     /* strdup.c replacement of strdup, which is not standard */
266     char *
267     gengetopt_strdup (const char *s)
268     {
269     char *result = NULL;
270     if (!s)
271     return result;
272    
273     result = (char*)malloc(strlen(s) + 1);
274     if (result == (char*)0)
275     return (char*)0;
276     strcpy(result, s);
277     return result;
278     }
279    
280     static char *
281     get_multiple_arg_token(const char *arg)
282     {
283     char *tok, *ret;
284     size_t len;
285    
286     if (!arg)
287     return NULL;
288    
289     tok = strchr (arg, ',');
290    
291     if (tok)
292     len = (size_t)(tok - arg + 1);
293     else
294     len = strlen (arg) + 1;
295    
296     ret = (char *) malloc (len);
297     strncpy (ret, arg, len-1);
298     ret[len-1] = '\0';
299    
300     return ret;
301     }
302    
303     static char *
304     get_multiple_arg_token_next(const char *arg)
305     {
306     char *tok;
307    
308     if (!arg)
309     return NULL;
310    
311     tok = strchr (arg, ',');
312    
313     if (! tok || strlen(tok) == 1)
314     return 0;
315    
316     return tok+1;
317     }
318    
319     int
320     cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
321     {
322     return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
323     }
324    
325     int
326     cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
327     {
328     int result;
329    
330     result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
331    
332     if (result == EXIT_FAILURE)
333     {
334     cmdline_parser_free (args_info);
335     exit (EXIT_FAILURE);
336     }
337    
338     return result;
339     }
340    
341     int
342     cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
343     {
344     int result = EXIT_SUCCESS;
345    
346     if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
347     result = EXIT_FAILURE;
348    
349     if (result == EXIT_FAILURE)
350     {
351     cmdline_parser_free (args_info);
352     exit (EXIT_FAILURE);
353     }
354    
355     return result;
356     }
357    
358     int
359     cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
360     {
361     int error = 0;
362    
363     if (! args_info->density_given)
364     {
365     fprintf (stderr, "%s: '--density' option required%s\n", prog_name, (additional_error ? additional_error : ""));
366     error = 1;
367     }
368     if (! args_info->nx_given)
369     {
370     fprintf (stderr, "%s: '--nx' option required%s\n", prog_name, (additional_error ? additional_error : ""));
371     error = 1;
372     }
373     if (! args_info->ny_given)
374     {
375     fprintf (stderr, "%s: '--ny' option required%s\n", prog_name, (additional_error ? additional_error : ""));
376     error = 1;
377     }
378     if (! args_info->nz_given)
379     {
380     fprintf (stderr, "%s: '--nz' option required%s\n", prog_name, (additional_error ? additional_error : ""));
381     error = 1;
382     }
383    
384     return error;
385     }
386    
387     int
388     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)
389     {
390     int c; /* Character of the parsed option. */
391     char *multi_token, *multi_next; /* for multiple options */
392    
393     int i; /* Counter */
394    
395     struct molFraction_list * molFraction_list = NULL,* molFraction_new = NULL;
396     int error = 0;
397     struct gengetopt_args_info local_args_info;
398    
399     if (initialize)
400     cmdline_parser_init (args_info);
401    
402     cmdline_parser_init (&local_args_info);
403    
404     optarg = 0;
405     optind = 1;
406     opterr = 1;
407     optopt = '?';
408    
409     while (1)
410     {
411     int option_index = 0;
412     char *stop_char;
413    
414     static struct option long_options[] = {
415     { "help", 0, NULL, 'h' },
416     { "version", 0, NULL, 'V' },
417     { "output", 1, NULL, 'o' },
418     { "latticetype", 1, NULL, 0 },
419     { "density", 1, NULL, 0 },
420     { "nx", 1, NULL, 0 },
421     { "ny", 1, NULL, 0 },
422     { "nz", 1, NULL, 0 },
423     { "molFraction", 1, NULL, 0 },
424     { NULL, 0, NULL, 0 }
425     };
426    
427     stop_char = 0;
428     c = getopt_long (argc, argv, "hVo:", long_options, &option_index);
429    
430     if (c == -1) break; /* Exit from `while (1)' loop. */
431    
432     switch (c)
433     {
434     case 'h': /* Print help and exit. */
435     cmdline_parser_print_help ();
436     cmdline_parser_free (&local_args_info);
437     exit (EXIT_SUCCESS);
438    
439     case 'V': /* Print version and exit. */
440     cmdline_parser_print_version ();
441     cmdline_parser_free (&local_args_info);
442     exit (EXIT_SUCCESS);
443    
444     case 'o': /* Output file name. */
445     if (local_args_info.output_given)
446     {
447     fprintf (stderr, "%s: `--output' (`-o') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
448     goto failure;
449     }
450     if (args_info->output_given && ! override)
451     continue;
452     local_args_info.output_given = 1;
453     args_info->output_given = 1;
454     if (args_info->output_arg)
455     free (args_info->output_arg); /* free previous string */
456     args_info->output_arg = gengetopt_strdup (optarg);
457     if (args_info->output_orig)
458     free (args_info->output_orig); /* free previous string */
459     args_info->output_orig = gengetopt_strdup (optarg);
460     break;
461    
462    
463     case 0: /* Long option with no short option */
464     /* Lattice type string. Valid types are fcc,hcp,bcc and hcp-water.. */
465     if (strcmp (long_options[option_index].name, "latticetype") == 0)
466     {
467     if (local_args_info.latticetype_given)
468     {
469     fprintf (stderr, "%s: `--latticetype' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
470     goto failure;
471     }
472     if (args_info->latticetype_given && ! override)
473     continue;
474     local_args_info.latticetype_given = 1;
475     args_info->latticetype_given = 1;
476     if (args_info->latticetype_arg)
477     free (args_info->latticetype_arg); /* free previous string */
478     args_info->latticetype_arg = gengetopt_strdup (optarg);
479     if (args_info->latticetype_orig)
480     free (args_info->latticetype_orig); /* free previous string */
481     args_info->latticetype_orig = gengetopt_strdup (optarg);
482     }
483     /* density (g/cm^3). */
484     else if (strcmp (long_options[option_index].name, "density") == 0)
485     {
486     if (local_args_info.density_given)
487     {
488     fprintf (stderr, "%s: `--density' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
489     goto failure;
490     }
491     if (args_info->density_given && ! override)
492     continue;
493     local_args_info.density_given = 1;
494     args_info->density_given = 1;
495     args_info->density_arg = strtod (optarg, &stop_char);
496     if (!(stop_char && *stop_char == '\0')) {
497     fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
498     goto failure;
499     }
500     if (args_info->density_orig)
501     free (args_info->density_orig); /* free previous string */
502     args_info->density_orig = gengetopt_strdup (optarg);
503     }
504     /* number of unit cells in x. */
505     else if (strcmp (long_options[option_index].name, "nx") == 0)
506     {
507     if (local_args_info.nx_given)
508     {
509     fprintf (stderr, "%s: `--nx' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
510     goto failure;
511     }
512     if (args_info->nx_given && ! override)
513     continue;
514     local_args_info.nx_given = 1;
515     args_info->nx_given = 1;
516     args_info->nx_arg = strtol (optarg, &stop_char, 0);
517     if (!(stop_char && *stop_char == '\0')) {
518     fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
519     goto failure;
520     }
521     if (args_info->nx_orig)
522     free (args_info->nx_orig); /* free previous string */
523     args_info->nx_orig = gengetopt_strdup (optarg);
524     }
525     /* number of unit cells in y. */
526     else if (strcmp (long_options[option_index].name, "ny") == 0)
527     {
528     if (local_args_info.ny_given)
529     {
530     fprintf (stderr, "%s: `--ny' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
531     goto failure;
532     }
533     if (args_info->ny_given && ! override)
534     continue;
535     local_args_info.ny_given = 1;
536     args_info->ny_given = 1;
537     args_info->ny_arg = strtol (optarg, &stop_char, 0);
538     if (!(stop_char && *stop_char == '\0')) {
539     fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
540     goto failure;
541     }
542     if (args_info->ny_orig)
543     free (args_info->ny_orig); /* free previous string */
544     args_info->ny_orig = gengetopt_strdup (optarg);
545     }
546     /* number of unit cells in z. */
547     else if (strcmp (long_options[option_index].name, "nz") == 0)
548     {
549     if (local_args_info.nz_given)
550     {
551     fprintf (stderr, "%s: `--nz' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
552     goto failure;
553     }
554     if (args_info->nz_given && ! override)
555     continue;
556     local_args_info.nz_given = 1;
557     args_info->nz_given = 1;
558     args_info->nz_arg = strtol (optarg, &stop_char, 0);
559     if (!(stop_char && *stop_char == '\0')) {
560     fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
561     goto failure;
562     }
563     if (args_info->nz_orig)
564     free (args_info->nz_orig); /* free previous string */
565     args_info->nz_orig = gengetopt_strdup (optarg);
566     }
567     /* (Default) Builds a multi-component random mixed nanoparticle. Mole Fraction must be specified for each componet > 1 in MD file.. */
568     else if (strcmp (long_options[option_index].name, "molFraction") == 0)
569     {
570     local_args_info.molFraction_given++;
571    
572     multi_token = get_multiple_arg_token(optarg);
573     multi_next = get_multiple_arg_token_next (optarg);
574    
575     while (1)
576     {
577     molFraction_new = (struct molFraction_list *) malloc (sizeof (struct molFraction_list));
578     molFraction_new->next = molFraction_list;
579     molFraction_list = molFraction_new;
580     molFraction_new->molFraction_arg = strtod (multi_token, &stop_char);
581     if (!(stop_char && *stop_char == '\0')) {
582     fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], multi_token);
583     goto failure;
584     }
585     molFraction_new->molFraction_orig = multi_token;
586    
587     if (multi_next)
588     {
589     multi_token = get_multiple_arg_token(multi_next);
590     multi_next = get_multiple_arg_token_next (multi_next);
591     local_args_info.molFraction_given++;
592     }
593     else
594     break;
595     }
596     break;
597     }
598    
599     break;
600     case '?': /* Invalid option. */
601     /* `getopt_long' already printed an error message. */
602     goto failure;
603    
604     default: /* bug: option not considered. */
605     fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
606     abort ();
607     } /* switch */
608     } /* while */
609    
610    
611     if (local_args_info.molFraction_given && molFraction_list)
612     {
613     struct molFraction_list *tmp;
614     args_info->molFraction_arg = (double *) realloc (args_info->molFraction_arg, (args_info->molFraction_given + local_args_info.molFraction_given) * sizeof (double));
615     args_info->molFraction_orig = (char **) realloc (args_info->molFraction_orig, (args_info->molFraction_given + local_args_info.molFraction_given) * sizeof (char *));
616     for (i = (local_args_info.molFraction_given - 1); i >= 0; --i)
617     {
618     tmp = molFraction_list;
619     args_info->molFraction_arg [i + args_info->molFraction_given] = molFraction_list->molFraction_arg;
620     args_info->molFraction_orig [i + args_info->molFraction_given] = molFraction_list->molFraction_orig;
621     molFraction_list = molFraction_list->next;
622     free (tmp);
623     }
624     }
625    
626    
627     args_info->molFraction_given += local_args_info.molFraction_given;
628     local_args_info.molFraction_given = 0;
629    
630     if (check_required)
631     {
632     error += cmdline_parser_required2 (args_info, argv[0], additional_error);
633     }
634    
635     cmdline_parser_release (&local_args_info);
636    
637     if ( error )
638     return (EXIT_FAILURE);
639    
640     if (optind < argc)
641     {
642     int i = 0 ;
643    
644     args_info->inputs_num = argc - optind ;
645     args_info->inputs =
646     (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
647     while (optind < argc)
648     args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
649     }
650    
651     return 0;
652    
653     failure:
654     if (molFraction_list)
655     {
656     struct molFraction_list *tmp;
657     while (molFraction_list)
658     {
659     tmp = molFraction_list;
660     free (molFraction_list->molFraction_orig);
661     molFraction_list = molFraction_list->next;
662     free (tmp);
663     }
664     }
665    
666     cmdline_parser_release (&local_args_info);
667     return (EXIT_FAILURE);
668     }