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