Insert operation obeys DICT_type = 1 (slack in page).
[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.8  1995-12-07 11:48:56  adam
8  * Insert operation obeys DICT_type = 1 (slack in page).
9  * Function dict_open exists if page size or magic aren't right.
10  *
11  * Revision 1.7  1995/09/04  12:33:32  adam
12  * Various cleanup. YAZ util used instead.
13  *
14  * Revision 1.6  1994/10/05  12:16:52  adam
15  * Pagesize is a resource now.
16  *
17  * Revision 1.5  1994/09/01  17:49:39  adam
18  * Removed stupid line. Work on insertion in dictionary. Not finished yet.
19  *
20  * Revision 1.4  1994/09/01  17:44:10  adam
21  * depend include change.
22  *
23  * Revision 1.3  1994/08/18  12:40:58  adam
24  * Some development of dictionary. Not finished at all!
25  *
26  * Revision 1.2  1994/08/17  13:32:20  adam
27  * Use cache in dict - not in bfile.
28  *
29  * Revision 1.1  1994/08/16  16:26:49  adam
30  * Added dict.
31  *
32  */
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37
38 #include <dict.h>
39
40 Dict dict_open (const char *name, int cache, int rw)
41 {
42     Dict dict;
43     void *head_buf;
44     struct Dict_head *dh;
45     char resource_str[80];
46     int page_size;
47
48     dict = xmalloc (sizeof(*dict));
49
50     sprintf (resource_str, "dict.%s.pagesize", name);
51
52     page_size = atoi (res_get_def (common_resource, resource_str, 
53                                    DICT_DEFAULT_PAGESIZE));
54     if (page_size < 2048)
55     {
56         logf (LOG_WARN, "Resource %s was too small. Set to 2048",
57               resource_str);
58         page_size = 2048;
59     }
60     dict->dbf = dict_bf_open (name, page_size, cache, rw);
61     dict->rw = rw;
62
63     if(!dict->dbf)
64     {
65         logf (LOG_WARN, "Cannot open `%s'", name);
66         xfree (dict);
67         return NULL;
68     }
69     if (dict_bf_readp (dict->dbf, 0, &head_buf) <= 0)
70     {
71         if (rw) 
72         {   /* create header with information (page 0) */
73             dict_bf_newp (dict->dbf, 0, &head_buf);
74             dh = (struct Dict_head *) head_buf;
75             strcpy(dh->magic_str, DICT_MAGIC);
76             dh->free_list = dh->last = 1;
77             dh->page_size = page_size;
78             memcpy (&dict->head, dh, sizeof(*dh));
79         }
80         else
81         {   /* no header present, i.e. no dictionary at all */
82             dict->head.free_list = dict->head.last = 0;
83             dict->head.page_size = page_size;
84         }
85     }
86     else /* header was there, check magic and page size */
87     {
88         dh = (struct Dict_head *) head_buf;
89         if (strcmp (dh->magic_str, DICT_MAGIC))
90         {
91             logf (LOG_WARN, "Bad magic of `%s'", name);
92             exit (1);
93         }
94         if (dh->page_size != page_size)
95         {
96             logf (LOG_WARN, "Resource %s is %d and pagesize of `%s' is %d",
97                   resource_str, page_size, name, dh->page_size);
98             exit (1);
99         }
100         memcpy (&dict->head, dh, sizeof(*dh));
101     }
102     return dict;
103 }
104
105 int dict_strcmp (const Dict_char *s1, const Dict_char *s2)
106 {
107     return strcmp (s1, s2);
108 }
109
110 int dict_strlen (const Dict_char *s)
111 {
112     return strlen(s);
113 }