1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/*
Copyright (C) 2018, Rocky Bernstein <rocky@gnu.org>
Copyright (C) 2000, 2005 Herbert Valerio Riedel <hvr@gnu.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <cdio/cdio.h>
/* #define STREAM_DEBUG */
/* Public headers */
#include <libvcd/logging.h>
/* Private headers */
#include "vcd_assert.h"
#include "stream.h"
#include "util.h"
/*
* DataSource implementations
*/
struct _VcdDataSink {
void* user_data;
vcd_data_sink_io_functions op;
int is_open;
long position;
};
static void
_vcd_data_sink_open_if_necessary(VcdDataSink *obj)
{
vcd_assert (obj != NULL);
if (!obj->is_open) {
if (obj->op.open(obj->user_data))
vcd_error("could not opening output stream...");
else {
obj->is_open = 1;
obj->position = 0;
}
}
}
VcdDataSink*
vcd_data_sink_new(void *user_data, const vcd_data_sink_io_functions *funcs)
{
VcdDataSink *new_obj;
new_obj = calloc(1, sizeof(VcdDataSink));
new_obj->user_data = user_data;
memcpy(&(new_obj->op), funcs, sizeof(vcd_data_sink_io_functions));
return new_obj;
}
long
vcd_data_sink_seek(VcdDataSink* obj, long offset)
{
vcd_assert (obj != NULL);
_vcd_data_sink_open_if_necessary(obj);
if (obj->position != offset) {
vcd_warn("had to reposition DataSink from %ld to %ld!", obj->position, offset);
obj->position = offset;
return obj->op.seek(obj->user_data, offset);
}
return 0;
}
long
vcd_data_sink_write(VcdDataSink* obj, const void *ptr, long size, long nmemb)
{
long written;
vcd_assert (obj != NULL);
_vcd_data_sink_open_if_necessary(obj);
written = obj->op.write(obj->user_data, ptr, size*nmemb);
obj->position += written;
return written;
}
long
vcd_data_sink_printf (VcdDataSink *obj, const char format[], ...)
{
char buf[4096] = { 0, };
long retval;
int len;
va_list args;
va_start (args, format);
len = vsnprintf (buf, sizeof(buf), format, args);
if (len < 0 || len > (sizeof (buf) - 1))
vcd_error ("vsnprintf() returned %d", len);
retval = vcd_data_sink_write (obj, buf, 1, len);
va_end (args);
return retval;
}
void
vcd_data_sink_close(VcdDataSink* obj)
{
vcd_assert (obj != NULL);
if (obj->is_open) {
obj->op.close(obj->user_data);
obj->is_open = 0;
obj->position = 0;
}
}
void
vcd_data_sink_destroy(VcdDataSink* obj)
{
vcd_assert (obj != NULL);
vcd_data_sink_close(obj);
obj->op.free(obj->user_data);
}
/*
* DataSource implementations
*/
struct _VcdDataSource {
void* user_data;
vcd_data_source_io_functions op;
int is_open;
long position;
};
static void
_vcd_data_source_open_if_necessary(VcdDataSource_t *obj)
{
vcd_assert (obj != NULL);
if (!obj->is_open) {
if (obj->op.open(obj->user_data))
vcd_error ("could not opening input stream...");
else {
#ifdef STREAM_DEBUG
vcd_debug ("opened source...");
#endif
obj->is_open = 1;
obj->position = 0;
}
}
}
long
vcd_data_source_seek(VcdDataSource_t* p_obj, long offset)
{
vcd_assert (p_obj != NULL);
_vcd_data_source_open_if_necessary(p_obj);
if (p_obj->position != offset) {
#ifdef STREAM_DEBUG
vcd_warn("had to reposition DataSource from %ld to %ld!", p_obj->position,
offset);
#endif
p_obj->position = offset;
return p_obj->op.seek(p_obj->user_data, offset);
}
return 0;
}
VcdDataSource_t *
vcd_data_source_new(void *p_user_data,
const vcd_data_source_io_functions *funcs)
{
VcdDataSource_t *new_obj;
new_obj = calloc(1, sizeof (VcdDataSource_t));
new_obj->user_data = p_user_data;
memcpy(&(new_obj->op), funcs, sizeof(vcd_data_source_io_functions));
return new_obj;
}
/**
read size*nmemb bytes from obj into ptr
*/
long
vcd_data_source_read(VcdDataSource_t* obj, void *ptr, long size, long nmemb)
{
long read_bytes;
vcd_assert (obj != NULL);
_vcd_data_source_open_if_necessary(obj);
read_bytes = obj->op.read(obj->user_data, ptr, size*nmemb);
obj->position += read_bytes;
return read_bytes;
}
long
vcd_data_source_stat(VcdDataSource_t *p_obj)
{
vcd_assert (p_obj != NULL);
_vcd_data_source_open_if_necessary(p_obj);
return p_obj->op.stat(p_obj->user_data);
}
void
vcd_data_source_close(VcdDataSource_t *p_obj)
{
vcd_assert (p_obj != NULL);
if (p_obj->is_open) {
#ifdef STREAM_DEBUG
vcd_debug ("closed source...");
#endif
p_obj->op.close(p_obj->user_data);
p_obj->is_open = 0;
p_obj->position = 0;
}
}
void
vcd_data_source_destroy(VcdDataSource_t *p_obj)
{
vcd_assert (p_obj != NULL);
vcd_data_source_close(p_obj);
p_obj->op.free(p_obj->user_data);
}
/*
* Local variables:
* c-file-style: "gnu"
* tab-width: 8
* indent-tabs-mode: nil
* End:
*/
|