From a25c34e280977a22b036ea1258b864abcfb339cc Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Sat, 29 Oct 2011 01:16:08 +0200 Subject: [PATCH] Support res directly in sokoke_find_data_filename One allocation less each time, less fragmentation. --- extensions/formhistory.c | 10 ++-------- midori/midori-app.c | 2 +- midori/midori-browser.c | 2 +- midori/midori-view.c | 24 ++++++------------------ midori/sokoke.c | 13 ++++++++----- midori/sokoke.h | 3 ++- 6 files changed, 20 insertions(+), 34 deletions(-) diff --git a/extensions/formhistory.c b/extensions/formhistory.c index b74e350a..04af872b 100644 --- a/extensions/formhistory.c +++ b/extensions/formhistory.c @@ -32,27 +32,22 @@ formhistory_toggle_state_cb (GtkAction* action, static gboolean formhistory_prepare_js () { - gchar* data_path; gchar* autosuggest; gchar* style; guint i; gchar* file; - data_path = g_build_filename (PACKAGE_NAME, "res", "autosuggestcontrol.js", NULL); - file = sokoke_find_data_filename (data_path); + file = sokoke_find_data_filename ("autosuggestcontrol.js", TRUE); if (!g_file_get_contents (file, &autosuggest, NULL, NULL)) { - g_free (data_path); g_free (file); return FALSE; } g_strchomp (autosuggest); - katze_assign (data_path, g_build_filename (PACKAGE_NAME, "res", "autosuggestcontrol.css", NULL)); - katze_assign (file, sokoke_find_data_filename (data_path)); + katze_assign (file, sokoke_find_data_filename ("autosuggestcontrol.css", TRUE)); if (!g_file_get_contents (file, &style, NULL, NULL)) { - g_free (data_path); g_free (file); return FALSE; } @@ -83,7 +78,6 @@ formhistory_prepare_js () autosuggest, style); g_strstrip (jsforms); - g_free (data_path); g_free (file); g_free (style); g_free (autosuggest); diff --git a/midori/midori-app.c b/midori/midori-app.c index ca8abe36..646038e4 100644 --- a/midori/midori-app.c +++ b/midori/midori-app.c @@ -1348,7 +1348,7 @@ midori_app_setup (gchar** argument_vector) else #ifdef G_OS_WIN32 { - gchar* path = sokoke_find_data_filename ("locale"); + gchar* path = sokoke_find_data_filename ("locale", FALSE); bindtextdomain (GETTEXT_PACKAGE, path); g_free (path); } diff --git a/midori/midori-browser.c b/midori/midori-browser.c index b15594b4..fee98075 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -4853,7 +4853,7 @@ _action_help_link_activate (GtkAction* action, #ifdef G_OS_WIN32 { #ifdef DOCDIR - gchar* path = sokoke_find_data_filename ("doc/midori/faq.html"); + gchar* path = sokoke_find_data_filename ("doc/midori/faq.html", FALSE); uri = free_uri = g_filename_to_uri (path, NULL, NULL); if (g_access (path, F_OK) != 0) { diff --git a/midori/midori-view.c b/midori/midori-view.c index 00701110..bdfa8fcc 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -1130,12 +1130,8 @@ midori_view_web_view_resource_request_cb (WebKitWebView* web_view, if (g_str_has_prefix (uri, "res://")) { - gchar* filename = g_build_filename ("midori", "res", &uri[6], NULL); - gchar* filepath = sokoke_find_data_filename (filename); - gchar* file_uri; - - g_free (filename); - file_uri = g_filename_to_uri (filepath, NULL, NULL); + gchar* filepath = sokoke_find_data_filename (&uri[6], TRUE); + gchar* file_uri = g_filename_to_uri (filepath, NULL, NULL); g_free (filepath); webkit_network_request_set_uri (request, file_uri); g_free (file_uri); @@ -1426,11 +1422,9 @@ midori_view_display_error (MidoriView* view, const gchar* try_again, WebKitWebFrame* web_frame) { - gchar* template_file = g_build_filename ("midori", "res", "error.html", NULL); - gchar* path = sokoke_find_data_filename (template_file); + gchar* path = sokoke_find_data_filename ("error.html", TRUE); gchar* template; - g_free (template_file); if (g_file_get_contents (path, &template, NULL, NULL)) { gchar* title_escaped; @@ -3696,19 +3690,13 @@ prepare_speed_dial_html (MidoriView* view) guint slot_count = 1, i, grid_index = 3, slot_size; gchar* speed_dial_head; gchar* file_path; - gchar* file_name; gchar** groups; g_object_get (browser, "speed-dial", &key_file, NULL); - if (!key_file) - return g_strdup (""); + file_path = sokoke_find_data_filename ("speeddial-head-" MIDORI_VERSION ".html", TRUE); - file_name = g_build_filename ("midori", "res", - "speeddial-head-" MIDORI_VERSION ".html", NULL); - file_path = sokoke_find_data_filename (file_name); - g_free (file_name); - - if (g_access (file_path, F_OK) == 0 + if (key_file != NULL + && g_access (file_path, F_OK) == 0 && g_file_get_contents (file_path, &speed_dial_head, NULL, NULL)) { gchar* header = sokoke_replace_variables (speed_dial_head, diff --git a/midori/sokoke.c b/midori/sokoke.c index 69ca9829..71c294da 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -1342,8 +1342,11 @@ gchar* sokoke_find_lib_path (const gchar* folder) * Return value: a newly allocated full path **/ gchar* -sokoke_find_data_filename (const gchar* filename) +sokoke_find_data_filename (const gchar* filename, + gboolean res) { + const gchar* res1 = res ? PACKAGE_NAME : ""; + const gchar* res2 = res ? "res" : ""; const gchar* const* data_dirs = g_get_system_data_dirs (); guint i = 0; const gchar* data_dir; @@ -1351,7 +1354,7 @@ sokoke_find_data_filename (const gchar* filename) #ifdef G_OS_WIN32 gchar* install_path = g_win32_get_package_installation_directory_of_module (NULL); - path = g_build_filename (install_path, "share", filename, NULL); + path = g_build_filename (install_path, "share", res1, res2, filename, NULL); g_free (install_path); if (g_access (path, F_OK) == 0) return path; @@ -1359,19 +1362,19 @@ sokoke_find_data_filename (const gchar* filename) g_free (path); #endif - path = g_build_filename (g_get_user_data_dir (), filename, NULL); + path = g_build_filename (g_get_user_data_dir (), res1, res2, filename, NULL); if (g_access (path, F_OK) == 0) return path; g_free (path); while ((data_dir = data_dirs[i++])) { - path = g_build_filename (data_dir, filename, NULL); + path = g_build_filename (data_dir, res1, res2, filename, NULL); if (g_access (path, F_OK) == 0) return path; g_free (path); } - return g_build_filename (MDATADIR, filename, NULL); + return g_build_filename (MDATADIR, res1, res2, filename, NULL); } /** diff --git a/midori/sokoke.h b/midori/sokoke.h index 8d70b5f0..a589f06d 100644 --- a/midori/sokoke.h +++ b/midori/sokoke.h @@ -148,7 +148,8 @@ gchar* sokoke_find_lib_path (const gchar* folder); gchar* -sokoke_find_data_filename (const gchar* filename); +sokoke_find_data_filename (const gchar* filename, + gboolean res); gchar** sokoke_get_argv (gchar** argument_vector);