home · contact · privacy
Minor refactoring in file parsing.
[plomrogue] / src / common / parse_file.c
1 /* src/common/parse_file.c */
2
3 #define _POSIX_C_SOURCE 200809L /* strdup() */
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 <unistd.h> /* access(), F_OK */
11 #include "readwrite.h" /* try_fopen(), try_fclose(), textfile_width() */
12 #include "rexit.h" /* exit_err(), exit_trouble() */
13 #include "try_malloc.h" /* try_malloc() */
14
15
16
17 /* Set by parse_file(), used by err_line() for more informative messages. */
18 static uint32_t err_line_count = 0;
19 static char * err_line_line = NULL;
20 static char * err_line_intro = NULL;
21
22
23
24 /* Determines the end of the token_from_line() token. */
25 static void set_token_end(char ** start, char ** limit_char);
26
27
28
29 static void set_token_end(char ** start, char ** limit_char)
30 {
31     char * end_quote = ('\'' == (* start)[0]) ? strchr(* start + 1, '\''): NULL;
32     * start = (end_quote) ? * start + 1 : *start;
33     if (end_quote)
34     {
35         * end_quote = '\0';
36         * limit_char = end_quote;
37         return;
38     }
39     char * space = strchr(*start, ' ');
40     char * tab   = strchr(*start, '\t');
41     space = (!space || (tab && tab < space)) ? tab : space;
42     if (space)
43     {
44         * space = '\0';
45     }
46     *limit_char = strchr(*start, '\0');
47 }
48
49
50
51 extern void parse_file(char * path, void (* token_to_entry) (char *, char *))
52 {
53     char * f_name = "read_new_config_file()";
54     char * prefix = "Failed reading config file: \"";
55     char * affix = "\". ";
56     size_t size = strlen(prefix) + strlen(path) + strlen(affix) + 1;
57     err_line_intro = try_malloc(size, f_name);
58     int test = snprintf(err_line_intro, size, "%s%s%s", prefix, path, affix);
59     exit_trouble(test < 0, f_name, "snprintf()");
60     exit_err(access(path, F_OK), err_line_intro);
61     FILE * file = try_fopen(path, "r", f_name);
62     uint32_t linemax = textfile_width(file);
63     err_line_line = try_malloc(linemax + 1, f_name);
64     err_line_count = 0;
65     err_line(0 == linemax, "File is empty.");
66     char * token0 = NULL; /* For final token_to_entry() if while() stagnates. */
67     char * token1;
68     char * err_val = "No value given.";
69     while (try_fgets(err_line_line, linemax + 1, file, f_name))
70     {
71         err_line_count++;
72         err_line(UINT32_MAX == err_line_count, "Line reaches max lines limit.");
73         char * line_copy = strdup(err_line_line);
74         token0 = token_from_line(line_copy);
75         if (token0)
76         {
77             err_line(0 == (token1 = token_from_line(NULL)), err_val);
78             token_to_entry(token0, token1);
79             token0 = NULL;
80         }
81         free(line_copy);
82     }
83     token_to_entry(token0, token1);
84     try_fclose(file, f_name);
85     free(err_line_line);
86     free(err_line_intro);
87 }
88
89
90
91 extern void err_line(uint8_t test, char * msg)
92 {
93     if (!test)
94     {
95         return;
96     }
97     char * f_name = "err_line()";
98     char * prefix = " Offending line ";
99     char * affix = ":\n";
100     size_t size =   strlen(err_line_intro) + strlen(msg) + strlen(prefix)
101                   + 10                 /* strlen for uint32_t representations */
102                   + strlen(affix) + strlen(err_line_line) + 1;
103     char * err = try_malloc(size, f_name);
104     int ret = snprintf(err, size, "%s%s%s%d%s%s", err_line_intro, msg, prefix,
105                        err_line_count, affix, err_line_line);
106     exit_trouble(ret < 0, f_name, "snprintf()");
107     exit_err(1, err);
108 }
109
110
111
112 extern char * token_from_line(char * line)
113 {
114     static char * final_char = NULL;
115     static char * limit_char = NULL;
116     char * start = limit_char + 1;
117     if (line)
118     {
119         start      = line;
120         limit_char = start;
121         final_char = &(line[strlen(line)]);
122         if ('\n' == *(final_char - 1))
123         {
124             *(--final_char) = '\0';
125         }
126     }
127     uint8_t empty = 1;
128     uint32_t i;
129     for (i = 0; '\0' != start[i]; i++)
130     {
131         if (' ' != start[i] && '\t' != start[i])
132         {
133             start = &start[i];
134             empty = 0;
135             break;
136         }
137     }
138     if (empty)
139     {
140         return start = NULL;
141     }
142     set_token_end(&start, &limit_char);
143     return start;
144 }
145
146
147
148 extern void test_for_int(char * string, char type)
149 {
150     char * err;
151     if ('8' == type)
152     {
153         err = "Value must be proper representation of unsigned 8 bit integer.";
154     }
155     if ('i' == type)
156     {
157         err = "Value must be proper representation of signed 16 bit integer.";
158     }
159     err_line(strlen(string) < 1, err);
160     uint8_t i;
161     uint8_t test;
162     for (i = 0; '\0' != string[i]; i++)
163     {
164         char * err_many = "Value of too many characters.";
165         err_line(string[i + 1] && UINT8_MAX == i, err_many);
166         test = (   (0 == i && ('-' == string[i] || '+' == string[i]))
167                 || ('0' <= string[i]  && string[i] <= '9'));
168         err_line(!test, err);
169     }
170     err_line(strlen(string) < 2 && ('-' == string[i] || '+' == string[i]), err);
171     err_line('8'==type && (atoi(string) < 0 || atoi(string) > UINT8_MAX), err);
172     test = 'i'==type && (atoi(string) < INT16_MIN || atoi(string) > INT16_MAX);
173     err_line(test, err);
174 }
175
176
177
178 extern uint8_t set_val(char * token0, char * token1, char * comparand,
179                        uint8_t * flags, uint8_t set_flag, char type,
180                        char * element)
181 {
182     if (!strcmp(token0, comparand))
183     {
184         char * err_out = "Outside appropriate definition's context.";
185         char * err_singlechar = "Value must be single ASCII character.";
186         err_line(!(*flags & EDIT_STARTED), err_out);
187         *flags = *flags | set_flag;
188         if      ('s' == type)
189         {
190             * (char **) element = strdup(token1);
191         }
192         else if ('c' == type)
193         {
194             err_line(1 != strlen(token1), err_singlechar);
195             *element = (token1)[0];
196         }
197         else if ('8' == type)
198         {
199             test_for_int(token1, '8');
200             * (uint8_t *) element = atoi(token1);
201         }
202         else if ('i' == type)
203         {
204             test_for_int(token1, 'i');
205             * (int16_t *) element = atoi(token1);
206         }
207         return 1;
208     }
209     return 0;
210 }
211
212
213
214 extern void finalize_by_readyflag(uint8_t * flags, uint8_t ready_flag)
215 {
216     char * err_fin = "Last definition block not finished yet.";
217     err_line((*flags & ready_flag) ^ ready_flag, err_fin);
218     *flags = ready_flag;
219 }