IDZEBRA  2.2.7
lookup.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) 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 
unsigned Dict_ptr
Definition: dict-p.h:34
#define DICT_EOS
Definition: dict-p.h:102
int dict_bf_readp(Dict_BFile bf, int no, void **bufp)
Definition: drdwr.c:188
int dict_strlen(const Dict_char *s)
Definition: open.c:118
int dict_strcmp(const Dict_char *s1, const Dict_char *s2)
Definition: open.c:108
#define DICT_bsize(x)
Definition: dict-p.h:105
unsigned char Dict_char
Definition: dict-p.h:33
#define DICT_nodir(x)
Definition: dict-p.h:106
static Dict dict
Definition: dicttest.c:35
char * dict_lookup(Dict dict, const char *p)
lookup item in dictionary
Definition: lookup.c:100
static char * dict_look(Dict dict, const Dict_char *str, Dict_ptr ptr)
Definition: lookup.c:32
Dict_ptr root
Definition: dict-p.h:40
zint no_lookup
Definition: dict-p.h:82
struct Dict_head head
Definition: dict-p.h:83
Dict_BFile dbf
Definition: dict-p.h:74