annotate src/io/io_buf.c @ 0:d39e1d0d75b6

initial add
author paulo@hit-nxdomain.opendns.com
date Sat, 20 Feb 2010 21:18:28 -0800
parents
children
rev   line source
paulo@0 1 /*
paulo@0 2 * $Id: io_buf.c,v 1.2 2003/09/17 17:44:11 hipnod Exp $
paulo@0 3 *
paulo@0 4 * Copyright (C) 2003 giFT project (gift.sourceforge.net)
paulo@0 5 *
paulo@0 6 * This program is free software; you can redistribute it and/or modify it
paulo@0 7 * under the terms of the GNU General Public License as published by the
paulo@0 8 * Free Software Foundation; either version 2, or (at your option) any
paulo@0 9 * later version.
paulo@0 10 *
paulo@0 11 * This program is distributed in the hope that it will be useful, but
paulo@0 12 * WITHOUT ANY WARRANTY; without even the implied warranty of
paulo@0 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
paulo@0 14 * General Public License for more details.
paulo@0 15 */
paulo@0 16
paulo@0 17 #include "gt_gnutella.h"
paulo@0 18
paulo@0 19 #include "io_buf.h"
paulo@0 20
paulo@0 21 /*****************************************************************************/
paulo@0 22
paulo@0 23 struct io_buf *io_buf_new (size_t size)
paulo@0 24 {
paulo@0 25 struct io_buf *buf;
paulo@0 26 uint8_t *data;
paulo@0 27
paulo@0 28 if (!(data = gift_malloc (size + 1)))
paulo@0 29 return NULL;
paulo@0 30
paulo@0 31 if (!(buf = gift_malloc (sizeof (struct io_buf))))
paulo@0 32 {
paulo@0 33 free (data);
paulo@0 34 return NULL;
paulo@0 35 }
paulo@0 36
paulo@0 37 buf->data = data;
paulo@0 38 buf->size = size;
paulo@0 39 buf->r_offs = 0;
paulo@0 40 buf->w_offs = 0;
paulo@0 41
paulo@0 42 /* null terminate the buffer */
paulo@0 43 buf->data[size] = 0;
paulo@0 44
paulo@0 45 return buf;
paulo@0 46 }
paulo@0 47
paulo@0 48 void io_buf_free (struct io_buf *buf)
paulo@0 49 {
paulo@0 50 uint8_t *data;
paulo@0 51
paulo@0 52 if (!buf)
paulo@0 53 return;
paulo@0 54
paulo@0 55 data = io_buf_free_keep (buf);
paulo@0 56 free (data);
paulo@0 57 }
paulo@0 58
paulo@0 59 uint8_t *io_buf_free_keep (struct io_buf *buf)
paulo@0 60 {
paulo@0 61 uint8_t *data;
paulo@0 62
paulo@0 63 data = buf->data;
paulo@0 64 free (buf);
paulo@0 65
paulo@0 66 return data;
paulo@0 67 }
paulo@0 68
paulo@0 69 /*
paulo@0 70 * Resize the underlying buffer. This includes +1 for the null terminator.
paulo@0 71 */
paulo@0 72 BOOL io_buf_resize (struct io_buf *buf, size_t len)
paulo@0 73 {
paulo@0 74 uint8_t *resized;
paulo@0 75
paulo@0 76 if (buf->size >= len)
paulo@0 77 return TRUE;
paulo@0 78
paulo@0 79 if (!(resized = gift_realloc (buf->data, len + 1)))
paulo@0 80 return FALSE;
paulo@0 81
paulo@0 82 buf->data = resized;
paulo@0 83 buf->size = len;
paulo@0 84
paulo@0 85 /* ensure null-termination */
paulo@0 86 buf->data[len] = 0;
paulo@0 87
paulo@0 88 return TRUE;
paulo@0 89 }
paulo@0 90
paulo@0 91 void io_buf_reset (struct io_buf *buf)
paulo@0 92 {
paulo@0 93 buf->w_offs = 0;
paulo@0 94 buf->r_offs = 0;
paulo@0 95 }
paulo@0 96
paulo@0 97 void io_buf_push (struct io_buf *buf, size_t len)
paulo@0 98 {
paulo@0 99 assert (len + buf->w_offs <= buf->size);
paulo@0 100 buf->w_offs += len;
paulo@0 101 }
paulo@0 102
paulo@0 103 void io_buf_pop (struct io_buf *buf, size_t len)
paulo@0 104 {
paulo@0 105 assert (len + buf->r_offs <= buf->w_offs);
paulo@0 106 buf->r_offs += len;
paulo@0 107 }
paulo@0 108
paulo@0 109 size_t io_buf_copy (struct io_buf *dst, struct io_buf *src, size_t len)
paulo@0 110 {
paulo@0 111 size_t src_avail = io_buf_read_avail (src);
paulo@0 112 size_t dst_avail = io_buf_write_avail (dst);
paulo@0 113
paulo@0 114 if (len > src_avail)
paulo@0 115 len = src_avail;
paulo@0 116
paulo@0 117 if (len > dst_avail)
paulo@0 118 len = dst_avail;
paulo@0 119
paulo@0 120 memcpy (dst->data + dst->w_offs, src->data + src->r_offs, len);
paulo@0 121
paulo@0 122 dst->w_offs += len;
paulo@0 123 src->r_offs += len;
paulo@0 124
paulo@0 125 return len;
paulo@0 126 }