Add unit tests for some more methods
[glib.git] / glib / gcompletion.c
blob5d2607846e6d40fbf256f60a8bbd47286a382c7b
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 /*
28 * MT safe
31 #include "config.h"
33 #include <string.h>
35 #include "glib.h"
36 #include "galias.h"
38 static void completion_check_cache (GCompletion* cmp,
39 gchar** new_prefix);
41 GCompletion*
42 g_completion_new (GCompletionFunc func)
44 GCompletion* gcomp;
46 gcomp = g_new (GCompletion, 1);
47 gcomp->items = NULL;
48 gcomp->cache = NULL;
49 gcomp->prefix = NULL;
50 gcomp->func = func;
51 gcomp->strncmp_func = strncmp;
53 return gcomp;
56 void
57 g_completion_add_items (GCompletion* cmp,
58 GList* items)
60 GList* it;
62 g_return_if_fail (cmp != NULL);
64 /* optimize adding to cache? */
65 if (cmp->cache)
67 g_list_free (cmp->cache);
68 cmp->cache = NULL;
71 if (cmp->prefix)
73 g_free (cmp->prefix);
74 cmp->prefix = NULL;
77 it = items;
78 while (it)
80 cmp->items = g_list_prepend (cmp->items, it->data);
81 it = it->next;
85 void
86 g_completion_remove_items (GCompletion* cmp,
87 GList* items)
89 GList* it;
91 g_return_if_fail (cmp != NULL);
93 it = items;
94 while (cmp->items && it)
96 cmp->items = g_list_remove (cmp->items, it->data);
97 it = it->next;
100 it = items;
101 while (cmp->cache && it)
103 cmp->cache = g_list_remove(cmp->cache, it->data);
104 it = it->next;
108 void
109 g_completion_clear_items (GCompletion* cmp)
111 g_return_if_fail (cmp != NULL);
113 g_list_free (cmp->items);
114 cmp->items = NULL;
115 g_list_free (cmp->cache);
116 cmp->cache = NULL;
117 g_free (cmp->prefix);
118 cmp->prefix = NULL;
121 static void
122 completion_check_cache (GCompletion* cmp,
123 gchar** new_prefix)
125 register GList* list;
126 register gsize len;
127 register gsize i;
128 register gsize plen;
129 gchar* postfix;
130 gchar* s;
132 if (!new_prefix)
133 return;
134 if (!cmp->cache)
136 *new_prefix = NULL;
137 return;
140 len = strlen(cmp->prefix);
141 list = cmp->cache;
142 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
143 postfix = s + len;
144 plen = strlen (postfix);
145 list = list->next;
147 while (list && plen)
149 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
150 s += len;
151 for (i = 0; i < plen; ++i)
153 if (postfix[i] != s[i])
154 break;
156 plen = i;
157 list = list->next;
160 *new_prefix = g_new0 (gchar, len + plen + 1);
161 strncpy (*new_prefix, cmp->prefix, len);
162 strncpy (*new_prefix + len, postfix, plen);
166 * g_completion_complete_utf8:
167 * @cmp: the #GCompletion
168 * @prefix: the prefix string, typically used by the user, which is compared
169 * with each of the items
170 * @new_prefix: if non-%NULL, returns the longest prefix which is common to all
171 * items that matched @prefix, or %NULL if no items matched @prefix.
172 * This string should be freed when no longer needed.
174 * Attempts to complete the string @prefix using the #GCompletion target items.
175 * In contrast to g_completion_complete(), this function returns the largest common
176 * prefix that is a valid UTF-8 string, omitting a possible common partial
177 * character.
179 * You should use this function instead of g_completion_complete() if your
180 * items are UTF-8 strings.
182 * Return value: the list of items whose strings begin with @prefix. This should
183 * not be changed.
185 * Since: 2.4
187 GList*
188 g_completion_complete_utf8 (GCompletion *cmp,
189 const gchar *prefix,
190 gchar **new_prefix)
192 GList *list;
193 gchar *p, *q;
195 list = g_completion_complete (cmp, prefix, new_prefix);
197 if (new_prefix && *new_prefix)
199 p = *new_prefix + strlen (*new_prefix);
200 q = g_utf8_find_prev_char (*new_prefix, p);
202 switch (g_utf8_get_char_validated (q, p - q))
204 case (gunichar)-2:
205 case (gunichar)-1:
206 *q = 0;
207 break;
208 default: ;
213 return list;
216 GList*
217 g_completion_complete (GCompletion* cmp,
218 const gchar* prefix,
219 gchar** new_prefix)
221 gsize plen, len;
222 gboolean done = FALSE;
223 GList* list;
225 g_return_val_if_fail (cmp != NULL, NULL);
226 g_return_val_if_fail (prefix != NULL, NULL);
228 len = strlen (prefix);
229 if (cmp->prefix && cmp->cache)
231 plen = strlen (cmp->prefix);
232 if (plen <= len && ! cmp->strncmp_func (prefix, cmp->prefix, plen))
234 /* use the cache */
235 list = cmp->cache;
236 while (list)
238 GList *next = list->next;
240 if (cmp->strncmp_func (prefix,
241 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
242 len))
243 cmp->cache = g_list_delete_link (cmp->cache, list);
245 list = next;
247 done = TRUE;
251 if (!done)
253 /* normal code */
254 g_list_free (cmp->cache);
255 cmp->cache = NULL;
256 list = cmp->items;
257 while (*prefix && list)
259 if (!cmp->strncmp_func (prefix,
260 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
261 len))
262 cmp->cache = g_list_prepend (cmp->cache, list->data);
263 list = list->next;
266 if (cmp->prefix)
268 g_free (cmp->prefix);
269 cmp->prefix = NULL;
271 if (cmp->cache)
272 cmp->prefix = g_strdup (prefix);
273 completion_check_cache (cmp, new_prefix);
275 return *prefix ? cmp->cache : cmp->items;
278 void
279 g_completion_free (GCompletion* cmp)
281 g_return_if_fail (cmp != NULL);
283 g_completion_clear_items (cmp);
284 g_free (cmp);
287 void
288 g_completion_set_compare(GCompletion *cmp,
289 GCompletionStrncmpFunc strncmp_func)
291 cmp->strncmp_func = strncmp_func;
294 #ifdef TEST_COMPLETION
295 #include <stdio.h>
297 main (int argc,
298 char* argv[])
300 FILE *file;
301 gchar buf[1024];
302 GList *list;
303 GList *result;
304 GList *tmp;
305 GCompletion *cmp;
306 gint i;
307 gchar *longp = NULL;
309 if (argc < 3)
311 g_warning ("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
312 return 1;
315 file = fopen (argv[1], "r");
316 if (!file)
318 g_warning ("Cannot open %s\n", argv[1]);
319 return 1;
322 cmp = g_completion_new (NULL);
323 list = g_list_alloc ();
324 while (fgets (buf, 1024, file))
326 list->data = g_strdup (buf);
327 g_completion_add_items (cmp, list);
329 fclose (file);
331 for (i = 2; i < argc; ++i)
333 printf ("COMPLETING: %s\n", argv[i]);
334 result = g_completion_complete (cmp, argv[i], &longp);
335 g_list_foreach (result, (GFunc) printf, NULL);
336 printf ("LONG MATCH: %s\n", longp);
337 g_free (longp);
338 longp = NULL;
341 g_list_foreach (cmp->items, (GFunc) g_free, NULL);
342 g_completion_free (cmp);
343 g_list_free (list);
345 return 0;
347 #endif
349 #define __G_COMPLETION_C__
350 #include "galiasdef.c"