home · contact · privacy
9829e4edf6f094ba49fd287328ba8884b07a3605
[plomrogue] / src / common / parse_file.c
1 /* src/common/parse_file.c */
2
3 #define _POSIX_C_SOURCE 200809L /* strdup(), snprintf() */
4 #include "parse_file.h"
5 #include <stddef.h> /* size_t, NULL */
6 #include <stdio.h> /* FILE, snprintf() */
7 #include <stdint.h> /* int16_t,uint8_t,uint32_t, INT16_MIN, UINT{8,16,32}_MAX */
8 #include <stdlib.h> /* atoi(), free() */
9 #include <string.h> /* strchr, strcmp(), strdup(), strlen() */
10 #include "rexit.h" /* exit_err(), exit_trouble() */
11 #include "try_malloc.h" /* try_malloc() */
12
13
14
15 /* Set by parse_file(), helps err_line() deciding what to do/output on error. */
16 static uint32_t err_line_count = 0;
17 static char * err_line_line = NULL;
18 static char * err_line_intro = NULL;
19 static uint8_t err_line_exit = 1;
20
21
22
23 /* Determines the end of the token_from_line() token. */
24 static void set_token_end(char ** start, char ** limit_char);
25
26
27
28 static void set_token_end(char ** start, char ** limit_char)
29 {
30     char * end_quote = ('\'' == (* start)[0]) ? strchr(* start + 1, '\''): NULL;
31     * start = (end_quote) ? * start + 1 : *start;
32     if (end_quote)
33     {
34         * end_quote = '\0';
35         * limit_char = end_quote;
36         return;
37     }
38     char * space = strchr(*start, ' ');
39     char * tab   = strchr(*start, '\t');
40     space = (!space || (tab && tab < space)) ? tab : space;
41     if (space)
42     {
43         * space = '\0';
44     }
45     *limit_char = strchr(*start, '\0');
46 }
47
48
49
50 extern void set_err_line_options(char * intro, char * line, uint8_t exit)
51 {
52     err_line_line = line;
53     err_line_intro = intro;
54     err_line_exit = exit;
55 }
56
57
58
59 extern void err_line_inc()
60 {
61     err_line_count++;
62     err_line(UINT32_MAX == err_line_count, "Line reaches max lines limit.");
63 }
64
65
66
67 extern void err_line_zero()
68 {
69     err_line_count = 0;
70 }
71
72
73
74 extern uint8_t err_line(uint8_t test, char * msg)
75 {
76     if (!test)
77     {
78         return 0;
79     }
80     char * prefix = " Offending line ";
81     char * affix = ": ";
82     size_t size =   strlen(err_line_intro) + strlen(msg) + strlen(prefix)
83                   + 10                 /* strlen for uint32_t representations */
84                   + strlen(affix) + strlen(err_line_line) + 1;
85     char * err = try_malloc(size, __func__);
86     int ret = snprintf(err, size, "%s%s%s%d%s%s", err_line_intro, msg, prefix,
87                        err_line_count, affix, err_line_line);
88     exit_trouble(ret < 0, __func__, "snprintf");
89     if (err_line_exit)
90     {
91         exit_err(1, err);
92     }
93     exit_trouble(0 > printf("%s\n", err), __func__, "printf");
94     exit_trouble(EOF == fflush(stdout), __func__, "fflush");
95     free(err);
96     return 1;
97 }
98
99
100
101 extern char * token_from_line(char * line)
102 {
103     static char * final_char = NULL;
104     static char * limit_char = NULL;
105     char * start = limit_char + 1;
106     if (line)
107     {
108         start      = line;
109         limit_char = start;
110         final_char = &(line[strlen(line)]);
111         if ('\n' == *(final_char - 1))
112         {
113             *(--final_char) = '\0';
114         }
115     }
116     if (final_char < start)
117     {
118         return NULL;
119     }
120     uint8_t empty = 1;
121     uint32_t i;
122     for (i = 0; '\0' != start[i]; i++)
123     {
124         if (' ' != start[i] && '\t' != start[i])
125         {
126             start = &start[i];
127             empty = 0;
128             break;
129         }
130     }
131     if (empty)
132     {
133         return NULL;
134     }
135     set_token_end(&start, &limit_char);
136     return start;
137 }
138
139
140
141 extern uint8_t parsetest_int(char * string, char type)
142 {
143     char * err_8 = "Value must represent proper unsigned 8 bit integer.";
144     char * err_i = "Value must represent proper signed 16 bit integer.";
145     char * err_u = "Value must represent proper unsigned 16 bit integer.";
146     char * err_U = "Value must represent proper unsigned 32 bit integer.";
147     char * err = ('8' == type) ? err_8 : err_U;
148     err = ('i' == type) ? err_i : err;
149     err = ('u' == type) ? err_u : err;
150     uint8_t ret = err_line(strlen(string) < 1, err);
151     uint8_t i;
152     uint8_t test;
153     for (i = 0; '\0' != string[i]; i++)
154     {
155         char * err_many = "Value of too many characters.";
156         ret = ret + err_line(string[i + 1] && UINT8_MAX == i, err_many);
157         test = (   (0 == i && ('-' == string[i] || '+' == string[i]))
158                 || ('0' <= string[i]  && string[i] <= '9'));
159         ret = ret + err_line(!test, err);
160     }
161     ret = ret + err_line(   strlen(string) < 2
162                          && ('-' == string[i] || '+' == string[i]), err);
163     test =     (   '8' == type
164                 && (   strlen(string) > 4
165                     || atoi(string) < 0 || atoi(string) > UINT8_MAX))
166             || (   'i' == type
167                 && (   strlen(string) > 6
168                     || atol(string) < INT16_MIN || atol(string) > INT16_MAX))
169             || (   'u' == type
170                 && (   strlen(string) > 6
171                     || atoll(string) < 0 || atol(string) > UINT16_MAX))
172             || (   'U' == type
173                 && (   strlen(string) > 11
174                     || atoll(string) < 0 || atoll(string) > UINT32_MAX));
175     ret = ret + err_line(test, err);
176     return ret;
177 }
178
179
180
181 extern uint8_t parsetest_singlechar(char * string)
182 {
183     return err_line(1 !=strlen(string),"Value must be single ASCII character.");
184 }
185
186
187
188 extern uint8_t parse_val(char * token0, char * token1, char * comparand,
189                          char type, char * element)
190 {
191     if (!strcmp(token0, comparand))
192     {
193         if      ('s' == type)
194         {
195             free(* (char **) element);
196             * (char **) element = strdup(token1);
197         }
198         else if ('c' == type)
199         {
200             if (!parsetest_singlechar(token1))
201             {
202                 *element = (token1)[0];
203             }
204         }
205         else if (!parsetest_int(token1, type))
206         {
207             if ('8' == type)
208             {
209                 * (uint8_t *) element = atoi(token1);
210             }
211             else if ('i' == type)
212             {
213                 * (int16_t *) element = atoi(token1);
214             }
215             else if ('u' == type)
216             {
217                 * (uint16_t *) element = atol(token1);
218             }
219             else if ('U' == type)
220             {
221                 * (uint32_t *) element = atoll(token1);
222             }
223         }
224         return 1;
225     }
226     return 0;
227 }