*
* This library 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
+ * 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 Library General Public
#include "glib.h"
#include <string.h>
-static void completion_check_cache (GCompletion* cmp, gchar** new_prefix);
+static void completion_check_cache (GCompletion* cmp,
+ gchar** new_prefix);
GCompletion*
-g_completion_new (GCompletionFunc func) {
- GCompletion* gcomp;
-
- gcomp = g_new(GCompletion, 1);
- gcomp->items = NULL;
- gcomp->cache = NULL;
- gcomp->prefix = NULL;
- if ( func )
- gcomp->func = func;
- else
- gcomp->func = NULL;
- return gcomp;
+g_completion_new (GCompletionFunc func)
+{
+ GCompletion* gcomp;
+
+ gcomp = g_new (GCompletion, 1);
+ gcomp->items = NULL;
+ gcomp->cache = NULL;
+ gcomp->prefix = NULL;
+ gcomp->func = func;
+
+ return gcomp;
}
void
-g_completion_add_items (GCompletion* cmp, GList* items) {
- GList* it;
-
- g_return_if_fail( cmp != NULL);
- g_return_if_fail( items != NULL);
-
- /* optimize adding to cache? */
- if ( cmp->cache ) {
- g_list_free(cmp->cache);
- cmp->cache = NULL;
- }
- if ( cmp->prefix ) {
- g_free(cmp->prefix);
- cmp->prefix = NULL;
- }
-
- it = items;
- while ( it ) {
- cmp->items = g_list_prepend(cmp->items, it->data);
- it = it->next;
- }
+g_completion_add_items (GCompletion* cmp,
+ GList* items)
+{
+ GList* it;
+
+ g_return_if_fail (cmp != NULL);
+ g_return_if_fail (items != NULL);
+
+ /* optimize adding to cache? */
+ if (cmp->cache)
+ {
+ g_list_free (cmp->cache);
+ cmp->cache = NULL;
+ }
+
+ if (cmp->prefix)
+ {
+ g_free (cmp->prefix);
+ cmp->prefix = NULL;
+ }
+
+ it = items;
+ while (it)
+ {
+ cmp->items = g_list_prepend (cmp->items, it->data);
+ it = it->next;
+ }
}
void
-g_completion_remove_items (GCompletion* cmp, GList* items) {
- GList* it;
-
- g_return_if_fail( cmp != NULL);
- g_return_if_fail( items != NULL);
-
- it = items;
- while ( cmp->items && it ) {
- cmp->items = g_list_remove(cmp->items, it->data);
- it = it->next;
- }
- it = items;
- while ( cmp->cache && it ) {
- cmp->cache = g_list_remove(cmp->cache, it->data);
- it = it->next;
- }
+g_completion_remove_items (GCompletion* cmp,
+ GList* items)
+{
+ GList* it;
+
+ g_return_if_fail (cmp != NULL);
+ g_return_if_fail (items != NULL);
+
+ it = items;
+ while (cmp->items && it)
+ {
+ cmp->items = g_list_remove (cmp->items, it->data);
+ it = it->next;
+ }
+
+ it = items;
+ while (cmp->cache && it)
+ {
+ cmp->cache = g_list_remove(cmp->cache, it->data);
+ it = it->next;
+ }
}
void
-g_completion_clear_items (GCompletion* cmp) {
- g_return_if_fail(cmp != NULL);
-
- g_list_free(cmp->items);
- cmp->items = NULL;
- g_list_free(cmp->cache);
- cmp->cache = NULL;
- g_free(cmp->prefix);
- cmp->prefix = NULL;
+g_completion_clear_items (GCompletion* cmp)
+{
+ g_return_if_fail (cmp != NULL);
+
+ g_list_free (cmp->items);
+ cmp->items = NULL;
+ g_list_free (cmp->cache);
+ cmp->cache = NULL;
+ g_free (cmp->prefix);
+ cmp->prefix = NULL;
}
static void
-completion_check_cache (GCompletion* cmp, gchar** new_prefix) {
- register GList* list;
- register gint len;
- register gint i;
- register gint plen;
- gchar* postfix=NULL;
- gchar* s;
-
- if ( !new_prefix )
- return;
- if ( !cmp->cache ) {
- *new_prefix = NULL;
- return;
+completion_check_cache (GCompletion* cmp,
+ gchar** new_prefix)
+{
+ register GList* list;
+ register gint len;
+ register gint i;
+ register gint plen;
+ gchar* postfix=NULL;
+ gchar* s;
+
+ if (!new_prefix)
+ return;
+ if (!cmp->cache)
+ {
+ *new_prefix = NULL;
+ return;
+ }
+
+ len = strlen(cmp->prefix);
+ list = cmp->cache;
+ s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
+ postfix = s + len;
+ plen = strlen (postfix);
+ list = list->next;
+
+ while (list && plen)
+ {
+ s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
+ s += len;
+ for (i = 0; i < plen; ++i)
+ {
+ if (postfix[i] != s[i])
+ break;
}
-
- len = strlen(cmp->prefix);
- list = cmp->cache;
- s = cmp->func?(*cmp->func)(list->data):(gchar*)list->data;
- postfix = s + len;
- plen = strlen(postfix);
- list = list->next;
-
- while (list && plen) {
- s = cmp->func?(*cmp->func)(list->data):(gchar*)list->data;
- s += len;
- for (i=0; i < plen; ++i) {
- if ( postfix[i] != s[i] )
- break;
- }
- plen = i;
- list = list->next;
- }
-
- *new_prefix = g_new0(gchar, len+plen+1);
- strncpy(*new_prefix, cmp->prefix, len);
- strncpy(*new_prefix+len, postfix, plen);
+ plen = i;
+ list = list->next;
+ }
+
+ *new_prefix = g_new0 (gchar, len + plen + 1);
+ strncpy (*new_prefix, cmp->prefix, len);
+ strncpy (*new_prefix + len, postfix, plen);
}
GList*
-g_completion_complete (GCompletion* cmp, gchar* prefix, gchar** new_prefix) {
- gint plen, len;
- gint done=0;
- GList* list;
-
- g_return_val_if_fail(cmp != NULL, NULL);
- g_return_val_if_fail(prefix != NULL, NULL);
-
- len = strlen(prefix);
- if ( cmp->prefix && cmp->cache ) {
- plen = strlen(cmp->prefix);
- if ( plen <= len && !strncmp(prefix, cmp->prefix, plen) ) {
- /* use the cache */
- list = cmp->cache;
- while ( list ) {
- if ( strncmp(prefix, cmp->func?(*cmp->func)(list->data):(gchar*)list->data, len) ) {
- list = g_list_remove_link(cmp->cache, list);
- if ( list != cmp->cache )
- cmp->cache = list;
- } else
- list = list->next;
- }
- done = 1;
- }
- }
-
- if (!done) { /* normal code */
- g_list_free(cmp->cache);
- cmp->cache = NULL;
- list = cmp->items;
- while (*prefix && list) {
- if ( !strncmp(prefix, cmp->func?(*cmp->func)(list->data):(gchar*)list->data, len) )
- cmp->cache = g_list_prepend(cmp->cache, list->data);
- list = list->next;
+g_completion_complete (GCompletion* cmp,
+ gchar* prefix,
+ gchar** new_prefix)
+{
+ gint plen, len;
+ gint done = 0;
+ GList* list;
+
+ g_return_val_if_fail (cmp != NULL, NULL);
+ g_return_val_if_fail (prefix != NULL, NULL);
+
+ len = strlen (prefix);
+ if (cmp->prefix && cmp->cache)
+ {
+ plen = strlen (cmp->prefix);
+ if (plen <= len && !strncmp (prefix, cmp->prefix, plen))
+ {
+ /* use the cache */
+ list = cmp->cache;
+ while (list)
+ {
+ if (strncmp (prefix,
+ cmp->func ? cmp->func (list->data) : (gchar*) list->data,
+ len))
+ {
+ list = g_list_remove_link (cmp->cache, list);
+ if (list != cmp->cache)
+ cmp->cache = list;
}
+ else
+ list = list->next;
+ }
+ done = 1;
}
- if ( cmp->prefix ) {
- g_free(cmp->prefix);
- cmp->prefix = NULL;
+ }
+
+ if (!done)
+ {
+ /* normal code */
+ g_list_free (cmp->cache);
+ cmp->cache = NULL;
+ list = cmp->items;
+ while (*prefix && list)
+ {
+ if (!strncmp (prefix,
+ cmp->func ? cmp->func (list->data) : (gchar*) list->data,
+ len))
+ cmp->cache = g_list_prepend (cmp->cache, list->data);
+ list = list->next;
}
- if ( cmp->cache )
- cmp->prefix = g_strdup(prefix);
- completion_check_cache(cmp, new_prefix);
- return *prefix?cmp->cache:cmp->items;
-
+ }
+ if (cmp->prefix)
+ {
+ g_free (cmp->prefix);
+ cmp->prefix = NULL;
+ }
+ if (cmp->cache)
+ cmp->prefix = g_strdup (prefix);
+ completion_check_cache (cmp, new_prefix);
+
+ return *prefix ? cmp->cache : cmp->items;
}
void
-g_completion_free (GCompletion* cmp) {
- g_return_if_fail(cmp != NULL);
-
- g_completion_clear_items(cmp);
- g_free(cmp);
+g_completion_free (GCompletion* cmp)
+{
+ g_return_if_fail (cmp != NULL);
+
+ g_completion_clear_items (cmp);
+ g_free (cmp);
}
#ifdef TEST_COMPLETION
-
#include <stdio.h>
-
-int main (int argc, char* argv[]) {
-
- FILE * file;
- gchar buf[1024];
- GList *list;
- GList *result;
- GList *tmp;
- GCompletion * cmp;
- gint i;
- gchar* longp=NULL;
-
- if ( argc < 3 ) {
- g_warning("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
- return 1;
- }
-
- if ( !(file=fopen(argv[1], "r")) ) {
- g_warning("Cannot open %s\n", argv[1]);
- return 1;
- }
-
- cmp = g_completion_new(NULL);
- list = g_list_alloc();
- while (fgets(buf, 1024, file)) {
- list->data = g_strdup(buf);
- g_completion_add_items(cmp, list);
- }
- fclose(file);
-
- for ( i= 2; i < argc; ++i) {
- printf("COMPLETING: %s\n", argv[i]);
- result = g_completion_complete(cmp, argv[i], &longp);
- g_list_foreach(result, (GFunc)printf, NULL);
- printf("LONG MATCH: %s\n", longp);
- g_free(longp);
- longp = NULL;
- }
-
- g_list_foreach(cmp->items, (GFunc)g_free, NULL);
- g_completion_free(cmp);
- g_list_free(list);
- return 0;
+int
+main (int argc,
+ char* argv[])
+{
+ FILE *file;
+ gchar buf[1024];
+ GList *list;
+ GList *result;
+ GList *tmp;
+ GCompletion *cmp;
+ gint i;
+ gchar *longp = NULL;
+
+ if (argc < 3)
+ {
+ g_warning ("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
+ return 1;
+ }
+
+ file = fopen (argv[1], "r");
+ if (!file)
+ {
+ g_warning ("Cannot open %s\n", argv[1]);
+ return 1;
+ }
+
+ cmp = g_completion_new (NULL);
+ list = g_list_alloc ();
+ while (fgets (buf, 1024, file))
+ {
+ list->data = g_strdup (buf);
+ g_completion_add_items (cmp, list);
+ }
+ fclose (file);
+
+ for (i = 2; i < argc; ++i)
+ {
+ printf ("COMPLETING: %s\n", argv[i]);
+ result = g_completion_complete (cmp, argv[i], &longp);
+ g_list_foreach (result, (GFunc) printf, NULL);
+ printf ("LONG MATCH: %s\n", longp);
+ g_free (longp);
+ longp = NULL;
+ }
+
+ g_list_foreach (cmp->items, (GFunc) g_free, NULL);
+ g_completion_free (cmp);
+ g_list_free (list);
+
+ return 0;
}
-
#endif
typedef struct _GScannerConfig GScannerConfig;
typedef struct _GScanner GScanner;
typedef union _GValue GValue;
+typedef struct _GCompletion GCompletion;
typedef struct _GRelation GRelation;
typedef struct _GTuples GTuples;
typedef guint (*GHashFunc) (gconstpointer key);
typedef gint (*GCompareFunc) (gconstpointer a,
gconstpointer b);
+typedef gchar* (*GCompletionFunc) (gpointer);
struct _GList
{
gchar* g_basename (const gchar *file_name);
gchar* g_getcwd (void);
gchar* g_dirname (const gchar *file_name);
-
+
/* We make the assumption that if memmove isn't available, then
* bcopy will do the job. This isn't safe everywhere. (bcopy can't
gint query);
+
/* String Chunks
*/
GStringChunk* g_string_chunk_new (gint size);
/* unused fields */
gpointer user_data;
guint max_parse_errors;
-
+
/* g_scanner_error() increments this field */
guint parse_errors;
-
+
/* name of input stream, featured by the default message handler */
const gchar *input_name;
-
+
/* data pointer for derived structures */
gpointer derived_data;
-
+
/* link into the scanner configuration */
GScannerConfig *config;
-
+
/* fields filled in after g_scanner_get_next_token() */
GTokenType token;
GValue value;
guint line;
guint position;
-
+
/* fields filled in after g_scanner_peek_next_token() */
GTokenType next_token;
GValue next_value;
guint next_line;
guint next_position;
-
+
/* to be considered private */
GHashTable *symbol_table;
const gchar *text;
gint input_fd;
gint peeked_char;
guint scope_id;
-
+
/* handler function for _warn and _error */
GScannerMsgFunc msg_handler;
};
/* Completion */
-typedef gchar* (*GCompletionFunc)(gpointer);
-
-typedef struct _GCompletion GCompletion;
-
-struct _GCompletion {
- GList* items;
- GCompletionFunc func;
-
- gchar* prefix;
- GList* cache;
-
+struct _GCompletion
+{
+ GList* items;
+ GCompletionFunc func;
+
+ gchar* prefix;
+ GList* cache;
};
GCompletion* g_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);
-GList* g_completion_complete (GCompletion* cmp,
- gchar* prefix,
- gchar** new_prefix);
-void g_completion_free (GCompletion* cmp);
+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);
+GList* g_completion_complete (GCompletion* cmp,
+ gchar* prefix,
+ gchar** new_prefix);
+void g_completion_free (GCompletion* cmp);
+
/* GRelation: Indexed Relations. Imagine a really simple table in a
* database. Relations are not ordered. This data type is meant for
*
* This library 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
+ * 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 Library General Public
#include "glib.h"
#include <string.h>
-static void completion_check_cache (GCompletion* cmp, gchar** new_prefix);
+static void completion_check_cache (GCompletion* cmp,
+ gchar** new_prefix);
GCompletion*
-g_completion_new (GCompletionFunc func) {
- GCompletion* gcomp;
-
- gcomp = g_new(GCompletion, 1);
- gcomp->items = NULL;
- gcomp->cache = NULL;
- gcomp->prefix = NULL;
- if ( func )
- gcomp->func = func;
- else
- gcomp->func = NULL;
- return gcomp;
+g_completion_new (GCompletionFunc func)
+{
+ GCompletion* gcomp;
+
+ gcomp = g_new (GCompletion, 1);
+ gcomp->items = NULL;
+ gcomp->cache = NULL;
+ gcomp->prefix = NULL;
+ gcomp->func = func;
+
+ return gcomp;
}
void
-g_completion_add_items (GCompletion* cmp, GList* items) {
- GList* it;
-
- g_return_if_fail( cmp != NULL);
- g_return_if_fail( items != NULL);
-
- /* optimize adding to cache? */
- if ( cmp->cache ) {
- g_list_free(cmp->cache);
- cmp->cache = NULL;
- }
- if ( cmp->prefix ) {
- g_free(cmp->prefix);
- cmp->prefix = NULL;
- }
-
- it = items;
- while ( it ) {
- cmp->items = g_list_prepend(cmp->items, it->data);
- it = it->next;
- }
+g_completion_add_items (GCompletion* cmp,
+ GList* items)
+{
+ GList* it;
+
+ g_return_if_fail (cmp != NULL);
+ g_return_if_fail (items != NULL);
+
+ /* optimize adding to cache? */
+ if (cmp->cache)
+ {
+ g_list_free (cmp->cache);
+ cmp->cache = NULL;
+ }
+
+ if (cmp->prefix)
+ {
+ g_free (cmp->prefix);
+ cmp->prefix = NULL;
+ }
+
+ it = items;
+ while (it)
+ {
+ cmp->items = g_list_prepend (cmp->items, it->data);
+ it = it->next;
+ }
}
void
-g_completion_remove_items (GCompletion* cmp, GList* items) {
- GList* it;
-
- g_return_if_fail( cmp != NULL);
- g_return_if_fail( items != NULL);
-
- it = items;
- while ( cmp->items && it ) {
- cmp->items = g_list_remove(cmp->items, it->data);
- it = it->next;
- }
- it = items;
- while ( cmp->cache && it ) {
- cmp->cache = g_list_remove(cmp->cache, it->data);
- it = it->next;
- }
+g_completion_remove_items (GCompletion* cmp,
+ GList* items)
+{
+ GList* it;
+
+ g_return_if_fail (cmp != NULL);
+ g_return_if_fail (items != NULL);
+
+ it = items;
+ while (cmp->items && it)
+ {
+ cmp->items = g_list_remove (cmp->items, it->data);
+ it = it->next;
+ }
+
+ it = items;
+ while (cmp->cache && it)
+ {
+ cmp->cache = g_list_remove(cmp->cache, it->data);
+ it = it->next;
+ }
}
void
-g_completion_clear_items (GCompletion* cmp) {
- g_return_if_fail(cmp != NULL);
-
- g_list_free(cmp->items);
- cmp->items = NULL;
- g_list_free(cmp->cache);
- cmp->cache = NULL;
- g_free(cmp->prefix);
- cmp->prefix = NULL;
+g_completion_clear_items (GCompletion* cmp)
+{
+ g_return_if_fail (cmp != NULL);
+
+ g_list_free (cmp->items);
+ cmp->items = NULL;
+ g_list_free (cmp->cache);
+ cmp->cache = NULL;
+ g_free (cmp->prefix);
+ cmp->prefix = NULL;
}
static void
-completion_check_cache (GCompletion* cmp, gchar** new_prefix) {
- register GList* list;
- register gint len;
- register gint i;
- register gint plen;
- gchar* postfix=NULL;
- gchar* s;
-
- if ( !new_prefix )
- return;
- if ( !cmp->cache ) {
- *new_prefix = NULL;
- return;
+completion_check_cache (GCompletion* cmp,
+ gchar** new_prefix)
+{
+ register GList* list;
+ register gint len;
+ register gint i;
+ register gint plen;
+ gchar* postfix=NULL;
+ gchar* s;
+
+ if (!new_prefix)
+ return;
+ if (!cmp->cache)
+ {
+ *new_prefix = NULL;
+ return;
+ }
+
+ len = strlen(cmp->prefix);
+ list = cmp->cache;
+ s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
+ postfix = s + len;
+ plen = strlen (postfix);
+ list = list->next;
+
+ while (list && plen)
+ {
+ s = cmp->func ? cmp->func (list->data) : (gchar*) list->data;
+ s += len;
+ for (i = 0; i < plen; ++i)
+ {
+ if (postfix[i] != s[i])
+ break;
}
-
- len = strlen(cmp->prefix);
- list = cmp->cache;
- s = cmp->func?(*cmp->func)(list->data):(gchar*)list->data;
- postfix = s + len;
- plen = strlen(postfix);
- list = list->next;
-
- while (list && plen) {
- s = cmp->func?(*cmp->func)(list->data):(gchar*)list->data;
- s += len;
- for (i=0; i < plen; ++i) {
- if ( postfix[i] != s[i] )
- break;
- }
- plen = i;
- list = list->next;
- }
-
- *new_prefix = g_new0(gchar, len+plen+1);
- strncpy(*new_prefix, cmp->prefix, len);
- strncpy(*new_prefix+len, postfix, plen);
+ plen = i;
+ list = list->next;
+ }
+
+ *new_prefix = g_new0 (gchar, len + plen + 1);
+ strncpy (*new_prefix, cmp->prefix, len);
+ strncpy (*new_prefix + len, postfix, plen);
}
GList*
-g_completion_complete (GCompletion* cmp, gchar* prefix, gchar** new_prefix) {
- gint plen, len;
- gint done=0;
- GList* list;
-
- g_return_val_if_fail(cmp != NULL, NULL);
- g_return_val_if_fail(prefix != NULL, NULL);
-
- len = strlen(prefix);
- if ( cmp->prefix && cmp->cache ) {
- plen = strlen(cmp->prefix);
- if ( plen <= len && !strncmp(prefix, cmp->prefix, plen) ) {
- /* use the cache */
- list = cmp->cache;
- while ( list ) {
- if ( strncmp(prefix, cmp->func?(*cmp->func)(list->data):(gchar*)list->data, len) ) {
- list = g_list_remove_link(cmp->cache, list);
- if ( list != cmp->cache )
- cmp->cache = list;
- } else
- list = list->next;
- }
- done = 1;
- }
- }
-
- if (!done) { /* normal code */
- g_list_free(cmp->cache);
- cmp->cache = NULL;
- list = cmp->items;
- while (*prefix && list) {
- if ( !strncmp(prefix, cmp->func?(*cmp->func)(list->data):(gchar*)list->data, len) )
- cmp->cache = g_list_prepend(cmp->cache, list->data);
- list = list->next;
+g_completion_complete (GCompletion* cmp,
+ gchar* prefix,
+ gchar** new_prefix)
+{
+ gint plen, len;
+ gint done = 0;
+ GList* list;
+
+ g_return_val_if_fail (cmp != NULL, NULL);
+ g_return_val_if_fail (prefix != NULL, NULL);
+
+ len = strlen (prefix);
+ if (cmp->prefix && cmp->cache)
+ {
+ plen = strlen (cmp->prefix);
+ if (plen <= len && !strncmp (prefix, cmp->prefix, plen))
+ {
+ /* use the cache */
+ list = cmp->cache;
+ while (list)
+ {
+ if (strncmp (prefix,
+ cmp->func ? cmp->func (list->data) : (gchar*) list->data,
+ len))
+ {
+ list = g_list_remove_link (cmp->cache, list);
+ if (list != cmp->cache)
+ cmp->cache = list;
}
+ else
+ list = list->next;
+ }
+ done = 1;
}
- if ( cmp->prefix ) {
- g_free(cmp->prefix);
- cmp->prefix = NULL;
+ }
+
+ if (!done)
+ {
+ /* normal code */
+ g_list_free (cmp->cache);
+ cmp->cache = NULL;
+ list = cmp->items;
+ while (*prefix && list)
+ {
+ if (!strncmp (prefix,
+ cmp->func ? cmp->func (list->data) : (gchar*) list->data,
+ len))
+ cmp->cache = g_list_prepend (cmp->cache, list->data);
+ list = list->next;
}
- if ( cmp->cache )
- cmp->prefix = g_strdup(prefix);
- completion_check_cache(cmp, new_prefix);
- return *prefix?cmp->cache:cmp->items;
-
+ }
+ if (cmp->prefix)
+ {
+ g_free (cmp->prefix);
+ cmp->prefix = NULL;
+ }
+ if (cmp->cache)
+ cmp->prefix = g_strdup (prefix);
+ completion_check_cache (cmp, new_prefix);
+
+ return *prefix ? cmp->cache : cmp->items;
}
void
-g_completion_free (GCompletion* cmp) {
- g_return_if_fail(cmp != NULL);
-
- g_completion_clear_items(cmp);
- g_free(cmp);
+g_completion_free (GCompletion* cmp)
+{
+ g_return_if_fail (cmp != NULL);
+
+ g_completion_clear_items (cmp);
+ g_free (cmp);
}
#ifdef TEST_COMPLETION
-
#include <stdio.h>
-
-int main (int argc, char* argv[]) {
-
- FILE * file;
- gchar buf[1024];
- GList *list;
- GList *result;
- GList *tmp;
- GCompletion * cmp;
- gint i;
- gchar* longp=NULL;
-
- if ( argc < 3 ) {
- g_warning("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
- return 1;
- }
-
- if ( !(file=fopen(argv[1], "r")) ) {
- g_warning("Cannot open %s\n", argv[1]);
- return 1;
- }
-
- cmp = g_completion_new(NULL);
- list = g_list_alloc();
- while (fgets(buf, 1024, file)) {
- list->data = g_strdup(buf);
- g_completion_add_items(cmp, list);
- }
- fclose(file);
-
- for ( i= 2; i < argc; ++i) {
- printf("COMPLETING: %s\n", argv[i]);
- result = g_completion_complete(cmp, argv[i], &longp);
- g_list_foreach(result, (GFunc)printf, NULL);
- printf("LONG MATCH: %s\n", longp);
- g_free(longp);
- longp = NULL;
- }
-
- g_list_foreach(cmp->items, (GFunc)g_free, NULL);
- g_completion_free(cmp);
- g_list_free(list);
- return 0;
+int
+main (int argc,
+ char* argv[])
+{
+ FILE *file;
+ gchar buf[1024];
+ GList *list;
+ GList *result;
+ GList *tmp;
+ GCompletion *cmp;
+ gint i;
+ gchar *longp = NULL;
+
+ if (argc < 3)
+ {
+ g_warning ("Usage: %s filename prefix1 [prefix2 ...]\n", argv[0]);
+ return 1;
+ }
+
+ file = fopen (argv[1], "r");
+ if (!file)
+ {
+ g_warning ("Cannot open %s\n", argv[1]);
+ return 1;
+ }
+
+ cmp = g_completion_new (NULL);
+ list = g_list_alloc ();
+ while (fgets (buf, 1024, file))
+ {
+ list->data = g_strdup (buf);
+ g_completion_add_items (cmp, list);
+ }
+ fclose (file);
+
+ for (i = 2; i < argc; ++i)
+ {
+ printf ("COMPLETING: %s\n", argv[i]);
+ result = g_completion_complete (cmp, argv[i], &longp);
+ g_list_foreach (result, (GFunc) printf, NULL);
+ printf ("LONG MATCH: %s\n", longp);
+ g_free (longp);
+ longp = NULL;
+ }
+
+ g_list_foreach (cmp->items, (GFunc) g_free, NULL);
+ g_completion_free (cmp);
+ g_list_free (list);
+
+ return 0;
}
-
#endif
typedef struct _GScannerConfig GScannerConfig;
typedef struct _GScanner GScanner;
typedef union _GValue GValue;
+typedef struct _GCompletion GCompletion;
typedef struct _GRelation GRelation;
typedef struct _GTuples GTuples;
typedef guint (*GHashFunc) (gconstpointer key);
typedef gint (*GCompareFunc) (gconstpointer a,
gconstpointer b);
+typedef gchar* (*GCompletionFunc) (gpointer);
struct _GList
{
gchar* g_basename (const gchar *file_name);
gchar* g_getcwd (void);
gchar* g_dirname (const gchar *file_name);
-
+
/* We make the assumption that if memmove isn't available, then
* bcopy will do the job. This isn't safe everywhere. (bcopy can't
gint query);
+
/* String Chunks
*/
GStringChunk* g_string_chunk_new (gint size);
/* unused fields */
gpointer user_data;
guint max_parse_errors;
-
+
/* g_scanner_error() increments this field */
guint parse_errors;
-
+
/* name of input stream, featured by the default message handler */
const gchar *input_name;
-
+
/* data pointer for derived structures */
gpointer derived_data;
-
+
/* link into the scanner configuration */
GScannerConfig *config;
-
+
/* fields filled in after g_scanner_get_next_token() */
GTokenType token;
GValue value;
guint line;
guint position;
-
+
/* fields filled in after g_scanner_peek_next_token() */
GTokenType next_token;
GValue next_value;
guint next_line;
guint next_position;
-
+
/* to be considered private */
GHashTable *symbol_table;
const gchar *text;
gint input_fd;
gint peeked_char;
guint scope_id;
-
+
/* handler function for _warn and _error */
GScannerMsgFunc msg_handler;
};
/* Completion */
-typedef gchar* (*GCompletionFunc)(gpointer);
-
-typedef struct _GCompletion GCompletion;
-
-struct _GCompletion {
- GList* items;
- GCompletionFunc func;
-
- gchar* prefix;
- GList* cache;
-
+struct _GCompletion
+{
+ GList* items;
+ GCompletionFunc func;
+
+ gchar* prefix;
+ GList* cache;
};
GCompletion* g_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);
-GList* g_completion_complete (GCompletion* cmp,
- gchar* prefix,
- gchar** new_prefix);
-void g_completion_free (GCompletion* cmp);
+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);
+GList* g_completion_complete (GCompletion* cmp,
+ gchar* prefix,
+ gchar** new_prefix);
+void g_completion_free (GCompletion* cmp);
+
/* GRelation: Indexed Relations. Imagine a really simple table in a
* database. Relations are not ordered. This data type is meant for
scanner->next_value.v_int = 0;
scanner->next_line = 1;
scanner->next_position = 0;
-
+
scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
scanner->text = NULL;
scanner->text_len = 0;
scanner->input_fd = -1;
scanner->peeked_char = -1;
scanner->scope_id = 0;
-
+
scanner->msg_handler = g_scanner_msg_handler;
return scanner;
gpointer _data)
{
GScannerKey *key = _key;
-
+
g_free (key->symbol);
g_free (key);
}
gint is_error)
{
g_return_if_fail (scanner != NULL);
-
+
fprintf (stdout, "%s:%d: ", scanner->input_name, scanner->line);
if (is_error)
fprintf (stdout, "error: ");
{
g_return_if_fail (scanner != NULL);
g_return_if_fail (format != NULL);
-
+
scanner->parse_errors++;
-
+
if (scanner->msg_handler)
{
va_list args, args2;
string = g_vsprintf ((gchar*) format, &args, &args2);
va_end (args);
va_end (args2);
-
+
string = g_strdup (string);
-
+
scanner->msg_handler (scanner, string, TRUE);
-
+
g_free (string);
}
}
{
register const GScannerKey *key1 = v1;
register const GScannerKey *key2 = v2;
-
+
return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
}
register const GScannerKey *key = v;
register gchar *c;
register guint h;
-
+
h = key->scope_id;
for (c = key->symbol; *c; c++)
{
register guint g;
-
+
h = (h << 4) + *c;
g = h & 0xf0000000;
if (g)
h = h ^ g;
}
}
-
+
return h;
}
{
register GScannerKey *key_p;
GScannerKey key;
-
+
key.scope_id = scope_id;
if (!scanner->config->case_sensitive)
if (!scanner->config->case_sensitive)
{
register gchar *c;
-
+
c = key->symbol;
while (*c != 0)
{
g_return_if_fail (scanner != NULL);
g_return_if_fail (symbol != NULL);
-
+
key = g_scanner_lookup_internal (scanner, scope_id, symbol);
if (key)
if (!symbol)
return NULL;
-
+
scope_id = scanner->scope_id;
key = g_scanner_lookup_internal (scanner, scope_id, symbol);
if (!key && scope_id && scanner->config->scope_0_fallback)
guint scope_id)
{
register guint old_scope_id;
-
+
g_return_val_if_fail (scanner != NULL, 0);
-
+
old_scope_id = scanner->scope_id;
scanner->scope_id = scope_id;
-
+
return old_scope_id;
}
register GHFunc func;
register gpointer func_data;
register guint *scope_id;
-
+
d = _user_data;
func = (GHFunc) d[0];
func_data = d[1];
scope_id = d[2];
key = _value;
-
+
if (key->scope_id == *scope_id)
func (key->symbol, key->value, func_data);
}
gpointer func_data)
{
gpointer d[3];
-
+
g_return_if_fail (scanner != NULL);
-
+
d[0] = (gpointer) func;
d[1] = func_data;
d[2] = &scope_id;
-
+
g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
}
g_scanner_freeze_symbol_table (GScanner *scanner)
{
g_return_if_fail (scanner != NULL);
-
+
g_hash_table_freeze (scanner->symbol_table);
}
g_scanner_thaw_symbol_table (GScanner *scanner)
{
g_return_if_fail (scanner != NULL);
-
+
g_hash_table_thaw (scanner->symbol_table);
}
{
struct stat *stat_buf;
gint st_mode;
-
+
stat_buf = g_new0 (struct stat, 1);
-
+
lstat (filename, stat_buf);
-
+
st_mode = stat_buf->st_mode;
-
+
g_free (stat_buf);
-
+
return st_mode;
}
{
register GScannerKey *key;
register guint scope_id;
-
+
scope_id = scanner->scope_id;
key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
if (!key && scope_id && scanner->config->scope_0_fallback)
value.v_symbol = key->value;
}
}
-
+
if (token == G_TOKEN_IDENTIFIER &&
config->scan_identifier_NULL &&
strlen (value.v_identifier) == 4)
gchar *fail_pos_2;
gdouble val_1;
gdouble val_2 = 0;
-
+
g_return_val_if_fail (nptr != NULL, 0);
-
+
fail_pos_1 = NULL;
fail_pos_2 = NULL;
-
+
val_1 = strtod (nptr, &fail_pos_1);
-
+
if (fail_pos_1 && fail_pos_1[0] != 0)
{
gchar *old_locale;
-
+
old_locale = setlocale (LC_NUMERIC, "C");
val_2 = strtod (nptr, &fail_pos_2);
setlocale (LC_NUMERIC, old_locale);
}
-
+
if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
{
if (endptr)
#else /* NO_SYS_ERRLIST */
extern int sys_nerr;
extern char *sys_errlist[];
-
+
if ((errnum > 0) && (errnum <= sys_nerr))
return sys_errlist [errnum];
#endif /* NO_SYS_ERRLIST */
-
+
sprintf (msg, "unknown error (%d)", errnum);
return msg;
}
g_strsignal (gint signum)
{
static char msg[64];
-
+
#ifdef HAVE_STRSIGNAL
extern char *strsignal (int sig);
return strsignal (signum);
extern char *sys_siglist[];
return sys_siglist [signum];
#endif /* NO_SYS_SIGLIST */
-
+
sprintf (msg, "unknown signal (%d)", signum);
return msg;
}
g_strdown (gchar *string)
{
register gchar *s;
-
+
g_return_if_fail (string != NULL);
-
+
s = string;
-
+
while (*s)
{
*s = tolower (*s);
}
void
-g_strup (gchar *string)
+g_strup (gchar *string)
{
register gchar *s;
-
+
g_return_if_fail (string != NULL);
-
+
s = string;
-
+
while (*s)
{
*s = toupper (*s);
}
void
-g_strreverse (gchar *string)
+g_strreverse (gchar *string)
{
g_return_if_fail (string != NULL);
-
+
if (*string)
{
register gchar *h, *t;
-
+
h = string;
t = string + strlen (string) - 1;
return strcasecmp (s1, s2);
#else
gint c1, c2;
-
+
while (*s1 && *s2)
{
/* According to A. Cox, some platforms have islower's that
c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
if (c1 != c2)
- return (c1 - c2);
+ return (c1 - c2);
s1++; s2++;
}
-
+
return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
#endif
}
void
-g_strdelimit (gchar *string,
+g_strdelimit (gchar *string,
const gchar *delimiters,
- gchar new_delim)
+ gchar new_delim)
{
register gchar *c;
-
+
g_return_if_fail (string != NULL);
-
+
if (!delimiters)
delimiters = G_STR_DELIMITERS;
-
+
for (c = string; *c; c++)
{
if (strchr (delimiters, *c))
while (base > file_name && *base == '/')
base--;
len = (guint) 1 + base - file_name;
-
+
base = g_new (gchar, len + 1);
g_memmove (base, file_name, len);
base[len] = 0;
-
+
return base;
}
g_set_prgname (const gchar *prgname)
{
gchar *c = g_prgname;
-
+
g_prgname = g_strdup (prgname);
g_free (c);
}
scanner->next_value.v_int = 0;
scanner->next_line = 1;
scanner->next_position = 0;
-
+
scanner->symbol_table = g_hash_table_new (g_scanner_key_hash, g_scanner_key_equal);
scanner->text = NULL;
scanner->text_len = 0;
scanner->input_fd = -1;
scanner->peeked_char = -1;
scanner->scope_id = 0;
-
+
scanner->msg_handler = g_scanner_msg_handler;
return scanner;
gpointer _data)
{
GScannerKey *key = _key;
-
+
g_free (key->symbol);
g_free (key);
}
gint is_error)
{
g_return_if_fail (scanner != NULL);
-
+
fprintf (stdout, "%s:%d: ", scanner->input_name, scanner->line);
if (is_error)
fprintf (stdout, "error: ");
{
g_return_if_fail (scanner != NULL);
g_return_if_fail (format != NULL);
-
+
scanner->parse_errors++;
-
+
if (scanner->msg_handler)
{
va_list args, args2;
string = g_vsprintf ((gchar*) format, &args, &args2);
va_end (args);
va_end (args2);
-
+
string = g_strdup (string);
-
+
scanner->msg_handler (scanner, string, TRUE);
-
+
g_free (string);
}
}
{
register const GScannerKey *key1 = v1;
register const GScannerKey *key2 = v2;
-
+
return (key1->scope_id == key2->scope_id) && (strcmp (key1->symbol, key2->symbol) == 0);
}
register const GScannerKey *key = v;
register gchar *c;
register guint h;
-
+
h = key->scope_id;
for (c = key->symbol; *c; c++)
{
register guint g;
-
+
h = (h << 4) + *c;
g = h & 0xf0000000;
if (g)
h = h ^ g;
}
}
-
+
return h;
}
{
register GScannerKey *key_p;
GScannerKey key;
-
+
key.scope_id = scope_id;
if (!scanner->config->case_sensitive)
if (!scanner->config->case_sensitive)
{
register gchar *c;
-
+
c = key->symbol;
while (*c != 0)
{
g_return_if_fail (scanner != NULL);
g_return_if_fail (symbol != NULL);
-
+
key = g_scanner_lookup_internal (scanner, scope_id, symbol);
if (key)
if (!symbol)
return NULL;
-
+
scope_id = scanner->scope_id;
key = g_scanner_lookup_internal (scanner, scope_id, symbol);
if (!key && scope_id && scanner->config->scope_0_fallback)
guint scope_id)
{
register guint old_scope_id;
-
+
g_return_val_if_fail (scanner != NULL, 0);
-
+
old_scope_id = scanner->scope_id;
scanner->scope_id = scope_id;
-
+
return old_scope_id;
}
register GHFunc func;
register gpointer func_data;
register guint *scope_id;
-
+
d = _user_data;
func = (GHFunc) d[0];
func_data = d[1];
scope_id = d[2];
key = _value;
-
+
if (key->scope_id == *scope_id)
func (key->symbol, key->value, func_data);
}
gpointer func_data)
{
gpointer d[3];
-
+
g_return_if_fail (scanner != NULL);
-
+
d[0] = (gpointer) func;
d[1] = func_data;
d[2] = &scope_id;
-
+
g_hash_table_foreach (scanner->symbol_table, g_scanner_foreach_internal, d);
}
g_scanner_freeze_symbol_table (GScanner *scanner)
{
g_return_if_fail (scanner != NULL);
-
+
g_hash_table_freeze (scanner->symbol_table);
}
g_scanner_thaw_symbol_table (GScanner *scanner)
{
g_return_if_fail (scanner != NULL);
-
+
g_hash_table_thaw (scanner->symbol_table);
}
{
struct stat *stat_buf;
gint st_mode;
-
+
stat_buf = g_new0 (struct stat, 1);
-
+
lstat (filename, stat_buf);
-
+
st_mode = stat_buf->st_mode;
-
+
g_free (stat_buf);
-
+
return st_mode;
}
{
register GScannerKey *key;
register guint scope_id;
-
+
scope_id = scanner->scope_id;
key = g_scanner_lookup_internal (scanner, scope_id, value.v_identifier);
if (!key && scope_id && scanner->config->scope_0_fallback)
value.v_symbol = key->value;
}
}
-
+
if (token == G_TOKEN_IDENTIFIER &&
config->scan_identifier_NULL &&
strlen (value.v_identifier) == 4)
gchar *fail_pos_2;
gdouble val_1;
gdouble val_2 = 0;
-
+
g_return_val_if_fail (nptr != NULL, 0);
-
+
fail_pos_1 = NULL;
fail_pos_2 = NULL;
-
+
val_1 = strtod (nptr, &fail_pos_1);
-
+
if (fail_pos_1 && fail_pos_1[0] != 0)
{
gchar *old_locale;
-
+
old_locale = setlocale (LC_NUMERIC, "C");
val_2 = strtod (nptr, &fail_pos_2);
setlocale (LC_NUMERIC, old_locale);
}
-
+
if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
{
if (endptr)
#else /* NO_SYS_ERRLIST */
extern int sys_nerr;
extern char *sys_errlist[];
-
+
if ((errnum > 0) && (errnum <= sys_nerr))
return sys_errlist [errnum];
#endif /* NO_SYS_ERRLIST */
-
+
sprintf (msg, "unknown error (%d)", errnum);
return msg;
}
g_strsignal (gint signum)
{
static char msg[64];
-
+
#ifdef HAVE_STRSIGNAL
extern char *strsignal (int sig);
return strsignal (signum);
extern char *sys_siglist[];
return sys_siglist [signum];
#endif /* NO_SYS_SIGLIST */
-
+
sprintf (msg, "unknown signal (%d)", signum);
return msg;
}
g_strdown (gchar *string)
{
register gchar *s;
-
+
g_return_if_fail (string != NULL);
-
+
s = string;
-
+
while (*s)
{
*s = tolower (*s);
}
void
-g_strup (gchar *string)
+g_strup (gchar *string)
{
register gchar *s;
-
+
g_return_if_fail (string != NULL);
-
+
s = string;
-
+
while (*s)
{
*s = toupper (*s);
}
void
-g_strreverse (gchar *string)
+g_strreverse (gchar *string)
{
g_return_if_fail (string != NULL);
-
+
if (*string)
{
register gchar *h, *t;
-
+
h = string;
t = string + strlen (string) - 1;
return strcasecmp (s1, s2);
#else
gint c1, c2;
-
+
while (*s1 && *s2)
{
/* According to A. Cox, some platforms have islower's that
c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
if (c1 != c2)
- return (c1 - c2);
+ return (c1 - c2);
s1++; s2++;
}
-
+
return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
#endif
}
void
-g_strdelimit (gchar *string,
+g_strdelimit (gchar *string,
const gchar *delimiters,
- gchar new_delim)
+ gchar new_delim)
{
register gchar *c;
-
+
g_return_if_fail (string != NULL);
-
+
if (!delimiters)
delimiters = G_STR_DELIMITERS;
-
+
for (c = string; *c; c++)
{
if (strchr (delimiters, *c))
while (base > file_name && *base == '/')
base--;
len = (guint) 1 + base - file_name;
-
+
base = g_new (gchar, len + 1);
g_memmove (base, file_name, len);
base[len] = 0;
-
+
return base;
}
g_set_prgname (const gchar *prgname)
{
gchar *c = g_prgname;
-
+
g_prgname = g_strdup (prgname);
g_free (c);
}