From d19f2cb8621191ae1182031b5ca5c7741acf35d3 Mon Sep 17 00:00:00 2001 From: Thomas Thurman Date: Fri, 28 Aug 2009 13:42:29 -0400 Subject: [PATCH 1/1] start of proper filtering --- belltower.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/belltower.c b/belltower.c index 9649b04..0572fd4 100644 --- a/belltower.c +++ b/belltower.c @@ -19,6 +19,15 @@ GtkWidget *window; +typedef enum { + /** stop scanning the database */ + FILTER_STOP, + /** ignore this one */ + FILTER_PASS, + /** add this one to the list */ + FILTER_ACCEPT +} FilterResult; + /* FIXME: We should really do this by looking at the header row of the table. @@ -260,7 +269,7 @@ get_countries_cb (tower *details, g_strdup (details->fields[FieldCountry])); } - return TRUE; + return FILTER_PASS; } typedef struct { @@ -275,10 +284,10 @@ get_counties_cb (tower *details, country_cb_data *d = (country_cb_data *)data; if (details->serial==0) - return TRUE; /* header row */ + return FILTER_PASS; /* header row */ if (strcmp(details->fields[FieldCountry], d->country_name)!=0) - return TRUE; /* wrong country */ + return FILTER_PASS; /* wrong country */ if (!g_hash_table_lookup_extended (d->hash, details->fields[FieldCounty], @@ -289,7 +298,7 @@ get_counties_cb (tower *details, g_strdup (details->fields[FieldCounty])); } - return TRUE; + return FILTER_PASS; } static gboolean @@ -306,7 +315,7 @@ single_tower_cb (tower *details, if (strcmp(details->fields[FieldPrimaryKey], primary_key)!=0) { /* not this one; keep going */ - return TRUE; + return FILTER_PASS; } tower_window = hildon_stackable_window_new (); @@ -399,11 +408,12 @@ single_tower_cb (tower *details, details->fields[FieldLong]); gtk_widget_show_all (GTK_WIDGET (tower_window)); - return FALSE; + return FILTER_STOP; } static void parse_dove (ParseDoveCallback callback, + GSList **filter_results, gpointer data) { FILE *dove = fopen("/usr/share/belltower/dove.txt", "r"); @@ -450,10 +460,22 @@ parse_dove (ParseDoveCallback callback, result.fields[FieldCountry] = "England"; } - if (!callback (&result, data)) + switch (callback (&result, data)) { + case FILTER_PASS: + /* nothing */ + break; + + case FILTER_STOP: fclose (dove); return; + + case FILTER_ACCEPT: + if (filter_results) + { + *filter_results = g_slist_append (*filter_results, + result.fields[FieldPrimaryKey]); + } } result.serial++; @@ -490,8 +512,7 @@ nearby_towers (void) static void show_tower (char *primary_key) { - parse_dove (single_tower_cb, - primary_key); + parse_dove (single_tower_cb, NULL, primary_key); } static gint strcmp_f (gconstpointer a, @@ -532,8 +553,7 @@ towers_by_subarea (gchar *area) gtk_window_set_title (GTK_WINDOW (dialog), title); g_free (title); - parse_dove (get_counties_cb, - &d); + parse_dove (get_counties_cb, NULL, &d); g_hash_table_foreach (hash, put_areas_into_list, @@ -572,8 +592,7 @@ towers_by_area (void) gtk_window_set_title (GTK_WINDOW (dialog), "Areas of the world"); - parse_dove (get_countries_cb, - hash); + parse_dove (get_countries_cb, NULL, hash); g_hash_table_foreach (hash, put_areas_into_list, -- 1.7.9.5