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