diff options
author | Brendan Taylor <whateley@gmail.com> | 2010-10-29 12:16:20 -0600 |
---|---|---|
committer | Brendan Taylor <whateley@gmail.com> | 2010-10-29 12:16:20 -0600 |
commit | 457642eb3fea682f06b3aee5d5b61cf88e5b2991 (patch) | |
tree | be3face969fc7e4dc4219f3e6c2eb6331930e86f | |
parent | 5ed250ef5aafaaa578db68deae228f0af9cbda14 (diff) |
split off some generic functions into util.c
-rw-r--r-- | src/callbacks.c | 2 | ||||
-rw-r--r-- | src/util.c | 91 | ||||
-rw-r--r-- | src/util.h | 15 | ||||
-rw-r--r-- | src/uzbl-core.c | 83 | ||||
-rw-r--r-- | src/uzbl-core.h | 18 |
5 files changed, 108 insertions, 101 deletions
diff --git a/src/callbacks.c b/src/callbacks.c index 0fbf589..f07d63e 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -6,7 +6,7 @@ #include "uzbl-core.h" #include "callbacks.h" #include "events.h" - +#include "util.h" void set_proxy_url() { diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..f6c2e0a --- /dev/null +++ b/src/util.c @@ -0,0 +1,91 @@ +#define _POSIX_SOURCE + +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include "util.h" + +XDG_Var XDG[] = +{ + { "XDG_CONFIG_HOME", "~/.config" }, + { "XDG_DATA_HOME", "~/.local/share" }, + { "XDG_CACHE_HOME", "~/.cache" }, + { "XDG_CONFIG_DIRS", "/etc/xdg" }, + { "XDG_DATA_DIRS", "/usr/local/share/:/usr/share/" }, +}; + +/*@null@*/ gchar* +get_xdg_var (XDG_Var xdg) { + const gchar* actual_value = getenv (xdg.environmental); + const gchar* home = getenv ("HOME"); + gchar* return_value; + + if (! actual_value || strcmp (actual_value, "") == 0) { + if (xdg.default_value) { + return_value = str_replace ("~", home, xdg.default_value); + } else { + return_value = NULL; + } + } else { + return_value = str_replace("~", home, actual_value); + } + + return return_value; +} + +/*@null@*/ gchar* +find_xdg_file (int xdg_type, const char* filename) { + /* xdg_type = 0 => config + xdg_type = 1 => data + xdg_type = 2 => cache*/ + + gchar* xdgv = get_xdg_var (XDG[xdg_type]); + gchar* temporary_file = g_strconcat (xdgv, filename, NULL); + g_free (xdgv); + + gchar* temporary_string; + char* saveptr; + char* buf; + + if (! file_exists (temporary_file) && xdg_type != 2) { + buf = get_xdg_var (XDG[3 + xdg_type]); + temporary_string = (char *) strtok_r (buf, ":", &saveptr); + g_free(buf); + + while ((temporary_string = (char * ) strtok_r (NULL, ":", &saveptr)) && ! file_exists (temporary_file)) { + g_free (temporary_file); + temporary_file = g_strconcat (temporary_string, filename, NULL); + } + } + + //g_free (temporary_string); - segfaults. + + if (file_exists (temporary_file)) { + return temporary_file; + } else { + g_free(temporary_file); + return NULL; + } +} + +gboolean +file_exists (const char * filename) { + return (access(filename, F_OK) == 0); +} + +char * +str_replace (const char* search, const char* replace, const char* string) { + gchar **buf; + char *ret; + + if(!string) + return NULL; + + buf = g_strsplit (string, search, -1); + ret = g_strjoinv (replace, buf); + g_strfreev(buf); + + return ret; +} + diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..2a00b8a --- /dev/null +++ b/src/util.h @@ -0,0 +1,15 @@ +#include <glib.h> + +typedef struct { + gchar* environmental; + gchar* default_value; +} XDG_Var; + +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); diff --git a/src/uzbl-core.c b/src/uzbl-core.c index 12d20ae..9cb6f08 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -34,6 +34,7 @@ #include "events.h" #include "inspector.h" #include "config.h" +#include "util.h" UzblCore uzbl; @@ -62,15 +63,6 @@ GOptionEntry entries[] = { NULL, 0, 0, 0, NULL, NULL, NULL } }; -XDG_Var XDG[] = -{ - { "XDG_CONFIG_HOME", "~/.config" }, - { "XDG_DATA_HOME", "~/.local/share" }, - { "XDG_CACHE_HOME", "~/.cache" }, - { "XDG_CONFIG_DIRS", "/etc/xdg" }, - { "XDG_DATA_DIRS", "/usr/local/share/:/usr/share/" }, -}; - /* abbreviations to help keep the table's width humane */ #define PTR_V_STR(var, d, fun) { .ptr.s = &(var), .type = TYPE_STR, .dump = d, .writeable = 1, .func = fun } #define PTR_V_INT(var, d, fun) { .ptr.i = (int*)&(var), .type = TYPE_INT, .dump = d, .writeable = 1, .func = fun } @@ -372,21 +364,6 @@ strfree(gchar *str) { gchar* argv_idx(const GArray *a, const guint idx) { return g_array_index(a, gchar*, idx); } -char * -str_replace (const char* search, const char* replace, const char* string) { - gchar **buf; - char *ret; - - if(!string) - return NULL; - - buf = g_strsplit (string, search, -1); - ret = g_strjoinv (replace, buf); - g_strfreev(buf); - - return ret; -} - GArray* read_file_by_line (const gchar *path) { GIOChannel *chan = NULL; @@ -734,11 +711,6 @@ builtins() { /* -- CORE FUNCTIONS -- */ -bool -file_exists (const char * filename) { - return (access(filename, F_OK) == 0); -} - void set_var(WebKitWebView *page, GArray *argv, GString *result) { (void) page; (void) result; @@ -2108,59 +2080,6 @@ run_handler (const gchar *act, const gchar *args) { g_strfreev(parts); } -/*@null@*/ gchar* -get_xdg_var (XDG_Var xdg) { - const gchar* actual_value = getenv (xdg.environmental); - const gchar* home = getenv ("HOME"); - gchar* return_value; - - if (! actual_value || strcmp (actual_value, "") == 0) { - if (xdg.default_value) { - return_value = str_replace ("~", home, xdg.default_value); - } else { - return_value = NULL; - } - } else { - return_value = str_replace("~", home, actual_value); - } - - return return_value; -} - -/*@null@*/ gchar* -find_xdg_file (int xdg_type, const char* filename) { - /* xdg_type = 0 => config - xdg_type = 1 => data - xdg_type = 2 => cache*/ - - gchar* xdgv = get_xdg_var (XDG[xdg_type]); - gchar* temporary_file = g_strconcat (xdgv, filename, NULL); - g_free (xdgv); - - gchar* temporary_string; - char* saveptr; - char* buf; - - if (! file_exists (temporary_file) && xdg_type != 2) { - buf = get_xdg_var (XDG[3 + xdg_type]); - temporary_string = (char *) strtok_r (buf, ":", &saveptr); - g_free(buf); - - while ((temporary_string = (char * ) strtok_r (NULL, ":", &saveptr)) && ! file_exists (temporary_file)) { - g_free (temporary_file); - temporary_file = g_strconcat (temporary_string, filename, NULL); - } - } - - //g_free (temporary_string); - segfaults. - - if (file_exists (temporary_file)) { - return temporary_file; - } else { - g_free(temporary_file); - return NULL; - } -} void settings_init () { State *s = &uzbl.state; diff --git a/src/uzbl-core.h b/src/uzbl-core.h index b4b251d..08fdce2 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -206,12 +206,6 @@ extern UzblCore uzbl; typedef void sigfunc(int); -/* XDG Stuff */ -typedef struct { - gchar* environmental; - gchar* default_value; -} XDG_Var; - /* uzbl variables */ enum ptr_type {TYPE_INT, TYPE_STR, TYPE_FLOAT}; typedef struct { @@ -230,9 +224,6 @@ typedef struct { char * itos(int val); -char * -str_replace (const char* search, const char* replace, const char* string); - gchar* strfree(gchar *str); @@ -263,9 +254,6 @@ print(WebKitWebView *page, GArray *argv, GString *result); void commands_hash(void); -bool -file_exists (const char * filename); - void load_uri (WebKitWebView * web_view, GArray *argv, GString *result); @@ -348,12 +336,6 @@ create_plug (); void run_handler (const gchar *act, const gchar *args); -/*@null@*/ gchar* -get_xdg_var (XDG_Var xdg); - -/*@null@*/ gchar* -find_xdg_file (int xdg_type, const char* filename); - void settings_init (); |