Use higher level function to read config file.
authorDaniel Carl <danielcarl@gmx.de>
Mon, 7 Jan 2013 19:55:30 +0000 (20:55 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 7 Jan 2013 19:57:39 +0000 (20:57 +0100)
src/main.c
src/util.c
src/util.h

index 3f1caa7..ce103d9 100644 (file)
@@ -569,34 +569,30 @@ static void vp_init(void)
 
 static void vp_read_config(void)
 {
-    FILE* fp;
-    gchar line[255];
-
     /* load default config */
     for (guint i = 0; default_config[i].command != NULL; i++) {
         vp_process_input(default_config[i].command);
     }
 
     /* read config from config files */
-    if (access(vp.files[FILES_CONFIG], F_OK) != 0) {
-        fprintf(stderr, "Could not find config file");
-        return;
-    }
-    fp = fopen(vp.files[FILES_CONFIG], "r");
-    if (fp == NULL) {
-        fprintf(stderr, "Could not read config file");
-        return;
-    }
-    while (fgets(line, 254, fp)) {
-        if (!g_ascii_isalpha(line[0])) {
-            continue;
-        }
-        if (!vp_process_input(line)) {
-            fprintf(stderr, "Invalid config: %s", line);
+    gchar **lines = util_get_lines(vp.files[FILES_CONFIG]);
+    gchar *line;
+
+    if (lines) {
+        gint length = g_strv_length(lines) - 1;
+        for (gint i = 0; i < length; i++) {
+            line = lines[i];
+            g_strstrip(line);
+
+            if (!g_ascii_isalpha(line[0])) {
+                continue;
+            }
+            if (vp_process_input(line)) {
+                fprintf(stderr, "Invalid config: %s\n", line);
+            }
         }
+        g_strfreev(lines);
     }
-
-    fclose(fp);
 }
 
 static void vp_init_gui(void)
index 3a84eae..6e7a050 100644 (file)
@@ -79,3 +79,20 @@ gchar* util_get_file_contents(const gchar* filename, gsize* length)
     }
     return content;
 }
+
+/**
+ * Retrieves the file content as lines.
+ *
+ * The result have to be freed by g_strfreev().
+ */
+gchar** util_get_lines(const gchar* filename)
+{
+    gchar* content = util_get_file_contents(filename, NULL);
+    gchar** lines  = NULL;
+    if (content) {
+        /* split the file content into lines */
+        lines = g_strsplit(content, "\n", -1);
+        g_free(content);
+    }
+    return lines;
+}
index 85e1db7..5dd747b 100644 (file)
@@ -28,5 +28,6 @@ const gchar* util_get_home_dir(void);
 void util_create_dir_if_not_exists(const gchar* dirpath);
 void util_create_file_if_not_exists(const gchar* filename);
 gchar* util_get_file_contents(const gchar* filename, gsize* length);
+gchar** util_get_lines(const gchar* filename);
 
 #endif /* end of include guard: UTIL_H */