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