New parameter: result-set file descriptor (RSFD) to support multiple
[idzebra-moved-to-github.git] / rset / rsisam.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsisam.c,v $
7  * Revision 1.9  1995-09-07 13:58:43  adam
8  * New parameter: result-set file descriptor (RSFD) to support multiple
9  * positions within the same result-set.
10  * Boolean operators: and, or, not implemented.
11  *
12  * Revision 1.8  1995/09/06  16:11:56  adam
13  * More work on boolean sets.
14  *
15  * Revision 1.7  1995/09/06  10:35:44  adam
16  * Null set implemented.
17  *
18  * Revision 1.6  1995/09/05  11:43:24  adam
19  * Complete version of temporary sets. Not tested yet though.
20  *
21  * Revision 1.5  1995/09/04  12:33:56  adam
22  * Various cleanup. YAZ util used instead.
23  *
24  * Revision 1.4  1995/09/04  09:10:55  adam
25  * Minor changes.
26  *
27  * Revision 1.3  1994/11/22  13:15:37  quinn
28  * Simple
29  *
30  * Revision 1.2  1994/11/04  14:53:12  quinn
31  * Work
32  *
33  */
34
35 #include <stdio.h>
36 #include <assert.h>
37 #include <rsisam.h>
38 #include <alexutil.h>
39
40 static rset_control *r_create(const struct rset_control *sel, void *parms);
41 static RSFD r_open (rset_control *ct, int wflag);
42 static void r_close (RSFD rfd);
43 static void r_delete (rset_control *ct);
44 static void r_rewind (RSFD rfd);
45 static int r_count (rset_control *ct);
46 static int r_read (RSFD rfd, void *buf);
47 static int r_write (RSFD rfd, const void *buf);
48
49 static const rset_control control = 
50 {
51     "ISAM set type",
52     0,
53     r_create,
54     r_open,
55     r_close,
56     r_delete,
57     r_rewind,
58     r_count,
59     r_read,
60     r_write
61 };
62
63 const rset_control *rset_kind_isam = &control;
64
65 struct rset_ispt_info {
66     ISPT   pt;
67     struct rset_ispt_info *next;
68     struct rset_isam_info *info;
69 };
70
71 struct rset_isam_info {
72     ISAM   is;
73     ISAM_P pos;
74     struct rset_ispt_info *ispt_list;
75 };
76
77 static rset_control *r_create(const struct rset_control *sel, void *parms)
78 {
79     rset_control *newct;
80     rset_isam_parms *pt = parms;
81     struct rset_isam_info *info;
82
83     logf (LOG_DEBUG, "rsisam_create(%s)", sel->desc);
84     newct = xmalloc(sizeof(*newct));
85     memcpy(newct, sel, sizeof(*sel));
86
87     if (!(newct->buf = xmalloc (sizeof(struct rset_isam_info))))
88         return 0;
89     info = newct->buf;
90     info->is = pt->is;
91     info->pos = pt->pos;
92     info->ispt_list = NULL;
93     return newct;
94 }
95
96 RSFD r_open (rset_control *ct, int wflag)
97 {
98     struct rset_isam_info *info = ct->buf;
99     struct rset_ispt_info *ptinfo;
100
101     logf (LOG_DEBUG, "risam_open");
102     if (wflag)
103     {
104         logf (LOG_FATAL, "ISAM set type is read-only");
105         return NULL;
106     }
107     ptinfo = xmalloc (sizeof(*ptinfo));
108     ptinfo->next = info->ispt_list;
109     info->ispt_list = ptinfo;
110     ptinfo->pt = is_position (info->is, info->pos);
111     ptinfo->info = info;
112     return ptinfo;
113 }
114
115 static void r_close (RSFD rfd)
116 {
117     struct rset_isam_info *info = ((struct rset_ispt_info*) rfd)->info;
118     struct rset_ispt_info **ptinfop;
119
120     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
121         if (*ptinfop == rfd)
122         {
123             is_pt_free ((*ptinfop)->pt);
124             *ptinfop = (*ptinfop)->next;
125             free (rfd);
126             return;
127         }
128     logf (LOG_FATAL, "r_close but no rfd match!");
129     assert (0);
130 }
131
132 static void r_delete (rset_control *ct)
133 {
134     struct rset_isam_info *info = ct->buf;
135
136     logf (LOG_DEBUG, "rsisam_delete");
137     assert (info->ispt_list == NULL);
138     xfree (info);
139     xfree (ct);
140 }
141
142 static void r_rewind (RSFD rfd)
143 {   
144     logf (LOG_DEBUG, "rsisam_rewind");
145     is_rewind( ((struct rset_ispt_info*) rfd)->pt);
146 }
147
148 static int r_count (rset_control *ct)
149 {
150     return 0;
151 }
152
153 static int r_read (RSFD rfd, void *buf)
154 {
155     return is_readkey( ((struct rset_ispt_info*) rfd)->pt, buf);
156 }
157
158 static int r_write (RSFD rfd, const void *buf)
159 {
160     logf (LOG_FATAL, "ISAM set type is read-only");
161     return -1;
162 }