From 3e1ed30d7cae92070685c8cdac69ad38b50029a2 Mon Sep 17 00:00:00 2001 From: Evgeny Grablyk Date: Tue, 28 Apr 2009 14:16:10 +0300 Subject: Move to GLib hash functionality. Use GHashTable instad of struct Binding. --- uzbl.c | 54 +++++++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) (limited to 'uzbl.c') diff --git a/uzbl.c b/uzbl.c index 3528bf4..f1348ec 100644 --- a/uzbl.c +++ b/uzbl.c @@ -70,18 +70,11 @@ static gboolean status_top = FALSE; static gchar* modkey = NULL; static guint modmask = 0; -typedef struct { - const char *binding; - const char *action; -} Binding; - /* settings from config: group bindings_internal */ -static Binding internal_bindings[256]; -static int num_internal_bindings = 0; +static GHashTable *internal_bindings; /* settings from config: group bindings_external */ -static Binding external_bindings[256]; -static int num_external_bindings = 0; +static GHashTable *external_bindings; /* commandline arguments (set initial values for the state variables) */ static GOptionEntry entries[] = @@ -353,7 +346,7 @@ static gboolean key_press_cb (WebKitWebView* page, GdkEventKey* event) { (void) page; - int i; + gpointer act; gboolean result=FALSE; //TRUE to stop other handlers from being invoked for the event. FALSE to propagate the event further. if (event->type != GDK_KEY_PRESS) return result; @@ -366,25 +359,19 @@ key_press_cb (WebKitWebView* page, GdkEventKey* event) } //INTERNAL BINDINGS - for (i = 0; i < num_internal_bindings; i++) { - if (strcmp(event->string, internal_bindings[i].binding) == 0) { - if (!insert_mode || (event->state == modmask)) { - parse_command (internal_bindings[i].action); - result = TRUE; - } - } - } + if((act = g_hash_table_lookup(internal_bindings, event->string)) != NULL) + if (!insert_mode || (event->state == modmask)) { + parse_command (act); + result = TRUE; + } //EXTERNAL BINDINGS - for (i = 0; i < num_external_bindings; i++) { - if (strcmp(event->string, external_bindings[i].binding) == 0) { - if (!insert_mode || (event->state == modmask)) { - run_command(external_bindings[i].action, NULL); - result = TRUE; - } - } - } - + if((act = g_hash_table_lookup(external_bindings, event->string)) != NULL) + if (!insert_mode || (event->state == modmask)) { + parse_command (act); + result = TRUE; + } + if (!result) result = (insert_mode ? FALSE : TRUE); @@ -431,14 +418,8 @@ GtkWidget* create_window () { static void add_binding (char *binding, char *action, bool internal) { - Binding bind = {binding, action}; - if (internal) { - internal_bindings[num_internal_bindings] = bind; - num_internal_bindings ++; - } else { - external_bindings[num_external_bindings] = bind; - num_external_bindings ++; - } + g_hash_table_insert(internal ? internal_bindings : external_bindings, + binding, action); } static void @@ -590,6 +571,9 @@ main (int argc, char* argv[]) { g_option_context_add_main_entries (context, entries, NULL); g_option_context_add_group (context, gtk_get_option_group (TRUE)); g_option_context_parse (context, &argc, &argv, &error); + /* initialize has tables */ + internal_bindings = g_hash_table_new(g_str_hash, g_str_equal); + external_bindings = g_hash_table_new(g_str_hash, g_str_equal); settings_init (); if (always_insert_mode) -- cgit v1.2.3 From 668a9aa22e51d2e31e2316d05c9136b53a6e5bce Mon Sep 17 00:00:00 2001 From: Evgeny Grablyk Date: Tue, 28 Apr 2009 22:40:48 +0300 Subject: Finish moving to GLib hashes. --- uzbl.c | 83 +++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 36 deletions(-) (limited to 'uzbl.c') diff --git a/uzbl.c b/uzbl.c index f1348ec..fae9d5f 100644 --- a/uzbl.c +++ b/uzbl.c @@ -76,6 +76,9 @@ static GHashTable *internal_bindings; /* settings from config: group bindings_external */ static GHashTable *external_bindings; +/* command list */ +static GHashTable *commands; + /* commandline arguments (set initial values for the state variables) */ static GOptionEntry entries[] = { @@ -88,7 +91,7 @@ static GOptionEntry entries[] = /* for internal list of commands */ typedef struct { - const char *command; + gpointer command; void (*func_1_param)(WebKitWebView*); void (*func_2_params)(WebKitWebView*, const gchar *); } Command; @@ -189,10 +192,11 @@ log_history_cb () { run_command(history_handler, args->str); } } - + /* -- command to callback/function map for things we cannot attach to any signals */ // TODO: reload, home, quit -static Command commands[] = + +static Command cmdlist[] = { { "back", &go_back_cb, NULL }, { "forward", &go_forward_cb, NULL }, @@ -201,10 +205,21 @@ static Command commands[] = { "zoom_in", &webkit_web_view_zoom_in, NULL }, //Can crash (when max zoom reached?). { "zoom_out", &webkit_web_view_zoom_out, NULL }, { "uri", NULL, &webkit_web_view_load_uri }, - { "toggle_status", &toggle_status_cb, NULL } + { "toggle_status", &toggle_status_cb, NULL }, + { NULL, NULL, NULL} //{ "get uri", &webkit_web_view_get_uri}, }; +static void +commands_hash(void) +{ + unsigned int i = 0; + commands = g_hash_table_new(g_str_hash, g_str_equal); + + while(cmdlist[i].command != NULL) + g_hash_table_insert(commands, cmdlist[i++].command, &cmdlist[i]); +} + /* -- CORE FUNCTIONS -- */ static bool @@ -235,42 +250,36 @@ run_command(const char *command, const char *args) { static void parse_command(const char *cmd) { - unsigned int i; - Command *c = NULL; - char buffer[512]; - strcpy (buffer, cmd); - char * command_name = strtok (buffer, " "); - gchar * command_param = strtok (NULL, " ,"); - - Command *c_tmp = NULL; - for (i = 0; i < LENGTH (commands); i++) { - c_tmp = &commands[i]; - if (strncmp (command_name, c_tmp->command, strlen (c_tmp->command)) == 0) { - c = c_tmp; - } - } + Command *c = NULL; + char buffer[512]; + strcpy (buffer, cmd); + char * command_name = strtok (buffer, " "); + gchar * command_param = strtok (NULL, " ,"); + + if((c = g_hash_table_lookup(commands, command_name)) != NULL){ if (c != NULL) { - if (c->func_2_params != NULL) { - if (command_param != NULL) { - printf ("command executing: \"%s %s\"\n", command_name, command_param); - c->func_2_params (web_view, command_param); - } else { - if (c->func_1_param != NULL) { - printf ("command executing: \"%s\"\n", command_name); - c->func_1_param (web_view); - } else { - fprintf (stderr, "command needs a parameter. \"%s\" is not complete\n", command_name); - } - } - } else if (c->func_1_param != NULL) { - printf ("command executing: \"%s\"\n", command_name); - c->func_1_param (web_view); - } + if (c->func_2_params != NULL) { + if (command_param != NULL) { + printf ("command executing: \"%s %s\"\n", command_name, command_param); + c->func_2_params (web_view, command_param); + } else { + if (c->func_1_param != NULL) { + printf ("command executing: \"%s\"\n", command_name); + c->func_1_param (web_view); + } else { + fprintf (stderr, "command needs a parameter. \"%s\" is not complete\n", command_name); + } + } + } else if (c->func_1_param != NULL) { + printf ("command executing: \"%s\"\n", command_name); + c->func_1_param (web_view); + } } else { - fprintf (stderr, "command \"%s\" not understood. ignoring.\n", cmd); + fprintf (stderr, "command \"%s\" not understood. ignoring.\n", cmd); } + } } - + static void *control_fifo() { if (fifodir) { @@ -576,6 +585,8 @@ main (int argc, char* argv[]) { external_bindings = g_hash_table_new(g_str_hash, g_str_equal); settings_init (); + commands_hash (); + if (always_insert_mode) insert_mode = TRUE; -- cgit v1.2.3 From 9c372a283433c453fa7c096677ba673571d9cfdc Mon Sep 17 00:00:00 2001 From: Evgeny Grablyk Date: Tue, 28 Apr 2009 22:45:59 +0300 Subject: Small fix. --- uzbl.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'uzbl.c') diff --git a/uzbl.c b/uzbl.c index fae9d5f..4c80f6c 100644 --- a/uzbl.c +++ b/uzbl.c @@ -216,8 +216,10 @@ commands_hash(void) unsigned int i = 0; commands = g_hash_table_new(g_str_hash, g_str_equal); - while(cmdlist[i].command != NULL) - g_hash_table_insert(commands, cmdlist[i++].command, &cmdlist[i]); + while(cmdlist[i].command != NULL){ + g_hash_table_insert(commands, cmdlist[i].command, &cmdlist[i]); + i++; + } } /* -- CORE FUNCTIONS -- */ -- cgit v1.2.3 From eec53cbdef182192363fe8171243194738e23bb2 Mon Sep 17 00:00:00 2001 From: Evgeny Grablyk Date: Wed, 29 Apr 2009 15:40:40 +0300 Subject: Remove unneeded if statement. --- uzbl.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'uzbl.c') diff --git a/uzbl.c b/uzbl.c index 4c80f6c..538698b 100644 --- a/uzbl.c +++ b/uzbl.c @@ -259,27 +259,23 @@ parse_command(const char *cmd) { gchar * command_param = strtok (NULL, " ,"); if((c = g_hash_table_lookup(commands, command_name)) != NULL){ - if (c != NULL) { - if (c->func_2_params != NULL) { - if (command_param != NULL) { + if (c->func_2_params != NULL) { + if (command_param != NULL) { printf ("command executing: \"%s %s\"\n", command_name, command_param); c->func_2_params (web_view, command_param); - } else { - if (c->func_1_param != NULL) { - printf ("command executing: \"%s\"\n", command_name); - c->func_1_param (web_view); - } else { - fprintf (stderr, "command needs a parameter. \"%s\" is not complete\n", command_name); - } - } - } else if (c->func_1_param != NULL) { - printf ("command executing: \"%s\"\n", command_name); - c->func_1_param (web_view); + } else { + if (c->func_1_param != NULL) { + printf ("command executing: \"%s\"\n", command_name); + c->func_1_param (web_view); + } else + fprintf (stderr, "command needs a parameter. \"%s\" is not complete\n", command_name); } - } else { - fprintf (stderr, "command \"%s\" not understood. ignoring.\n", cmd); + } else if (c->func_1_param != NULL) { + printf ("command executing: \"%s\"\n", command_name); + c->func_1_param (web_view); } - } + } else + fprintf (stderr, "command \"%s\" not understood. ignoring.\n", cmd); } static void -- cgit v1.2.3