diff options
| author | jvoisin | 2017-10-01 20:54:03 +0200 |
|---|---|---|
| committer | jvoisin | 2017-10-02 15:20:53 +0200 |
| commit | 36c06637ad262f0e5fc0c8e70f4c1fc6a565f056 (patch) | |
| tree | ea55e322dd3e02ae7fdc2f35a815a9ba8330f301 /src/sp_config_utils.c | |
| parent | 7418a0e1e9b02aef8535e33d30cfb8f082680f69 (diff) | |
First pass for #9
Diffstat (limited to 'src/sp_config_utils.c')
| -rw-r--r-- | src/sp_config_utils.c | 50 |
1 files changed, 3 insertions, 47 deletions
diff --git a/src/sp_config_utils.c b/src/sp_config_utils.c index 39951cc..3ea82d0 100644 --- a/src/sp_config_utils.c +++ b/src/sp_config_utils.c | |||
| @@ -2,18 +2,8 @@ | |||
| 2 | 2 | ||
| 3 | size_t sp_line_no; | 3 | size_t sp_line_no; |
| 4 | 4 | ||
| 5 | static int validate_int(const char *value); | ||
| 6 | static int validate_str(const char *value); | 5 | static int validate_str(const char *value); |
| 7 | 6 | ||
| 8 | static sp_pure int validate_int(const char *value) { | ||
| 9 | for (size_t i = 0; i < strlen(value); i++) { | ||
| 10 | if (!isdigit(value[i])) { | ||
| 11 | return -1; | ||
| 12 | } | ||
| 13 | } | ||
| 14 | return 0; | ||
| 15 | } | ||
| 16 | |||
| 17 | static sp_pure int validate_str(const char *value) { | 7 | static sp_pure int validate_str(const char *value) { |
| 18 | int balance = 0; // ghetto [] validation | 8 | int balance = 0; // ghetto [] validation |
| 19 | 9 | ||
| @@ -124,48 +114,14 @@ err: | |||
| 124 | return NULL; | 114 | return NULL; |
| 125 | } | 115 | } |
| 126 | 116 | ||
| 127 | static char *get_misc(char *restrict line, const char *restrict keyword) { | ||
| 128 | size_t i = 0; | ||
| 129 | char *ret = pecalloc(sizeof(char), 1024, 1); | ||
| 130 | |||
| 131 | while (i < 1024 - 1 && line[i] && line[i] != SP_TOKEN_END_PARAM) { | ||
| 132 | ret[i] = line[i]; | ||
| 133 | i++; | ||
| 134 | } | ||
| 135 | |||
| 136 | if (line[i] != SP_TOKEN_END_PARAM) { | ||
| 137 | if (i >= 1024 - 1) { | ||
| 138 | sp_log_err("config", "The following line is too long: %s on line %zu.", line, sp_line_no); | ||
| 139 | } else { | ||
| 140 | sp_log_err("config", "Missing closing %c in line %s on line %zu.", SP_TOKEN_END_PARAM, | ||
| 141 | line, sp_line_no); | ||
| 142 | } | ||
| 143 | return NULL; | ||
| 144 | } else if (i == 0) { | ||
| 145 | sp_log_err("config", "The keyword %s%c is expecting a parameter on line %zu.", | ||
| 146 | keyword, SP_TOKEN_END_PARAM, sp_line_no); | ||
| 147 | return NULL; | ||
| 148 | } | ||
| 149 | return ret; | ||
| 150 | } | ||
| 151 | |||
| 152 | char *get_param(size_t *consumed, char *restrict line, sp_type type, | 117 | char *get_param(size_t *consumed, char *restrict line, sp_type type, |
| 153 | const char *restrict keyword) { | 118 | const char *restrict keyword) { |
| 154 | char *retval = NULL; | 119 | char *retval = get_string(consumed, line, keyword); |
| 155 | if (type == SP_TYPE_STR) { | ||
| 156 | retval = get_string(consumed, line, keyword); | ||
| 157 | } else { | ||
| 158 | retval = get_misc(line, keyword); | ||
| 159 | *consumed = retval ? strlen(retval) : 0; | ||
| 160 | } | ||
| 161 | 120 | ||
| 162 | if (retval) { | 121 | if (retval && 0 == validate_str(retval)) { |
| 163 | if (type == SP_TYPE_STR && 0 == validate_str(retval)) { | ||
| 164 | return retval; | ||
| 165 | } else if (type == SP_TYPE_INT && 0 == validate_int(retval)) { | ||
| 166 | return retval; | 122 | return retval; |
| 167 | } | ||
| 168 | } | 123 | } |
| 124 | |||
| 169 | return NULL; | 125 | return NULL; |
| 170 | } | 126 | } |
| 171 | 127 | ||
