diff options
| author | Bjoern Brandenburg <bbb@mpi-sws.org> | 2016-07-25 12:00:10 -0400 |
|---|---|---|
| committer | Bjoern Brandenburg <bbb@mpi-sws.org> | 2017-03-10 10:36:34 -0500 |
| commit | f43c80e7865b670a6de2a631dd49cb10c982959c (patch) | |
| tree | 56f2573447bda2771c079b9b9a9e2b786e1a0269 /include | |
| parent | b44dc82b1379e482a0751f8c2aebbf8b4d1df819 (diff) | |
Add str2int() and str2double() helper functions
Don't silently ignore incorrect parameters.
Diffstat (limited to 'include')
| -rw-r--r-- | include/common.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/include/common.h b/include/common.h index fec3721..216f01c 100644 --- a/include/common.h +++ b/include/common.h | |||
| @@ -12,4 +12,55 @@ | |||
| 12 | */ | 12 | */ |
| 13 | void bail_out(const char* msg); | 13 | void bail_out(const char* msg); |
| 14 | 14 | ||
| 15 | /** | ||
| 16 | * Convert the given argument to an integer; set the flag if it fails. | ||
| 17 | * @param arg String to convert to integer (must not be NULL). | ||
| 18 | * @param failure_flag pointer to flag that will be set if conversion failed (may be NULL). | ||
| 19 | * @return integer value of arg | ||
| 20 | */ | ||
| 21 | int str2int(const char* arg, int *failure_flag); | ||
| 22 | |||
| 23 | /** | ||
| 24 | * Convert the given argument to a double; set the flag if it fails. | ||
| 25 | * @param arg String to convert to double (must not be NULL). | ||
| 26 | * @param failure_flag pointer to flag that will be set if conversion failed (may be NULL). | ||
| 27 | * @return integer value of arg | ||
| 28 | */ | ||
| 29 | double str2double(const char* arg, int *failure_flag); | ||
| 30 | |||
| 31 | |||
| 32 | /* the following macros assume that there is a no-return function called usage() */ | ||
| 33 | |||
| 34 | #define want_int_min(arg, min, msg) ({ \ | ||
| 35 | int __val; \ | ||
| 36 | int __fail; \ | ||
| 37 | __val = str2int(arg, &__fail); \ | ||
| 38 | if (__fail || __val < min) \ | ||
| 39 | usage(msg); \ | ||
| 40 | __val;}) | ||
| 41 | |||
| 42 | #define want_double_min(arg, min, msg) ({ \ | ||
| 43 | double __val; \ | ||
| 44 | int __fail; \ | ||
| 45 | __val = str2double(arg, &__fail); \ | ||
| 46 | if (__fail || __val < min) \ | ||
| 47 | usage(msg); \ | ||
| 48 | __val;}) | ||
| 49 | |||
| 50 | #define want_non_negative_int(arg, name) \ | ||
| 51 | want_int_min(arg, 0, "option " name " requires a non-negative integer argument") | ||
| 52 | |||
| 53 | #define want_positive_int(arg, name) \ | ||
| 54 | want_int_min(arg, 1, "option " name " requires a positive integer argument") | ||
| 55 | |||
| 56 | #define want_non_negative_double(arg, name) \ | ||
| 57 | want_double_min(arg, 0, "option " name " requires a non-negative argument") | ||
| 58 | |||
| 59 | #define want_positive_double(arg, name) ({ \ | ||
| 60 | double __val = want_double_min(arg, 0, "option " name " requires a positive argument"); \ | ||
| 61 | if (!__val) \ | ||
| 62 | usage("option " name " requires a positive argument"); \ | ||
| 63 | __val; }) | ||
| 64 | |||
| 65 | |||
| 15 | #endif | 66 | #endif |
