diff options
Diffstat (limited to 'plugins/utils.c')
-rw-r--r-- | plugins/utils.c | 110 |
1 files changed, 44 insertions, 66 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index 71c0bdd8..7e14b6e4 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
@@ -147,98 +147,80 @@ print_revision (const char *command_name, const char *revision) | |||
147 | command_name, revision, PACKAGE, VERSION); | 147 | command_name, revision, PACKAGE, VERSION); |
148 | } | 148 | } |
149 | 149 | ||
150 | int | 150 | bool is_numeric (char *number) { |
151 | is_numeric (char *number) | ||
152 | { | ||
153 | char tmp[1]; | 151 | char tmp[1]; |
154 | float x; | 152 | float x; |
155 | 153 | ||
156 | if (!number) | 154 | if (!number) |
157 | return FALSE; | 155 | return false; |
158 | else if (sscanf (number, "%f%c", &x, tmp) == 1) | 156 | else if (sscanf (number, "%f%c", &x, tmp) == 1) |
159 | return TRUE; | 157 | return true; |
160 | else | 158 | else |
161 | return FALSE; | 159 | return false; |
162 | } | 160 | } |
163 | 161 | ||
164 | int | 162 | bool is_positive (char *number) { |
165 | is_positive (char *number) | ||
166 | { | ||
167 | if (is_numeric (number) && atof (number) > 0.0) | 163 | if (is_numeric (number) && atof (number) > 0.0) |
168 | return TRUE; | 164 | return true; |
169 | else | 165 | else |
170 | return FALSE; | 166 | return false; |
171 | } | 167 | } |
172 | 168 | ||
173 | int | 169 | bool is_negative (char *number) { |
174 | is_negative (char *number) | ||
175 | { | ||
176 | if (is_numeric (number) && atof (number) < 0.0) | 170 | if (is_numeric (number) && atof (number) < 0.0) |
177 | return TRUE; | 171 | return true; |
178 | else | 172 | else |
179 | return FALSE; | 173 | return false; |
180 | } | 174 | } |
181 | 175 | ||
182 | int | 176 | bool is_nonnegative (char *number) { |
183 | is_nonnegative (char *number) | ||
184 | { | ||
185 | if (is_numeric (number) && atof (number) >= 0.0) | 177 | if (is_numeric (number) && atof (number) >= 0.0) |
186 | return TRUE; | 178 | return true; |
187 | else | 179 | else |
188 | return FALSE; | 180 | return false; |
189 | } | 181 | } |
190 | 182 | ||
191 | int | 183 | bool is_percentage (char *number) { |
192 | is_percentage (char *number) | ||
193 | { | ||
194 | int x; | 184 | int x; |
195 | if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100) | 185 | if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100) |
196 | return TRUE; | 186 | return true; |
197 | else | 187 | else |
198 | return FALSE; | 188 | return false; |
199 | } | 189 | } |
200 | 190 | ||
201 | int | 191 | bool is_integer (char *number) { |
202 | is_integer (char *number) | ||
203 | { | ||
204 | long int n; | 192 | long int n; |
205 | 193 | ||
206 | if (!number || (strspn (number, "-0123456789 ") != strlen (number))) | 194 | if (!number || (strspn (number, "-0123456789 ") != strlen (number))) |
207 | return FALSE; | 195 | return false; |
208 | 196 | ||
209 | n = strtol (number, NULL, 10); | 197 | n = strtol (number, NULL, 10); |
210 | 198 | ||
211 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) | 199 | if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) |
212 | return TRUE; | 200 | return true; |
213 | else | 201 | else |
214 | return FALSE; | 202 | return false; |
215 | } | 203 | } |
216 | 204 | ||
217 | int | 205 | bool is_intpos (char *number) { |
218 | is_intpos (char *number) | ||
219 | { | ||
220 | if (is_integer (number) && atoi (number) > 0) | 206 | if (is_integer (number) && atoi (number) > 0) |
221 | return TRUE; | 207 | return true; |
222 | else | 208 | else |
223 | return FALSE; | 209 | return false; |
224 | } | 210 | } |
225 | 211 | ||
226 | int | 212 | bool is_intneg (char *number) { |
227 | is_intneg (char *number) | ||
228 | { | ||
229 | if (is_integer (number) && atoi (number) < 0) | 213 | if (is_integer (number) && atoi (number) < 0) |
230 | return TRUE; | 214 | return true; |
231 | else | 215 | else |
232 | return FALSE; | 216 | return false; |
233 | } | 217 | } |
234 | 218 | ||
235 | int | 219 | bool is_intnonneg (char *number) { |
236 | is_intnonneg (char *number) | ||
237 | { | ||
238 | if (is_integer (number) && atoi (number) >= 0) | 220 | if (is_integer (number) && atoi (number) >= 0) |
239 | return TRUE; | 221 | return true; |
240 | else | 222 | else |
241 | return FALSE; | 223 | return false; |
242 | } | 224 | } |
243 | 225 | ||
244 | /* | 226 | /* |
@@ -246,19 +228,19 @@ is_intnonneg (char *number) | |||
246 | * On success the number will be written to the _target_ address, if _target_ is not set | 228 | * On success the number will be written to the _target_ address, if _target_ is not set |
247 | * to NULL. | 229 | * to NULL. |
248 | */ | 230 | */ |
249 | int is_int64(char *number, int64_t *target) { | 231 | bool is_int64(char *number, int64_t *target) { |
250 | errno = 0; | 232 | errno = 0; |
251 | uint64_t tmp = strtoll(number, NULL, 10); | 233 | uint64_t tmp = strtoll(number, NULL, 10); |
252 | if (errno != 0) { | 234 | if (errno != 0) { |
253 | return 0; | 235 | return false; |
254 | } | 236 | } |
255 | if (tmp < INT64_MIN || tmp > INT64_MAX) { | 237 | if (tmp < INT64_MIN || tmp > INT64_MAX) { |
256 | return 0; | 238 | return false; |
257 | } | 239 | } |
258 | if (target != NULL) { | 240 | if (target != NULL) { |
259 | *target = tmp; | 241 | *target = tmp; |
260 | } | 242 | } |
261 | return 1; | 243 | return true; |
262 | } | 244 | } |
263 | 245 | ||
264 | /* | 246 | /* |
@@ -266,40 +248,36 @@ int is_int64(char *number, int64_t *target) { | |||
266 | * On success the number will be written to the _target_ address, if _target_ is not set | 248 | * On success the number will be written to the _target_ address, if _target_ is not set |
267 | * to NULL. | 249 | * to NULL. |
268 | */ | 250 | */ |
269 | int is_uint64(char *number, uint64_t *target) { | 251 | bool is_uint64(char *number, uint64_t *target) { |
270 | errno = 0; | 252 | errno = 0; |
271 | uint64_t tmp = strtoll(number, NULL, 10); | 253 | uint64_t tmp = strtoll(number, NULL, 10); |
272 | if (errno != 0) { | 254 | if (errno != 0) { |
273 | return 0; | 255 | return false; |
274 | } | 256 | } |
275 | if (tmp < 0 || tmp > UINT64_MAX) { | 257 | if (tmp < 0 || tmp > UINT64_MAX) { |
276 | return 0; | 258 | return false; |
277 | } | 259 | } |
278 | if (target != NULL) { | 260 | if (target != NULL) { |
279 | *target = tmp; | 261 | *target = tmp; |
280 | } | 262 | } |
281 | return 1; | 263 | return true; |
282 | } | 264 | } |
283 | 265 | ||
284 | int | 266 | bool is_intpercent (char *number) { |
285 | is_intpercent (char *number) | ||
286 | { | ||
287 | int i; | 267 | int i; |
288 | if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100) | 268 | if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100) |
289 | return TRUE; | 269 | return true; |
290 | else | 270 | else |
291 | return FALSE; | 271 | return false; |
292 | } | 272 | } |
293 | 273 | ||
294 | int | 274 | bool is_option (char *str) { |
295 | is_option (char *str) | ||
296 | { | ||
297 | if (!str) | 275 | if (!str) |
298 | return FALSE; | 276 | return false; |
299 | else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) | 277 | else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) |
300 | return TRUE; | 278 | return true; |
301 | else | 279 | else |
302 | return FALSE; | 280 | return false; |
303 | } | 281 | } |
304 | 282 | ||
305 | #ifdef NEED_GETTIMEOFDAY | 283 | #ifdef NEED_GETTIMEOFDAY |