Zebra with full functionality
[idzebra-moved-to-github.git] / rset / rsisamd.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsisamd.c,v $
7  * Revision 1.2  2002-04-05 08:46:26  adam
8  * Zebra with full functionality
9  *
10  * Revision 1.1  2001/01/16 19:17:18  heikki
11  * Added rsisamd.c
12  *
13  *
14  */
15
16
17 #include <stdio.h>
18 #include <assert.h>
19 #include <zebrautl.h>
20 #include <rsisamd.h>
21
22 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
23 static RSFD r_open (RSET ct, int flag);
24 static void r_close (RSFD rfd);
25 static void r_delete (RSET ct);
26 static void r_rewind (RSFD rfd);
27 static int r_count (RSET ct);
28 static int r_read (RSFD rfd, void *buf, int *term_index);
29 static int r_write (RSFD rfd, const void *buf);
30
31 static const struct rset_control control = 
32 {
33     "isamd",
34     r_create,
35     r_open,
36     r_close,
37     r_delete,
38     r_rewind,
39     r_count,
40     r_read,
41     r_write,
42 };
43
44 const struct rset_control *rset_kind_isamd = &control;
45
46 struct rset_pp_info {
47     ISAMD_PP pt;
48     struct rset_pp_info *next;
49     struct rset_isamd_info *info;
50 };
51
52 struct rset_isamd_info {
53     ISAMD   is;
54     ISAMD_P pos;
55     struct rset_pp_info *ispt_list;
56 };
57
58 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
59 {
60     rset_isamd_parms *pt = (rset_isamd_parms *) parms;
61     struct rset_isamd_info *info;
62
63     ct->flags |= RSET_FLAG_VOLATILE;
64     info = (struct rset_isamd_info *) xmalloc (sizeof(*info));
65     info->is = pt->is;
66     info->pos = pt->pos;
67     info->ispt_list = NULL;
68     ct->no_rset_terms = 1;
69     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
70     ct->rset_terms[0] = pt->rset_term;
71     return info;
72 }
73
74 RSFD r_open (RSET ct, int flag)
75 {
76     struct rset_isamd_info *info = (struct rset_isamd_info *) ct->buf;
77     struct rset_pp_info *ptinfo;
78
79     logf (LOG_DEBUG, "risamd_open");
80     if (flag & RSETF_WRITE)
81     {
82         logf (LOG_FATAL, "ISAMD set type is read-only");
83         return NULL;
84     }
85     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
86     ptinfo->next = info->ispt_list;
87     info->ispt_list = ptinfo;
88     ptinfo->pt = isamd_pp_open (info->is, info->pos);
89     ptinfo->info = info;
90     if (ct->rset_terms[0]->nn < 0)
91         ct->rset_terms[0]->nn = isamd_pp_num (ptinfo->pt);
92     return ptinfo;
93 }
94
95 static void r_close (RSFD rfd)
96 {
97     struct rset_isamd_info *info = ((struct rset_pp_info*) rfd)->info;
98     struct rset_pp_info **ptinfop;
99
100     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
101         if (*ptinfop == rfd)
102         {
103             isamd_pp_close ((*ptinfop)->pt);
104             *ptinfop = (*ptinfop)->next;
105             xfree (rfd);
106             return;
107         }
108     logf (LOG_FATAL, "r_close but no rfd match!");
109     assert (0);
110 }
111
112 static void r_delete (RSET ct)
113 {
114     struct rset_isamd_info *info = (struct rset_isamd_info *) ct->buf;
115
116     logf (LOG_DEBUG, "rsisamd_delete");
117     assert (info->ispt_list == NULL);
118     rset_term_destroy (ct->rset_terms[0]);
119     xfree (ct->rset_terms);
120     xfree (info);
121 }
122
123 static void r_rewind (RSFD rfd)
124 {   
125     logf (LOG_DEBUG, "rsisamd_rewind");
126     abort ();
127 }
128
129 static int r_count (RSET ct)
130 {
131     return 0;
132 }
133
134 static int r_read (RSFD rfd, void *buf, int *term_index)
135 {
136     *term_index = 0;
137     return isamd_pp_read( ((struct rset_pp_info*) rfd)->pt, buf);
138 }
139
140 static int r_write (RSFD rfd, const void *buf)
141 {
142     logf (LOG_FATAL, "ISAMD set type is read-only");
143     return -1;
144 }