Moved zebrautl.h to idzebra/util.h.
[idzebra-moved-to-github.git] / rset / rsisamb.c
1 /* $Id: rsisamb.c,v 1.30 2005-03-30 09:25:24 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <idzebra/util.h>
26 #include <rset.h>
27 #include <string.h>
28
29
30 static RSFD r_open (RSET ct, int flag);
31 static void r_close (RSFD rfd);
32 static void r_delete (RSET ct);
33 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf);
34 static void r_pos (RSFD rfd, double *current, double *total);
35 static int r_read (RSFD rfd, void *buf, TERMID *term);
36 static int r_write (RSFD rfd, const void *buf);
37
38 static const struct rset_control control = 
39 {
40     "isamb",
41     r_delete,
42     rset_get_one_term,
43     r_open,
44     r_close,
45     r_forward, 
46     r_pos,
47     r_read,
48     r_write,
49 };
50
51 const struct rset_control *rset_kind_isamb = &control;
52
53 struct rset_pp_info {
54     ISAMB_PP pt;
55     void *buf;
56 };
57
58 struct rset_isamb_info {
59     ISAMB   is;
60     ISAMB_P pos;
61 };
62
63 static int log_level = 0;
64 static int log_level_initialized = 0;
65
66 RSET rsisamb_create( NMEM nmem, const struct key_control *kcontrol, int scope,
67             ISAMB is, ISAMB_P pos, TERMID term)
68 {
69     RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, term);
70     struct rset_isamb_info *info;
71     if (!log_level_initialized)
72     {
73         log_level = yaz_log_module_level("rsisamb");
74         log_level_initialized = 1;
75     }
76     info = (struct rset_isamb_info *) nmem_malloc(rnew->nmem,sizeof(*info));
77     info->is = is;
78     info->pos = pos;
79     rnew->priv = info;
80     yaz_log(log_level,"rsisamb_create");
81     return rnew;
82 }
83
84 static void r_delete (RSET ct)
85 {
86     yaz_log(log_level, "rsisamb_delete");
87 }
88
89 RSFD r_open (RSET ct, int flag)
90 {
91     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->priv;
92     RSFD rfd;
93     struct rset_pp_info *ptinfo;
94
95     if (flag & RSETF_WRITE)
96     {
97         yaz_log(YLOG_FATAL, "ISAMB set type is read-only");
98         return NULL;
99     }
100     rfd = rfd_create_base(ct);
101     if (rfd->priv)
102         ptinfo = (struct rset_pp_info *) (rfd->priv);
103     else {
104         ptinfo = (struct rset_pp_info *)nmem_malloc(ct->nmem,sizeof(*ptinfo));
105         ptinfo->buf = nmem_malloc (ct->nmem,ct->keycontrol->key_size);
106         rfd->priv = ptinfo;
107     }
108     ptinfo->pt = isamb_pp_open (info->is, info->pos, ct->scope );
109     yaz_log(log_level,"rsisamb_open");
110     return rfd;
111 }
112
113 static void r_close (RSFD rfd)
114 {
115     struct rset_pp_info *ptinfo = (struct rset_pp_info *)(rfd->priv);
116     isamb_pp_close (ptinfo->pt);
117     rfd_delete_base(rfd);
118     yaz_log(log_level,"rsisamb_close");
119 }
120
121
122 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf)
123 {
124     struct rset_pp_info *pinfo = (struct rset_pp_info *)(rfd->priv);
125     int rc;
126     rc = isamb_pp_forward(pinfo->pt, buf, untilbuf);
127     if (rc && term)
128         *term = rfd->rset->term;
129     yaz_log(log_level,"rsisamb_forward");
130     return rc; 
131 }
132
133 static void r_pos (RSFD rfd, double *current, double *total)
134 {
135     struct rset_pp_info *pinfo = (struct rset_pp_info *)(rfd->priv);
136     assert(rfd);
137     isamb_pp_pos(pinfo->pt, current, total);
138     yaz_log(log_level,"isamb.r_pos returning %0.1f/%0.1f",
139               *current, *total);
140 }
141
142 static int r_read (RSFD rfd, void *buf, TERMID *term)
143 {
144     struct rset_pp_info *pinfo = (struct rset_pp_info *)(rfd->priv);
145     int rc;
146     rc = isamb_pp_read(pinfo->pt, buf);
147     if (rc && term)
148         *term = rfd->rset->term;
149     yaz_log(log_level,"isamb.r_read ");
150     return rc;
151 }
152
153 static int r_write (RSFD rfd, const void *buf)
154 {
155     yaz_log(YLOG_FATAL, "ISAMB set type is read-only");
156     return -1;
157 }