]> git.ozlabs.org Git - ccan/blob - ccan/ccan_tokenizer/documentation
various: add LICENSE comments.
[ccan] / ccan / ccan_tokenizer / documentation
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.
5
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.
7
8 readui() uses errno to indicate success or failure.  It will set errno to one of the following:
9
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
13
14 Example (UNTESTED):
15
16 uint64_t read_number(const char *str) {
17         const char *s = str, *e = strchr(str, 0);
18         readui_base base = READUI_DEC;
19         uint64_t result;
20         
21         //See if the number has a 0x (for hex) or 0 (for octal) prefix
22         if (s+2<=e && *s=='0') {
23                 s++;
24                 if (*s=='x' || *s=='X') {
25                         base = READUI_HEX;
26                         s++;
27                 } else
28                         base = READUI_OCT;
29         }
30         
31         result = readui(&s, e, base);
32         
33         if (errno)
34                 perror("read_number");
35         
36         return result;
37 }
38
39 Rules for a token list:
40
41 It always has and starts with a TOK_STARTLINE
42
43
44 Misc.:
45
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:
47
48 #if 0
49 0b101.0p0
50 #endif
51