From: Daniel Carl Date: Mon, 7 Jan 2013 19:55:30 +0000 (+0100) Subject: Use higher level function to read config file. X-Git-Url: https://git.owens.tech/git.owens.tech/git.owens.tech/git?a=commitdiff_plain;h=6b7f18a7d9398090a5ade2a74f11c41c69265074;p=vimb.git Use higher level function to read config file. --- diff --git a/src/main.c b/src/main.c index 3f1caa7..ce103d9 100644 --- a/src/main.c +++ b/src/main.c @@ -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) diff --git a/src/util.c b/src/util.c index 3a84eae..6e7a050 100644 --- a/src/util.c +++ b/src/util.c @@ -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; +} diff --git a/src/util.h b/src/util.h index 85e1db7..5dd747b 100644 --- a/src/util.h +++ b/src/util.h @@ -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 */