From 22502eaa7a8966f0170efc22927465d2c25e10fa Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Tue, 15 Feb 2011 14:50:04 -0700 Subject: further tidying of utility functions --- src/util.c | 58 ++++++++++++++++++--------------------------------------- src/util.h | 4 ---- src/uzbl-core.c | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/src/util.c b/src/util.c index 09b0ad7..cc51048 100644 --- a/src/util.c +++ b/src/util.c @@ -21,12 +21,11 @@ get_xdg_var (XDG_Var xdg) { const gchar *actual_value = getenv(xdg.environmental); const gchar *home = getenv("HOME"); - if (!actual_value || !actual_value[0]) { - if (!xdg.default_value) - return NULL; + if (!actual_value || !actual_value[0]) + actual_value = xdg.default_value; - return str_replace ("~", home, xdg.default_value); - } + if (!actual_value) + return NULL; return str_replace("~", home, actual_value); } @@ -44,18 +43,16 @@ find_xdg_file (int xdg_type, const char* basename) { if (file_exists(path)) return path; + if (xdg_type == 2) + return NULL; + /* the file doesn't exist in the expected directory. * check if it exists in one of the system-wide directories. */ - if (!file_exists(path) && xdg_type != 2) { - char *system_dirs = get_xdg_var(XDG[3 + xdg_type]); - path = find_existing_file2(system_dirs, basename); - g_free(system_dirs); - } + char *system_dirs = get_xdg_var(XDG[3 + xdg_type]); + path = find_existing_file2(system_dirs, basename); + g_free(system_dirs); - if(path) - return path; - - return NULL; + return path; } gboolean @@ -85,36 +82,17 @@ for_each_line_in_file(const gchar *path, void (*callback)(const gchar *l, void * GIOChannel *chan = g_io_channel_new_file(path, "r", NULL); - if (chan) { - while (g_io_channel_read_line(chan, &line, &len, NULL, NULL) == G_IO_STATUS_NORMAL) { - callback(line, user_data); - g_free(line); - } - g_io_channel_unref (chan); + if (!chan) + return FALSE; - return TRUE; + while (g_io_channel_read_line(chan, &line, &len, NULL, NULL) == G_IO_STATUS_NORMAL) { + callback(line, user_data); + g_free(line); } - return FALSE; -} - + g_io_channel_unref (chan); -enum exp_type -get_exp_type(const gchar *s) { - /* variables */ - if(*(s+1) == '(') - return EXP_EXPR; - else if(*(s+1) == '{') - return EXP_BRACED_VAR; - else if(*(s+1) == '<') - return EXP_JS; - else if(*(s+1) == '[') - return EXP_ESCAPE; - else - return EXP_SIMPLE_VAR; - - /*@notreached@*/ -return EXP_ERR; + return TRUE; } /* This function searches the directories in the : separated ($PATH style) diff --git a/src/util.h b/src/util.h index d2e7f6c..126166b 100644 --- a/src/util.h +++ b/src/util.h @@ -6,14 +6,10 @@ typedef struct { gchar* default_value; } XDG_Var; -enum exp_type {EXP_ERR, EXP_SIMPLE_VAR, EXP_BRACED_VAR, EXP_EXPR, EXP_JS, EXP_ESCAPE}; - - gchar* get_xdg_var(XDG_Var xdg); gchar* find_xdg_file(int xdg_type, const char* filename); gboolean file_exists(const char* filename); char* str_replace(const char* search, const char* replace, const char* string); gboolean for_each_line_in_file(const gchar *path, void (*callback)(const gchar *l, void *c), void *user_data); -enum exp_type get_exp_type(const gchar*); gchar* find_existing_file(const gchar*); gchar* argv_idx(const GArray*, const guint); diff --git a/src/uzbl-core.c b/src/uzbl-core.c index 4c709f7..a4d43af 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -165,6 +165,20 @@ create_var_to_name_hash() { /* --- UTILITY FUNCTIONS --- */ +enum exp_type { + EXP_ERR, EXP_SIMPLE_VAR, EXP_BRACED_VAR, EXP_EXPR, EXP_JS, EXP_ESCAPE +}; + +enum exp_type +get_exp_type(const gchar *s) { + switch(*(s+1)) { + case '(': return EXP_EXPR; + case '{': return EXP_BRACED_VAR; + case '<': return EXP_JS; + case '[': return EXP_ESCAPE; + default: return EXP_SIMPLE_VAR; + } +} /* * recurse == 1: don't expand '@(command)@' -- cgit v1.2.3