Annotation of win32/gnome/gmime-x.x.x/gmime-stream.c, revision 1.1
1.1 ! paf 1: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
! 2: /*
! 3: * Authors: Jeffrey Stedfast <fejj@ximian.com>
! 4: *
! 5: * Copyright 2001 Ximian, Inc. (www.ximian.com)
! 6: *
! 7: * This program is free software; you can redistribute it and/or modify
! 8: * it under the terms of the GNU General Public License as published by
! 9: * the Free Software Foundation; either version 2 of the License, or
! 10: * (at your option) any later version.
! 11: *
! 12: * This program is distributed in the hope that it will be useful,
! 13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
! 14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 15: * GNU General Public License for more details.
! 16: *
! 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., 59 Temple Street #330, Boston, MA 02111-1307, USA.
! 20: *
! 21: */
! 22:
! 23:
! 24: #ifdef HAVE_CONFIG_H
! 25: #include <config.h>
! 26: #endif
! 27:
! 28:
! 29: #include "gmime-stream.h"
! 30: #include <string.h>
! 31:
! 32:
! 33: /**
! 34: * g_mime_stream_construct:
! 35: * @stream: stream
! 36: * @stream_template: stream template
! 37: * @type: stream type
! 38: * @start: start boundary
! 39: * @end: end boundary
! 40: *
! 41: * Initializes a new stream of type @type, using the virtual methods
! 42: * from @stream_template, with bounds @start and @end.
! 43: **/
! 44: void
! 45: g_mime_stream_construct (GMimeStream *stream, GMimeStream *stream_template, unsigned int type, off_t start, off_t end)
! 46: {
! 47: stream->super_stream = NULL;
! 48: stream->refcount = 1;
! 49: stream->type = type;
! 50:
! 51: stream->position = start;
! 52: stream->bound_start = start;
! 53: stream->bound_end = end;
! 54:
! 55: stream->destroy = stream_template->destroy;
! 56: stream->read = stream_template->read;
! 57: stream->write = stream_template->write;
! 58: stream->flush = stream_template->flush;
! 59: stream->close = stream_template->close;
! 60: stream->reset = stream_template->reset;
! 61: stream->seek = stream_template->seek;
! 62: stream->tell = stream_template->tell;
! 63: stream->eos = stream_template->eos;
! 64: stream->length = stream_template->length;
! 65: stream->substream = stream_template->substream;
! 66: }
! 67:
! 68:
! 69: /**
! 70: * g_mime_stream_read:
! 71: * @stream: stream
! 72: * @buf: buffer
! 73: * @len: buffer length
! 74: *
! 75: * Attempts to read up to @len bytes from @stream into @buf.
! 76: *
! 77: * Returns the number of bytes read or -1 on fail.
! 78: **/
! 79: ssize_t
! 80: g_mime_stream_read (GMimeStream *stream, char *buf, size_t len)
! 81: {
! 82: g_return_val_if_fail (stream != NULL, -1);
! 83: g_return_val_if_fail (buf != NULL, -1);
! 84:
! 85: return stream->read (stream, buf, len);
! 86: }
! 87:
! 88:
! 89: /**
! 90: * g_mime_stream_write:
! 91: * @stream: stream
! 92: * @buf: buffer
! 93: * @len: buffer length
! 94: *
! 95: * Attempts to write up to @len bytes of @buf to @stream.
! 96: *
! 97: * Returns the number of bytes written or -1 on fail.
! 98: **/
! 99: ssize_t
! 100: g_mime_stream_write (GMimeStream *stream, char *buf, size_t len)
! 101: {
! 102: g_return_val_if_fail (stream != NULL, -1);
! 103: g_return_val_if_fail (buf != NULL, -1);
! 104:
! 105: return stream->write (stream, buf, len);
! 106: }
! 107:
! 108:
! 109: /**
! 110: * g_mime_stream_flush:
! 111: * @stream: stream
! 112: *
! 113: * Sync's the stream to disk.
! 114: *
! 115: * Returns 0 on success or -1 on fail.
! 116: **/
! 117: int
! 118: g_mime_stream_flush (GMimeStream *stream)
! 119: {
! 120: g_return_val_if_fail (stream != NULL, -1);
! 121:
! 122: return stream->flush (stream);
! 123: }
! 124:
! 125:
! 126: /**
! 127: * g_mime_stream_close:
! 128: * @stream: stream
! 129: *
! 130: * Closes the stream.
! 131: *
! 132: * Returns 0 on success or -1 on fail.
! 133: **/
! 134: int
! 135: g_mime_stream_close (GMimeStream *stream)
! 136: {
! 137: g_return_val_if_fail (stream != NULL, -1);
! 138:
! 139: return stream->close (stream);
! 140: }
! 141:
! 142:
! 143: /**
! 144: * g_mime_stream_eos:
! 145: * @stream: stream
! 146: *
! 147: * Tests the end-of-stream indicator for @stream.
! 148: *
! 149: * Returns %TRUE on EOS or %FALSE otherwise.
! 150: **/
! 151: gboolean
! 152: g_mime_stream_eos (GMimeStream *stream)
! 153: {
! 154: g_return_val_if_fail (stream != NULL, TRUE);
! 155:
! 156: if (stream->bound_end != -1 && stream->position >= stream->bound_end)
! 157: return TRUE;
! 158:
! 159: return stream->eos (stream);
! 160: }
! 161:
! 162:
! 163: /**
! 164: * g_mime_stream_reset:
! 165: * @stream: stream
! 166: *
! 167: * Resets the stream.
! 168: *
! 169: * Returns 0 on success or -1 on fail.
! 170: **/
! 171: int
! 172: g_mime_stream_reset (GMimeStream *stream)
! 173: {
! 174: g_return_val_if_fail (stream != NULL, -1);
! 175:
! 176: return stream->reset (stream);
! 177: }
! 178:
! 179:
! 180: /**
! 181: * g_mime_stream_seek:
! 182: * @stream: stream
! 183: * @offset: positional offset
! 184: * @whence: seek directive
! 185: *
! 186: * Repositions the offset of the stream @stream to
! 187: * the argument @offset according to the
! 188: * directive @whence as follows:
! 189: *
! 190: * #GMIME_STREAM_SEEK_SET: The offset is set to @offset bytes.
! 191: *
! 192: * #GMIME_STREAM_SEEK_CUR: The offset is set to its current
! 193: * location plus @offset bytes.
! 194: *
! 195: * #GMIME_STREAM_SEEK_END: The offset is set to the size of the
! 196: * stream plus @offset bytes.
! 197: *
! 198: * Returns the resultant position on success or -1 on fail.
! 199: **/
! 200: off_t
! 201: g_mime_stream_seek (GMimeStream *stream, off_t offset, GMimeSeekWhence whence)
! 202: {
! 203: g_return_val_if_fail (stream != NULL, -1);
! 204:
! 205: return stream->seek (stream, offset, whence);
! 206: }
! 207:
! 208:
! 209: /**
! 210: * g_mime_stream_tell:
! 211: * @stream: stream
! 212: *
! 213: * Gets the current offset within the stream.
! 214: *
! 215: * Returns the current position within the stream or -1 on fail.
! 216: **/
! 217: off_t
! 218: g_mime_stream_tell (GMimeStream *stream)
! 219: {
! 220: g_return_val_if_fail (stream != NULL, -1);
! 221:
! 222: return stream->tell (stream);
! 223: }
! 224:
! 225:
! 226: /**
! 227: * g_mime_stream_length:
! 228: * @stream: stream
! 229: *
! 230: * Gets the length of the stream.
! 231: *
! 232: * Returns the length of the stream or -1 on fail.
! 233: **/
! 234: ssize_t
! 235: g_mime_stream_length (GMimeStream *stream)
! 236: {
! 237: g_return_val_if_fail (stream != NULL, -1);
! 238:
! 239: return stream->length (stream);
! 240: }
! 241:
! 242:
! 243: /**
! 244: * g_mime_stream_substream:
! 245: * @stream: stream
! 246: * @start: start boundary
! 247: * @end: end boundary
! 248: *
! 249: * Creates a new substream of @stream with bounds @start and @end.
! 250: *
! 251: * Returns a substream of @stream with bounds @start and @end.
! 252: **/
! 253: GMimeStream *
! 254: g_mime_stream_substream (GMimeStream *stream, off_t start, off_t end)
! 255: {
! 256: GMimeStream *sub;
! 257:
! 258: g_return_val_if_fail (stream != NULL, NULL);
! 259:
! 260: sub = stream->substream (stream, start, end);
! 261: sub->super_stream = stream;
! 262: g_mime_stream_ref (stream);
! 263:
! 264: return sub;
! 265: }
! 266:
! 267:
! 268: /**
! 269: * g_mime_stream_ref:
! 270: * @stream: stream
! 271: *
! 272: * Ref's a stream.
! 273: **/
! 274: void
! 275: g_mime_stream_ref (GMimeStream *stream)
! 276: {
! 277: g_return_if_fail (stream != NULL);
! 278:
! 279: stream->refcount++;
! 280: }
! 281:
! 282:
! 283: /**
! 284: * g_mime_stream_unref:
! 285: * @stream: stream
! 286: *
! 287: * Unref's a stream.
! 288: **/
! 289: void
! 290: g_mime_stream_unref (GMimeStream *stream)
! 291: {
! 292: g_return_if_fail (stream != NULL);
! 293:
! 294: if (stream->refcount <= 1) {
! 295: if (stream->super_stream)
! 296: g_mime_stream_unref (stream->super_stream);
! 297:
! 298: stream->destroy (stream);
! 299: } else {
! 300: stream->refcount--;
! 301: }
! 302: }
! 303:
! 304:
! 305: /**
! 306: * g_mime_stream_set_bounds:
! 307: * @stream: stream
! 308: * @start: start boundary
! 309: * @end: end boundary
! 310: *
! 311: * Sets the bounds on the stream @stream.
! 312: **/
! 313: void
! 314: g_mime_stream_set_bounds (GMimeStream *stream, off_t start, off_t end)
! 315: {
! 316: g_return_if_fail (stream != NULL);
! 317:
! 318: stream->bound_start = start;
! 319: stream->bound_end = end;
! 320:
! 321: if (stream->position < start)
! 322: stream->position = start;
! 323: else if (stream->position > end && end != -1)
! 324: stream->position = end;
! 325: }
! 326:
! 327:
! 328: /**
! 329: * g_mime_stream_write_string:
! 330: * @stream: stream
! 331: * @string: string to write
! 332: *
! 333: * Writes @string to @stream.
! 334: *
! 335: * Returns the number of bytes written or -1 on fail.
! 336: **/
! 337: ssize_t
! 338: g_mime_stream_write_string (GMimeStream *stream, const char *string)
! 339: {
! 340: g_return_val_if_fail (stream != NULL, -1);
! 341: g_return_val_if_fail (string != NULL, -1);
! 342:
! 343: return g_mime_stream_write (stream, (char *) string, strlen (string));
! 344: }
! 345:
! 346:
! 347: /**
! 348: * g_mime_stream_printf:
! 349: * @stream: stream
! 350: * @fmt: format
! 351: * @Varargs: arguments
! 352: *
! 353: * Write formatted output to a stream.
! 354: *
! 355: * Returns the number of bytes written or -1 on fail.
! 356: **/
! 357: ssize_t
! 358: g_mime_stream_printf (GMimeStream *stream, const char *fmt, ...)
! 359: {
! 360: va_list args;
! 361: char *string;
! 362: ssize_t ret;
! 363:
! 364: g_return_val_if_fail (stream != NULL, -1);
! 365: g_return_val_if_fail (fmt != NULL, -1);
! 366:
! 367: va_start (args, fmt);
! 368: string = g_strdup_vprintf (fmt, args);
! 369: va_end (args);
! 370:
! 371: if (!string)
! 372: return -1;
! 373:
! 374: ret = g_mime_stream_write (stream, string, strlen (string));
! 375: g_free (string);
! 376:
! 377: return ret;
! 378: }
! 379:
! 380:
! 381: /**
! 382: * g_mime_stream_write_to_stream:
! 383: * @src: source stream
! 384: * @dest: destination stream
! 385: *
! 386: * Attempts to write stream @src to stream @dest.
! 387: *
! 388: * Returns the number of bytes written or -1 on fail.
! 389: **/
! 390: ssize_t
! 391: g_mime_stream_write_to_stream (GMimeStream *src, GMimeStream *dest)
! 392: {
! 393: ssize_t nread, nwritten, total = 0;
! 394: char buf[4096];
! 395:
! 396: g_return_val_if_fail (src != NULL, -1);
! 397: g_return_val_if_fail (dest != NULL, -1);
! 398:
! 399: while (!g_mime_stream_eos (src)) {
! 400: nread = g_mime_stream_read (src, buf, sizeof (buf));
! 401: if (nread < 0)
! 402: return -1;
! 403:
! 404: if (nread > 0) {
! 405: nwritten = 0;
! 406: while (nwritten < nread) {
! 407: ssize_t len;
! 408:
! 409: len = g_mime_stream_write (dest, buf + nwritten, nread - nwritten);
! 410: if (len < 0)
! 411: return -1;
! 412:
! 413: nwritten += len;
! 414: }
! 415:
! 416: total += nwritten;
! 417: }
! 418: }
! 419:
! 420: return total;
! 421: }
! 422:
! 423:
! 424: /**
! 425: * g_mime_stream_writev:
! 426: * @stream: stream
! 427: * @vector: i/o vector
! 428: * @count: number of vector elements
! 429: *
! 430: * Writes at most @count blocks described by @vector to @stream.
! 431: *
! 432: * Returns the number of bytes written or -1 on fail.
! 433: **/
! 434: ssize_t
! 435: g_mime_stream_writev (GMimeStream *stream, IOVector *vector, size_t count)
! 436: {
! 437: ssize_t total = 0;
! 438: size_t i;
! 439:
! 440: for (i = 0; i < count; i++) {
! 441: ssize_t n, nwritten = 0;
! 442:
! 443: while (nwritten < vector[i].len) {
! 444: n = g_mime_stream_write (stream, (char*)vector[i].data + nwritten,
! 445: vector[i].len - nwritten);
! 446:
! 447: if (n == -1)
! 448: return -1;
! 449:
! 450: nwritten += n;
! 451: }
! 452:
! 453: total += nwritten;
! 454: }
! 455:
! 456: return total;
! 457: }
E-mail: