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