MCPcopy Create free account
hub / github.com/GJDuck/e9patch / parseIntOptArg

Function parseIntOptArg

src/e9tool/e9tool.cpp:667–689  ·  view source on GitHub ↗

* Parse an integer from an optarg. */

Source from the content-addressed store, hash-verified

665 * Parse an integer from an optarg.
666 */
667static intptr_t parseIntOptArg(const char *option, const char *optarg,
668 intptr_t lb, intptr_t ub)
669{
670 const char *optarg_0 = optarg;
671 bool neg = (optarg[0] == '-');
672 if (neg)
673 optarg++;
674 int base = 10;
675 if (optarg[0] == '0' && optarg[1] == 'x')
676 base = 16;
677 errno = 0;
678 char *end = nullptr;
679 intptr_t r = (intptr_t)strtoul(optarg, &end, base);
680 r = (neg? -r: r);
681 if (errno != 0 || end == optarg ||
682 (end != nullptr && *end != '\0') || r < lb || r > ub)
683 {
684 error("failed to parse argument \"%s\" for the `%s' option; "
685 "expected a number within the range %zd..%zd", option,
686 optarg_0, lb, ub);
687 }
688 return r;
689}
690
691/*
692 * Entry.

Callers 1

main_2Function · 0.70

Calls 2

strtoulFunction · 0.85
errorFunction · 0.85

Tested by

no test coverage detected