diff options
author | Brendan Taylor <whateley@gmail.com> | 2010-11-02 20:59:23 -0600 |
---|---|---|
committer | Brendan Taylor <whateley@gmail.com> | 2010-11-02 20:59:23 -0600 |
commit | 2f6fe6720ca69b58224bd5f45c9b47be9b7f8fbe (patch) | |
tree | 25b4199afbbd613271018ab3bcdf5ccc27cc0ea2 /src/uzbl-core.c | |
parent | 9bea9618424093c6d2cc93954f4cbdd0be4b7637 (diff) |
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
Diffstat (limited to 'src/uzbl-core.c')
-rw-r--r-- | src/uzbl-core.c | 91 |
1 files changed, 30 insertions, 61 deletions
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(); |