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