summaryrefslogtreecommitdiffstats
path: root/src/lib/strnum.h
blob: 471d4aa3657f57182cb9792da79dc4b124d733e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef STRNUM_H
#define STRNUM_H

/* str_to_*() functions return 0 if string is nothing more than a valid number
   in valid range. Otherwise -1 is returned and num_r is left untouched

   str_parse_*() helpers do not require the number to be the entire string
   and pass back the pointer just past a valid parsed integer in endp_r if
   it is non-NULL. What is written to endp_r in error cases is undefined.
*/

/*
 * Unsigned decimal
 */

int str_to_uint(const char *str, unsigned int *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uint(const char *str, unsigned int *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_ulong(const char *str, unsigned long *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_ulong(const char *str, unsigned long *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_ullong(const char *str, unsigned long long *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_ullong(const char *str, unsigned long long *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_uint32(const char *str, uint32_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uint32(const char *str, uint32_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_uint64(const char *str, uint64_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uint64(const char *str, uint64_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_uintmax(const char *str, uintmax_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uintmax(const char *str, uintmax_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

/* Returns TRUE if str is a valid unsigned number that equals to num. */
bool str_uint_equals(const char *str, uintmax_t num);

/*
 * Unsigned hexadecimal
 */

int str_to_uint_hex(const char *str, unsigned int *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uint_hex(const char *str, unsigned int *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_ulong_hex(const char *str, unsigned long *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_ulong_hex(const char *str, unsigned long *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_ullong_hex(const char *str, unsigned long long *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_ullong_hex(const char *str, unsigned long long *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_uint32_hex(const char *str, uint32_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uint32_hex(const char *str, uint32_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_uint64_hex(const char *str, uint64_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uint64_hex(const char *str, uint64_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_uintmax_hex(const char *str, uintmax_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uintmax_hex(const char *str, uintmax_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

/*
 * Unsigned octal
 */

int str_to_uint_oct(const char *str, unsigned int *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uint_oct(const char *str, unsigned int *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_ulong_oct(const char *str, unsigned long *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_ulong_oct(const char *str, unsigned long *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_ullong_oct(const char *str, unsigned long long *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_ullong_oct(const char *str, unsigned long long *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_uint32_oct(const char *str, uint32_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uint32_oct(const char *str, uint32_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_uint64_oct(const char *str, uint64_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uint64_oct(const char *str, uint64_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_uintmax_oct(const char *str, uintmax_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uintmax_oct(const char *str, uintmax_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

/*
 * Signed
 */

int str_to_int(const char *str, int *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_int(const char *str, int *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_long(const char *str, long *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_long(const char *str, long *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_llong(const char *str, long long *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_llong(const char *str, long long *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_int32(const char *str, int32_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_int32(const char *str, int32_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_int64(const char *str, int64_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_int64(const char *str, int64_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_intmax(const char *str, intmax_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_intmax(const char *str, intmax_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

/*
 * Special numeric types
 */

int str_to_uid(const char *str, uid_t *num_r)
	ATTR_WARN_UNUSED_RESULT;

int str_to_gid(const char *str, gid_t *num_r)
	ATTR_WARN_UNUSED_RESULT;

int str_to_pid(const char *str, pid_t *num_r)
	ATTR_WARN_UNUSED_RESULT;

int str_to_ino(const char *str, ino_t *num_r)
	ATTR_WARN_UNUSED_RESULT;

int str_to_uoff(const char *str, uoff_t *num_r)
	ATTR_WARN_UNUSED_RESULT;
int str_parse_uoff(const char *str, uoff_t *num_r,
	const char **endp_r) ATTR_WARN_UNUSED_RESULT ATTR_NULL(3);

int str_to_time(const char *str, time_t *num_r)
	ATTR_WARN_UNUSED_RESULT;

/*
 * Utility
 */

/* Return TRUE if all characters in string are numbers.
   Stop when `end_char' is found from string. */
bool str_is_numeric(const char *str, char end_char) ATTR_PURE;

/* Return TRUE when string has one or more numbers, followed
   with zero or one dot, followed with at least one number. */
bool str_is_float(const char *str, char end_char) ATTR_PURE;

/* Returns human readable string about what is wrong with the string.
   This function assumes that str_to_*() had already returned -1 for the
   string. */
const char *str_num_error(const char *str);

#endif