Minor changes.
[idzebra-moved-to-github.git] / dict / lookup.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lookup.c,v $
7  * Revision 1.3  1994-09-26 10:17:25  adam
8  * Minor changes.
9  *
10  * Revision 1.2  1994/09/16  15:39:14  adam
11  * Initial code of lookup - not tested yet.
12  *
13  * Revision 1.1  1994/08/16  16:26:48  adam
14  * Added dict.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <assert.h>
22
23 #include <dict.h>
24
25 static char *dict_look (Dict dict, Dict_char *str)
26 {
27     Dict_ptr ptr = 1;
28     int mid, lo, hi;
29     int cmp;
30     void *p;
31     short *indxp;
32     char *info;
33
34     dict_bf_readp (dict->dbf, ptr, &p);
35     mid = lo = 0;
36     hi = DICT_nodir(p)-1;
37     indxp = (short*) ((char*) p+DICT_PAGESIZE-sizeof(short));    
38     while (lo <= hi)
39     {
40         mid = (lo+hi)/2;
41         if (indxp[-mid] > 0)
42         {
43             /* string (Dict_char *) DICT_EOS terminated */
44             /* unsigned char        length of information */
45             /* char *               information */
46             info = (char*)p + indxp[-mid];
47             cmp = dict_strcmp((Dict_char*) info, str);
48             if (!cmp)
49                 return info+(dict_strlen (info)+1)*sizeof(Dict_char);
50         }
51         else
52         {
53             Dict_char dc;
54             Dict_ptr subptr;
55
56             /* Dict_ptr             subptr */
57             /* Dict_char            sub char */
58             /* unsigned char        length of information */
59             /* char *               information */
60             info = (char*)p - indxp[-mid];
61             memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
62             cmp = dc- *str;
63             if (!cmp)
64             {
65                 memcpy (&subptr, info, sizeof(Dict_ptr));
66                 if (*++str == DICT_EOS)
67                 {
68                     if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
69                         return info+sizeof(Dict_ptr)+sizeof(Dict_char);
70                     return NULL;
71                 }
72                 else
73                 {
74                     if (subptr == 0)
75                         return NULL;
76                     ptr = subptr;
77                     dict_bf_readp (dict->dbf, ptr, &p);
78                     mid = lo = 0;
79                     hi = DICT_nodir(p)-1;
80                     indxp = (short*) ((char*) p+DICT_PAGESIZE-sizeof(short));
81                     continue;
82                 }
83             }
84         }
85         if (cmp < 0)
86             lo = mid+1;
87         else
88             hi = mid-1;
89     }
90     return NULL;
91 }
92
93 char *dict_lookup (Dict dict, Dict_char *p)
94 {
95     if (dict->head.last == 1)
96         return NULL;
97     return dict_look (dict, p);
98 }
99
100