String arg in lookup is const.
[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.5  1995-09-04 09:09:15  adam
8  * String arg in lookup is const.
9  *
10  * Revision 1.4  1994/10/05  12:16:51  adam
11  * Pagesize is a resource now.
12  *
13  * Revision 1.3  1994/09/26  10:17:25  adam
14  * Minor changes.
15  *
16  * Revision 1.2  1994/09/16  15:39:14  adam
17  * Initial code of lookup - not tested yet.
18  *
19  * Revision 1.1  1994/08/16  16:26:48  adam
20  * Added dict.
21  *
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <assert.h>
28
29 #include <dict.h>
30
31 static char *dict_look (Dict dict, const Dict_char *str)
32 {
33     Dict_ptr ptr = 1;
34     int mid, lo, hi;
35     int cmp;
36     void *p;
37     short *indxp;
38     char *info;
39
40     dict_bf_readp (dict->dbf, ptr, &p);
41     mid = lo = 0;
42     hi = DICT_nodir(p)-1;
43     indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));    
44     while (lo <= hi)
45     {
46         mid = (lo+hi)/2;
47         if (indxp[-mid] > 0)
48         {
49             /* string (Dict_char *) DICT_EOS terminated */
50             /* unsigned char        length of information */
51             /* char *               information */
52             info = (char*)p + indxp[-mid];
53             cmp = dict_strcmp((Dict_char*) info, str);
54             if (!cmp)
55                 return info+(dict_strlen (info)+1)*sizeof(Dict_char);
56         }
57         else
58         {
59             Dict_char dc;
60             Dict_ptr subptr;
61
62             /* Dict_ptr             subptr */
63             /* Dict_char            sub char */
64             /* unsigned char        length of information */
65             /* char *               information */
66             info = (char*)p - indxp[-mid];
67             memcpy (&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
68             cmp = dc- *str;
69             if (!cmp)
70             {
71                 memcpy (&subptr, info, sizeof(Dict_ptr));
72                 if (*++str == DICT_EOS)
73                 {
74                     if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
75                         return info+sizeof(Dict_ptr)+sizeof(Dict_char);
76                     return NULL;
77                 }
78                 else
79                 {
80                     if (subptr == 0)
81                         return NULL;
82                     ptr = subptr;
83                     dict_bf_readp (dict->dbf, ptr, &p);
84                     mid = lo = 0;
85                     hi = DICT_nodir(p)-1;
86                     indxp = (short*) ((char*) p+DICT_pagesize(dict)
87                                       -sizeof(short));
88                     continue;
89                 }
90             }
91         }
92         if (cmp < 0)
93             lo = mid+1;
94         else
95             hi = mid-1;
96     }
97     return NULL;
98 }
99
100 char *dict_lookup (Dict dict, const Dict_char *p)
101 {
102     if (dict->head.last == 1)
103         return NULL;
104     return dict_look (dict, p);
105 }
106
107