1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/***************************************************************************
* search.c
*
* Copyright 2005 Ian McIntosh
* ian_mcintosh@linuxadvocate.org
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <gtk/gtk.h>
#include "main.h"
#include "util.h"
#include "search.h"
#include "search_road.h"
#include "search_location.h"
#include "search_city.h"
#include "search_coordinate.h"
#include "searchwindow.h"
// functions common to all searches
void search_clean_string(gchar* p)
{
g_assert(p != NULL);
gchar* pReader = p;
gchar* pWriter = p;
// skip white
while(g_ascii_isspace(*pReader)) {
pReader++;
}
// remove double spaces
while(*pReader != '\0') {
if(g_ascii_isspace(*pReader)) {
if(g_ascii_isspace(*(pReader+1)) || *(pReader+1) == '\0') {
// don't copy this character (space) if the next one is a space
// or if it's the last character
}
else {
// yes, copy this space
*pWriter = ' '; // this also turns newlines etc. into spaces
pWriter++;
}
}
else if(g_ascii_isalnum(*pReader)) { // copy alphanumeric only
// yes, copy this character
*pWriter = *pReader;
pWriter++;
}
pReader++;
}
*pWriter = '\0';
}
gboolean search_address_number_atoi(const gchar* pszText, gint* pnReturn)
{
g_assert(pszText != NULL);
g_assert(pnReturn != NULL);
const gchar* pReader = pszText;
gint nNumber = 0;
while(*pReader != '\0') {
if(g_ascii_isdigit(*pReader)) {
nNumber *= 10;
nNumber += g_ascii_digit_value(*pReader);
}
else {
return FALSE;
}
pReader++;
}
*pnReturn = nNumber;
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...
}
TIMER_BEGIN(search, "BEGIN SearchAll");
gchar* pszCleanedSentence = g_strdup(pszSentence);
search_clean_string(pszCleanedSentence);
// Search each object type
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");
g_free(pszCleanedSentence);
}
|