2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-09-06 16:11:56 adam
8 * More work on boolean sets.
10 * Revision 1.5 1995/09/05 16:36:59 adam
13 * Revision 1.4 1995/09/05 11:43:24 adam
14 * Complete version of temporary sets. Not tested yet though.
16 * Revision 1.3 1995/09/04 15:20:40 adam
17 * More work on temp sets. is_open member removed.
19 * Revision 1.2 1995/09/04 09:10:56 adam
22 * Revision 1.1 1994/11/04 13:21:30 quinn
30 #include <sys/types.h>
36 static struct rset_control *r_create(const struct rset_control *sel,
38 static int r_open(struct rset_control *ct, int wflag);
39 static void r_close(struct rset_control *ct);
40 static void r_delete(struct rset_control *ct);
41 static void r_rewind(struct rset_control *ct);
42 static int r_count(struct rset_control *ct);
43 static int r_read (rset_control *ct, void *buf);
44 static int r_write (rset_control *ct, const void *buf);
46 static const rset_control control =
60 const rset_control *rset_kind_temp = &control;
62 struct rset_temp_private {
75 static struct rset_control *r_create(const struct rset_control *sel,
79 rset_temp_parms *temp_parms = parms;
80 struct rset_temp_private *info;
82 logf (LOG_DEBUG, "ritemp_create(%s)", sel->desc);
83 newct = xmalloc(sizeof(*newct));
84 memcpy(newct, sel, sizeof(*sel));
85 newct->buf = xmalloc (sizeof(struct rset_temp_private));
90 info->key_size = temp_parms->key_size;
91 info->buf_size = 1024;
92 info->buf_mem = xmalloc (info->buf_size);
101 static int r_open(struct rset_control *ct, int wflag)
103 struct rset_temp_private *info = ct->buf;
105 assert (info->fd == -1);
109 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
111 info->fd = open (info->fname, O_RDONLY);
114 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
122 static void r_flush (struct rset_control *ct, int mk)
124 struct rset_temp_private *info = ct->buf;
126 if (!info->fname && mk)
128 char *s = (char*) tempnam (NULL, "zrs");
130 info->fname = xmalloc (strlen(s)+1);
131 strcpy (info->fname, s);
133 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
136 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
140 if (info->fname && info->fd != -1 && info->dirty)
144 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
146 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
149 count = info->buf_size;
150 if (count > info->pos_end - info->pos_buf)
151 count = info->pos_end - info->pos_buf;
152 if ((r = write (info->fd, info->buf_mem, count)) < count)
155 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
157 logf (LOG_FATAL, "write of %ld but got %ld",
158 (long) count, (long) r);
165 static void r_close (struct rset_control *ct)
167 struct rset_temp_private *info = ct->buf;
170 if (info->fname && info->fd != -1)
177 static void r_delete (struct rset_control *ct)
179 struct rset_temp_private *info = ct->buf;
183 unlink (info->fname);
184 free (info->buf_mem);
189 static void r_reread (struct rset_control *ct)
191 struct rset_temp_private *info = ct->buf;
197 info->pos_border = info->pos_cur + info->buf_size;
198 if (info->pos_border > info->pos_end)
199 info->pos_border = info->pos_end;
200 count = info->pos_border - info->pos_buf;
202 if ((r = read (info->fd, info->buf_mem, count)) < count)
205 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
207 logf (LOG_FATAL, "read of %ld but got %ld",
208 (long) count, (long) r);
213 info->pos_border = info->pos_end;
216 static void r_rewind (struct rset_control *ct)
218 struct rset_temp_private *info = ct->buf;
226 static int r_count (struct rset_control *ct)
228 struct rset_temp_private *info = ct->buf;
230 return info->pos_end / info->key_size;
233 static int r_read (rset_control *ct, void *buf)
235 struct rset_temp_private *info = ct->buf;
236 size_t nc = info->pos_cur + info->key_size;
238 if (nc > info->pos_border)
240 if (nc > info->pos_end)
243 info->pos_buf = info->pos_cur;
246 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
252 static int r_write (rset_control *ct, const void *buf)
254 struct rset_temp_private *info = ct->buf;
255 size_t nc = info->pos_cur + info->key_size;
257 if (nc > info->pos_buf + info->buf_size)
260 info->pos_buf = info->pos_cur;
263 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
266 info->pos_border = info->pos_cur = nc;