Make internal lookup ec function static. Reformat a little
[idzebra-moved-to-github.git] / dict / open.c
1 /* $Id: open.c,v 1.26 2006-08-14 10:40:09 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "dict-p.h"
30
31 Dict dict_open (BFiles bfs, const char *name, int cache, int rw,
32                 int compact_flag, int page_size)
33 {
34     Dict dict;
35     void *head_buf;
36
37     dict = (Dict) xmalloc (sizeof(*dict));
38
39     if (cache < 5)
40         cache = 5;
41
42     dict->grep_cmap = NULL;
43     page_size = DICT_DEFAULT_PAGESIZE;
44     if (page_size < 2048)
45     {
46         yaz_log (YLOG_WARN, "Page size for dict %s %d<2048. Set to 2048",
47               name, page_size);
48         page_size = 2048;
49     }
50     dict->dbf = dict_bf_open (bfs, name, page_size, cache, rw);
51     dict->rw = rw;
52
53     if(!dict->dbf)
54     {
55         yaz_log (YLOG_WARN, "Cannot open `%s'", name);
56         xfree (dict);
57         return NULL;
58     }
59     if (dict_bf_readp (dict->dbf, 0, &head_buf) <= 0)
60     {
61         memset (dict->head.magic_str, 0, sizeof(dict->head.magic_str));
62         strcpy (dict->head.magic_str, DICT_MAGIC);
63         dict->head.last = 1;
64         dict->head.root = 0;
65         dict->head.freelist = 0;
66         dict->head.page_size = page_size;
67         dict->head.compact_flag = compact_flag;
68         
69         /* create header with information (page 0) */
70         if (rw) 
71             dict_bf_newp (dict->dbf, 0, &head_buf, page_size);
72     }
73     else /* header was there, check magic and page size */
74     {
75         memcpy (&dict->head, head_buf, sizeof(dict->head));
76         if (strcmp (dict->head.magic_str, DICT_MAGIC))
77         {
78             yaz_log (YLOG_WARN, "Bad magic of `%s'", name);
79             dict_bf_close(dict->dbf);
80             xfree(dict);
81             return 0;
82         }
83         if (dict->head.page_size != page_size)
84         {
85             yaz_log (YLOG_WARN, "Page size for existing dict %s is %d. Current is %d",
86                   name, dict->head.page_size, page_size);
87         }
88     }
89     if (dict->head.compact_flag)
90         dict_bf_compact(dict->dbf);
91     return dict;
92 }
93
94 int dict_strcmp (const Dict_char *s1, const Dict_char *s2)
95 {
96     return strcmp ((const char *) s1, (const char *) s2);
97 }
98
99 int dict_strncmp (const Dict_char *s1, const Dict_char *s2, size_t n)
100 {
101     return strncmp ((const char *) s1, (const char *) s2, n);
102 }
103
104 int dict_strlen (const Dict_char *s)
105 {
106     return strlen((const char *) s);
107 }
108 /*
109  * Local variables:
110  * c-basic-offset: 4
111  * indent-tabs-mode: nil
112  * End:
113  * vim: shiftwidth=4 tabstop=8 expandtab
114  */
115