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/.
34 static void completion_check_cache (GCompletion* cmp,
38 g_completion_new (GCompletionFunc func)
42 gcomp = g_new (GCompletion, 1);
47 gcomp->strncmp_func = strncmp;
53 g_completion_add_items (GCompletion* cmp,
58 g_return_if_fail (cmp != NULL);
59 g_return_if_fail (items != NULL);
61 /* optimize adding to cache? */
64 g_list_free (cmp->cache);
77 cmp->items = g_list_prepend (cmp->items, it->data);
83 g_completion_remove_items (GCompletion* cmp,
88 g_return_if_fail (cmp != NULL);
89 g_return_if_fail (items != NULL);
92 while (cmp->items && it)
94 cmp->items = g_list_remove (cmp->items, it->data);
99 while (cmp->cache && it)
101 cmp->cache = g_list_remove(cmp->cache, it->data);
107 g_completion_clear_items (GCompletion* cmp)
109 g_return_if_fail (cmp != NULL);
111 g_list_free (cmp->items);
113 g_list_free (cmp->cache);
115 g_free (cmp->prefix);
120 completion_check_cache (GCompletion* cmp,
123 register GList* list;
138 len = strlen(cmp->prefix);
140 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
142 plen = strlen (postfix);
147 s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
149 for (i = 0; i < plen; ++i)
151 if (postfix[i] != s[i])
158 *new_prefix = g_new0 (gchar, len + plen + 1);
159 strncpy (*new_prefix, cmp->prefix, len);
160 strncpy (*new_prefix + len, postfix, plen);
164 g_completion_complete (GCompletion* cmp,
169 gboolean done = FALSE;
172 g_return_val_if_fail (cmp != NULL, NULL);
173 g_return_val_if_fail (prefix != NULL, NULL);
175 len = strlen (prefix);
176 if (cmp->prefix && cmp->cache)
178 plen = strlen (cmp->prefix);
179 if (plen <= len && ! cmp->strncmp_func (prefix, cmp->prefix, plen))
185 GList *next = list->next;
187 if (cmp->strncmp_func (prefix,
188 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
190 cmp->cache = g_list_delete_link (cmp->cache, list);
201 g_list_free (cmp->cache);
204 while (*prefix && list)
206 if (!cmp->strncmp_func (prefix,
207 cmp->func ? cmp->func (list->data) : (gchar*) list->data,
209 cmp->cache = g_list_prepend (cmp->cache, list->data);
215 g_free (cmp->prefix);
219 cmp->prefix = g_strdup (prefix);
220 completion_check_cache (cmp, new_prefix);
222 return *prefix ? cmp->cache : cmp->items;
226 g_completion_free (GCompletion* cmp)
228 g_return_if_fail (cmp != NULL);
230 g_completion_clear_items (cmp);
235 g_completion_set_compare(GCompletion *cmp,
236 GCompletionStrncmpFunc strncmp_func)
238 cmp->strncmp_func = strncmp_func;
241 #ifdef TEST_COMPLETION
258 g_warning ("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
262 file = fopen (argv[1], "r");
265 g_warning ("Cannot open %s\n", argv[1]);
269 cmp = g_completion_new (NULL);
270 list = g_list_alloc ();
271 while (fgets (buf, 1024, file))
273 list->data = g_strdup (buf);
274 g_completion_add_items (cmp, list);
278 for (i = 2; i < argc; ++i)
280 printf ("COMPLETING: %s\n", argv[i]);
281 result = g_completion_complete (cmp, argv[i], &longp);
282 g_list_foreach (result, (GFunc) printf, NULL);
283 printf ("LONG MATCH: %s\n", longp);
288 g_list_foreach (cmp->items, (GFunc) g_free, NULL);
289 g_completion_free (cmp);