Main Page | Class List | Directories | File List | Class Members | File Members

gcompletion.c File Reference

#include "glib.h"
#include <string.h>

Go to the source code of this file.

Functions

static void completion_check_cache (GCompletion *cmp, gchar **new_prefix)
GCompletiong_completion_new (GCompletionFunc func)
void g_completion_add_items (GCompletion *cmp, GList *items)
void g_completion_remove_items (GCompletion *cmp, GList *items)
void g_completion_clear_items (GCompletion *cmp)
GListg_completion_complete (GCompletion *cmp, gchar *prefix, gchar **new_prefix)
void g_completion_free (GCompletion *cmp)


Function Documentation

static void completion_check_cache GCompletion cmp,
gchar **  new_prefix
[static]
 

Definition at line 119 of file gcompletion.c.

References _GCompletion::cache, _GList::data, _GCompletion::func, g_new0, _GList::next, NULL, and _GCompletion::prefix.

Referenced by g_completion_complete().

00121 {
00122   register GList* list;
00123   register gint len;
00124   register gint i;
00125   register gint plen;
00126   gchar* postfix;
00127   gchar* s;
00128   
00129   if (!new_prefix)
00130     return;
00131   if (!cmp->cache)
00132     {
00133       *new_prefix = NULL;
00134       return;
00135     }
00136   
00137   len = strlen(cmp->prefix);
00138   list = cmp->cache;
00139   s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
00140   postfix = s + len;
00141   plen = strlen (postfix);
00142   list = list->next;
00143   
00144   while (list && plen)
00145     {
00146       s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
00147       s += len;
00148       for (i = 0; i < plen; ++i)
00149         {
00150           if (postfix[i] != s[i])
00151             break;
00152         }
00153       plen = i;
00154       list = list->next;
00155     }
00156   
00157   *new_prefix = g_new0 (gchar, len + plen + 1);
00158   strncpy (*new_prefix, cmp->prefix, len);
00159   strncpy (*new_prefix + len, postfix, plen);
00160 }

void g_completion_add_items GCompletion cmp,
GList items
 

Definition at line 52 of file gcompletion.c.

References _GCompletion::cache, _GList::data, g_free(), g_list_free(), g_list_prepend(), g_return_if_fail, _GCompletion::items, _GList::next, NULL, and _GCompletion::prefix.

00054 {
00055   GList* it;
00056   
00057   g_return_if_fail (cmp != NULL);
00058   g_return_if_fail (items != NULL);
00059   
00060   /* optimize adding to cache? */
00061   if (cmp->cache)
00062     {
00063       g_list_free (cmp->cache);
00064       cmp->cache = NULL;
00065     }
00066 
00067   if (cmp->prefix)
00068     {
00069       g_free (cmp->prefix);
00070       cmp->prefix = NULL;
00071     }
00072   
00073   it = items;
00074   while (it)
00075     {
00076       cmp->items = g_list_prepend (cmp->items, it->data);
00077       it = it->next;
00078     }
00079 }

void g_completion_clear_items GCompletion cmp  ) 
 

Definition at line 106 of file gcompletion.c.

References _GCompletion::cache, g_free(), g_list_free(), g_return_if_fail, _GCompletion::items, NULL, and _GCompletion::prefix.

Referenced by g_completion_free().

00107 {
00108   g_return_if_fail (cmp != NULL);
00109   
00110   g_list_free (cmp->items);
00111   cmp->items = NULL;
00112   g_list_free (cmp->cache);
00113   cmp->cache = NULL;
00114   g_free (cmp->prefix);
00115   cmp->prefix = NULL;
00116 }

GList* g_completion_complete GCompletion cmp,
gchar prefix,
gchar **  new_prefix
 

Definition at line 163 of file gcompletion.c.

References _GCompletion::cache, completion_check_cache(), _GList::data, _GCompletion::func, g_free(), g_list_free(), g_list_prepend(), g_list_remove_link(), g_return_val_if_fail, g_strdup(), _GCompletion::items, _GList::next, NULL, and _GCompletion::prefix.

00166 {
00167   gint plen, len;
00168   gint done = 0;
00169   GList* list;
00170   
00171   g_return_val_if_fail (cmp != NULL, NULL);
00172   g_return_val_if_fail (prefix != NULL, NULL);
00173   
00174   len = strlen (prefix);
00175   if (cmp->prefix && cmp->cache)
00176     {
00177       plen = strlen (cmp->prefix);
00178       if (plen <= len && !strncmp (prefix, cmp->prefix, plen))
00179         { 
00180           /* use the cache */
00181           list = cmp->cache;
00182           while (list)
00183             {
00184               if (strncmp (prefix,
00185                            cmp->func ? cmp->func (list->data) : (gchar*) list->data,
00186                            len))
00187                 {
00188                   list = g_list_remove_link (cmp->cache, list);
00189                   if (list != cmp->cache)
00190                     cmp->cache = list;
00191                 }
00192               else
00193                 list = list->next;
00194             }
00195           done = 1;
00196         }
00197     }
00198   
00199   if (!done)
00200     {
00201       /* normal code */
00202       g_list_free (cmp->cache);
00203       cmp->cache = NULL;
00204       list = cmp->items;
00205       while (*prefix && list)
00206         {
00207           if (!strncmp (prefix,
00208                         cmp->func ? cmp->func (list->data) : (gchar*) list->data,
00209                         len))
00210             cmp->cache = g_list_prepend (cmp->cache, list->data);
00211           list = list->next;
00212         }
00213     }
00214   if (cmp->prefix)
00215     {
00216       g_free (cmp->prefix);
00217       cmp->prefix = NULL;
00218     }
00219   if (cmp->cache)
00220     cmp->prefix = g_strdup (prefix);
00221   completion_check_cache (cmp, new_prefix);
00222   
00223   return *prefix ? cmp->cache : cmp->items;
00224 }

void g_completion_free GCompletion cmp  ) 
 

Definition at line 227 of file gcompletion.c.

References g_completion_clear_items(), g_free(), g_return_if_fail, and NULL.

00228 {
00229   g_return_if_fail (cmp != NULL);
00230   
00231   g_completion_clear_items (cmp);
00232   g_free (cmp);
00233 }

GCompletion* g_completion_new GCompletionFunc  func  ) 
 

Definition at line 38 of file gcompletion.c.

References _GCompletion::cache, _GCompletion::func, g_new, _GCompletion::items, NULL, and _GCompletion::prefix.

00039 {
00040   GCompletion* gcomp;
00041   
00042   gcomp = g_new (GCompletion, 1);
00043   gcomp->items = NULL;
00044   gcomp->cache = NULL;
00045   gcomp->prefix = NULL;
00046   gcomp->func = func;
00047 
00048   return gcomp;
00049 }

void g_completion_remove_items GCompletion cmp,
GList items
 

Definition at line 82 of file gcompletion.c.

References _GCompletion::cache, _GList::data, g_list_remove(), g_return_if_fail, _GCompletion::items, _GList::next, and NULL.

00084 {
00085   GList* it;
00086   
00087   g_return_if_fail (cmp != NULL);
00088   g_return_if_fail (items != NULL);
00089   
00090   it = items;
00091   while (cmp->items && it)
00092     {
00093       cmp->items = g_list_remove (cmp->items, it->data);
00094       it = it->next;
00095     }
00096 
00097   it = items;
00098   while (cmp->cache && it)
00099     {
00100       cmp->cache = g_list_remove(cmp->cache, it->data);
00101       it = it->next;
00102     }
00103 }


© sourcejam.com 2005-2008