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