1 /* src/common/parse_file.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #define _POSIX_C_SOURCE 200809L /* strdup(), snprintf() */
9 #include "parse_file.h"
10 #include <stddef.h> /* size_t, NULL */
11 #include <stdio.h> /* FILE, snprintf() */
12 #include <stdint.h> /* int16_t,uint8_t,uint32_t, INT16_MIN, UINT{8,16,32}_MAX */
13 #include <stdlib.h> /* atoi(), free() */
14 #include <string.h> /* strchr, strcmp(), strdup(), strlen() */
15 #include "rexit.h" /* exit_err(), exit_trouble() */
16 #include "try_malloc.h" /* try_malloc() */
20 /* Set by parse_file(), helps err_line() deciding what to do/output on error. */
21 static uint32_t err_line_count = 0;
22 static char * err_line_line = NULL;
23 static char * err_line_intro = NULL;
24 static uint8_t err_line_exit = 1;
28 /* Determines the end of the token_from_line() token. */
29 static void set_token_end(char ** start, char ** limit_char);
33 static void set_token_end(char ** start, char ** limit_char)
35 char * end_quote = ('\'' == (* start)[0]) ? strchr(* start + 1, '\''): NULL;
36 * start = (end_quote) ? * start + 1 : *start;
40 * limit_char = end_quote;
43 char * space = strchr(*start, ' ');
44 char * tab = strchr(*start, '\t');
45 space = (!space || (tab && tab < space)) ? tab : space;
50 *limit_char = strchr(*start, '\0');
55 extern void set_err_line_options(char * intro, char * line, uint8_t exit)
58 err_line_intro = intro;
64 extern void err_line_inc()
67 err_line(UINT32_MAX == err_line_count, "Line reaches max lines limit.");
72 extern void err_line_zero()
79 extern uint8_t err_line(uint8_t test, char * msg)
85 char * prefix = " Offending line ";
87 size_t size = strlen(err_line_intro) + strlen(msg) + strlen(prefix)
88 + 10 /* strlen for uint32_t representations */
89 + strlen(affix) + strlen(err_line_line) + 1;
90 char * err = try_malloc(size, __func__);
91 int ret = snprintf(err, size, "%s%s%s%d%s%s", err_line_intro, msg, prefix,
92 err_line_count, affix, err_line_line);
93 exit_trouble(ret < 0, __func__, "snprintf");
98 exit_trouble(0 > printf("%s\n", err), __func__, "printf");
99 exit_trouble(EOF == fflush(stdout), __func__, "fflush");
106 extern char * token_from_line(char * line)
108 static char * final_char = NULL;
109 static char * limit_char = NULL;
110 char * start = limit_char + 1;
115 final_char = &(line[strlen(line)]);
116 if ('\n' == *(final_char - 1))
118 *(--final_char) = '\0';
121 if (final_char < start)
127 for (i = 0; '\0' != start[i]; i++)
129 if (' ' != start[i] && '\t' != start[i])
140 set_token_end(&start, &limit_char);
146 extern uint8_t parsetest_int(char * string, char type)
148 char * err_8 = "Value must represent proper unsigned 8 bit integer.";
149 char * err_i = "Value must represent proper signed 16 bit integer.";
150 char * err_u = "Value must represent proper unsigned 16 bit integer.";
151 char * err_U = "Value must represent proper unsigned 32 bit integer.";
152 char * err = ('8' == type) ? err_8 : err_U;
153 err = ('i' == type) ? err_i : err;
154 err = ('u' == type) ? err_u : err;
155 uint8_t ret = err_line(strlen(string) < 1, err);
158 for (i = 0; '\0' != string[i]; i++)
160 char * err_many = "Value of too many characters.";
161 ret = ret + err_line(string[i + 1] && UINT8_MAX == i, err_many);
162 test = ( (0 == i && ('-' == string[i] || '+' == string[i]))
163 || ('0' <= string[i] && string[i] <= '9'));
164 ret = ret + err_line(!test, err);
166 ret = ret + err_line( strlen(string) < 2
167 && ('-' == string[i] || '+' == string[i]), err);
169 && ( strlen(string) > 4
170 || atoi(string) < 0 || atoi(string) > UINT8_MAX))
172 && ( strlen(string) > 6
173 || atol(string) < INT16_MIN || atol(string) > INT16_MAX))
175 && ( strlen(string) > 6
176 || atoll(string) < 0 || atol(string) > UINT16_MAX))
178 && ( strlen(string) > 11
179 || atoll(string) < 0 || atoll(string) > UINT32_MAX));
180 ret = ret + err_line(test, err);
186 extern uint8_t parsetest_singlechar(char * string)
188 return err_line(1 !=strlen(string),"Value must be single ASCII character.");
193 extern uint8_t parse_val(char * token0, char * token1, char * comparand,
194 char type, char * element)
196 if (!strcmp(token0, comparand))
200 free(* (char **) element);
201 * (char **) element = strdup(token1);
203 else if ('c' == type)
205 if (!parsetest_singlechar(token1))
207 *element = (token1)[0];
210 else if (!parsetest_int(token1, type))
214 * (uint8_t *) element = atoi(token1);
216 else if ('i' == type)
218 * (int16_t *) element = atoi(token1);
220 else if ('u' == type)
222 * (uint16_t *) element = atol(token1);
224 else if ('U' == type)
226 * (uint32_t *) element = atoll(token1);