Towards GPL
[idzebra-moved-to-github.git] / dict / open.c
1 /* $Id: open.c,v 1.19 2002-08-02 19:26:55 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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <dict.h>
30
31 Dict dict_open (BFiles bfs, const char *name, int cache, int rw,
32                 int compact_flag)
33 {
34     Dict dict;
35     void *head_buf;
36     char resource_str[80];
37     int page_size;
38
39     dict = (Dict) xmalloc (sizeof(*dict));
40
41     if (cache < 5)
42         cache = 5;
43     sprintf (resource_str, "dict.%s.pagesize", name);
44
45     dict->grep_cmap = NULL;
46     page_size = DICT_DEFAULT_PAGESIZE;
47     if (page_size < 2048)
48     {
49         logf (LOG_WARN, "Resource %s was too small. Set to 2048",
50               resource_str);
51         page_size = 2048;
52     }
53     dict->dbf = dict_bf_open (bfs, name, page_size, cache, rw);
54     dict->rw = rw;
55
56     if(!dict->dbf)
57     {
58         logf (LOG_WARN, "Cannot open `%s'", name);
59         xfree (dict);
60         return NULL;
61     }
62     if (dict_bf_readp (dict->dbf, 0, &head_buf) <= 0)
63     {
64         memset (dict->head.magic_str, 0, sizeof(dict->head.magic_str));
65         strcpy (dict->head.magic_str, DICT_MAGIC);
66         dict->head.last = 1;
67         dict->head.root = 0;
68         dict->head.freelist = 0;
69         dict->head.page_size = page_size;
70         dict->head.compact_flag = compact_flag;
71         
72         /* create header with information (page 0) */
73         if (rw) 
74             dict_bf_newp (dict->dbf, 0, &head_buf, page_size);
75     }
76     else /* header was there, check magic and page size */
77     {
78         memcpy (&dict->head, head_buf, sizeof(dict->head));
79         if (strcmp (dict->head.magic_str, DICT_MAGIC))
80         {
81             logf (LOG_WARN, "Bad magic of `%s'", name);
82             exit (1);
83         }
84         if (dict->head.page_size != page_size)
85         {
86             logf (LOG_WARN, "Resource %s is %d and pagesize of `%s' is %d",
87                   resource_str, page_size, name, dict->head.page_size);
88             return 0;
89         }
90     }
91     if (dict->head.compact_flag)
92         dict_bf_compact(dict->dbf);
93     return dict;
94 }
95
96 int dict_strcmp (const Dict_char *s1, const Dict_char *s2)
97 {
98     return strcmp ((const char *) s1, (const char *) s2);
99 }
100
101 int dict_strncmp (const Dict_char *s1, const Dict_char *s2, size_t n)
102 {
103     return strncmp ((const char *) s1, (const char *) s2, n);
104 }
105
106 int dict_strlen (const Dict_char *s)
107 {
108     return strlen((const char *) s);
109 }