Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UIparsing.hh
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4UIparsing
27//
28// Utilities for parsing/checking inputs in G4UIparameter/G4UIcommand
29
30#ifndef G4UIparsing_hh
31#define G4UIparsing_hh 1
32
33#include "G4UItokenNum.hh"
34#include "globals.hh"
35
36#include <cctype>
37
38namespace G4UIparsing
39{
40// Convert G4String to value of type T
41template<typename T>
42inline T StoT(const G4String& s)
43{
44 T vl;
45 std::istringstream is(s);
46 is >> vl;
47 return vl;
48}
49
50// Convert value of type T to G4String
51template<typename T>
52inline G4String TtoS(T value)
53{
54 std::ostringstream os;
55 os << value;
56 return os.str();
57}
58
59// Check if the value is within the range of (long) int
60inline G4bool ChkMax(const char* str, short maxDigits)
61{
62 if(maxDigits > 10) {
63 // long int assumed
64 auto tmpval = std::stoll(str);
65 if(tmpval > LONG_MAX || tmpval < LONG_MIN) {
66 G4cerr << "input string '" << str << "' out-of-range for conversion to 'long int' value" << G4endl;
67 return false;
68 }
69 } else {
70 // int assumed
71 auto tmpval = std::stol(str);
72 if(tmpval > INT_MAX || tmpval < INT_MIN) {
73 G4cerr << "input string '" << str << "' out-of-range for conversion to 'int' value" << G4endl;
74 return false;
75 }
76 }
77 return true;
78}
79
80// Return true if `str` parses to an integral number no more than `maxDigit` digits
81inline G4bool IsInt(const char* str, short maxDigits)
82{
83 const char* p = str;
84 G4int length = 0;
85 if (*p == '+' || *p == '-') {
86 ++p;
87 }
88 if (isdigit((G4int)(*p)) != 0) {
89 while (isdigit((G4int)(*p)) != 0) {
90 ++p;
91 ++length;
92 }
93 if (*p == '\0') {
94 if (length > maxDigits) {
95 G4cerr << "digit length exceeds" << G4endl;
96 return false;
97 }
98 return ChkMax(str,maxDigits);
99 }
100 }
101 return false;
102}
103
104// Return true if `str` parses to an exponent
105//
106// A valid exponent is an integer of no more than 7 digits
107inline G4bool ExpectExponent(const char* str)
108{
109 return IsInt(str, 7);
110}
111
112inline G4bool IsDouble(const char* str)
113{
114 const char* p = str;
115 switch (*p) {
116 case '+':
117 case '-':
118 ++p;
119 if (isdigit(*p) != 0) {
120 while (isdigit((G4int)(*p)) != 0) {
121 ++p;
122 }
123 switch (*p) {
124 case '\0':
125 return true; // break;
126 case 'E':
127 case 'e':
128 return ExpectExponent(++p); // break;
129 case '.':
130 ++p;
131 if (*p == '\0') {
132 return true;
133 }
134 if (*p == 'e' || *p == 'E') {
135 return ExpectExponent(++p);
136 }
137 if (isdigit(*p) != 0) {
138 while (isdigit((G4int)(*p)) != 0) {
139 ++p;
140 }
141 if (*p == '\0') {
142 return true;
143 }
144 if (*p == 'e' || *p == 'E') {
145 return ExpectExponent(++p);
146 }
147 }
148 else {
149 return false;
150 }
151 break;
152 default:
153 return false;
154 }
155 }
156 if (*p == '.') {
157 ++p;
158 if (isdigit(*p) != 0) {
159 while (isdigit((G4int)(*p)) != 0) {
160 ++p;
161 }
162 if (*p == '\0') {
163 return true;
164 }
165 if (*p == 'e' || *p == 'E') {
166 return ExpectExponent(++p);
167 }
168 }
169 }
170 break;
171 case '.':
172 ++p;
173 if (isdigit(*p) != 0) {
174 while (isdigit((G4int)(*p)) != 0) {
175 ++p;
176 }
177 if (*p == '\0') {
178 return true;
179 }
180 if (*p == 'e' || *p == 'E') {
181 return ExpectExponent(++p);
182 }
183 }
184 break;
185 default: // digit is expected
186 if (isdigit(*p) != 0) {
187 while (isdigit((G4int)(*p)) != 0) {
188 ++p;
189 }
190 if (*p == '\0') {
191 return true;
192 }
193 if (*p == 'e' || *p == 'E') {
194 return ExpectExponent(++p);
195 }
196 if (*p == '.') {
197 ++p;
198 if (*p == '\0') {
199 return true;
200 }
201 if (*p == 'e' || *p == 'E') {
202 return ExpectExponent(++p);
203 }
204 if (isdigit(*p) != 0) {
205 while (isdigit((G4int)(*p)) != 0) {
206 ++p;
207 }
208 if (*p == '\0') {
209 return true;
210 }
211 if (*p == 'e' || *p == 'E') {
212 return ExpectExponent(++p);
213 }
214 }
215 }
216 }
217 }
218 return false;
219}
220
221// --------------------------------------------------------------------
222inline G4int CompareInt(G4int arg1, G4int op, G4int arg2, G4int& errCode)
223{
224 G4int result = -1;
225 switch (op) {
226 case G4UItokenNum::GT:
227 result = static_cast<G4int>(arg1 > arg2);
228 break;
229 case G4UItokenNum::GE:
230 result = static_cast<G4int>(arg1 >= arg2);
231 break;
232 case G4UItokenNum::LT:
233 result = static_cast<G4int>(arg1 < arg2);
234 break;
235 case G4UItokenNum::LE:
236 result = static_cast<G4int>(arg1 <= arg2);
237 break;
238 case G4UItokenNum::EQ:
239 result = static_cast<G4int>(arg1 == arg2);
240 break;
241 case G4UItokenNum::NE:
242 result = static_cast<G4int>(arg1 != arg2);
243 break;
244 default:
245 G4cerr << "Parameter range: error at CompareInt" << G4endl;
246 errCode = 1;
247 }
248 return result;
249}
250
251// --------------------------------------------------------------------
252inline G4int CompareLong(G4long arg1, G4int op, G4long arg2, G4int& errCode)
253{
254 G4int result = -1;
255 switch (op) {
256 case G4UItokenNum::GT:
257 result = static_cast<G4int>(arg1 > arg2);
258 break;
259 case G4UItokenNum::GE:
260 result = static_cast<G4int>(arg1 >= arg2);
261 break;
262 case G4UItokenNum::LT:
263 result = static_cast<G4int>(arg1 < arg2);
264 break;
265 case G4UItokenNum::LE:
266 result = static_cast<G4int>(arg1 <= arg2);
267 break;
268 case G4UItokenNum::EQ:
269 result = static_cast<G4int>(arg1 == arg2);
270 break;
271 case G4UItokenNum::NE:
272 result = static_cast<G4int>(arg1 != arg2);
273 break;
274 default:
275 G4cerr << "Parameter range: error at CompareInt" << G4endl;
276 errCode = 1;
277 }
278 return result;
279}
280
281// --------------------------------------------------------------------
282inline G4int CompareDouble(G4double arg1, G4int op, G4double arg2, G4int& errCode)
283{
284 G4int result = -1;
285 switch (op) {
286 case G4UItokenNum::GT:
287 result = static_cast<G4int>(arg1 > arg2);
288 break;
289 case G4UItokenNum::GE:
290 result = static_cast<G4int>(arg1 >= arg2);
291 break;
292 case G4UItokenNum::LT:
293 result = static_cast<G4int>(arg1 < arg2);
294 break;
295 case G4UItokenNum::LE:
296 result = static_cast<G4int>(arg1 <= arg2);
297 break;
298 case G4UItokenNum::EQ:
299 result = static_cast<G4int>(arg1 == arg2);
300 break;
301 case G4UItokenNum::NE:
302 result = static_cast<G4int>(arg1 != arg2);
303 break;
304 default:
305 G4cerr << "Parameter range: error at CompareDouble" << G4endl;
306 errCode = 1;
307 }
308 return result;
309}
310
311} // namespace G4UIparsing
312
313#endif
double G4double
Definition G4Types.hh:83
long G4long
Definition G4Types.hh:87
bool G4bool
Definition G4Types.hh:86
int G4int
Definition G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition G4ios.hh:67
G4bool IsDouble(const char *str)
G4bool ExpectExponent(const char *str)
G4bool ChkMax(const char *str, short maxDigits)
T StoT(const G4String &s)
G4int CompareLong(G4long arg1, G4int op, G4long arg2, G4int &errCode)
G4String TtoS(T value)
G4int CompareDouble(G4double arg1, G4int op, G4double arg2, G4int &errCode)
G4bool IsInt(const char *str, short maxDigits)
G4int CompareInt(G4int arg1, G4int op, G4int arg2, G4int &errCode)
#define INT_MIN
Definition templates.hh:94
#define INT_MAX
Definition templates.hh:90