comparison nnchat.c @ 49:0bcc38910a77

Simple ringbuffer implementation.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 29 Oct 2008 02:51:51 +0200
parents 65b1ac6a1e2e
children d08bf3b6561d
comparison
equal deleted inserted replaced
48:4e3616709752 49:0bcc38910a77
17 #include <string.h> 17 #include <string.h>
18 #include <errno.h> 18 #include <errno.h>
19 #include <time.h> 19 #include <time.h>
20 #include <ncurses.h> 20 #include <ncurses.h>
21 21
22
23 #define SET_MAX_BACKBUF (1024)
22 #define SET_MAX_HISTORY (16) 24 #define SET_MAX_HISTORY (16)
23 #define SET_BUFSIZE (4096) 25 #define SET_BUFSIZE (4096)
24 #define SET_ALLOC_SIZE (128) 26 #define SET_ALLOC_SIZE (128)
25 #define SET_DELAY (15) 27 #define SET_DELAY (15)
26 #define SET_DELAY_USEC (SET_DELAY * 1000) 28 #define SET_DELAY_USEC (SET_DELAY * 1000)
29
27 30
28 /* Options 31 /* Options
29 */ 32 */
30 int optPort = 8005; 33 int optPort = 8005;
31 int optUserColor = 0x006080; 34 int optUserColor = 0x006080;
149 return FALSE; 152 return FALSE;
150 } 153 }
151 154
152 return TRUE; 155 return TRUE;
153 } 156 }
157
158
159 typedef struct {
160 char **data;
161 size_t n, size;
162 } ringbuf_t;
163
164
165 ringbuf_t * newRingBuf(const size_t size)
166 {
167 ringbuf_t *res = th_calloc(1, sizeof(ringbuf_t));
168
169 res->data = (char **) th_malloc(size * sizeof(char *));
170 res->size = size;
171 res->n = 0;
172
173 return res;
174 }
175
176
177 void freeRingBuf(ringbuf_t *buf)
178 {
179 size_t i;
180
181 for (i = 0; i < buf->n; i++)
182 th_free(buf->data[i]);
183
184 th_free(buf->data);
185 th_free(buf);
186 }
187
188
189 void addRingBuf(ringbuf_t *buf, const char *str)
190 {
191 if (buf->n < buf->size) {
192 buf->data[buf->n] = th_strdup(str);
193 buf->n++;
194 } else {
195 th_free(buf->data[0]);
196 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1);
197 buf->data[buf->size - 1] = th_strdup(str);
198 }
199 }
200
154 201
155 typedef struct { 202 typedef struct {
156 ssize_t pos, len, size; 203 ssize_t pos, len, size;
157 char *data; 204 char *data;
158 } editbuf_t; 205 } editbuf_t;