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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Authors: Jeffrey Stedfast <fejj@ximian.com>
*
* Copyright 2001-2004 Ximian, Inc. (www.ximian.com)
*
* 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 Street #330, Boston, MA 02111-1307, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include "gmime-stream-null.h"
static void g_mime_stream_null_class_init (GMimeStreamNullClass *klass);
static void g_mime_stream_null_init (GMimeStreamNull *stream, GMimeStreamNullClass *klass);
static void g_mime_stream_null_finalize (GObject *object);
static ssize_t stream_read (GMimeStream *stream, char *buf, size_t len);
static ssize_t stream_write (GMimeStream *stream, char *buf, size_t len);
static int stream_flush (GMimeStream *stream);
static int stream_close (GMimeStream *stream);
static gboolean stream_eos (GMimeStream *stream);
static int stream_reset (GMimeStream *stream);
static off_t stream_seek (GMimeStream *stream, off_t offset, GMimeSeekWhence whence);
static off_t stream_tell (GMimeStream *stream);
static ssize_t stream_length (GMimeStream *stream);
static GMimeStream *stream_substream (GMimeStream *stream, off_t start, off_t end);
static GMimeStreamClass *parent_class = NULL;
GType
g_mime_stream_null_get_type (void)
{
static GType type = 0;
if (!type) {
static const GTypeInfo info = {
sizeof (GMimeStreamNullClass),
NULL, /* base_class_init */
NULL, /* base_class_finalize */
(GClassInitFunc) g_mime_stream_null_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GMimeStreamNull),
16, /* n_preallocs */
(GInstanceInitFunc) g_mime_stream_null_init,
};
type = g_type_register_static (GMIME_TYPE_STREAM, "GMimeStreamNull", &info, 0);
}
return type;
}
static void
g_mime_stream_null_class_init (GMimeStreamNullClass *klass)
{
GMimeStreamClass *stream_class = GMIME_STREAM_CLASS (klass);
GObjectClass *object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_ref (GMIME_TYPE_STREAM);
object_class->finalize = g_mime_stream_null_finalize;
stream_class->read = stream_read;
stream_class->write = stream_write;
stream_class->flush = stream_flush;
stream_class->close = stream_close;
stream_class->eos = stream_eos;
stream_class->reset = stream_reset;
stream_class->seek = stream_seek;
stream_class->tell = stream_tell;
stream_class->length = stream_length;
stream_class->substream = stream_substream;
}
static void
g_mime_stream_null_init (GMimeStreamNull *stream, GMimeStreamNullClass *klass)
{
stream->written = 0;
}
static void
g_mime_stream_null_finalize (GObject *object)
{
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static ssize_t
stream_read (GMimeStream *stream, char *buf, size_t len)
{
memset (buf, 0, len);
stream->position += len;
return len;
}
static ssize_t
stream_write (GMimeStream *stream, char *buf, size_t len)
{
GMimeStreamNull *null = (GMimeStreamNull *) stream;
null->written += len;
stream->position += len;
return len;
}
static int
stream_flush (GMimeStream *stream)
{
return 0;
}
static int
stream_close (GMimeStream *stream)
{
return 0;
}
static gboolean
stream_eos (GMimeStream *stream)
{
return TRUE;
}
static int
stream_reset (GMimeStream *stream)
{
GMimeStreamNull *null = (GMimeStreamNull *) stream;
null->written = 0;
return 0;
}
static off_t
stream_seek (GMimeStream *stream, off_t offset, GMimeSeekWhence whence)
{
GMimeStreamNull *null = (GMimeStreamNull *) stream;
off_t bound_end;
bound_end = stream->bound_end != -1 ? stream->bound_end : null->written;
switch (whence) {
case GMIME_STREAM_SEEK_SET:
stream->position = MIN (offset + stream->bound_start, bound_end);
break;
case GMIME_STREAM_SEEK_END:
stream->position = MAX (offset + bound_end, 0);
break;
case GMIME_STREAM_SEEK_CUR:
stream->position += offset;
if (stream->position < stream->bound_start)
stream->position = stream->bound_start;
else if (stream->position > bound_end)
stream->position = bound_end;
}
return stream->position;
}
static off_t
stream_tell (GMimeStream *stream)
{
return stream->position;
}
static ssize_t
stream_length (GMimeStream *stream)
{
GMimeStreamNull *null = GMIME_STREAM_NULL (stream);
off_t bound_end;
bound_end = stream->bound_end != -1 ? stream->bound_end : null->written;
return bound_end - stream->bound_start;
}
static GMimeStream *
stream_substream (GMimeStream *stream, off_t start, off_t end)
{
GMimeStreamNull *null;
null = g_object_new (GMIME_TYPE_STREAM_NULL, NULL, NULL);
g_mime_stream_construct (GMIME_STREAM (null), start, end);
return GMIME_STREAM (null);
}
/**
* g_mime_stream_null_new:
*
* Creates a new GMimeStreamNull object.
*
* Returns a new null stream (similar to /dev/null on Unix).
**/
GMimeStream *
g_mime_stream_null_new (void)
{
GMimeStreamNull *null;
null = g_object_new (GMIME_TYPE_STREAM_NULL, NULL, NULL);
g_mime_stream_construct (GMIME_STREAM (null), 0, -1);
return GMIME_STREAM (null);
}
|