2f1e3abedd4b6a060ea671ea8a2f5480bfd43c86
[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 <ctype.h>
43
44 /*
45  * Returns when the line containing the section is found.
46  * The file pointer is positioned at the start of the next line.
47  * Returns EOF if no section was found, 0 otherwise.
48  */
49 static int findSection(KFile *fd, const char *section, size_t section_len, char *line, size_t size)
50 {
51         while (kfile_gets(fd, line, size) != EOF)
52         {
53                 char *ptr = line;
54                 unsigned i;
55                 /* accept only sections that begin at first char */
56                 if (*ptr++ != '[')
57                         continue;
58
59                 /* find the end-of-section character */
60                 for (i = 0; i < size && *ptr != ']'; ++i, ++ptr)
61                         ;
62
63                 /* The found section could be long that our section key */
64                 if (section_len != i)
65                         continue;
66
67                 /* did we find the correct section? */
68 #if CONFIG_INI_CASE_INSENSITIVE
69                 if(strncasecmp(&line[1], section, section_len))
70 #else
71                 if(strncmp(&line[1], section, section_len))
72 #endif
73                         continue;
74                 else
75                         return 0;
76         }
77         return EOF;
78 }
79
80 /*
81  * Fills the argument with the key found in line
82  */
83 static char *getKey(const char *line, char *key, size_t size)
84 {
85         /* null-terminated string */
86         while (isspace((unsigned char)*line))
87                 ++line;
88         int i = 0;
89         while (*line != '=' && !isspace((unsigned char)*line) && size)
90         {
91                 key[i++] = *line;
92                 ++line;
93                 --size;
94         }
95         size ? (key[i] = '\0') : (key[i-1] = '\0');
96         return key;
97 }
98
99 /*
100  * Fills the argument with the value found in line.
101  */
102 static char *getValue(const char *line, char *value, size_t size)
103 {
104         while (*line++ != '=')
105                 ;
106         while (isspace((unsigned char)*line))
107                 ++line;
108         int i = 0;
109         while (*line && size)
110         {
111                 value[i++] = *line++;
112                 --size;
113         }
114         size ? (value[i] = '\0') : (value[i-1] = '\0');
115         return value;
116 }
117
118 /**
119  * Look for key inside a section.
120  *
121  * The function reads lines from input file. It fills the line parameter to allow splitting
122  * the key-value couple. It returns with error if a new section begins and no key was found.
123  * \return 0 if key was found, EOF on errors.
124  */
125 static int findKey(KFile *fd, const char *key, char *line, size_t size)
126 {
127         int err;
128         do
129         {
130                 err = kfile_gets(fd, line, size);
131                 char curr_key[30];
132                 getKey(line, curr_key, 30);
133                 /* check key */
134 #if CONFIG_INI_CASE_INSENSITIVE
135                 if (!strcasecmp(curr_key, key))
136 #else
137                 if (!strcmp(curr_key, key))
138 #endif
139                         return 0;
140         }
141         while (err != EOF && *line != '[');
142         return EOF;
143 }
144
145 /*
146  * On errors, the function returns EOF and fills the buffer with the default value.
147  */
148 int ini_getString(KFile *fd, const char *section, const char *key, const char *default_value, char *buf, size_t size)
149 {
150         char line[CONFIG_INI_MAX_LINE_LEN];
151
152         if (kfile_seek(fd, 0, KSM_SEEK_SET) == EOF)
153             goto error;
154
155         if (findSection(fd, section, strlen(section), line, CONFIG_INI_MAX_LINE_LEN) == EOF)
156                 goto error;
157
158         if (findKey(fd, key, line, CONFIG_INI_MAX_LINE_LEN) == EOF)
159                 goto error;
160         else
161                 getValue(line, buf, size);
162         return 0;
163
164 error:
165         strncpy(buf, default_value, size);
166         if (size > 0)
167                 buf[size - 1] = '\0';
168         return EOF;
169 }