ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/randomBuilder/randomBuilderCmd.c
Revision: 3041
Committed: Tue Oct 10 18:34:12 2006 UTC (17 years, 9 months ago) by gezelter
Content type: text/plain
File size: 19130 byte(s)
Log Message:
fixing missing lattice arguments, adding a builder sample

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->density_given = 0 ;
52 args_info->nx_given = 0 ;
53 args_info->ny_given = 0 ;
54 args_info->nz_given = 0 ;
55 args_info->molFraction_given = 0 ;
56 }
57
58 static
59 void clear_args (struct gengetopt_args_info *args_info)
60 {
61 args_info->output_arg = NULL;
62 args_info->output_orig = NULL;
63 args_info->density_orig = NULL;
64 args_info->nx_orig = NULL;
65 args_info->ny_orig = NULL;
66 args_info->nz_orig = NULL;
67 args_info->molFraction_arg = NULL;
68 args_info->molFraction_orig = NULL;
69
70 }
71
72 void
73 cmdline_parser_print_version (void)
74 {
75 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
76 }
77
78 void
79 cmdline_parser_print_help (void)
80 {
81 cmdline_parser_print_version ();
82 printf("\nUsage: randomBuilder [OPTIONS]... [FILES]...\n\n");
83 printf("%s\n"," -h, --help Print help and exit");
84 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
92 }
93
94 void
95 cmdline_parser_init (struct gengetopt_args_info *args_info)
96 {
97 clear_given (args_info);
98 clear_args (args_info);
99
100 args_info->inputs = NULL;
101 args_info->inputs_num = 0;
102 }
103
104 static void
105 cmdline_parser_release (struct gengetopt_args_info *args_info)
106 {
107
108 unsigned int i;
109 if (args_info->output_arg)
110 {
111 free (args_info->output_arg); /* free previous argument */
112 args_info->output_arg = 0;
113 }
114 if (args_info->output_orig)
115 {
116 free (args_info->output_orig); /* free previous argument */
117 args_info->output_orig = 0;
118 }
119 if (args_info->density_orig)
120 {
121 free (args_info->density_orig); /* free previous argument */
122 args_info->density_orig = 0;
123 }
124 if (args_info->nx_orig)
125 {
126 free (args_info->nx_orig); /* free previous argument */
127 args_info->nx_orig = 0;
128 }
129 if (args_info->ny_orig)
130 {
131 free (args_info->ny_orig); /* free previous argument */
132 args_info->ny_orig = 0;
133 }
134 if (args_info->nz_orig)
135 {
136 free (args_info->nz_orig); /* free previous argument */
137 args_info->nz_orig = 0;
138 }
139 if (args_info->molFraction_arg)
140 {
141 for (i = 0; i < args_info->molFraction_given; ++i)
142 {
143 if (args_info->molFraction_orig [i])
144 {
145 free (args_info->molFraction_orig [i]); /* free previous argument */
146 args_info->molFraction_orig [i] = 0;
147 }
148 }
149 free (args_info->molFraction_arg); /* free previous argument */
150 args_info->molFraction_arg = 0;
151 free (args_info->molFraction_orig); /* free previous argument */
152 args_info->molFraction_orig = 0;
153 }
154
155 for (i = 0; i < args_info->inputs_num; ++i)
156 free (args_info->inputs [i]);
157
158 if (args_info->inputs_num)
159 free (args_info->inputs);
160
161 clear_given (args_info);
162 }
163
164 int
165 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
166 {
167 FILE *outfile;
168 int i = 0;
169
170 outfile = fopen(filename, "w");
171
172 if (!outfile)
173 {
174 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
175 return EXIT_FAILURE;
176 }
177
178 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
230 fclose (outfile);
231
232 i = EXIT_SUCCESS;
233 return i;
234 }
235
236 void
237 cmdline_parser_free (struct gengetopt_args_info *args_info)
238 {
239 cmdline_parser_release (args_info);
240 }
241
242
243 /* gengetopt_strdup() */
244 /* strdup.c replacement of strdup, which is not standard */
245 char *
246 gengetopt_strdup (const char *s)
247 {
248 char *result = NULL;
249 if (!s)
250 return result;
251
252 result = (char*)malloc(strlen(s) + 1);
253 if (result == (char*)0)
254 return (char*)0;
255 strcpy(result, s);
256 return result;
257 }
258
259 static char *
260 get_multiple_arg_token(const char *arg)
261 {
262 char *tok, *ret;
263 size_t len;
264
265 if (!arg)
266 return NULL;
267
268 tok = strchr (arg, ',');
269
270 if (tok)
271 len = (size_t)(tok - arg + 1);
272 else
273 len = strlen (arg) + 1;
274
275 ret = (char *) malloc (len);
276 strncpy (ret, arg, len-1);
277 ret[len-1] = '\0';
278
279 return ret;
280 }
281
282 static char *
283 get_multiple_arg_token_next(const char *arg)
284 {
285 char *tok;
286
287 if (!arg)
288 return NULL;
289
290 tok = strchr (arg, ',');
291
292 if (! tok || strlen(tok) == 1)
293 return 0;
294
295 return tok+1;
296 }
297
298 int
299 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
300 {
301 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
302 }
303
304 int
305 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
306 {
307 int result;
308
309 result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
310
311 if (result == EXIT_FAILURE)
312 {
313 cmdline_parser_free (args_info);
314 exit (EXIT_FAILURE);
315 }
316
317 return result;
318 }
319
320 int
321 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
322 {
323 int result = EXIT_SUCCESS;
324
325 if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
326 result = EXIT_FAILURE;
327
328 if (result == EXIT_FAILURE)
329 {
330 cmdline_parser_free (args_info);
331 exit (EXIT_FAILURE);
332 }
333
334 return result;
335 }
336
337 int
338 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
339 {
340 int error = 0;
341
342 if (! args_info->output_given)
343 {
344 fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
345 error = 1;
346 }
347 if (! args_info->density_given)
348 {
349 fprintf (stderr, "%s: '--density' option required%s\n", prog_name, (additional_error ? additional_error : ""));
350 error = 1;
351 }
352 if (! args_info->nx_given)
353 {
354 fprintf (stderr, "%s: '--nx' option required%s\n", prog_name, (additional_error ? additional_error : ""));
355 error = 1;
356 }
357 if (! args_info->ny_given)
358 {
359 fprintf (stderr, "%s: '--ny' option required%s\n", prog_name, (additional_error ? additional_error : ""));
360 error = 1;
361 }
362 if (! args_info->nz_given)
363 {
364 fprintf (stderr, "%s: '--nz' option required%s\n", prog_name, (additional_error ? additional_error : ""));
365 error = 1;
366 }
367
368 return error;
369 }
370
371 int
372 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)
373 {
374 int c; /* Character of the parsed option. */
375 char *multi_token, *multi_next; /* for multiple options */
376
377 int i; /* Counter */
378
379 struct molFraction_list * molFraction_list = NULL,* molFraction_new = NULL;
380 int error = 0;
381 struct gengetopt_args_info local_args_info;
382
383 if (initialize)
384 cmdline_parser_init (args_info);
385
386 cmdline_parser_init (&local_args_info);
387
388 optarg = 0;
389 optind = 0;
390 opterr = 1;
391 optopt = '?';
392
393 while (1)
394 {
395 int option_index = 0;
396 char *stop_char;
397
398 static struct option long_options[] = {
399 { "help", 0, NULL, 'h' },
400 { "version", 0, NULL, 'V' },
401 { "output", 1, NULL, 'o' },
402 { "density", 1, NULL, 0 },
403 { "nx", 1, NULL, 0 },
404 { "ny", 1, NULL, 0 },
405 { "nz", 1, NULL, 0 },
406 { "molFraction", 1, NULL, 0 },
407 { NULL, 0, NULL, 0 }
408 };
409
410 stop_char = 0;
411 c = getopt_long (argc, argv, "hVo:", long_options, &option_index);
412
413 if (c == -1) break; /* Exit from `while (1)' loop. */
414
415 switch (c)
416 {
417 case 'h': /* Print help and exit. */
418 cmdline_parser_print_help ();
419 cmdline_parser_free (&local_args_info);
420 exit (EXIT_SUCCESS);
421
422 case 'V': /* Print version and exit. */
423 cmdline_parser_print_version ();
424 cmdline_parser_free (&local_args_info);
425 exit (EXIT_SUCCESS);
426
427 case 'o': /* Output file name. */
428 if (local_args_info.output_given)
429 {
430 fprintf (stderr, "%s: `--output' (`-o') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
431 goto failure;
432 }
433 if (args_info->output_given && ! override)
434 continue;
435 local_args_info.output_given = 1;
436 args_info->output_given = 1;
437 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);
443 break;
444
445
446 case 0: /* Long option with no short option */
447 /* density (g/cm^3). */
448 if (strcmp (long_options[option_index].name, "density") == 0)
449 {
450 if (local_args_info.density_given)
451 {
452 fprintf (stderr, "%s: `--density' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
453 goto failure;
454 }
455 if (args_info->density_given && ! override)
456 continue;
457 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);
462 goto failure;
463 }
464 if (args_info->density_orig)
465 free (args_info->density_orig); /* free previous string */
466 args_info->density_orig = gengetopt_strdup (optarg);
467 }
468 /* number of unit cells in x. */
469 else if (strcmp (long_options[option_index].name, "nx") == 0)
470 {
471 if (local_args_info.nx_given)
472 {
473 fprintf (stderr, "%s: `--nx' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
474 goto failure;
475 }
476 if (args_info->nx_given && ! override)
477 continue;
478 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);
483 goto failure;
484 }
485 if (args_info->nx_orig)
486 free (args_info->nx_orig); /* free previous string */
487 args_info->nx_orig = gengetopt_strdup (optarg);
488 }
489 /* number of unit cells in y. */
490 else if (strcmp (long_options[option_index].name, "ny") == 0)
491 {
492 if (local_args_info.ny_given)
493 {
494 fprintf (stderr, "%s: `--ny' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
495 goto failure;
496 }
497 if (args_info->ny_given && ! override)
498 continue;
499 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);
504 goto failure;
505 }
506 if (args_info->ny_orig)
507 free (args_info->ny_orig); /* free previous string */
508 args_info->ny_orig = gengetopt_strdup (optarg);
509 }
510 /* number of unit cells in z. */
511 else if (strcmp (long_options[option_index].name, "nz") == 0)
512 {
513 if (local_args_info.nz_given)
514 {
515 fprintf (stderr, "%s: `--nz' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
516 goto failure;
517 }
518 if (args_info->nz_given && ! override)
519 continue;
520 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);
525 goto failure;
526 }
527 if (args_info->nz_orig)
528 free (args_info->nz_orig); /* free previous string */
529 args_info->nz_orig = gengetopt_strdup (optarg);
530 }
531 /* (Default) Builds a multi-component random mixed nanoparticle. Mole Fraction must be specified for each componet > 1 in MD file.. */
532 else if (strcmp (long_options[option_index].name, "molFraction") == 0)
533 {
534 local_args_info.molFraction_given++;
535
536 multi_token = get_multiple_arg_token(optarg);
537 multi_next = get_multiple_arg_token_next (optarg);
538
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;
561 }
562
563 break;
564 case '?': /* Invalid option. */
565 /* `getopt_long' already printed an error message. */
566 goto failure;
567
568 default: /* bug: option not considered. */
569 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
570 abort ();
571 } /* switch */
572 } /* while */
573
574
575 if (local_args_info.molFraction_given && molFraction_list)
576 {
577 struct molFraction_list *tmp;
578 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
590
591 args_info->molFraction_given += local_args_info.molFraction_given;
592 local_args_info.molFraction_given = 0;
593
594 if (check_required)
595 {
596 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
597 }
598
599 cmdline_parser_release (&local_args_info);
600
601 if ( error )
602 return (EXIT_FAILURE);
603
604 if (optind < argc)
605 {
606 int i = 0 ;
607
608 args_info->inputs_num = argc - optind ;
609 args_info->inputs =
610 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
611 while (optind < argc)
612 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
613 }
614
615 return 0;
616
617 failure:
618 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 }
629
630 cmdline_parser_release (&local_args_info);
631 return (EXIT_FAILURE);
632 }