-
Notifications
You must be signed in to change notification settings - Fork 761
/
QuickFill.c
264 lines (209 loc) · 7.2 KB
/
QuickFill.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/********************************************************************\
* QuickFill.h -- the quickfill tree data structure *
* Copyright (C) 1997 Robin D. Clark *
* Copyright (C) 1998 Linas Vepstas *
* Copyright (C) 2000 Dave Peticolas *
* *
* 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA gnu@gnu.org *
* *
\********************************************************************/
#include <ctype.h>
#include <string.h>
#include "config.h"
#include "QuickFill.h"
#include "util.h"
struct _QuickFill
{
char * text; /* the first matching text string */
GHashTable *matches; /* array of children in the tree */
};
/** PROTOTYPES ******************************************************/
static void quickfill_insert_recursive (QuickFill *qf, const char *text,
int depth, QuickFillSort sort);
/* This static indicates the debugging module that this .o belongs to. */
static short module = MOD_REGISTER;
/********************************************************************\
\********************************************************************/
static guint
quickfill_hash (gconstpointer key)
{
return GPOINTER_TO_UINT (key);
}
static gint
quickfill_compare (gconstpointer key1, gconstpointer key2)
{
guint k1 = GPOINTER_TO_UINT (key1);
guint k2 = GPOINTER_TO_UINT (key2);
return (k1 == k2);
}
/********************************************************************\
\********************************************************************/
QuickFill *
gnc_quickfill_new (void)
{
QuickFill *qf;
qf = g_new (QuickFill, 1);
qf->text = NULL;
qf->matches = g_hash_table_new (quickfill_hash, quickfill_compare);
return qf;
}
/********************************************************************\
\********************************************************************/
static void
destroy_helper (gpointer key, gpointer value, gpointer data)
{
gnc_quickfill_destroy (value);
}
void
gnc_quickfill_destroy (QuickFill *qf)
{
if (qf == NULL)
return;
g_hash_table_foreach (qf->matches, destroy_helper, NULL);
g_hash_table_destroy (qf->matches);
qf->matches = NULL;
g_free(qf->text);
qf->text = NULL;
g_free(qf);
}
/********************************************************************\
\********************************************************************/
const char *
gnc_quickfill_string (QuickFill *qf)
{
if (qf == NULL)
return NULL;
return qf->text;
}
/********************************************************************\
\********************************************************************/
QuickFill *
gnc_quickfill_get_char_match (QuickFill *qf, char c)
{
guint key = toupper(c);
if (qf == NULL)
return NULL;
DEBUG ("xaccGetQuickFill(): index = %u\n", key);
return g_hash_table_lookup (qf->matches, GUINT_TO_POINTER (key));
}
/********************************************************************\
\********************************************************************/
QuickFill *
gnc_quickfill_get_string_len_match (QuickFill *qf, const char *str, int len)
{
if (str == NULL)
return NULL;
while ((*str != '\0') && (len > 0))
{
if (qf == NULL)
return NULL;
qf = gnc_quickfill_get_char_match (qf, *str);
str++;
len--;
}
return qf;
}
/********************************************************************\
\********************************************************************/
QuickFill *
gnc_quickfill_get_string_match (QuickFill *qf, const char *str)
{
if (str == NULL)
return NULL;
return gnc_quickfill_get_string_len_match (qf, str, strlen(str));
}
/********************************************************************\
\********************************************************************/
static void
unique_len_helper (gpointer key, gpointer value, gpointer data)
{
QuickFill **qf_p = data;
*qf_p = value;
}
QuickFill *
gnc_quickfill_get_unique_len_match (QuickFill *qf, int * length)
{
if (length != NULL)
*length = 0;
if (qf == NULL)
return NULL;
while (1)
{
guint count;
count = g_hash_table_size (qf->matches);
if (count != 1)
return qf;
g_hash_table_foreach (qf->matches, unique_len_helper, &qf);
if (length != NULL)
(*length)++;
}
}
/********************************************************************\
\********************************************************************/
void
gnc_quickfill_insert (QuickFill *qf, const char * text, QuickFillSort sort)
{
quickfill_insert_recursive (qf, text, 0, sort);
}
/********************************************************************\
\********************************************************************/
static void
quickfill_insert_recursive (QuickFill *qf, const char *text, int depth,
QuickFillSort sort)
{
guint key;
char *old_text;
QuickFill *match_qf;
if (qf == NULL)
return;
if ((text == NULL) || (text[depth] == '\0'))
return;
key = toupper(text[depth]);
match_qf = g_hash_table_lookup (qf->matches, GUINT_TO_POINTER (key));
if (match_qf == NULL)
{
match_qf = gnc_quickfill_new ();
g_hash_table_insert (qf->matches, GUINT_TO_POINTER (key), match_qf);
}
old_text = match_qf->text;
switch(sort)
{
case QUICKFILL_ALPHA:
if ((old_text != NULL) &&
(safe_strcmp(text, old_text) >= 0))
break;
case QUICKFILL_LIFO:
default:
/* If there's no string there already, just put the new one in. */
if (old_text == NULL)
{
match_qf->text = g_strdup (text);
break;
}
/* Leave prefixes in place */
if ((strlen(text) > strlen(old_text)) &&
(strncmp(text, old_text, strlen(old_text)) == 0))
break;
g_free (old_text);
match_qf->text = g_strdup (text);
break;
}
quickfill_insert_recursive (match_qf, text, ++depth, sort);
}
/********************** END OF FILE *********************************\
\********************************************************************/