Remove \version svn tag.
[bertos.git] / bertos / mware / ini_reader.c
index 037570dce3a8188c25be3903843c37a489fe378e..d16c41a31bb387a72930d18d3a2200c52d591eea 100644 (file)
@@ -32,7 +32,6 @@
  *
  * \brief Ini file reader module.
  *
- * \version $Id$
  * \author Luca Ottaviano <lottaviano@develer.com>
  */
 
@@ -47,7 +46,7 @@
  * The file pointer is positioned at the start of the next line.
  * Returns EOF if no section was found, 0 otherwise.
  */
-static int findSection(KFile *fd, const char *section, char *line, size_t size)
+static int findSection(KFile *fd, const char *section, size_t section_len, char *line, size_t size)
 {
        while (kfile_gets(fd, line, size) != EOF)
        {
@@ -56,11 +55,17 @@ static int findSection(KFile *fd, const char *section, char *line, size_t size)
                /* accept only sections that begin at first char */
                if (*ptr++ != '[')
                        continue;
+
                /* find the end-of-section character */
                for (i = 0; i < size && *ptr != ']'; ++i, ++ptr)
                        ;
+
+               /* The found section could be long that our section key */
+               if (section_len != i)
+                       continue;
+
                /* did we find the correct section? */
-               if(strncmp(&line[1], section, i))
+               if(strncmp(&line[1], section, section_len))
                        continue;
                else
                        return 0;
@@ -115,14 +120,17 @@ static char *getValue(const char *line, char *value, size_t size)
  */
 static int findKey(KFile *fd, const char *key, char *line, size_t size)
 {
-       while (kfile_gets(fd, line, size) != EOF && *line != '[')
+       int err;
+       do
        {
+               err = kfile_gets(fd, line, size);
                char curr_key[30];
                getKey(line, curr_key, 30);
                /* check key */
                if (!strcmp(curr_key, key))
                        return 0;
        }
+       while (err != EOF && *line != '[');
        return EOF;
 }
 
@@ -132,10 +140,11 @@ static int findKey(KFile *fd, const char *key, char *line, size_t size)
 int ini_getString(KFile *fd, const char *section, const char *key, const char *default_value, char *buf, size_t size)
 {
        char line[CONFIG_INI_MAX_LINE_LEN];
+
        if (kfile_seek(fd, 0, KSM_SEEK_SET) == EOF)
            goto error;
 
-       if (findSection(fd, section, line, CONFIG_INI_MAX_LINE_LEN) == EOF)
+       if (findSection(fd, section, strlen(section), line, CONFIG_INI_MAX_LINE_LEN) == EOF)
                goto error;
 
        if (findKey(fd, key, line, CONFIG_INI_MAX_LINE_LEN) == EOF)