5689cc4c5c63ac795c77e5cdebe5a018aba43c07
[idzebra-moved-to-github.git] / dict / open.c
1 /* $Id: open.c,v 1.19.2.2 2006-12-05 21:14:40 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <yaz/xmalloc.h>
27 #include <dict.h>
28
29 Dict dict_open (BFiles bfs, const char *name, int cache, int rw,
30                 int compact_flag)
31 {
32     Dict dict;
33     void *head_buf;
34     char resource_str[80];
35     int page_size;
36
37     dict = (Dict) xmalloc (sizeof(*dict));
38
39     if (cache < 5)
40         cache = 5;
41     sprintf (resource_str, "dict.%s.pagesize", name);
42
43     dict->grep_cmap = NULL;
44     page_size = DICT_DEFAULT_PAGESIZE;
45     if (page_size < 2048)
46     {
47         yaz_log(YLOG_WARN, "Resource %s was too small. Set to 2048",
48               resource_str);
49         page_size = 2048;
50     }
51     dict->dbf = dict_bf_open (bfs, name, page_size, cache, rw);
52     dict->rw = rw;
53
54     if(!dict->dbf)
55     {
56         yaz_log(YLOG_WARN, "Cannot open `%s'", name);
57         xfree (dict);
58         return NULL;
59     }
60     if (dict_bf_readp (dict->dbf, 0, &head_buf) <= 0)
61     {
62         memset (dict->head.magic_str, 0, sizeof(dict->head.magic_str));
63         strcpy (dict->head.magic_str, DICT_MAGIC);
64         dict->head.last = 1;
65         dict->head.root = 0;
66         dict->head.freelist = 0;
67         dict->head.page_size = page_size;
68         dict->head.compact_flag = compact_flag;
69         
70         /* create header with information (page 0) */
71         if (rw) 
72             dict_bf_newp (dict->dbf, 0, &head_buf, page_size);
73     }
74     else /* header was there, check magic and page size */
75     {
76         memcpy (&dict->head, head_buf, sizeof(dict->head));
77         if (strcmp (dict->head.magic_str, DICT_MAGIC))
78         {
79             yaz_log(YLOG_WARN, "Bad magic of `%s'", name);
80             exit (1);
81         }
82         if (dict->head.page_size != page_size)
83         {
84             yaz_log(YLOG_WARN, "Resource %s is %d and pagesize of `%s' is %d",
85                   resource_str, page_size, name, dict->head.page_size);
86             return 0;
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 }