diff options
author | Jeff Garrett <jeff@jgarrett.org> | 2007-11-22 16:20:59 -0600 |
---|---|---|
committer | Jeff Garrett <jeff@jgarrett.org> | 2007-11-22 16:20:59 -0600 |
commit | 3ffce9e7e8b369dc9ce26a124ef35aacbe3f0909 (patch) | |
tree | 0abbcfa374b9dd422412d41ee81a39434e59139c | |
parent | fee3a2c05799588285e204c2067eba1dc786b55b (diff) |
Add search results to GUI only in the top search function
Remove all references to searchwindow_add_result in the
lower specific search functions and instead have them
return a GList of hits.
This lowers the references to the global search results
window, and makes possible other uses, e.g. CLI search...
-rw-r--r-- | src/search.c | 30 | ||||
-rw-r--r-- | src/search.h | 11 | ||||
-rw-r--r-- | src/search_city.c | 28 | ||||
-rw-r--r-- | src/search_city.h | 2 | ||||
-rw-r--r-- | src/search_coordinate.c | 26 | ||||
-rw-r--r-- | src/search_coordinate.h | 2 | ||||
-rw-r--r-- | src/search_location.c | 39 | ||||
-rw-r--r-- | src/search_location.h | 2 | ||||
-rw-r--r-- | src/search_road.c | 66 | ||||
-rw-r--r-- | src/search_road.h | 2 |
10 files changed, 161 insertions, 47 deletions
diff --git a/src/search.c b/src/search.c index 3b7ce38..36c6981 100644 --- a/src/search.c +++ b/src/search.c @@ -30,6 +30,7 @@ #include "search_location.h" #include "search_city.h" #include "search_coordinate.h" +#include "searchwindow.h" // functions common to all searches @@ -92,8 +93,18 @@ gboolean search_address_number_atoi(const gchar* pszText, gint* pnReturn) return TRUE; } +static GList *(*search_functions[])(const char *) = { + search_city_execute, + search_location_execute, + search_road_execute, + search_coordinate_execute, +}; + void search_all(const gchar* pszSentence) { + GList *p; + int i; + if(pszSentence[0] == 0) { return; // no results... } @@ -104,10 +115,21 @@ void search_all(const gchar* pszSentence) search_clean_string(pszCleanedSentence); // Search each object type - search_city_execute(pszCleanedSentence); - search_location_execute(pszCleanedSentence); - search_road_execute(pszCleanedSentence); - search_coordinate_execute(pszCleanedSentence); + for (i = 0; i < G_N_ELEMENTS(search_functions); i++) + { + GList *results = (search_functions[i])(pszCleanedSentence); + for (p = results; p; p = g_list_next(p)) + { + struct search_result *hit = g_list_nth_data(p, 0); + + searchwindow_add_result(hit->type, hit->text, hit->glyph, hit->point, hit->zoom_level); + + g_free(hit->point); + g_free(hit->text); + g_free(hit); + } + g_list_free(results); + } TIMER_END(search, "END SearchAll"); diff --git a/src/search.h b/src/search.h index 84e665d..ad3e3ce 100644 --- a/src/search.h +++ b/src/search.h @@ -24,6 +24,9 @@ #ifndef _SEARCH_H #define _SEARCH_H +#include "glyph.h" +#include "map.h" + typedef enum { SEARCH_RESULT_TYPE_COORDINATE, // in order of importance (for search results) SEARCH_RESULT_TYPE_COUNTRY, @@ -37,6 +40,14 @@ typedef enum { G_BEGIN_DECLS +struct search_result { + ESearchResultType type; + char *text; + glyph_t *glyph; + mappoint_t *point; + int zoom_level; +}; + void search_clean_string(gchar* p); void search_all(const gchar* pszSentence); diff --git a/src/search_city.c b/src/search_city.c index 9e71348..09702b5 100644 --- a/src/search_city.c +++ b/src/search_city.c @@ -30,26 +30,30 @@ #define CITY_RESULT_SUGGESTED_ZOOMLEVEL (3) -void search_city_on_words(gchar** aWords, gint nWordCount); +GList *search_city_on_words(gchar** aWords, gint nWordCount, GList *ret); static glyph_t* g_SearchResultTypeCityGlyph = NULL; -void search_city_execute(const gchar* pszSentence) +GList *search_city_execute(const gchar* pszSentence) { + GList *ret = NULL; + // Create an array of the words gchar** aWords = g_strsplit(pszSentence," ", 0); // " " = delimeters, 0 = no max # gint nWordCount = g_strv_length(aWords); if(nWordCount > 0) { - search_city_on_words(aWords, nWordCount); + ret = search_city_on_words(aWords, nWordCount, ret); } // cleanup g_strfreev(aWords); // free the array of strings + + return ret; } -void search_city_on_words(gchar** aWords, gint nWordCount) +GList *search_city_on_words(gchar** aWords, gint nWordCount, GList *ret) { g_assert(nWordCount > 0); @@ -136,14 +140,24 @@ void search_city_on_words(gchar** aWords, gint nWordCount) //gint nCountryID = atoi(aRow[2]); - mappoint_t point = {0,0}; + mappoint_t *point = g_new0(mappoint_t, 1); gchar* pszResultText = g_strdup_printf("<b>%s,\n%s</b>", aRow[0], aRow[1]); // XXX: add country? - searchwindow_add_result(SEARCH_RESULT_TYPE_CITY, pszResultText, g_SearchResultTypeCityGlyph, &point, CITY_RESULT_SUGGESTED_ZOOMLEVEL); - g_free(pszResultText); + + struct search_result *hit = g_new0(struct search_result, 1); + *hit = (struct search_result) { + .type = SEARCH_RESULT_TYPE_CITY, + .text = pszResultText, + .glyph = g_SearchResultTypeCityGlyph, + .point = point, + .zoom_level = CITY_RESULT_SUGGESTED_ZOOMLEVEL, + }; + ret = g_list_append(ret, hit); } db_free_result(pResultSet); } //g_print("%d city results\n", nCount); g_free(pszQuery); + + return ret; } diff --git a/src/search_city.h b/src/search_city.h index 505e282..cbd2a4f 100644 --- a/src/search_city.h +++ b/src/search_city.h @@ -26,7 +26,7 @@ G_BEGIN_DECLS -void search_city_execute(const gchar* pszSentence); +GList *search_city_execute(const gchar* pszSentence); G_END_DECLS diff --git a/src/search_coordinate.c b/src/search_coordinate.c index 10bdc4d..39e5399 100644 --- a/src/search_coordinate.c +++ b/src/search_coordinate.c @@ -36,7 +36,7 @@ #define FORMAT_COORDINATE_RESULT ("<b>Lat:</b> %10.5f\n<b>Lon:</b> %10.5f") -static void search_coordinate_add_result(const mappoint_t* pPoint); +static GList *search_coordinate_add_result(const mappoint_t* pPoint, GList *ret); // // globals @@ -47,7 +47,7 @@ static glyph_t* g_SearchResultTypeCoordinateGlyph = NULL; // Public API // -void search_coordinate_execute(const gchar* pszSentence) +GList *search_coordinate_execute(const gchar* pszSentence) { //mappoint_t ptResult = {0}; @@ -71,12 +71,14 @@ void search_coordinate_execute(const gchar* pszSentence) // Call only if we have a likely match! //search_coordinate_add_result(&ptResult); + + return NULL; } // // Private // -static void search_coordinate_add_result(const mappoint_t* pPoint) +static GList *search_coordinate_add_result(const mappoint_t* pPoint, GList *ret) { if (!g_SearchResultTypeCoordinateGlyph) g_SearchResultTypeCoordinateGlyph = glyph_load_at_size("search-result-type-coordinate", @@ -84,6 +86,20 @@ static void search_coordinate_add_result(const mappoint_t* pPoint) SEARCHWINDOW_SEARCH_RESULT_GLYPH_HEIGHT); gchar* pszBuffer = g_strdup_printf(FORMAT_COORDINATE_RESULT, pPoint->fLatitude, pPoint->fLongitude); - searchwindow_add_result(SEARCH_RESULT_TYPE_COORDINATE, pszBuffer, g_SearchResultTypeCoordinateGlyph, pPoint, COORDINATE_RESULT_SUGGESTED_ZOOMLEVEL); - g_free(pszBuffer); + + mappoint_t *point = g_new0(mappoint_t, 1); + *point = *pPoint; + + struct search_result *hit = g_new0(struct search_result, 1); + *hit = (struct search_result) { + .type = SEARCH_RESULT_TYPE_COORDINATE, + .text = pszBuffer, + .glyph = g_SearchResultTypeCoordinateGlyph, + .point = point, + .zoom_level = COORDINATE_RESULT_SUGGESTED_ZOOMLEVEL, + }; + + ret = g_list_append(ret, hit); + + return ret; } diff --git a/src/search_coordinate.h b/src/search_coordinate.h index 5fb02a5..c9e22f2 100644 --- a/src/search_coordinate.h +++ b/src/search_coordinate.h @@ -26,7 +26,7 @@ G_BEGIN_DECLS -void search_coordinate_execute(const gchar* pszSentence); +GList *search_coordinate_execute(const gchar* pszSentence); G_END_DECLS diff --git a/src/search_location.c b/src/search_location.c index cce3dfc..935d05c 100644 --- a/src/search_location.c +++ b/src/search_location.c @@ -46,17 +46,21 @@ // gint nWordCount; //} locationsearch_t; -void search_location_on_words(gchar** aWords, gint nWordCount); -void search_location_filter_result(gint nLocationID, gint nLocationSetID, const gchar* pszName, const gchar* pszAddress, const mappoint_t* pCoordinates); +GList *search_location_on_words(gchar** aWords, gint nWordCount, GList *ret); +GList *search_location_filter_result(gint nLocationID, gint nLocationSetID, const gchar* pszName, const gchar* pszAddress, const mappoint_t* pCoordinates, GList *ret); -void search_location_execute(const gchar* pszSentence) +GList *search_location_execute(const gchar* pszSentence) { + GList *ret = NULL; + // Create an array of the words gchar** aaWords = g_strsplit(pszSentence, " ", 0); // " " = delimeters, 0 = no max # gint nWords = g_strv_length(aaWords); - search_location_on_words(aaWords, nWords); + ret = search_location_on_words(aaWords, nWords, ret); g_strfreev(aaWords); // free entire array of strings + + return ret; } /* @@ -88,9 +92,9 @@ WHERE Matches.LocationID = Location.ID # join our list of Matches to Location to get needed data */ -void search_location_on_words(gchar** aWords, gint nWordCount) +GList *search_location_on_words(gchar** aWords, gint nWordCount, GList *ret) { - if(nWordCount == 0) return; + if(nWordCount == 0) return ret; gchar* pszInnerSelects = g_strdup(""); gint i; @@ -170,7 +174,7 @@ void search_location_on_words(gchar** aWords, gint nWordCount) gchar* pszLocationAddress = aRow[3]; db_parse_wkb_point(aRow[4], &pt); // Parse coordinates - search_location_filter_result(nLocationID, nLocationSetID, pszLocationName, pszLocationAddress, &pt); + ret = search_location_filter_result(nLocationID, nLocationSetID, pszLocationName, pszLocationAddress, &pt, ret); } } db_free_result(pResultSet); @@ -183,9 +187,11 @@ void search_location_on_words(gchar** aWords, gint nWordCount) else { g_print("search failed\n"); } + + return ret; } -void search_location_filter_result(gint nLocationID, gint nLocationSetID, const gchar* pszName, const gchar* pszAddress, const mappoint_t* pCoordinates) +GList *search_location_filter_result(gint nLocationID, gint nLocationSetID, const gchar* pszName, const gchar* pszAddress, const mappoint_t* pCoordinates, GList *ret) { gchar* pszResultText = g_strdup_printf("<b>%s</b>%s%s", pszName, (pszAddress == NULL || pszAddress[0] == '\0') ? "" : "\n", @@ -197,9 +203,22 @@ void search_location_filter_result(gint nLocationID, gint nLocationSetID, const if(locationset_find_by_id(nLocationSetID, &pLocationSet)) { pGlyph = pLocationSet->pGlyph; } - searchwindow_add_result(SEARCH_RESULT_TYPE_LOCATION, pszResultText, pGlyph, pCoordinates, LOCATION_RESULT_SUGGESTED_ZOOMLEVEL); - g_free(pszResultText); + mappoint_t *point = g_new0(mappoint_t, 1); + *point = *pCoordinates; + + struct search_result *hit = g_new0(struct search_result, 1); + *hit = (struct search_result) { + .type = SEARCH_RESULT_TYPE_LOCATION, + .text = pszResultText, + .glyph = pGlyph, + .point = pCoordinates, + .zoom_level = LOCATION_RESULT_SUGGESTED_ZOOMLEVEL, + }; + + ret = g_list_append(ret, hit); + + return ret; } // diff --git a/src/search_location.h b/src/search_location.h index 0a35231..71782c0 100644 --- a/src/search_location.h +++ b/src/search_location.h @@ -26,7 +26,7 @@ G_BEGIN_DECLS -void search_location_execute(const gchar* pszSentence); +GList *search_location_execute(const gchar* pszSentence); G_END_DECLS diff --git a/src/search_road.c b/src/search_road.c index bc26850..14bfa00 100644 --- a/src/search_road.c +++ b/src/search_road.c @@ -80,27 +80,32 @@ gboolean search_address_match_zipcode(const gchar* pszWord) // prototypes -void search_road_on_words(gchar** aWords, gint nWordCount); -void search_road_on_roadsearch_struct(const roadsearch_t* pRoadSearch); -void search_road_filter_result(const gchar* pszRoadName, gint nRoadNumber, gint nRoadSuffixID, gint nAddressLeftStart, gint nAddressLeftEnd, gint nAddressRightStart, gint nAddressRightEnd, const gchar* pszCityNameLeft, const gchar* pszCityNameRight, const gchar* pszStateNameLeft, const gchar* pszStateNameRight, const gchar* pszZIPLeft, const gchar* pszZIPRight, GArray* pMapPointsArray); +GList *search_road_on_words(gchar** aWords, gint nWordCount, GList *ret); +GList *search_road_on_roadsearch_struct(const roadsearch_t* pRoadSearch, GList *ret); +GList *search_road_filter_result(const gchar* pszRoadName, gint nRoadNumber, gint nRoadSuffixID, gint nAddressLeftStart, gint nAddressLeftEnd, gint nAddressRightStart, gint nAddressRightEnd, const gchar* pszCityNameLeft, const gchar* pszCityNameRight, const gchar* pszStateNameLeft, const gchar* pszStateNameRight, const gchar* pszZIPLeft, const gchar* pszZIPRight, GArray* pMapPointsArray, GList *ret); +GList *search_road_add_result(char *text, mappoint_t *pt, GList *ret); // functions -void search_road_execute(const gchar* pszSentence) +GList *search_road_execute(const gchar* pszSentence) { + GList *ret = NULL; + // Create an array of the words gchar** aWords = g_strsplit(pszSentence," ", 0); // " " = delimeters, 0 = no max # gint nWordCount = g_strv_length(aWords); if(nWordCount > 0) { - search_road_on_words(aWords, nWordCount); + ret = search_road_on_words(aWords, nWordCount, ret); } // cleanup g_strfreev(aWords); // free the array of strings + + return ret; } -void search_road_on_words(gchar** aWords, gint nWordCount) +GList *search_road_on_words(gchar** aWords, gint nWordCount, GList *ret) { g_assert(nWordCount > 0); roadsearch_t roadsearch = {0}; @@ -133,7 +138,7 @@ void search_road_on_words(gchar** aWords, gint nWordCount) if(nRemainingWordCount == 0) { // do a zip code search and return g_print("TODO: zip code search\n"); - return; + return ret; } // See if we can match a state name XXX: We need to match multi-word state names @@ -196,16 +201,19 @@ void search_road_on_words(gchar** aWords, gint nWordCount) if(nRemainingWordCount > 0) { roadsearch.pszRoadName = util_g_strjoinv_limit(" ", aWords, iFirst, iLast); - search_road_on_roadsearch_struct(&roadsearch); + + ret = search_road_on_roadsearch_struct(&roadsearch, ret); } else { // oops, no street name //g_print("no street name found in search\n"); } g_free(roadsearch.pszRoadName); + + return ret; } -void search_road_on_roadsearch_struct(const roadsearch_t* pRoadSearch) +GList *search_road_on_roadsearch_struct(const roadsearch_t* pRoadSearch, GList *ret) { // // Assemble the various optional clauses for the SQL statement @@ -351,7 +359,7 @@ void search_road_on_roadsearch_struct(const roadsearch_t* pRoadSearch) GArray* pMapPointsArray = g_array_new(FALSE, FALSE, sizeof(mappoint_t)); maprect_t r; db_parse_wkb_linestring(aRow[3], pMapPointsArray, &r); - search_road_filter_result(aRow[1], pRoadSearch->nNumber, atoi(aRow[2]), atoi(aRow[4]), atoi(aRow[5]), atoi(aRow[6]), atoi(aRow[7]), aRow[8], aRow[9], aRow[10], aRow[11], aRow[12], aRow[13], pMapPointsArray); + ret = search_road_filter_result(aRow[1], pRoadSearch->nNumber, atoi(aRow[2]), atoi(aRow[4]), atoi(aRow[5]), atoi(aRow[6]), atoi(aRow[7]), aRow[8], aRow[9], aRow[10], aRow[11], aRow[12], aRow[13], pMapPointsArray, ret); //g_print("%03d: Road.ID='%s' RoadName.Name='%s', Suffix=%s, L:%s-%s, R:%s-%s\n", nCount, aRow[0], aRow[1], aRow[3], aRow[4], aRow[5], aRow[6], aRow[7]); g_array_free(pMapPointsArray, TRUE); @@ -363,6 +371,8 @@ void search_road_on_roadsearch_struct(const roadsearch_t* pRoadSearch) g_print("road search failed\n"); } g_free(pszQuery); + + return ret; } // XXX: doesn't map.c have something like this? :) @@ -469,14 +479,14 @@ static gint max4(gint a, gint b, gint c, gint d) // do we need to filter out records where each side matches some of the criteria but not all? // #define BUFFER_SIZE 200 -void search_road_filter_result( +GList *search_road_filter_result( const gchar* pszRoadName, gint nRoadNumber, gint nRoadSuffixID, gint nAddressLeftStart, gint nAddressLeftEnd, gint nAddressRightStart, gint nAddressRightEnd, const gchar* pszCityNameLeft, const gchar* pszCityNameRight, const gchar* pszStateNameLeft, const gchar* pszStateNameRight, const gchar* pszZIPLeft, const gchar* pszZIPRight, - GArray* pMapPointsArray) + GArray* pMapPointsArray, GList *ret) { gchar azBuffer[BUFFER_SIZE]; @@ -544,7 +554,8 @@ void search_road_filter_result( // swap address to keep smaller number to the left g_snprintf(azBuffer, BUFFER_SIZE, "%d-%d %s %s\n%s", nAddressLeftEnd, nAddressLeftStart, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZLeft); } -*/ searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL); +*/ + ret = search_road_add_result(azBuffer, &ptAddress, ret); } else { // else the search had a road number // NOTE: we have to filter out results like "97-157" when searching for "124" because it's @@ -569,7 +580,7 @@ void search_road_filter_result( mappoint_array_walk_percentage(pMapPointsArray, fPercent, ROADSIDE_LEFT, &ptAddress); } g_snprintf(azBuffer, BUFFER_SIZE, FORMAT_ROAD_RESULT_WITH_NUMBER, nRoadNumber, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZLeft); - searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL); + ret = search_road_add_result(azBuffer, &ptAddress, ret); } else if(nRoadNumber >= nAddressLeftEnd && nRoadNumber <= nAddressLeftStart) { // MATCH: left side backwards @@ -585,7 +596,7 @@ void search_road_filter_result( mappoint_array_walk_percentage(pMapPointsArray, (100.0 - fPercent), ROADSIDE_RIGHT, &ptAddress); } g_snprintf(azBuffer, BUFFER_SIZE, FORMAT_ROAD_RESULT_WITH_NUMBER, nRoadNumber, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZLeft); - searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL); + ret = search_road_add_result(azBuffer, &ptAddress, ret); } } @@ -606,7 +617,7 @@ void search_road_filter_result( mappoint_array_walk_percentage(pMapPointsArray, fPercent, ROADSIDE_RIGHT, &ptAddress); } g_snprintf(azBuffer, BUFFER_SIZE, FORMAT_ROAD_RESULT_WITH_NUMBER, nRoadNumber, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZRight); - searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL); + ret = search_road_add_result(azBuffer, &ptAddress, ret); } else if(nRoadNumber >= nAddressRightEnd && nRoadNumber <= nAddressRightStart) { // MATCH: right side backwards @@ -622,10 +633,31 @@ void search_road_filter_result( mappoint_array_walk_percentage(pMapPointsArray, (100.0 - fPercent), ROADSIDE_LEFT, &ptAddress); } g_snprintf(azBuffer, BUFFER_SIZE, FORMAT_ROAD_RESULT_WITH_NUMBER, nRoadNumber, pszRoadName, road_suffix_itoa(nRoadSuffixID, ROAD_SUFFIX_LENGTH_LONG), pszCSZRight); - searchwindow_add_result(SEARCH_RESULT_TYPE_ROAD, azBuffer, g_SearchResultTypeRoadGlyph, &ptAddress, ROAD_RESULT_SUGGESTED_ZOOMLEVEL); + ret = search_road_add_result(azBuffer, &ptAddress, ret); } } } g_free(pszCSZLeft); g_free(pszCSZRight); + + return ret; +} + +GList *search_road_add_result(char *text, mappoint_t *pt, GList *ret) +{ + mappoint_t *point = g_new0(mappoint_t, 1); + *point = *pt; + + struct search_result *hit = g_new0(struct search_result, 1); + *hit = (struct search_result) { + .type = SEARCH_RESULT_TYPE_ROAD, + .text = g_strdup(text), + .glyph = g_SearchResultTypeRoadGlyph, + .point = point, + .zoom_level = ROAD_RESULT_SUGGESTED_ZOOMLEVEL, + }; + + ret = g_list_append(ret, hit); + + return ret; } diff --git a/src/search_road.h b/src/search_road.h index 3ef0232..fbdc563 100644 --- a/src/search_road.h +++ b/src/search_road.h @@ -26,7 +26,7 @@ G_BEGIN_DECLS -void search_road_execute(const gchar* pszSentence); +GList *search_road_execute(const gchar* pszSentence); G_END_DECLS |