Added dict.
[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.1  1994-08-16 16:26:49  adam
8  * Added dict.
9  *
10  */
11
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16
17 #include <dict.h>
18
19 Dict dict_open (const char *name, int cache, int rw)
20 {
21     Dict dict;
22     void *head_buf;
23     struct Dict_head *dh;
24
25     dict = xmalloc (sizeof(*dict));
26
27     if (rw)
28         dict->bf = bf_open_w (name, DICT_PAGESIZE, cache);
29     else
30         dict->bf = bf_open (name, DICT_PAGESIZE, cache);
31
32     if(!dict->bf)
33     {
34         free (dict);
35         return NULL;
36     }
37     if (bf_read (dict->bf, 0, &head_buf) <= 0)
38     {
39         if (rw) 
40         {   /* create header with information (page 0) */
41             bf_newp (dict->bf, 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             bf_close (dict->bf);
60             free (dict);
61             return NULL;
62         }
63         if (dh->page_size != DICT_PAGESIZE)
64         {
65             bf_close (dict->bf);
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 }