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