From 2f0932393fb205b2e9b941db50cf5aebfba5c385 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 18 Aug 1994 12:40:52 +0000 Subject: [PATCH] Some development of dictionary. Not finished at all! --- dict/close.c | 7 ++++-- dict/dclose.c | 8 +++--- dict/dicttest.c | 22 ++++++++++------- dict/drdwr.c | 6 ++--- dict/insert.c | 74 +++++++++++++++++++++++++------------------------------ dict/open.c | 12 +++++---- include/dict.h | 7 ++++-- 7 files changed, 71 insertions(+), 65 deletions(-) diff --git a/dict/close.c b/dict/close.c index 2d499d6..97e7035 100644 --- a/dict/close.c +++ b/dict/close.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: close.c,v $ - * Revision 1.2 1994-08-17 13:32:19 adam + * Revision 1.3 1994-08-18 12:40:52 adam + * Some development of dictionary. Not finished at all! + * + * Revision 1.2 1994/08/17 13:32:19 adam * Use cache in dict - not in bfile. * * Revision 1.1 1994/08/16 16:26:47 adam @@ -24,7 +27,7 @@ int dict_close (Dict dict) assert (dict); dict_bf_close (dict->dbf); - free (dict); + xfree (dict); return 0; } diff --git a/dict/dclose.c b/dict/dclose.c index 701b0cb..5a7a65e 100644 --- a/dict/dclose.c +++ b/dict/dclose.c @@ -11,10 +11,10 @@ int dict_bf_close (Dict_BFile dbf) int i; dict_bf_flush_blocks (dbf, -1); - free (dbf->all_blocks); - free (dbf->all_data); - free (dbf->hash_array); + xfree (dbf->all_blocks); + xfree (dbf->all_data); + xfree (dbf->hash_array); i = bf_close (dbf->bf); - free (dbf); + xfree (dbf); return i; } diff --git a/dict/dicttest.c b/dict/dicttest.c index 86a733d..0a2ad73 100644 --- a/dict/dicttest.c +++ b/dict/dicttest.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: dicttest.c,v $ - * Revision 1.1 1994-08-16 16:26:47 adam + * Revision 1.2 1994-08-18 12:40:54 adam + * Some development of dictionary. Not finished at all! + * + * Revision 1.1 1994/08/16 16:26:47 adam * Added dict. * */ @@ -14,7 +17,6 @@ #include #include -#include char *prog; Dict dict; @@ -26,10 +28,10 @@ int main (int argc, char **argv) int rw = 0; int cache = 10; int ret; - int verbose = 0; char *arg; prog = argv[0]; + log_init (LOG_DEFAULT_LEVEL, prog, NULL); if (argc < 2) { fprintf (stderr, "usage:\n" @@ -42,7 +44,7 @@ int main (int argc, char **argv) { if (name) { - fprintf (stderr, "%s: too many files specified\n", prog); + log (LOG_FATAL, "too many files specified\n"); exit (1); } name = arg; @@ -61,22 +63,24 @@ int main (int argc, char **argv) rw = 1; } else if (ret == 'v') - verbose = atoi(arg); + { + log_init (atoi(arg), prog, NULL); + } else { - fprintf (stderr, "%s: unknown option\n", prog); + log (LOG_FATAL, "unknown option"); exit (1); } } if (!name) { - fprintf (stderr, "%s: no dictionary file given\n", prog); + log (LOG_FATAL, "no dictionary file given"); exit (1); } dict = dict_open (name, cache, rw); if (!dict) { - fprintf (stderr, "%s: dict_open fail\n", prog); + log (LOG_FATAL, "dict_open fail"); exit (1); } if (inputfile) @@ -88,7 +92,7 @@ int main (int argc, char **argv) if (!(ipf = fopen(inputfile, "r"))) { - fprintf (stderr, "%s: cannot open %s\n", prog, inputfile); + log (LOG_FATAL|LOG_ERRNO, "cannot open %s", inputfile); exit (1); } diff --git a/dict/drdwr.c b/dict/drdwr.c index 8bf642b..3869dcb 100644 --- a/dict/drdwr.c +++ b/dict/drdwr.c @@ -121,7 +121,7 @@ static void move_to_front (Dict_BFile bf, struct Dict_file_block *p) bf->lru_front = p; } -int bf_readp (Dict_BFile bf, int no, void **bufp) +int dict_bf_readp (Dict_BFile bf, int no, void **bufp) { struct Dict_file_block *p; int i; @@ -145,7 +145,7 @@ int bf_readp (Dict_BFile bf, int no, void **bufp) return i; } -int bf_newp (Dict_BFile bf, int no, void **bufp) +int dict_bf_newp (Dict_BFile bf, int no, void **bufp) { struct Dict_file_block *p; if (!(p = find_block (bf, no))) @@ -162,7 +162,7 @@ int bf_newp (Dict_BFile bf, int no, void **bufp) return 1; } -int bf_touch (Dict_BFile bf, int no) +int dict_bf_touch (Dict_BFile bf, int no) { struct Dict_file_block *p; if ((p = find_block (bf, no))) diff --git a/dict/insert.c b/dict/insert.c index 8d1cffb..2d34580 100644 --- a/dict/insert.c +++ b/dict/insert.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: insert.c,v $ - * Revision 1.2 1994-08-17 13:32:19 adam + * Revision 1.3 1994-08-18 12:40:56 adam + * Some development of dictionary. Not finished at all! + * + * Revision 1.2 1994/08/17 13:32:19 adam * Use cache in dict - not in bfile. * * Revision 1.1 1994/08/16 16:26:48 adam @@ -41,7 +44,7 @@ static Dict_ptr new_page (Dict dict, Dict_ptr back_ptr, void **pp) DICT_backptr(p) = back_ptr; DICT_nextptr(p) = 0; DICT_nodir(p) = 0; - DICT_size(p) = 0; + DICT_size(p) = DICT_infoffset; *pp = p; return ptr; } @@ -49,22 +52,24 @@ static Dict_ptr new_page (Dict dict, Dict_ptr back_ptr, void **pp) static int dict_ins (Dict dict, const Dict_char *str, Dict_ptr back_ptr, void *p, void *userinfo) { + int i; Dict_ptr ptr = back_ptr, subptr; - short *indxp, *indxp1, *indxp2; + short *indxp, *indxp1; short newsize; + if (ptr == 0) ptr = new_page (dict, back_ptr, &p); assert (p); assert (ptr); indxp = (short*) ((char*) p+DICT_PAGESIZE); - while (*str != DICT_EOS) + for (i = DICT_nodir (p); --i >= 0; ) { char *info; + int cmp; if (*--indxp > 0) /* tail string here! */ { - int cmp; - info = DICT_info(p) + *indxp; + info = p + *indxp; cmp = dict_strcmp ((Dict_char*) (info+sizeof(Dict_info)+sizeof(Dict_ptr)), str); @@ -79,14 +84,13 @@ static int dict_ins (Dict dict, const Dict_char *str, Dict_ptr back_ptr, } else if(cmp < 0) break; - } - else if(*indxp < 0) /* tail of string in sub page */ + else /* tail of string in sub page */ { - int cmp; - info = DICT_info(p) - *indxp; + assert (*indxp < 0); + info = p - *indxp; cmp = memcmp (info+sizeof(Dict_info)+sizeof(Dict_ptr), str, - sizeof(Dict_char)); + sizeof(Dict_char)); if (!cmp) { Dict_ptr subptr; @@ -103,43 +107,33 @@ static int dict_ins (Dict dict, const Dict_char *str, Dict_ptr back_ptr, else if(cmp < 0) break; } - else - break; } newsize = DICT_size(p); subptr = 0; - memcpy (DICT_info(p) + newsize, &subptr, sizeof(subptr)); - memcpy (DICT_info(p) + newsize + sizeof(Dict_ptr), userinfo, - sizeof(Dict_info)); - memcpy (DICT_info(p) + newsize + sizeof(Dict_ptr)+sizeof(Dict_info), - str, dict_strlen (str)); - newsize = DICT_size(p) + - sizeof(Dict_info) + sizeof(Dict_ptr) + dict_strlen (str); - DICT_size (p) = newsize; - - DICT_nodir(p) = DICT_nodir(p)+1; - indxp2 = (short*)((char*) p + DICT_PAGESIZE - DICT_nodir(p)*sizeof(short)); - for (indxp1 = indxp2; indxp1 != indxp; indxp1++) - indxp[0] = indxp[1]; + memcpy (p+newsize, &subptr, sizeof(subptr)); + memcpy (p+newsize + sizeof(Dict_ptr), userinfo, sizeof(Dict_info)); + memcpy (p+newsize + sizeof(Dict_ptr)+sizeof(Dict_info), str, + dict_strlen (str)+1); + (DICT_nodir(p))++; + indxp1 = (short*)((char*) p + DICT_PAGESIZE - DICT_nodir(p)*sizeof(short)); + for (; indxp1 != indxp; indxp1++) + indxp1[0] = indxp1[1]; *indxp = -newsize; + + DICT_size(p) = newsize + sizeof(Dict_info)+sizeof(Dict_ptr) + +dict_strlen (str)+1; return 0; } int dict_insert (Dict dict, const Dict_char *str, void *userinfo) { - dict_ins (dict, str, 0, NULL, userinfo); + void *p; + if (dict->head.last == 1) + dict_ins (dict, str, 0, NULL, userinfo); + else + { + dict_bf_readp (dict->dbf, 1, &p); + dict_ins (dict, str, 1, p, userinfo); + } return 0; } - - - - - - - - - - - - - diff --git a/dict/open.c b/dict/open.c index 8068454..6ead607 100644 --- a/dict/open.c +++ b/dict/open.c @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: open.c,v $ - * Revision 1.2 1994-08-17 13:32:20 adam + * Revision 1.3 1994-08-18 12:40:58 adam + * Some development of dictionary. Not finished at all! + * + * Revision 1.2 1994/08/17 13:32:20 adam * Use cache in dict - not in bfile. * * Revision 1.1 1994/08/16 16:26:49 adam @@ -12,7 +15,6 @@ * */ - #include #include #include @@ -31,7 +33,7 @@ Dict dict_open (const char *name, int cache, int rw) if(!dict->dbf) { - free (dict); + xfree (dict); return NULL; } if (dict_bf_readp (dict->dbf, 0, &head_buf) <= 0) @@ -57,13 +59,13 @@ Dict dict_open (const char *name, int cache, int rw) if (!strcmp (dh->magic_str, DICT_MAGIC)) { dict_bf_close (dict->dbf); - free (dict); + xfree (dict); return NULL; } if (dh->page_size != DICT_PAGESIZE) { dict_bf_close (dict->dbf); - free (dict); + xfree (dict); return NULL; } memcpy (&dict->head, dh, sizeof(*dh)); diff --git a/include/dict.h b/include/dict.h index 8ca8378..df5cc28 100644 --- a/include/dict.h +++ b/include/dict.h @@ -4,7 +4,10 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: dict.h,v $ - * Revision 1.2 1994-08-17 13:32:33 adam + * Revision 1.3 1994-08-18 12:41:12 adam + * Some development of dictionary. Not finished at all! + * + * Revision 1.2 1994/08/17 13:32:33 adam * Use cache in dict - not in bfile. * * Revision 1.1 1994/08/16 16:26:53 adam @@ -82,7 +85,7 @@ int dict_strlen (const Dict_char *s); #define DICT_nextptr(x) 2[(Dict_ptr*) x] #define DICT_nodir(x) 0[(short*)((char*)(x)+3*sizeof(Dict_ptr))] #define DICT_size(x) 1[(short*)((char*)(x)+3*sizeof(Dict_ptr))] -#define DICT_info(x) ((char*)(x)+3*sizeof(Dict_ptr)+2*sizeof(short)) +#define DICT_infoffset (3*sizeof(Dict_ptr)+2*sizeof(short)) #define DICT_to_str(x) sizeof(Dict_info)+sizeof(Dict_ptr) -- 1.7.10.4