1 /* $Id: stream.c,v 1.2 2007-01-15 15:10:17 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
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
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
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 struct zebra_mem_control {
40 const char *record_int_buf;
44 struct zebra_ext_control {
50 static off_t zebra_mem_seek(struct ZebraRecStream *s, off_t offset)
52 struct zebra_mem_control *fc = (struct zebra_mem_control *) s->fh;
53 return (off_t) (fc->record_int_pos = offset);
56 static off_t zebra_mem_tell(struct ZebraRecStream *s)
58 struct zebra_mem_control *fc = (struct zebra_mem_control *) s->fh;
59 return (off_t) fc->record_int_pos;
62 static int zebra_mem_read(struct ZebraRecStream *s, char *buf, size_t count)
64 struct zebra_mem_control *fc = (struct zebra_mem_control *) s->fh;
65 int l = fc->record_int_len - fc->record_int_pos;
68 l = (l < (int) count) ? l : (int) count;
69 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
70 fc->record_int_pos += l;
74 static off_t zebra_mem_end(struct ZebraRecStream *s, off_t *offset)
76 struct zebra_mem_control *fc = (struct zebra_mem_control *) s->fh;
78 fc->offset_end = *offset;
79 return fc->offset_end;
82 static void zebra_mem_destroy(struct ZebraRecStream *s)
84 struct zebra_mem_control *fc = s->fh;
88 static int zebra_ext_read(struct ZebraRecStream *s, char *buf, size_t count)
90 struct zebra_ext_control *fc = (struct zebra_ext_control *) s->fh;
91 return read(fc->fd, buf, count);
94 static off_t zebra_ext_seek(struct ZebraRecStream *s, off_t offset)
96 struct zebra_ext_control *fc = (struct zebra_ext_control *) s->fh;
97 return lseek(fc->fd, offset + fc->record_offset, SEEK_SET);
100 static off_t zebra_ext_tell(struct ZebraRecStream *s)
102 struct zebra_ext_control *fc = (struct zebra_ext_control *) s->fh;
103 return lseek(fc->fd, 0, SEEK_CUR) - fc->record_offset;
106 static void zebra_ext_destroy(struct ZebraRecStream *s)
108 struct zebra_ext_control *fc = s->fh;
114 static off_t zebra_ext_end(struct ZebraRecStream *s, off_t *offset)
116 struct zebra_ext_control *fc = (struct zebra_ext_control *) s->fh;
118 fc->offset_end = *offset;
119 return fc->offset_end;
123 void zebra_create_stream_mem(struct ZebraRecStream *stream,
124 const char *buf, size_t sz)
126 struct zebra_mem_control *fc = xmalloc(sizeof(*fc));
127 fc->record_int_buf = buf;
128 fc->record_int_len = sz;
129 fc->record_int_pos = 0;
133 stream->readf = zebra_mem_read;
134 stream->seekf = zebra_mem_seek;
135 stream->tellf = zebra_mem_tell;
136 stream->endf = zebra_mem_end;
137 stream->destroy = zebra_mem_destroy;
140 void zebra_create_stream_fd(struct ZebraRecStream *stream,
141 int fd, off_t start_offset)
143 struct zebra_ext_control *fc = xmalloc(sizeof(*fc));
146 fc->record_offset = start_offset;
150 stream->readf = zebra_ext_read;
151 stream->seekf = zebra_ext_seek;
152 stream->tellf = zebra_ext_tell;
153 stream->endf = zebra_ext_end;
154 stream->destroy = zebra_ext_destroy;
155 zebra_ext_seek(stream, 0);
161 * indent-tabs-mode: nil
163 * vim: shiftwidth=4 tabstop=8 expandtab