Add more functions to manage ini file.
[bertos.git] / bertos / mware / ini_reader.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2009 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Ini file reader module.
34  *
35  * \author Luca Ottaviano <lottaviano@develer.com>
36  */
37
38 #include "ini_reader.h"
39 #include "cfg/cfg_ini_reader.h"
40 #include <string.h>
41 #include <stdio.h>
42 #include <stdlib.h> //strtol
43 #include <ctype.h>
44
45 static bool lineEmpty(const char *line)
46 {
47         while (*line)
48         {
49                 if (!isspace((unsigned char)*line++))
50                         return false;
51         }
52         return true;
53 }
54
55 /*
56  * Returns when the line containing the section is found.
57  * The file pointer is positioned at the start of the next line or
58  * after the last non-empty line if the section is not found.
59  * Returns EOF if no section was found, 0 otherwise.
60  */
61 static int findSection(KFile *fd, const char *section, size_t section_len, char *line, size_t size)
62 {
63         kfile_off_t last_full = fd->seek_pos;
64
65         int err;
66         do
67         {
68                 char *ptr = line;
69                 unsigned i;
70                 err = kfile_gets(fd, line, size);
71
72                 /* Remember the last filled line in file */
73                 if (!lineEmpty(line))
74                         last_full = fd->seek_pos;
75
76                 /* accept only sections that begin at first char */
77                 if (*ptr++ != '[')
78                         continue;
79
80                 /* find the end-of-section character */
81                 for (i = 0; i < size && *ptr != ']'; ++i, ++ptr)
82                         ;
83
84                 /* The found section could be long that our section key */
85                 if (section_len != i)
86                         continue;
87
88                 /* did we find the correct section? */
89                 if(strncmp(&line[1], section, section_len))
90                         continue;
91                 else
92                         return 0;
93         }
94         while (err != EOF);
95
96         kfile_seek(fd, last_full, KSM_SEEK_SET);
97         return EOF;
98 }
99
100 /*
101  * Fills the argument with the key found in line
102  */
103 static char *getKey(const char *line, char *key, size_t size)
104 {
105         /* null-terminated string */
106         while (isspace((unsigned char)*line))
107                 ++line;
108         int i = 0;
109         while (*line != '=' && !isspace((unsigned char)*line) && size)
110         {
111                 key[i++] = *line;
112                 ++line;
113                 --size;
114         }
115         size ? (key[i] = '\0') : (key[i-1] = '\0');
116         return key;
117 }
118
119 /*
120  * Fills the argument with the value found in line.
121  */
122 static char *getValue(const char *line, char *value, size_t size)
123 {
124         while (*line++ != '=')
125                 ;
126         while (isspace((unsigned char)*line))
127                 ++line;
128         int i = 0;
129         while (*line && size)
130         {
131                 value[i++] = *line++;
132                 --size;
133         }
134         size ? (value[i] = '\0') : (value[i-1] = '\0');
135         return value;
136 }
137
138 /**
139  * Look for key inside a section.
140  *
141  * The function reads lines from input file. It fills the line parameter to allow splitting
142  * the key-value couple. It returns with error if a new section begins and no key was found.
143  * \return 0 if key was found, EOF on errors.
144  */
145 static int findKey(KFile *fd, const char *key, char *line, size_t size)
146 {
147         int err;
148         char curr_key[30];
149         kfile_off_t last_full = fd->seek_pos;
150         kfile_off_t key_pos = fd->seek_pos;
151
152         do
153         {
154                 err = kfile_gets(fd, line, size);
155
156                 getKey(line, curr_key, 30);
157                 /* check key */
158                 if (!strcmp(curr_key, key))
159                 {
160                         kfile_seek(fd, key_pos, KSM_SEEK_SET);
161                         return 0;
162                 }
163
164                 /* Remember the last filled line in the section */
165                 if (!lineEmpty(line) && *line != '[')
166                         last_full = fd->seek_pos;
167                 key_pos = fd->seek_pos;
168         }
169         while (err != EOF && *line != '[');
170         kfile_seek(fd, last_full, KSM_SEEK_SET);
171         return EOF;
172 }
173
174 /*
175  * On errors, the function returns EOF and fills the buffer with the default value.
176  */
177 int ini_getString(KFile *fd, const char *section, const char *key, const char *default_value, char *buf, size_t size)
178 {
179         char line[CONFIG_INI_MAX_LINE_LEN];
180
181         if (kfile_seek(fd, 0, KSM_SEEK_SET) == EOF)
182             goto error;
183
184         if (findSection(fd, section, strlen(section), line, CONFIG_INI_MAX_LINE_LEN) == EOF)
185                 goto error;
186
187         if (findKey(fd, key, line, CONFIG_INI_MAX_LINE_LEN) == EOF)
188                 goto error;
189         else
190                 getValue(line, buf, size);
191         return 0;
192
193 error:
194         strncpy(buf, default_value, size);
195         if (size > 0)
196                 buf[size - 1] = '\0';
197         return EOF;
198 }
199
200 int ini_getInteger(KFile *fd, const char *section, const char *key, long default_value, long *val, int base)
201 {
202         char buf[CONFIG_INI_MAX_LINE_LEN];
203
204         if (ini_getString(fd, section, key, "", buf, sizeof(buf)) == EOF)
205                 goto error;
206
207         char **endptr = NULL;
208         *val = strtol(buf, endptr, base);
209
210         if (buf[0] == 0 || **endptr != 0)
211                 goto error;
212
213         return 0;
214
215 error:
216         *val = default_value;
217         return EOF;
218 }
219
220 /*
221  * Return the position immediatly following the last non-empty line in the file,
222  * starting from current position.
223  * The file seek position is unchanged at function exit.
224  */
225 static kfile_off_t findLastLine(KFile *fd, char *line, size_t size)
226 {
227         kfile_off_t start = fd->seek_pos;
228         kfile_off_t last_full = start;
229
230         int err;
231         do
232         {
233                 err = kfile_gets(fd, line, size);
234                 if (!lineEmpty(line))
235                         last_full = fd->seek_pos;
236         }
237         while (err != EOF);
238         kfile_seek(fd, start, KSM_SEEK_SET);
239         return last_full;
240 }
241
242 int ini_setString(KFile *in, KFile *out, const char *section, const char *key, const char *value)
243 {
244         char line[CONFIG_INI_MAX_LINE_LEN];
245
246         if (kfile_seek(in, 0, KSM_SEEK_SET) == EOF)
247             return EOF;
248
249         if (kfile_seek(out, 0, KSM_SEEK_SET) == EOF)
250             return EOF;
251
252         bool section_found = false;
253         bool key_found = false;
254         if (findSection(in, section, strlen(section), line, CONFIG_INI_MAX_LINE_LEN) != EOF)
255         {
256                 section_found = true;
257                 key_found = (findKey(in, key, line, CONFIG_INI_MAX_LINE_LEN) != EOF);
258         }
259
260         kfile_off_t len = in->seek_pos;
261
262         /* Copy until key */
263         if (kfile_seek(in, 0, KSM_SEEK_SET) == EOF)
264                         return EOF;
265         if (kfile_copy(in, out, len) != len)
266                         return EOF;
267
268         if (!section_found && value)
269         {
270                 if ((size_t)kfile_printf(out, "\n[%s]\n", section) != (strlen(section) + 4))
271                         return EOF;
272         }
273         if (value)
274         {
275                 if ((size_t)kfile_printf(out, "%s=%s\n", key, value) != (strlen(key) + strlen(value) + 2))
276                         return EOF;
277         }
278
279         /* Skip the old line with the key */
280         if (key_found)
281                 kfile_gets(in, line, CONFIG_INI_MAX_LINE_LEN);
282
283         /*
284          * Copy the rest of the input file.
285          * Do not return error if the copied bytes are less than expected
286          * but at least enough to write the last non-empty line.
287          * This is needed for example if the out KFile is of fixed size (like memories).
288          */
289         len = in->size - in->seek_pos;
290         kfile_off_t bytes_needed = findLastLine(in, line, CONFIG_INI_MAX_LINE_LEN) - in->seek_pos;
291         if (kfile_copy(in, out, len) < bytes_needed)
292                 return EOF;
293
294         /*
295          * Clear the rest of the out file in order to wipe out garbage if the resulting
296          * file is shorter than before.
297          */
298         kfile_off_t fill = out->size - out->seek_pos;
299
300         if (fill--)
301         {
302                 if (kfile_putc('\n', out) == EOF)
303                         return EOF;
304                 while (fill--)
305                 {
306                         if (kfile_putc(' ', out) == EOF)
307                                 return EOF;
308                 }
309         }
310
311         return 0;
312 }
313