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