Reformat: delete trailing whitespace
[idzebra-moved-to-github.git] / dict / lookup.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <assert.h>
29
30 #include "dict-p.h"
31
32 static char *dict_look(Dict dict, const Dict_char *str, Dict_ptr ptr)
33 {
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_bsize(p)-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 ((Dict_char*) info)+1)
56                     *sizeof(Dict_char);
57         }
58         else
59         {
60             Dict_char dc;
61             Dict_ptr subptr;
62
63             /* Dict_ptr             subptr */
64             /* Dict_char            sub char */
65             /* unsigned char        length of information */
66             /* char *               information */
67             info = (char*)p - indxp[-mid];
68             memcpy(&dc, info+sizeof(Dict_ptr), sizeof(Dict_char));
69             cmp = dc- *str;
70             if (!cmp)
71             {
72                 memcpy(&subptr, info, sizeof(Dict_ptr));
73                 if (*++str == DICT_EOS)
74                 {
75                     if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
76                         return info+sizeof(Dict_ptr)+sizeof(Dict_char);
77                     return NULL;
78                 }
79                 else
80                 {
81                     if (subptr == 0)
82                         return NULL;
83                     ptr = subptr;
84                     dict_bf_readp(dict->dbf, ptr, &p);
85                     mid = lo = 0;
86                     hi = DICT_nodir(p)-1;
87                     indxp = (short*) ((char*) p+DICT_bsize(p)-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 char *p)
101 {
102     dict->no_lookup++;
103     if (!dict->head.root)
104         return NULL;
105     return dict_look(dict, (const Dict_char *) p, dict->head.root);
106 }
107 /*
108  * Local variables:
109  * c-basic-offset: 4
110  * c-file-style: "Stroustrup"
111  * indent-tabs-mode: nil
112  * End:
113  * vim: shiftwidth=4 tabstop=8 expandtab
114  */
115