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