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