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