From 457642eb3fea682f06b3aee5d5b61cf88e5b2991 Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Fri, 29 Oct 2010 12:16:20 -0600 Subject: split off some generic functions into util.c --- src/uzbl-core.h | 18 ------------------ 1 file changed, 18 deletions(-) (limited to 'src/uzbl-core.h') 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 (); -- cgit v1.2.3 From 2f6fe6720ca69b58224bd5f45c9b47be9b7f8fbe Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Tue, 2 Nov 2010 20:59:23 -0600 Subject: get rid of read_file_by_line config files no longer have to be read entirely into memory, and external javascript no longer does pointless reallocations --- src/uzbl-core.c | 91 +++++++++++++++++++-------------------------------------- src/uzbl-core.h | 3 -- 2 files changed, 30 insertions(+), 64 deletions(-) (limited to 'src/uzbl-core.h') diff --git a/src/uzbl-core.c b/src/uzbl-core.c index 9cb6f08..8b2dba5 100644 --- a/src/uzbl-core.c +++ b/src/uzbl-core.c @@ -364,31 +364,24 @@ strfree(gchar *str) { gchar* argv_idx(const GArray *a, const guint idx) { return g_array_index(a, gchar*, idx); } -GArray* -read_file_by_line (const gchar *path) { - GIOChannel *chan = NULL; - gchar *readbuf = NULL; +void +for_each_line_in_file(gchar *path, void (*callback)(const gchar *l, void *c), void *user_data) { \ + gchar *line = NULL; gsize len; - GArray *lines = g_array_new(TRUE, FALSE, sizeof(gchar*)); - int i = 0; - chan = g_io_channel_new_file(path, "r", NULL); + GIOChannel *chan = g_io_channel_new_file(path, "r", NULL); + if (chan) { - while (g_io_channel_read_line(chan, &readbuf, &len, NULL, NULL) == G_IO_STATUS_NORMAL) { - const gchar* val = g_strdup (readbuf); - g_array_append_val (lines, val); - g_free (readbuf); - i ++; + 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); } else { gchar *tmp = g_strdup_printf("File %s can not be read.", path); send_event(COMMAND_ERROR, tmp, NULL); g_free(tmp); } - - return lines; } /* search a PATH style string for an existing file+path combination */ @@ -941,29 +934,26 @@ hardcopy(WebKitWebView *page, GArray *argv, GString *result) { webkit_web_frame_print(webkit_web_view_get_main_frame(page)); } +/* just a wrapper so parse_cmd_line can be used with for_each_line_in_file */ +static void +parse_cmd_line_cb(const char *line, void *user_data) { + (void) user_data; + parse_cmd_line(line, NULL); +} + void include(WebKitWebView *page, GArray *argv, GString *result) { (void) page; (void) result; gchar *pe = NULL, - *path = NULL, - *line; - int i=0; + *path = NULL; if(!argv_idx(argv, 0)) return; pe = parseenv(argv_idx(argv, 0)); if((path = find_existing_file(pe))) { - GArray* lines = read_file_by_line(path); - - while ((line = g_array_index(lines, gchar*, i))) { - parse_cmd_line (line, NULL); - i++; - g_free (line); - } - g_array_free (lines, TRUE); - + for_each_line_in_file(path, parse_cmd_line_cb, NULL); send_event(FILE_INCLUDED, path, NULL); g_free(path); } @@ -1127,32 +1117,23 @@ run_external_js (WebKitWebView * web_view, GArray *argv, GString *result) { if (argv_idx(argv, 0) && ((path = find_existing_file(argv_idx(argv, 0)))) ) { - GArray* lines = read_file_by_line (path); - gchar* js = NULL; - int i = 0; - gchar* line; - - while ((line = g_array_index(lines, gchar*, i))) { - if (js == NULL) { - js = g_strdup (line); - } else { - gchar* newjs = g_strconcat (js, line, NULL); - js = newjs; - } - i ++; - g_free (line); + gchar *file_contents = NULL; + + GIOChannel *chan = g_io_channel_new_file(path, "r", NULL); + if (chan) { + gsize len; + g_io_channel_read_to_end(chan, &file_contents, &len, NULL); + g_io_channel_unref (chan); } if (uzbl.state.verbose) printf ("External JavaScript file %s loaded\n", argv_idx(argv, 0)); - gchar* newjs = str_replace("%s", argv_idx (argv, 1)?argv_idx (argv, 1):"", js); - g_free (js); - js = newjs; + gchar *js = str_replace("%s", argv_idx (argv, 1) ? argv_idx (argv, 1) : "", file_contents); + g_free (file_contents); eval_js (web_view, js, result, path); g_free (js); - g_array_free (lines, TRUE); g_free(path); } } @@ -1575,7 +1556,6 @@ parse_cmd_line(const char *ctl_line, GString *result) { g_free(ctlstrip); } - /*@null@*/ gchar* build_stream_name(int type, const gchar* dir) { State *s = &uzbl.state; @@ -2099,21 +2079,10 @@ settings_init () { s->config_file = find_xdg_file (0, "/uzbl/config"); } - if (s->config_file) { - GArray* lines = read_file_by_line (s->config_file); - int i = 0; - gchar* line; - - while ((line = g_array_index(lines, gchar*, i))) { - parse_cmd_line (line, NULL); - i ++; - g_free (line); - } - g_array_free (lines, TRUE); - } else { - if (uzbl.state.verbose) - printf ("No configuration file loaded.\n"); - } + if (s->config_file) + for_each_line_in_file(s->config_file, parse_cmd_line_cb, NULL); + else if (uzbl.state.verbose) + printf ("No configuration file loaded.\n"); if(s->connect_socket_names) init_connect_socket(); diff --git a/src/uzbl-core.h b/src/uzbl-core.h index 08fdce2..5466507 100644 --- a/src/uzbl-core.h +++ b/src/uzbl-core.h @@ -227,9 +227,6 @@ itos(int val); gchar* strfree(gchar *str); -GArray* -read_file_by_line (const gchar *path); - gchar* parseenv (gchar* string); -- cgit v1.2.3