MCPcopy Index your code
hub / github.com/GJDuck/e9patch / parseIntOptArg

Function parseIntOptArg

src/e9patch/e9patch.cpp:184–213  ·  view source on GitHub ↗

* Parse an integer from an optarg. */

Source from the content-addressed store, hash-verified

182 * Parse an integer from an optarg.
183 */
184static intptr_t parseIntOptArg(const char *option, const char *optarg,
185 intptr_t lb, intptr_t ub, bool hex = false)
186{
187 const char *optarg_0 = optarg;
188 bool neg = (optarg[0] == '-');
189 if (neg)
190 optarg++;
191 int base = 10;
192 if (optarg[0] == '0' && optarg[1] == 'x')
193 base = 16;
194 errno = 0;
195 char *end = nullptr;
196 intptr_t r = (intptr_t)strtoul(optarg, &end, base);
197 r = (neg? -r: r);
198 if (errno != 0 || end == optarg ||
199 (end != nullptr && *end != '\0') || r < lb || r > ub)
200 {
201 if (!hex)
202 error("failed to parse argument \"%s\" for the `%s' option; "
203 "expected a number within the range %zd..%zd", option,
204 optarg_0, lb, ub);
205 else
206 error("failed to parse argument \"%s\" for the `%s' option; "
207 "expected a number within the range %s0x%lx..%s0x%lx",
208 option, optarg_0,
209 (lb < 0? "-": ""), std::abs(lb),
210 (ub < 0? "-": ""), std::abs(ub));
211 }
212 return r;
213}
214
215/*
216 * Parse a Boolean from an optarg.

Callers 1

parseOptionsFunction · 0.70

Calls 3

strtoulFunction · 0.85
errorFunction · 0.85
absFunction · 0.85

Tested by

no test coverage detected