comparison libpurple/circbuffer.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children c2d75b47198d
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 /*
2 * @file circbuffer.h Buffer Utility Functions
3 * @ingroup core
4 *
5 * Gaim is the legal property of its developers, whose names are too numerous
6 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * source distribution.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #include "internal.h"
24
25 #include "circbuffer.h"
26
27 #define DEFAULT_BUF_SIZE 256
28
29 GaimCircBuffer *
30 gaim_circ_buffer_new(gsize growsize) {
31 GaimCircBuffer *buf = g_new0(GaimCircBuffer, 1);
32 buf->growsize = growsize ? growsize : DEFAULT_BUF_SIZE;
33 return buf;
34 }
35
36 void gaim_circ_buffer_destroy(GaimCircBuffer *buf) {
37 g_return_if_fail(buf != NULL);
38
39 g_free(buf->buffer);
40 g_free(buf);
41 }
42
43 static void grow_circ_buffer(GaimCircBuffer *buf, gsize len) {
44 int in_offset = 0, out_offset = 0;
45 int start_buflen;
46
47 g_return_if_fail(buf != NULL);
48
49 start_buflen = buf->buflen;
50
51 while ((buf->buflen - buf->bufused) < len)
52 buf->buflen += buf->growsize;
53
54 if (buf->inptr != NULL) {
55 in_offset = buf->inptr - buf->buffer;
56 out_offset = buf->outptr - buf->buffer;
57 }
58 buf->buffer = g_realloc(buf->buffer, buf->buflen);
59
60 /* adjust the fill and remove pointer locations */
61 if (buf->inptr == NULL) {
62 buf->inptr = buf->outptr = buf->buffer;
63 } else {
64 buf->inptr = buf->buffer + in_offset;
65 buf->outptr = buf->buffer + out_offset;
66 }
67
68 /* If the fill pointer is wrapped to before the remove
69 * pointer, we need to shift the data */
70 if (in_offset < out_offset) {
71 int shift_n = MIN(buf->buflen - start_buflen,
72 in_offset);
73 memcpy(buf->buffer + start_buflen, buf->buffer,
74 shift_n);
75
76 /* If we couldn't fit the wrapped read buffer
77 * at the end */
78 if (shift_n < in_offset) {
79 memmove(buf->buffer,
80 buf->buffer + shift_n,
81 in_offset - shift_n);
82 buf->inptr = buf->buffer +
83 (in_offset - shift_n);
84 } else {
85 buf->inptr = buf->buffer +
86 start_buflen + in_offset;
87 }
88 }
89 }
90
91 void gaim_circ_buffer_append(GaimCircBuffer *buf, gconstpointer src, gsize len) {
92
93 int len_stored;
94
95 g_return_if_fail(buf != NULL);
96
97 /* Grow the buffer, if necessary */
98 if ((buf->buflen - buf->bufused) < len)
99 grow_circ_buffer(buf, len);
100
101 /* If there is not enough room to copy all of src before hitting
102 * the end of the buffer then we will need to do two copies.
103 * One copy from inptr to the end of the buffer, and the
104 * second copy from the start of the buffer to the end of src. */
105 if (buf->inptr >= buf->outptr)
106 len_stored = MIN(len, buf->buflen
107 - (buf->inptr - buf->buffer));
108 else
109 len_stored = len;
110
111 memcpy(buf->inptr, src, len_stored);
112
113 if (len_stored < len) {
114 memcpy(buf->buffer, (char*)src + len_stored, len - len_stored);
115 buf->inptr = buf->buffer + (len - len_stored);
116 } else if ((buf->buffer - buf->inptr) == len_stored) {
117 buf->inptr = buf->buffer;
118 } else {
119 buf->inptr += len_stored;
120 }
121
122 buf->bufused += len;
123 }
124
125 gsize gaim_circ_buffer_get_max_read(GaimCircBuffer *buf) {
126 int max_read;
127
128 g_return_val_if_fail(buf != NULL, 0);
129
130 if (buf->bufused == 0)
131 max_read = 0;
132 else if ((buf->outptr - buf->inptr) >= 0)
133 max_read = buf->buflen - (buf->outptr - buf->buffer);
134 else
135 max_read = buf->inptr - buf->outptr;
136
137 return max_read;
138 }
139
140 gboolean gaim_circ_buffer_mark_read(GaimCircBuffer *buf, gsize len) {
141 g_return_val_if_fail(buf != NULL, FALSE);
142 g_return_val_if_fail(gaim_circ_buffer_get_max_read(buf) >= len, FALSE);
143
144 buf->outptr += len;
145 buf->bufused -= len;
146 /* wrap to the start if we're at the end */
147 if ((buf->outptr - buf->buffer) == buf->buflen)
148 buf->outptr = buf->buffer;
149
150 return TRUE;
151 }
152