make checkergcc happy
[idzebra-moved-to-github.git] / dict / open.c
1 /*
2  * Copyright (C) 1994-2002, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: open.c,v $
7  * Revision 1.18  2002-05-03 13:47:15  adam
8  * make checkergcc happy
9  *
10  * Revision 1.17  2000/12/05 09:59:10  adam
11  * Work on dict_delete_subtree.
12  *
13  * Revision 1.16  1999/05/26 07:49:13  adam
14  * C++ compilation.
15  *
16  * Revision 1.15  1999/05/15 14:36:37  adam
17  * Updated dictionary. Implemented "compression" of dictionary.
18  *
19  * Revision 1.14  1999/03/09 13:07:06  adam
20  * Work on dict_compact routine.
21  *
22  * Revision 1.13  1999/02/02 14:50:27  adam
23  * Updated WIN32 code specific sections. Changed header.
24  *
25  * Revision 1.12  1997/09/17 12:19:07  adam
26  * Zebra version corresponds to YAZ version 1.4.
27  * Changed Zebra server so that it doesn't depend on global common_resource.
28  *
29  * Revision 1.11  1996/10/29 14:00:05  adam
30  * Page size given by DICT_DEFAULT_PAGESIZE in dict.h.
31  *
32  * Revision 1.10  1996/05/24 14:46:04  adam
33  * Added dict_grep_cmap function to define user-mapping in grep lookups.
34  *
35  * Revision 1.9  1996/02/02  13:43:51  adam
36  * The public functions simply use char instead of Dict_char to represent
37  * search strings. Dict_char is used internally only.
38  *
39  * Revision 1.8  1995/12/07  11:48:56  adam
40  * Insert operation obeys DICT_type = 1 (slack in page).
41  * Function dict_open exists if page size or magic aren't right.
42  *
43  * Revision 1.7  1995/09/04  12:33:32  adam
44  * Various cleanup. YAZ util used instead.
45  *
46  * Revision 1.6  1994/10/05  12:16:52  adam
47  * Pagesize is a resource now.
48  *
49  * Revision 1.5  1994/09/01  17:49:39  adam
50  * Removed stupid line. Work on insertion in dictionary. Not finished yet.
51  *
52  * Revision 1.4  1994/09/01  17:44:10  adam
53  * depend include change.
54  *
55  * Revision 1.3  1994/08/18  12:40:58  adam
56  * Some development of dictionary. Not finished at all!
57  *
58  * Revision 1.2  1994/08/17  13:32:20  adam
59  * Use cache in dict - not in bfile.
60  *
61  * Revision 1.1  1994/08/16  16:26:49  adam
62  * Added dict.
63  *
64  */
65
66 #include <stdlib.h>
67 #include <string.h>
68 #include <stdio.h>
69
70 #include <dict.h>
71
72 Dict dict_open (BFiles bfs, const char *name, int cache, int rw,
73                 int compact_flag)
74 {
75     Dict dict;
76     void *head_buf;
77     char resource_str[80];
78     int page_size;
79
80     dict = (Dict) xmalloc (sizeof(*dict));
81
82     if (cache < 5)
83         cache = 5;
84     sprintf (resource_str, "dict.%s.pagesize", name);
85
86     dict->grep_cmap = NULL;
87     page_size = DICT_DEFAULT_PAGESIZE;
88     if (page_size < 2048)
89     {
90         logf (LOG_WARN, "Resource %s was too small. Set to 2048",
91               resource_str);
92         page_size = 2048;
93     }
94     dict->dbf = dict_bf_open (bfs, name, page_size, cache, rw);
95     dict->rw = rw;
96
97     if(!dict->dbf)
98     {
99         logf (LOG_WARN, "Cannot open `%s'", name);
100         xfree (dict);
101         return NULL;
102     }
103     if (dict_bf_readp (dict->dbf, 0, &head_buf) <= 0)
104     {
105         memset (dict->head.magic_str, 0, sizeof(dict->head.magic_str));
106         strcpy (dict->head.magic_str, DICT_MAGIC);
107         dict->head.last = 1;
108         dict->head.root = 0;
109         dict->head.freelist = 0;
110         dict->head.page_size = page_size;
111         dict->head.compact_flag = compact_flag;
112         
113         /* create header with information (page 0) */
114         if (rw) 
115             dict_bf_newp (dict->dbf, 0, &head_buf, page_size);
116     }
117     else /* header was there, check magic and page size */
118     {
119         memcpy (&dict->head, head_buf, sizeof(dict->head));
120         if (strcmp (dict->head.magic_str, DICT_MAGIC))
121         {
122             logf (LOG_WARN, "Bad magic of `%s'", name);
123             exit (1);
124         }
125         if (dict->head.page_size != page_size)
126         {
127             logf (LOG_WARN, "Resource %s is %d and pagesize of `%s' is %d",
128                   resource_str, page_size, name, dict->head.page_size);
129             return 0;
130         }
131     }
132     if (dict->head.compact_flag)
133         dict_bf_compact(dict->dbf);
134     return dict;
135 }
136
137 int dict_strcmp (const Dict_char *s1, const Dict_char *s2)
138 {
139     return strcmp ((const char *) s1, (const char *) s2);
140 }
141
142 int dict_strncmp (const Dict_char *s1, const Dict_char *s2, size_t n)
143 {
144     return strncmp ((const char *) s1, (const char *) s2, n);
145 }
146
147 int dict_strlen (const Dict_char *s)
148 {
149     return strlen((const char *) s);
150 }