From 195cd8844b7a2e3b19e387f4157bb5c5141f0062 Mon Sep 17 00:00:00 2001 From: NotZed Date: Wed, 12 Apr 2000 15:09:58 +0000 Subject: Debug function to dump the whole index to stdout. 2000-04-12 NotZed * find.c (ibex_dump_all): Debug function to dump the whole index to stdout. * words.c (get_ibex_file): Use g_strdup(), not strdup(). 2000-04-11 NotZed * file.c (write_word): Always write out all words we have (even if its 0 ... the file expects it). No longer check for removed files. (store_word): Check for removed files here, and only add to the ordered tree if we have references left to this word. (ibex_write): First insert into the tree, to determine the wordcount to be saved in the output file, and then write that. (ibex_open): Remove some debug. * words.c (ibex_index_buffer): Always set 'unread', if it is a valid pointer (dont rely on caller to initialise it). svn path=/trunk/; revision=2409 --- libibex/ChangeLog | 20 ++++++++++++++++++ libibex/file.c | 63 ++++++++++++++++++++++++++++++------------------------- libibex/find.c | 20 ++++++++++++++++++ libibex/ibex.h | 3 +++ libibex/words.c | 7 ++++--- 5 files changed, 82 insertions(+), 31 deletions(-) (limited to 'libibex') diff --git a/libibex/ChangeLog b/libibex/ChangeLog index ef27870817..bc6d1d520a 100644 --- a/libibex/ChangeLog +++ b/libibex/ChangeLog @@ -1,3 +1,23 @@ +2000-04-12 NotZed + + * find.c (ibex_dump_all): Debug function to dump the whole index + to stdout. + + * words.c (get_ibex_file): Use g_strdup(), not strdup(). + +2000-04-11 NotZed + + * file.c (write_word): Always write out all words we have (even if + its 0 ... the file expects it). No longer check for removed files. + (store_word): Check for removed files here, and only add to the + ordered tree if we have references left to this word. + (ibex_write): First insert into the tree, to determine the + wordcount to be saved in the output file, and then write that. + (ibex_open): Remove some debug. + + * words.c (ibex_index_buffer): Always set 'unread', if it is a + valid pointer (dont rely on caller to initialise it). + 2000-03-26 NotZed * lookup.c (main): Fixed call to ibex_open. diff --git a/libibex/file.c b/libibex/file.c index f011312cab..5c292e198f 100644 --- a/libibex/file.c +++ b/libibex/file.c @@ -81,7 +81,6 @@ ibex_open (char *file, int flags, int mode) fd = open(file, flags, mode); if (fd == -1) { - printf("open failed :(\n"); return NULL; } @@ -113,7 +112,6 @@ ibex_open (char *file, int flags, int mode) f = fdopen(fd, modestr); if (f == NULL) { - printf("fdopen failed, modestr = '%s'\n", modestr); if (errno == 0) errno = ENOMEM; return NULL; @@ -147,8 +145,9 @@ ibex_open (char *file, int flags, int mode) for (i = 0; i < nfiles; i++) { ibfs[i] = g_malloc (sizeof (ibex_file)); ibfs[i]->name = get_compressed_word (f, &lastword); - if (!ibfs[i]->name) + if (!ibfs[i]->name) { goto errout; + } ibfs[i]->index = 0; g_tree_insert (ib->files, ibfs[i]->name, ibfs[i]); } @@ -158,16 +157,18 @@ ibex_open (char *file, int flags, int mode) lastword = NULL; for (i = 0; i < nwords; i++) { word = get_compressed_word (f, &lastword); - if (!word) + if (!word) { goto errout; + } nrefs = read_number (f); refs = g_ptr_array_new (); g_ptr_array_set_size (refs, nrefs); while (nrefs--) { ref = read_number (f); - if (ref >= nfiles) + if (ref >= nfiles) { goto errout; + } refs->pdata[nrefs] = ibfs[ref]; } @@ -230,43 +231,49 @@ write_file (gpointer key, gpointer value, gpointer data) return FALSE; } +/* scans for words which still exist in the index (after + index removals), and adds them to the ordered tree for + writing out in order */ static void store_word (gpointer key, gpointer value, gpointer data) { GTree *wtree = data; + GPtrArray *refs = value; + int i; + ibex_file *ibf; - g_tree_insert (wtree, key, value); + for (i = 0; i < refs->len; i++) { + ibf = g_ptr_array_index (refs, i); + if (ibf->index == -1) { + g_ptr_array_remove_index_fast (refs, i); + i--; + } + } + + if (refs->len > 0) { + g_tree_insert (wtree, key, value); + } } +/* writes a word out, in order */ static gint write_word (gpointer key, gpointer value, gpointer data) { char *word = key; GPtrArray *refs = value; struct ibex_write_data *iwd = data; + int i, prefix; ibex_file *ibf; - int i, ind, prefix; - for (i = ind = 0; i < refs->len; i++) { - ibf = g_ptr_array_index (refs, i); - if (ibf->index == -1) { - g_ptr_array_remove_index_fast (refs, i); - i--; - } else - ind++; - } - - if (ind != 0) { - prefix = get_prefix (iwd, word); - fprintf (iwd->f, "%c%s", prefix, word + prefix); - fputc (0, iwd->f); + prefix = get_prefix (iwd, word); + fprintf (iwd->f, "%c%s", prefix, word + prefix); + fputc (0, iwd->f); - write_number (iwd->f, ind); + write_number (iwd->f, refs->len); - for (i = 0; i < refs->len; i++) { - ibf = g_ptr_array_index (refs, i); - write_number (iwd->f, ibf->index); - } + for (i = 0; i < refs->len; i++) { + ibf = g_ptr_array_index (refs, i); + write_number (iwd->f, ibf->index); } return FALSE; } @@ -310,11 +317,11 @@ ibex_write (ibex *ib) goto lose; iwd.lastname = NULL; - write_number (iwd.f, g_hash_table_size (ib->words)); - if (ferror (iwd.f)) - goto lose; wtree = g_tree_new (strcmp); g_hash_table_foreach (ib->words, store_word, wtree); + write_number (iwd.f, g_tree_nnodes(wtree)); + if (ferror (iwd.f)) + goto lose; g_tree_traverse (wtree, write_word, G_IN_ORDER, &iwd); g_tree_destroy (wtree); if (ferror (iwd.f)) diff --git a/libibex/find.c b/libibex/find.c index 91e611401c..e0b5d263c8 100644 --- a/libibex/find.c +++ b/libibex/find.c @@ -160,3 +160,23 @@ ibex_find_all (ibex *ib, GPtrArray *words) g_tree_destroy (work); return ret; } + +static void +ibex_dump_foo(char *key, GPtrArray *refs, void *data) +{ + int i; + + printf("%s: ", key); + for (i=0;ilen;i++) { + ibex_file *ibf = g_ptr_array_index (refs, i); + printf("%c%s", ibf->index==-1?'-':' ', ibf->name); + } + printf("\n"); +} + +/* debug function to dump the tree, in key order */ +void +ibex_dump_all (ibex *ib) +{ + g_hash_table_foreach(ib->words, ibex_dump_foo, 0); +} diff --git a/libibex/ibex.h b/libibex/ibex.h index bb818e6d34..e24508aaf7 100644 --- a/libibex/ibex.h +++ b/libibex/ibex.h @@ -92,5 +92,8 @@ gboolean ibex_find_name (ibex *ib, char *name, char *word); */ GPtrArray *ibex_find_all (ibex *ib, GPtrArray *words); +/* Debug function, dumps the whole index tree, showing removed files as well */ +void ibex_dump_all (ibex *ib); + #endif /* ! IBEX_H */ diff --git a/libibex/words.c b/libibex/words.c index 40e0452403..7642b0a305 100644 --- a/libibex/words.c +++ b/libibex/words.c @@ -159,7 +159,7 @@ get_ibex_file (ibex *ib, char *name) ibf = g_tree_lookup (ib->files, name); if (!ibf) { ibf = g_malloc (sizeof (ibex_file)); - ibf->name = strdup (name); + ibf->name = g_strdup (name); ibf->index = 0; g_tree_insert (ib->files, ibf->name, ibf); ib->dirty = TRUE; @@ -213,6 +213,9 @@ ibex_index_buffer (ibex *ib, char *name, char *buffer, ibex_file *ibf = get_ibex_file (ib, name); int wordsiz, cat; + if (unread) + *unread = 0; + end = buffer + len; wordsiz = 20; word = g_malloc (wordsiz); @@ -261,8 +264,6 @@ ibex_index_buffer (ibex *ib, char *name, char *buffer, p = q; } - if (unread) - *unread = 0; g_free (word); return 0; } -- cgit