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