depend include change.
[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.4  1994-09-01 17:44:10  adam
8  * depend include change.
9  * CVS ----------------------------------------------------------------------
10  *
11  * Revision 1.3  1994/08/18  12:40:58  adam
12  * Some development of dictionary. Not finished at all!
13  *
14  * Revision 1.2  1994/08/17  13:32:20  adam
15  * Use cache in dict - not in bfile.
16  *
17  * Revision 1.1  1994/08/16  16:26:49  adam
18  * Added dict.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25
26 #include <dict.h>
27
28 Dict dict_open (const char *name, int cache, int rw)
29 {
30     Dict dict;
31     void *head_buf;
32     struct Dict_head *dh;
33
34     dict = xmalloc (sizeof(*dict));
35
36     dict->dbf = dict_bf_open (name, DICT_PAGESIZE, cache, rw);
37     dict->rw = rw;
38
39     if(!dict->dbf)
40     {
41         log (LOG_LOG, "cannot open `%s'", name);
42         xfree (dict);
43         return NULL;
44     }
45     if (dict_bf_readp (dict->dbf, 0, &head_buf) <= 0)
46     {
47         if (rw) 
48         {   /* create header with information (page 0) */
49             dict_bf_newp (dict->dbf, 0, &head_buf);
50             dh = (struct Dict_head *) head_buf;
51             strcpy(dh->magic_str, DICT_MAGIC);
52             dh->free_list = dh->last = 1;
53             dh->page_size = DICT_PAGESIZE;
54             memcpy (&dict->head, dh, sizeof(*dh));
55         }
56         else
57         {   /* no header present, i.e. no dictionary at all */
58             dict->head.free_list = dict->head.last = 0;
59             dict->head.page_size = DICT_PAGESIZE;
60         }
61     }
62     else /* header was there, check magic and page size */
63     {
64         dh = (struct Dict_head *) head_buf;
65         if (strcmp (dh->magic_str, DICT_MAGIC))
66         {
67             log (LOG_LOG, "bad magic of `%s'", name);
68             dict_bf_close (dict->dbf);
69             xfree (dict);
70             return NULL;
71         }
72         if (dh->page_size != DICT_PAGESIZE)
73         {
74             log (LOG_LOG, "page size mismatch of `%s'", name);
75             dict_bf_close (dict->dbf);
76             xfree (dict);
77             return NULL;
78         }
79         memcpy (&dict->head, dh, sizeof(*dh));
80     }
81     return dict;
82 }
83
84 int dict_strcmp (const Dict_char *s1, const Dict_char *s2)
85 {
86     return strcmp (s1, s2);
87 }
88
89 int dict_strlen (const Dict_char *s)
90 {
91     return strlen(s);
92 }