summaryrefslogtreecommitdiffstats
path: root/eglib
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@nokia.com>2007-01-21 22:47:52 +0000
committerJohan Hedberg <johan.hedberg@nokia.com>2007-01-21 22:47:52 +0000
commit55c2c934de2702933652af1d655e0e377a829e6c (patch)
treebc95b87aaec89c8c7ac13151fd0a4d92bdc5f2c8 /eglib
parentea16654e9f78891fabb4257b3ae88fd1c2991aaf (diff)
Cleanup for g_text_file_get_string
Diffstat (limited to 'eglib')
-rw-r--r--eglib/gmain.c77
1 files changed, 51 insertions, 26 deletions
diff --git a/eglib/gmain.c b/eglib/gmain.c
index 5be81a3c..9936143d 100644
--- a/eglib/gmain.c
+++ b/eglib/gmain.c
@@ -15,6 +15,7 @@
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/file.h>
+#include <ctype.h>
#include <gmain.h>
@@ -1221,15 +1222,29 @@ gboolean g_key_file_load_from_file(GKeyFile *key_file,
return TRUE;
}
+static char *next_line(const char *ptr)
+{
+ char *nl;
+
+ nl = strchr(ptr, '\n');
+ if (!nl)
+ return NULL;
+
+ if (nl[1] == '\0')
+ return NULL;
+
+ return nl + 1;
+}
+
gchar *g_key_file_get_string(GKeyFile *key_file,
const gchar *group_name,
const gchar *key,
GError **error)
{
struct stat st;
- char *map, *nl, *off, *group = NULL, *value = NULL, tmp[1024];
+ char *map, *line, *group = NULL, *value = NULL;
off_t size;
- size_t key_len, to_copy, value_len;
+ size_t key_len, group_len;
int fd, err = 0;
fd = open(key_file->filename, O_RDONLY);
@@ -1257,40 +1272,50 @@ gchar *g_key_file_get_string(GKeyFile *key_file,
goto unlock;
}
- group = strstr(map, group_name);
- if (!group)
- goto unmap;
-
- nl = strchr(group, '\n');
- if (!nl)
- goto unmap;
+ group_len = strlen(group_name);
+ key_len = strlen(key);
- off = strstr(nl + 1, key);
- if (!off)
- goto unmap;
+ for (line = map; line != NULL; line = next_line(line)) {
+ int i;
+ size_t to_copy, value_len;
+ char tmp[1024], *nl;
- key_len = strlen(key);
+ if (*line == '#')
+ continue;
- if (off[key_len] != '=')
- goto unmap;
+ if (!group) {
+ if (line[0] != '[' || strcmp(line + 1, group_name))
+ continue;
+ if (line[group_len + 1] == ']')
+ group = line + 1;
+ continue;
+ }
- off += key_len + 1;
+ if (strncmp(line, key, key_len))
+ continue;
- nl = strchr(off, '\n');
- if (!nl)
- goto unmap;
+ for (i = 0; line[i] != '\n'; i++) {
+ if (line[i] == '=')
+ break;
+ if (!isspace(line[i]))
+ break;
+ }
- value_len = nl - off;
+ if (line[i] != '=')
+ continue;
- to_copy = value_len > (sizeof(tmp) - 1) ? sizeof(tmp) - 1 : value_len;
+ nl = strchr(line, '\n');
+ if (!nl)
+ continue;
- memset(tmp, 0, sizeof(tmp));
+ value_len = nl - (line + i + 1);
+ to_copy = value_len > (sizeof(tmp) - 1) ? sizeof(tmp) - 1 : value_len;
+ memset(tmp, 0, sizeof(tmp));
+ strncpy(tmp, line + i + 1, to_copy);
- strncpy(tmp, off, to_copy);
+ value = g_strdup(tmp);
+ }
- value = g_strdup(tmp);
-
-unmap:
munmap(map, size);
unlock: