PSCF v1.2
getDimension.cpp
1/*
2* PSCF - Polymer Self-Consistent Field Theory
3*
4* Copyright 2016 - 2022, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "getDimension.h"
9#include <util/global.h>
10
11#include <unistd.h>
12#include <cstring>
13
14using namespace Util;
15
16namespace Pscf {
17namespace Prdc {
18
19 /*
20 * Extract integer argument of -d option.
21 */
22 int getDimension(int argc, char **argv)
23 {
24 // No arguments other than program name
25 if (argc < 2) {
26 UTIL_THROW("No command options found");
27 }
28
29 int length;
30 char* option = 0;
31 char* arg = 0;
32
33 bool found = false; // Has the -d option been found
34 bool done = false; // Has a valid parameter been found
35 int i = 1;
36 while (!done && i < argc) {
37 option = argv[i];
38 length = strlen(option);
39 if (!found) {
40 if (length > 1) {
41 if (option[0] == '-') {
42 if (option[1] == 'd') {
43 found = true;
44 }
45 }
46 if (found && length > 2) {
47 if (length == 3) {
48 // Option of form -d# with no space
49 arg = &option[2];
50 done = true;
51 } else {
52 // Option is too long
53 std::cout
54 << "Invalid parameter of command option -d:"
55 << " |" << option << "|" << std::endl;
56 UTIL_THROW("Invalid parameter of command option -d");
57 }
58 }
59 }
60 } else {
61 if (length == 1) {
62 // Single character option parameter
63 arg = option;
64 done = true;
65 } else {
66 // Option is too long
67 std::cout << "Invalid parameter of command option -d:"
68 << " |" << option << "|" << std::endl;
69 UTIL_THROW("Invalid parameter of command option -d");
70 }
71 }
72 ++i;
73 }
74
75 // Check status
76 if (!done) {
77 if (found) {
78 UTIL_THROW("Missing parameter for option -d");
79 } else {
80 UTIL_THROW("Missing required option -d");
81 }
82 }
83 UTIL_CHECK(1 == strlen(arg));
84
85 // Convert arg string to integer D
86 int D = atoi(arg);
87 if (D > 0 && D < 4) {
88 return D;
89 } else {
90 std::cout << "Invalid parameter of command line option -d:"
91 << " |" << arg << "|" << std::endl;
92 std::cout << "Value of attempted conversion to integer: "
93 << D << std::endl;
94 UTIL_THROW("Invalid parameter of command line option -d");
95 }
96
97 }
98
99}
100}
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:51
PSCF package top-level namespace.
Definition param_pc.dox:1
Utility classes for scientific computation.