1 readui - Flexible function for reading a 64-bit unsigned integer
2 @sp: Pointer to scanning pointer
3 @e: Pointer to end of string
4 @base: Typically one of READUI_DEC, READUI_HEX, READUI_OCT, or READUI_BIN.
6 readui() converts the string of digits from *sp to e to a number, setting *sp to the first invalid character or e if the entire string is valid or empty. It does not look at prefixes or suffixes, only digits. It skips preceding whitespace.
8 readui() uses errno to indicate success or failure. It will set errno to one of the following:
10 0: Input is valid and non-empty
11 EINVAL: Input is empty, does not start with any valid digits, or base is 0
12 ERANGE: Number given is greater than ULLONG_MAX
16 uint64_t read_number(const char *str) {
17 const char *s = str, *e = strchr(str, 0);
18 readui_base base = READUI_DEC;
21 //See if the number has a 0x (for hex) or 0 (for octal) prefix
22 if (s+2<=e && *s=='0') {
24 if (*s=='x' || *s=='X') {
31 result = readui(&s, e, base);
34 perror("read_number");
39 Rules for a token list:
41 It always has and starts with a TOK_STARTLINE
46 If the world were intuitive, the tokenizer would never report warnings or bugs on a source file that compiles successfully. However, one case where it does is when erroneous tokens appear within an #if 0 block. Example: