ini_reader.c

Go to the documentation of this file.
00001 
00039 #include "ini_reader.h"
00040 #include "cfg/cfg_ini_reader.h"
00041 #include <string.h>
00042 #include <stdio.h>
00043 #include <ctype.h>
00044 
00045 /*
00046  * Returns when the line containing the section is found.
00047  * The file pointer is positioned at the start of the next line.
00048  * Returns EOF if no section was found, 0 otherwise.
00049  */
00050 static int findSection(KFile *fd, const char *section, char *line, size_t size)
00051 {
00052     while (kfile_gets(fd, line, size) != EOF)
00053     {
00054         char *ptr = line;
00055         unsigned i;
00056         /* accept only sections that begin at first char */
00057         if (*ptr++ != '[')
00058             continue;
00059         /* find the end-of-section character */
00060         for (i = 0; i < size && *ptr != ']'; ++i, ++ptr)
00061             ;
00062         /* did we find the correct section? */
00063         if(strncmp(&line[1], section, i))
00064             continue;
00065         else
00066             return 0;
00067     }
00068     return EOF;
00069 }
00070 
00071 /*
00072  * Fills the argument with the key found in line
00073  */
00074 static char *getKey(const char *line, char *key, size_t size)
00075 {
00076     /* null-terminated string */
00077     while (isspace(*line))
00078         ++line;
00079     int i = 0;
00080     while (*line != '=' && !isspace(*line) && size)
00081     {
00082         key[i++] = *line;
00083         ++line;
00084         --size;
00085     }
00086     size ? (key[i] = '\0') : (key[i-1] = '\0');
00087     return key;
00088 }
00089 
00090 /*
00091  * Fills the argument with the value found in line.
00092  */
00093 static char *getValue(const char *line, char *value, size_t size)
00094 {
00095     while (*line++ != '=')
00096         ;
00097     while (isspace(*line))
00098         ++line;
00099     int i = 0;
00100     while (*line && size)
00101     {
00102         value[i++] = *line++;
00103         --size;
00104     }
00105     size ? (value[i] = '\0') : (value[i-1] = '\0');
00106     return value;
00107 }
00108 
00116 static int findKey(KFile *fd, const char *key, char *line, size_t size)
00117 {
00118     int err;
00119     do
00120     {
00121         err = kfile_gets(fd, line, size);
00122         char curr_key[30];
00123         getKey(line, curr_key, 30);
00124         /* check key */
00125         if (!strcmp(curr_key, key))
00126             return 0;
00127     }
00128     while (err != EOF && *line != '[');
00129     return EOF;
00130 }
00131 
00132 /*
00133  * On errors, the function returns EOF and fills the buffer with the default value.
00134  */
00135 int ini_getString(KFile *fd, const char *section, const char *key, const char *default_value, char *buf, size_t size)
00136 {
00137     char line[CONFIG_INI_MAX_LINE_LEN];
00138     if (kfile_seek(fd, 0, KSM_SEEK_SET) == EOF)
00139         goto error;
00140 
00141     if (findSection(fd, section, line, CONFIG_INI_MAX_LINE_LEN) == EOF)
00142         goto error;
00143 
00144     if (findKey(fd, key, line, CONFIG_INI_MAX_LINE_LEN) == EOF)
00145         goto error;
00146     else
00147         getValue(line, buf, size);
00148     return 0;
00149 
00150 error:
00151     strncpy(buf, default_value, size);
00152     if (size > 0)
00153         buf[size - 1] = '\0';
00154     return EOF;
00155 }