1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 struct zebra_mem_control {
37 const char *record_int_buf;
41 struct zebra_ext_control {
47 static off_t zebra_mem_seek(struct ZebraRecStream *s, off_t offset)
49 struct zebra_mem_control *fc = (struct zebra_mem_control *) s->fh;
50 return (off_t) (fc->record_int_pos = offset);
53 static off_t zebra_mem_tell(struct ZebraRecStream *s)
55 struct zebra_mem_control *fc = (struct zebra_mem_control *) s->fh;
56 return (off_t) fc->record_int_pos;
59 static int zebra_mem_read(struct ZebraRecStream *s, char *buf, size_t count)
61 struct zebra_mem_control *fc = (struct zebra_mem_control *) s->fh;
62 int l = fc->record_int_len - fc->record_int_pos;
65 l = (l < (int) count) ? l : (int) count;
66 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
67 fc->record_int_pos += l;
71 static off_t zebra_mem_end(struct ZebraRecStream *s, off_t *offset)
73 struct zebra_mem_control *fc = (struct zebra_mem_control *) s->fh;
75 fc->offset_end = *offset;
76 return fc->offset_end;
79 static void zebra_mem_destroy(struct ZebraRecStream *s)
81 struct zebra_mem_control *fc = s->fh;
85 static int zebra_ext_read(struct ZebraRecStream *s, char *buf, size_t count)
87 struct zebra_ext_control *fc = (struct zebra_ext_control *) s->fh;
88 return read(fc->fd, buf, count);
91 static off_t zebra_ext_seek(struct ZebraRecStream *s, off_t offset)
93 struct zebra_ext_control *fc = (struct zebra_ext_control *) s->fh;
94 return lseek(fc->fd, offset + fc->record_offset, SEEK_SET);
97 static off_t zebra_ext_tell(struct ZebraRecStream *s)
99 struct zebra_ext_control *fc = (struct zebra_ext_control *) s->fh;
100 return lseek(fc->fd, 0, SEEK_CUR) - fc->record_offset;
103 static void zebra_ext_destroy(struct ZebraRecStream *s)
105 struct zebra_ext_control *fc = s->fh;
111 static off_t zebra_ext_end(struct ZebraRecStream *s, off_t *offset)
113 struct zebra_ext_control *fc = (struct zebra_ext_control *) s->fh;
115 fc->offset_end = *offset;
116 return fc->offset_end;
120 void zebra_create_stream_mem(struct ZebraRecStream *stream,
121 const char *buf, size_t sz)
123 struct zebra_mem_control *fc = xmalloc(sizeof(*fc));
124 fc->record_int_buf = buf;
125 fc->record_int_len = sz;
126 fc->record_int_pos = 0;
130 stream->readf = zebra_mem_read;
131 stream->seekf = zebra_mem_seek;
132 stream->tellf = zebra_mem_tell;
133 stream->endf = zebra_mem_end;
134 stream->destroy = zebra_mem_destroy;
137 void zebra_create_stream_fd(struct ZebraRecStream *stream,
138 int fd, off_t start_offset)
140 struct zebra_ext_control *fc = xmalloc(sizeof(*fc));
143 fc->record_offset = start_offset;
147 stream->readf = zebra_ext_read;
148 stream->seekf = zebra_ext_seek;
149 stream->tellf = zebra_ext_tell;
150 stream->endf = zebra_ext_end;
151 stream->destroy = zebra_ext_destroy;
152 zebra_ext_seek(stream, 0);
158 * c-file-style: "Stroustrup"
159 * indent-tabs-mode: nil
161 * vim: shiftwidth=4 tabstop=8 expandtab