Mercurial > hg > index.fcgi > gift-gnutella > gift-gnutella-0.0.11-1pba
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/io/io_buf.c Sat Feb 20 21:18:28 2010 -0800 1.3 @@ -0,0 +1,126 @@ 1.4 +/* 1.5 + * $Id: io_buf.c,v 1.2 2003/09/17 17:44:11 hipnod Exp $ 1.6 + * 1.7 + * Copyright (C) 2003 giFT project (gift.sourceforge.net) 1.8 + * 1.9 + * This program is free software; you can redistribute it and/or modify it 1.10 + * under the terms of the GNU General Public License as published by the 1.11 + * Free Software Foundation; either version 2, or (at your option) any 1.12 + * later version. 1.13 + * 1.14 + * This program is distributed in the hope that it will be useful, but 1.15 + * WITHOUT ANY WARRANTY; without even the implied warranty of 1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.17 + * General Public License for more details. 1.18 + */ 1.19 + 1.20 +#include "gt_gnutella.h" 1.21 + 1.22 +#include "io_buf.h" 1.23 + 1.24 +/*****************************************************************************/ 1.25 + 1.26 +struct io_buf *io_buf_new (size_t size) 1.27 +{ 1.28 + struct io_buf *buf; 1.29 + uint8_t *data; 1.30 + 1.31 + if (!(data = gift_malloc (size + 1))) 1.32 + return NULL; 1.33 + 1.34 + if (!(buf = gift_malloc (sizeof (struct io_buf)))) 1.35 + { 1.36 + free (data); 1.37 + return NULL; 1.38 + } 1.39 + 1.40 + buf->data = data; 1.41 + buf->size = size; 1.42 + buf->r_offs = 0; 1.43 + buf->w_offs = 0; 1.44 + 1.45 + /* null terminate the buffer */ 1.46 + buf->data[size] = 0; 1.47 + 1.48 + return buf; 1.49 +} 1.50 + 1.51 +void io_buf_free (struct io_buf *buf) 1.52 +{ 1.53 + uint8_t *data; 1.54 + 1.55 + if (!buf) 1.56 + return; 1.57 + 1.58 + data = io_buf_free_keep (buf); 1.59 + free (data); 1.60 +} 1.61 + 1.62 +uint8_t *io_buf_free_keep (struct io_buf *buf) 1.63 +{ 1.64 + uint8_t *data; 1.65 + 1.66 + data = buf->data; 1.67 + free (buf); 1.68 + 1.69 + return data; 1.70 +} 1.71 + 1.72 +/* 1.73 + * Resize the underlying buffer. This includes +1 for the null terminator. 1.74 + */ 1.75 +BOOL io_buf_resize (struct io_buf *buf, size_t len) 1.76 +{ 1.77 + uint8_t *resized; 1.78 + 1.79 + if (buf->size >= len) 1.80 + return TRUE; 1.81 + 1.82 + if (!(resized = gift_realloc (buf->data, len + 1))) 1.83 + return FALSE; 1.84 + 1.85 + buf->data = resized; 1.86 + buf->size = len; 1.87 + 1.88 + /* ensure null-termination */ 1.89 + buf->data[len] = 0; 1.90 + 1.91 + return TRUE; 1.92 +} 1.93 + 1.94 +void io_buf_reset (struct io_buf *buf) 1.95 +{ 1.96 + buf->w_offs = 0; 1.97 + buf->r_offs = 0; 1.98 +} 1.99 + 1.100 +void io_buf_push (struct io_buf *buf, size_t len) 1.101 +{ 1.102 + assert (len + buf->w_offs <= buf->size); 1.103 + buf->w_offs += len; 1.104 +} 1.105 + 1.106 +void io_buf_pop (struct io_buf *buf, size_t len) 1.107 +{ 1.108 + assert (len + buf->r_offs <= buf->w_offs); 1.109 + buf->r_offs += len; 1.110 +} 1.111 + 1.112 +size_t io_buf_copy (struct io_buf *dst, struct io_buf *src, size_t len) 1.113 +{ 1.114 + size_t src_avail = io_buf_read_avail (src); 1.115 + size_t dst_avail = io_buf_write_avail (dst); 1.116 + 1.117 + if (len > src_avail) 1.118 + len = src_avail; 1.119 + 1.120 + if (len > dst_avail) 1.121 + len = dst_avail; 1.122 + 1.123 + memcpy (dst->data + dst->w_offs, src->data + src->r_offs, len); 1.124 + 1.125 + dst->w_offs += len; 1.126 + src->r_offs += len; 1.127 + 1.128 + return len; 1.129 +}