From 55c2c934de2702933652af1d655e0e377a829e6c Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Sun, 21 Jan 2007 22:47:52 +0000 Subject: Cleanup for g_text_file_get_string --- eglib/gmain.c | 77 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 26 deletions(-) (limited to 'eglib/gmain.c') 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 #include #include +#include #include @@ -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: -- cgit