ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/randomBuilder/randomBuilderCmd.c
Revision: 3036
Committed: Tue Oct 10 02:44:13 2006 UTC (17 years, 8 months ago) by gezelter
Content type: text/plain
File size: 21005 byte(s)
Log Message:
fixing bugs in randomBuilder

File Contents

# Content
1 /*
2 File autogenerated by gengetopt version 2.16
3 generated with the following command:
4 gengetopt -F 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: randomBuilder [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->output_given)
364 {
365 fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
366 error = 1;
367 }
368 if (! args_info->density_given)
369 {
370 fprintf (stderr, "%s: '--density' option required%s\n", prog_name, (additional_error ? additional_error : ""));
371 error = 1;
372 }
373 if (! args_info->nx_given)
374 {
375 fprintf (stderr, "%s: '--nx' option required%s\n", prog_name, (additional_error ? additional_error : ""));
376 error = 1;
377 }
378 if (! args_info->ny_given)
379 {
380 fprintf (stderr, "%s: '--ny' option required%s\n", prog_name, (additional_error ? additional_error : ""));
381 error = 1;
382 }
383 if (! args_info->nz_given)
384 {
385 fprintf (stderr, "%s: '--nz' option required%s\n", prog_name, (additional_error ? additional_error : ""));
386 error = 1;
387 }
388
389 return error;
390 }
391
392 int
393 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)
394 {
395 int c; /* Character of the parsed option. */
396 char *multi_token, *multi_next; /* for multiple options */
397
398 int i; /* Counter */
399
400 struct molFraction_list * molFraction_list = NULL,* molFraction_new = NULL;
401 int error = 0;
402 struct gengetopt_args_info local_args_info;
403
404 if (initialize)
405 cmdline_parser_init (args_info);
406
407 cmdline_parser_init (&local_args_info);
408
409 optarg = 0;
410 optind = 0;
411 opterr = 1;
412 optopt = '?';
413
414 while (1)
415 {
416 int option_index = 0;
417 char *stop_char;
418
419 static struct option long_options[] = {
420 { "help", 0, NULL, 'h' },
421 { "version", 0, NULL, 'V' },
422 { "output", 1, NULL, 'o' },
423 { "latticetype", 1, NULL, 0 },
424 { "density", 1, NULL, 0 },
425 { "nx", 1, NULL, 0 },
426 { "ny", 1, NULL, 0 },
427 { "nz", 1, NULL, 0 },
428 { "molFraction", 1, NULL, 0 },
429 { NULL, 0, NULL, 0 }
430 };
431
432 stop_char = 0;
433 c = getopt_long (argc, argv, "hVo:", long_options, &option_index);
434
435 if (c == -1) break; /* Exit from `while (1)' loop. */
436
437 switch (c)
438 {
439 case 'h': /* Print help and exit. */
440 cmdline_parser_print_help ();
441 cmdline_parser_free (&local_args_info);
442 exit (EXIT_SUCCESS);
443
444 case 'V': /* Print version and exit. */
445 cmdline_parser_print_version ();
446 cmdline_parser_free (&local_args_info);
447 exit (EXIT_SUCCESS);
448
449 case 'o': /* Output file name. */
450 if (local_args_info.output_given)
451 {
452 fprintf (stderr, "%s: `--output' (`-o') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
453 goto failure;
454 }
455 if (args_info->output_given && ! override)
456 continue;
457 local_args_info.output_given = 1;
458 args_info->output_given = 1;
459 if (args_info->output_arg)
460 free (args_info->output_arg); /* free previous string */
461 args_info->output_arg = gengetopt_strdup (optarg);
462 if (args_info->output_orig)
463 free (args_info->output_orig); /* free previous string */
464 args_info->output_orig = gengetopt_strdup (optarg);
465 break;
466
467
468 case 0: /* Long option with no short option */
469 /* Lattice type string. Valid types are fcc,hcp,bcc and hcp-water.. */
470 if (strcmp (long_options[option_index].name, "latticetype") == 0)
471 {
472 if (local_args_info.latticetype_given)
473 {
474 fprintf (stderr, "%s: `--latticetype' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
475 goto failure;
476 }
477 if (args_info->latticetype_given && ! override)
478 continue;
479 local_args_info.latticetype_given = 1;
480 args_info->latticetype_given = 1;
481 if (args_info->latticetype_arg)
482 free (args_info->latticetype_arg); /* free previous string */
483 args_info->latticetype_arg = gengetopt_strdup (optarg);
484 if (args_info->latticetype_orig)
485 free (args_info->latticetype_orig); /* free previous string */
486 args_info->latticetype_orig = gengetopt_strdup (optarg);
487 }
488 /* density (g/cm^3). */
489 else if (strcmp (long_options[option_index].name, "density") == 0)
490 {
491 if (local_args_info.density_given)
492 {
493 fprintf (stderr, "%s: `--density' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
494 goto failure;
495 }
496 if (args_info->density_given && ! override)
497 continue;
498 local_args_info.density_given = 1;
499 args_info->density_given = 1;
500 args_info->density_arg = strtod (optarg, &stop_char);
501 if (!(stop_char && *stop_char == '\0')) {
502 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
503 goto failure;
504 }
505 if (args_info->density_orig)
506 free (args_info->density_orig); /* free previous string */
507 args_info->density_orig = gengetopt_strdup (optarg);
508 }
509 /* number of unit cells in x. */
510 else if (strcmp (long_options[option_index].name, "nx") == 0)
511 {
512 if (local_args_info.nx_given)
513 {
514 fprintf (stderr, "%s: `--nx' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
515 goto failure;
516 }
517 if (args_info->nx_given && ! override)
518 continue;
519 local_args_info.nx_given = 1;
520 args_info->nx_given = 1;
521 args_info->nx_arg = strtol (optarg, &stop_char, 0);
522 if (!(stop_char && *stop_char == '\0')) {
523 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
524 goto failure;
525 }
526 if (args_info->nx_orig)
527 free (args_info->nx_orig); /* free previous string */
528 args_info->nx_orig = gengetopt_strdup (optarg);
529 }
530 /* number of unit cells in y. */
531 else if (strcmp (long_options[option_index].name, "ny") == 0)
532 {
533 if (local_args_info.ny_given)
534 {
535 fprintf (stderr, "%s: `--ny' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
536 goto failure;
537 }
538 if (args_info->ny_given && ! override)
539 continue;
540 local_args_info.ny_given = 1;
541 args_info->ny_given = 1;
542 args_info->ny_arg = strtol (optarg, &stop_char, 0);
543 if (!(stop_char && *stop_char == '\0')) {
544 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
545 goto failure;
546 }
547 if (args_info->ny_orig)
548 free (args_info->ny_orig); /* free previous string */
549 args_info->ny_orig = gengetopt_strdup (optarg);
550 }
551 /* number of unit cells in z. */
552 else if (strcmp (long_options[option_index].name, "nz") == 0)
553 {
554 if (local_args_info.nz_given)
555 {
556 fprintf (stderr, "%s: `--nz' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
557 goto failure;
558 }
559 if (args_info->nz_given && ! override)
560 continue;
561 local_args_info.nz_given = 1;
562 args_info->nz_given = 1;
563 args_info->nz_arg = strtol (optarg, &stop_char, 0);
564 if (!(stop_char && *stop_char == '\0')) {
565 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], optarg);
566 goto failure;
567 }
568 if (args_info->nz_orig)
569 free (args_info->nz_orig); /* free previous string */
570 args_info->nz_orig = gengetopt_strdup (optarg);
571 }
572 /* (Default) Builds a multi-component random mixed nanoparticle. Mole Fraction must be specified for each componet > 1 in MD file.. */
573 else if (strcmp (long_options[option_index].name, "molFraction") == 0)
574 {
575 local_args_info.molFraction_given++;
576
577 multi_token = get_multiple_arg_token(optarg);
578 multi_next = get_multiple_arg_token_next (optarg);
579
580 while (1)
581 {
582 molFraction_new = (struct molFraction_list *) malloc (sizeof (struct molFraction_list));
583 molFraction_new->next = molFraction_list;
584 molFraction_list = molFraction_new;
585 molFraction_new->molFraction_arg = strtod (multi_token, &stop_char);
586 if (!(stop_char && *stop_char == '\0')) {
587 fprintf(stderr, "%s: invalid numeric value: %s\n", argv[0], multi_token);
588 goto failure;
589 }
590 molFraction_new->molFraction_orig = multi_token;
591
592 if (multi_next)
593 {
594 multi_token = get_multiple_arg_token(multi_next);
595 multi_next = get_multiple_arg_token_next (multi_next);
596 local_args_info.molFraction_given++;
597 }
598 else
599 break;
600 }
601 break;
602 }
603
604 break;
605 case '?': /* Invalid option. */
606 /* `getopt_long' already printed an error message. */
607 goto failure;
608
609 default: /* bug: option not considered. */
610 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
611 abort ();
612 } /* switch */
613 } /* while */
614
615
616 if (local_args_info.molFraction_given && molFraction_list)
617 {
618 struct molFraction_list *tmp;
619 args_info->molFraction_arg = (double *) realloc (args_info->molFraction_arg, (args_info->molFraction_given + local_args_info.molFraction_given) * sizeof (double));
620 args_info->molFraction_orig = (char **) realloc (args_info->molFraction_orig, (args_info->molFraction_given + local_args_info.molFraction_given) * sizeof (char *));
621 for (i = (local_args_info.molFraction_given - 1); i >= 0; --i)
622 {
623 tmp = molFraction_list;
624 args_info->molFraction_arg [i + args_info->molFraction_given] = molFraction_list->molFraction_arg;
625 args_info->molFraction_orig [i + args_info->molFraction_given] = molFraction_list->molFraction_orig;
626 molFraction_list = molFraction_list->next;
627 free (tmp);
628 }
629 }
630
631
632 args_info->molFraction_given += local_args_info.molFraction_given;
633 local_args_info.molFraction_given = 0;
634
635 if (check_required)
636 {
637 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
638 }
639
640 cmdline_parser_release (&local_args_info);
641
642 if ( error )
643 return (EXIT_FAILURE);
644
645 if (optind < argc)
646 {
647 int i = 0 ;
648
649 args_info->inputs_num = argc - optind ;
650 args_info->inputs =
651 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
652 while (optind < argc)
653 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
654 }
655
656 return 0;
657
658 failure:
659 if (molFraction_list)
660 {
661 struct molFraction_list *tmp;
662 while (molFraction_list)
663 {
664 tmp = molFraction_list;
665 free (molFraction_list->molFraction_orig);
666 molFraction_list = molFraction_list->next;
667 free (tmp);
668 }
669 }
670
671 cmdline_parser_release (&local_args_info);
672 return (EXIT_FAILURE);
673 }