Mercurial > hg > nnchat
comparison libnnchat.c @ 71:3a23c2adfb78
Remove tabs from indentation.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 14 Nov 2008 08:49:40 +0200 |
parents | e763ef5cfd53 |
children | fe5fc76c0806 |
comparison
equal
deleted
inserted
replaced
70:5228ad7b4f57 | 71:3a23c2adfb78 |
---|---|
5 */ | 5 */ |
6 #include "libnnchat.h" | 6 #include "libnnchat.h" |
7 | 7 |
8 | 8 |
9 typedef struct { | 9 typedef struct { |
10 char c; | 10 char c; |
11 char *ent; | 11 char *ent; |
12 } html_entity_t; | 12 } html_entity_t; |
13 | 13 |
14 | 14 |
15 html_entity_t HTMLEntities[] = { | 15 html_entity_t HTMLEntities[] = { |
16 { '<', "<" }, | 16 { '<', "<" }, |
17 { '>', ">" }, | 17 { '>', ">" }, |
18 }; | 18 }; |
19 | 19 |
20 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(HTMLEntities[0])); | 20 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(HTMLEntities[0])); |
21 | 21 |
22 | 22 |
23 int openConnection(struct in_addr *addr, const int port) | 23 int openConnection(struct in_addr *addr, const int port) |
24 { | 24 { |
25 struct sockaddr_in tmpAddr; | 25 struct sockaddr_in tmpAddr; |
26 int sock = -1; | 26 int sock = -1; |
27 | 27 |
28 tmpAddr.sin_family = AF_INET; | 28 tmpAddr.sin_family = AF_INET; |
29 tmpAddr.sin_port = htons(port); | 29 tmpAddr.sin_port = htons(port); |
30 tmpAddr.sin_addr = *addr; | 30 tmpAddr.sin_addr = *addr; |
31 | 31 |
32 THMSG(1, "Connecting to %s:%d ...\n", | 32 THMSG(1, "Connecting to %s:%d ...\n", |
33 inet_ntoa(tmpAddr.sin_addr), port); | 33 inet_ntoa(tmpAddr.sin_addr), port); |
34 | 34 |
35 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) { | 35 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) { |
36 THERR("Could not open socket: %s\n", strerror(errno)); | 36 THERR("Could not open socket: %s\n", strerror(errno)); |
37 return -2; | 37 return -2; |
38 } | 38 } |
39 | 39 |
40 THMSG(2, "Using socket %d.\n", sock); | 40 THMSG(2, "Using socket %d.\n", sock); |
41 | 41 |
42 if (connect(sock, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) { | 42 if (connect(sock, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) { |
43 THERR("Could not connect: %s\n", strerror(errno)); | 43 THERR("Could not connect: %s\n", strerror(errno)); |
44 return -5; | 44 return -5; |
45 } | 45 } |
46 | 46 |
47 return sock; | 47 return sock; |
48 } | 48 } |
49 | 49 |
50 | 50 |
51 void closeConnection(const int sock) | 51 void closeConnection(const int sock) |
52 { | 52 { |
53 if (sock >= 0) { | 53 if (sock >= 0) { |
54 #ifdef __WIN32 | 54 #ifdef __WIN32 |
55 closesocket(sock); | 55 closesocket(sock); |
56 #else | 56 #else |
57 close(sock); | 57 close(sock); |
58 #endif | 58 #endif |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 | 62 |
63 BOOL sendToSocket(const int sock, char *buf, const size_t bufLen) | 63 BOOL sendToSocket(const int sock, char *buf, const size_t bufLen) |
64 { | 64 { |
65 size_t bufLeft = bufLen; | 65 size_t bufLeft = bufLen; |
66 char *bufPtr = buf; | 66 char *bufPtr = buf; |
67 | 67 |
68 while (bufLeft > 0) { | 68 while (bufLeft > 0) { |
69 ssize_t bufSent; | 69 ssize_t bufSent; |
70 bufSent = send(sock, bufPtr, bufLeft, 0); | 70 bufSent = send(sock, bufPtr, bufLeft, 0); |
71 if (bufSent < 0) return FALSE; | 71 if (bufSent < 0) return FALSE; |
72 bufLeft -= bufSent; | 72 bufLeft -= bufSent; |
73 bufPtr += bufSent; | 73 bufPtr += bufSent; |
74 } | 74 } |
75 return TRUE; | 75 return TRUE; |
76 } | 76 } |
77 | 77 |
78 | 78 |
79 BOOL bufRealloc(char **buf, size_t *size, size_t add) | 79 BOOL bufRealloc(char **buf, size_t *size, size_t add) |
80 { | 80 { |
81 return ((*buf = th_realloc(*buf, *size + add)) != NULL); | 81 return ((*buf = th_realloc(*buf, *size + add)) != NULL); |
82 } | 82 } |
83 | 83 |
84 #define PUSHCHAR(x) bufPushChar(&result, &resSize, &resPos, x) | 84 #define PUSHCHAR(x) bufPushChar(&result, &resSize, &resPos, x) |
85 BOOL bufPushChar(char **buf, size_t *size, size_t *pos, char ch) | 85 BOOL bufPushChar(char **buf, size_t *size, size_t *pos, char ch) |
86 { | 86 { |
87 if (*pos >= *size && !bufRealloc(buf, size, SET_ALLOC_SIZE)) | 87 if (*pos >= *size && !bufRealloc(buf, size, SET_ALLOC_SIZE)) |
88 return FALSE; | 88 return FALSE; |
89 | 89 |
90 (*buf)[*pos] = ch; | 90 (*buf)[*pos] = ch; |
91 (*pos)++; | 91 (*pos)++; |
92 return TRUE; | 92 return TRUE; |
93 } | 93 } |
94 | 94 |
95 #define PUSHSTR(x) bufPushStr(&result, &resSize, &resPos, x) | 95 #define PUSHSTR(x) bufPushStr(&result, &resSize, &resPos, x) |
96 BOOL bufPushStr(char **buf, size_t *size, size_t *pos, char *str) | 96 BOOL bufPushStr(char **buf, size_t *size, size_t *pos, char *str) |
97 { | 97 { |
98 size_t tmpLen; | 98 size_t tmpLen; |
99 | 99 |
100 if (!str) return FALSE; | 100 if (!str) return FALSE; |
101 tmpLen = strlen(str); | 101 tmpLen = strlen(str); |
102 | 102 |
103 if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + SET_ALLOC_SIZE)) | 103 if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + SET_ALLOC_SIZE)) |
104 return FALSE; | 104 return FALSE; |
105 | 105 |
106 strcpy(*buf + *pos, str); | 106 strcpy(*buf + *pos, str); |
107 (*pos) += tmpLen; | 107 (*pos) += tmpLen; |
108 return TRUE; | 108 return TRUE; |
109 } | 109 } |
110 | 110 |
111 | 111 |
112 char *encodeStr1(const char *str) | 112 char *encodeStr1(const char *str) |
113 { | 113 { |
114 const char *s = str; | 114 const char *s = str; |
115 char *result; | 115 char *result; |
116 size_t resSize, resPos = 0; | 116 size_t resSize, resPos = 0; |
117 | 117 |
118 if (!str) return NULL; | 118 if (!str) return NULL; |
119 | 119 |
120 resSize = strlen(str) + SET_ALLOC_SIZE; | 120 resSize = strlen(str) + SET_ALLOC_SIZE; |
121 if ((result = th_malloc(resSize)) == NULL) | 121 if ((result = th_malloc(resSize)) == NULL) |
122 return NULL; | 122 return NULL; |
123 | 123 |
124 while (*s) { | 124 while (*s) { |
125 switch (*s) { | 125 switch (*s) { |
126 case 32: | 126 case 32: |
127 PUSHCHAR('+'); | 127 PUSHCHAR('+'); |
128 break; | 128 break; |
129 | 129 |
130 default: | 130 default: |
131 if (th_isalnum(*s)) | 131 if (th_isalnum(*s)) |
132 PUSHCHAR(*s); | 132 PUSHCHAR(*s); |
133 else { | 133 else { |
134 char tmpStr[4]; | 134 char tmpStr[4]; |
135 sprintf(tmpStr, "%2X", (unsigned char) *s); | 135 sprintf(tmpStr, "%2X", (unsigned char) *s); |
136 PUSHCHAR('%'); | 136 PUSHCHAR('%'); |
137 PUSHSTR(tmpStr); | 137 PUSHSTR(tmpStr); |
138 } | 138 } |
139 break; | 139 break; |
140 } | 140 } |
141 s++; | 141 s++; |
142 } | 142 } |
143 PUSHCHAR(0); | 143 PUSHCHAR(0); |
144 | 144 |
145 return result; | 145 return result; |
146 } | 146 } |
147 | 147 |
148 | 148 |
149 static int getHexDigit(const int c, const int shift) | 149 static int getHexDigit(const int c, const int shift) |
150 { | 150 { |
151 int i; | 151 int i; |
152 | 152 |
153 if (c >= 'A' && c <= 'F') | 153 if (c >= 'A' && c <= 'F') |
154 i = c - 'A' + 10; | 154 i = c - 'A' + 10; |
155 else if (c >= 'a' && c <= 'f') | 155 else if (c >= 'a' && c <= 'f') |
156 i = c - 'a' + 10; | 156 i = c - 'a' + 10; |
157 else if (c >= '0' && c <= '9') | 157 else if (c >= '0' && c <= '9') |
158 i = c - '0'; | 158 i = c - '0'; |
159 else | 159 else |
160 return -1; | 160 return -1; |
161 | 161 |
162 return i << shift; | 162 return i << shift; |
163 } | 163 } |
164 | 164 |
165 | 165 |
166 char *decodeStr1(const char *str) | 166 char *decodeStr1(const char *str) |
167 { | 167 { |
168 const char *s = str; | 168 const char *s = str; |
169 char *result; | 169 char *result; |
170 size_t resSize, resPos = 0; | 170 size_t resSize, resPos = 0; |
171 int c; | 171 int c; |
172 | 172 |
173 if (!str) return NULL; | 173 if (!str) return NULL; |
174 | 174 |
175 resSize = strlen(str) + SET_ALLOC_SIZE; | 175 resSize = strlen(str) + SET_ALLOC_SIZE; |
176 if ((result = th_malloc(resSize)) == NULL) | 176 if ((result = th_malloc(resSize)) == NULL) |
177 return NULL; | 177 return NULL; |
178 | 178 |
179 while (*s) { | 179 while (*s) { |
180 switch (*s) { | 180 switch (*s) { |
181 case '+': | 181 case '+': |
182 PUSHCHAR(' '); | 182 PUSHCHAR(' '); |
183 s++; | 183 s++; |
184 break; | 184 break; |
185 | 185 |
186 case '½': | 186 case '½': |
187 /* Escape these .. */ | 187 /* Escape these .. */ |
188 PUSHCHAR('½'); | 188 PUSHCHAR('½'); |
189 PUSHCHAR('½'); | 189 PUSHCHAR('½'); |
190 s++; | 190 s++; |
191 break; | 191 break; |
192 | 192 |
193 case '\r': | 193 case '\r': |
194 PUSHCHAR(' '); | 194 PUSHCHAR(' '); |
195 s++; | 195 s++; |
196 break; | 196 break; |
197 | 197 |
198 case '%': | 198 case '%': |
199 s++; | 199 s++; |
200 if (*s == '%') | 200 if (*s == '%') |
201 PUSHCHAR('%'); | 201 PUSHCHAR('%'); |
202 else if ((c = getHexDigit(*s, 4)) >= 0) { | 202 else if ((c = getHexDigit(*s, 4)) >= 0) { |
203 int i = getHexDigit(*(++s), 0); | 203 int i = getHexDigit(*(++s), 0); |
204 if (i >= 0) { | 204 if (i >= 0) { |
205 PUSHCHAR(c | i); | 205 PUSHCHAR(c | i); |
206 } else { | 206 } else { |
207 PUSHCHAR('§'); | 207 PUSHCHAR(' '); |
208 PUSHCHAR(*s); | 208 PUSHCHAR(*s); |
209 } | 209 } |
210 } else { | 210 } else { |
211 PUSHCHAR('§'); | 211 PUSHCHAR(' '); |
212 PUSHCHAR(*s); | 212 PUSHCHAR(*s); |
213 } | 213 } |
214 s++; | 214 s++; |
215 break; | 215 break; |
216 | 216 |
217 default: | 217 default: |
218 PUSHCHAR(*s); | 218 PUSHCHAR(*s); |
219 s++; | 219 s++; |
220 } | 220 } |
221 } | 221 } |
222 PUSHCHAR(0); | 222 PUSHCHAR(0); |
223 | 223 |
224 return result; | 224 return result; |
225 } | 225 } |
226 | 226 |
227 | 227 |
228 char *stripXMLTags(const char *str) | 228 char *stripXMLTags(const char *str) |
229 { | 229 { |
230 const char *s = str; | 230 const char *s = str; |
231 char *result; | 231 char *result; |
232 size_t resSize, resPos = 0; | 232 size_t resSize, resPos = 0; |
233 | 233 |
234 if (!str) return NULL; | 234 if (!str) return NULL; |
235 | 235 |
236 resSize = strlen(str) + SET_ALLOC_SIZE; | 236 resSize = strlen(str) + SET_ALLOC_SIZE; |
237 if ((result = th_malloc(resSize)) == NULL) | 237 if ((result = th_malloc(resSize)) == NULL) |
238 return NULL; | 238 return NULL; |
239 | 239 |
240 while (*s) { | 240 while (*s) { |
241 if (*s == '<') { | 241 if (*s == '<') { |
242 while (*s && *s != '>') s++; | 242 while (*s && *s != '>') s++; |
243 if (*s == '>') s++; | 243 if (*s == '>') s++; |
244 } else | 244 } else |
245 PUSHCHAR(*s++); | 245 PUSHCHAR(*s++); |
246 } | 246 } |
247 PUSHCHAR(0); | 247 PUSHCHAR(0); |
248 | 248 |
249 return result; | 249 return result; |
250 } | 250 } |
251 | 251 |
252 | 252 |
253 char *encodeStr2(const char *str) | 253 char *encodeStr2(const char *str) |
254 { | 254 { |
255 const char *s = str; | 255 const char *s = str; |
256 char *result; | 256 char *result; |
257 size_t resSize, resPos = 0; | 257 size_t resSize, resPos = 0; |
258 | 258 |
259 if (!str) return NULL; | 259 if (!str) return NULL; |
260 | 260 |
261 resSize = strlen(str) + SET_ALLOC_SIZE; | 261 resSize = strlen(str) + SET_ALLOC_SIZE; |
262 if ((result = th_malloc(resSize)) == NULL) | 262 if ((result = th_malloc(resSize)) == NULL) |
263 return NULL; | 263 return NULL; |
264 | 264 |
265 while (*s) { | 265 while (*s) { |
266 int i; | 266 int i; |
267 BOOL found = FALSE; | 267 BOOL found = FALSE; |
268 for (i = 0; i < numHTMLEntities; i++) | 268 for (i = 0; i < numHTMLEntities; i++) |
269 if (HTMLEntities[i].c == *s) { | 269 if (HTMLEntities[i].c == *s) { |
270 PUSHSTR(HTMLEntities[i].ent); | 270 PUSHSTR(HTMLEntities[i].ent); |
271 found = TRUE; | 271 found = TRUE; |
272 break; | 272 break; |
273 } | 273 } |
274 if (!found) PUSHCHAR(*s); | 274 if (!found) PUSHCHAR(*s); |
275 | 275 |
276 s++; | 276 s++; |
277 } | 277 } |
278 PUSHCHAR(0); | 278 PUSHCHAR(0); |
279 | 279 |
280 return result; | 280 return result; |
281 } | 281 } |
282 | 282 |
283 | 283 |
284 char *decodeStr2(const char *str) | 284 char *decodeStr2(const char *str) |
285 { | 285 { |
286 const char *s = str; | 286 const char *s = str; |
287 char *result; | 287 char *result; |
288 size_t resSize, resPos = 0; | 288 size_t resSize, resPos = 0; |
289 | 289 |
290 if (!str) return NULL; | 290 if (!str) return NULL; |
291 | 291 |
292 resSize = strlen(str); | 292 resSize = strlen(str); |
293 if ((result = th_malloc(resSize)) == NULL) | 293 if ((result = th_malloc(resSize)) == NULL) |
294 return NULL; | 294 return NULL; |
295 | 295 |
296 while (*s) { | 296 while (*s) { |
297 if (*s == '&') { | 297 if (*s == '&') { |
298 int i; | 298 int i; |
299 BOOL found = FALSE; | 299 BOOL found = FALSE; |
300 for (i = 0; i < numHTMLEntities; i++) { | 300 for (i = 0; i < numHTMLEntities; i++) { |
301 html_entity_t *ent = &HTMLEntities[i]; | 301 html_entity_t *ent = &HTMLEntities[i]; |
302 int len = strlen(ent->ent); | 302 int len = strlen(ent->ent); |
303 if (!strncmp(s, ent->ent, len)) { | 303 if (!strncmp(s, ent->ent, len)) { |
304 PUSHCHAR(ent->c); | 304 PUSHCHAR(ent->c); |
305 s += len; | 305 s += len; |
306 found = TRUE; | 306 found = TRUE; |
307 break; | 307 break; |
308 } | 308 } |
309 } | 309 } |
310 if (!found) PUSHCHAR(*s++); | 310 if (!found) PUSHCHAR(*s++); |
311 } else | 311 } else |
312 PUSHCHAR(*s++); | 312 PUSHCHAR(*s++); |
313 } | 313 } |
314 PUSHCHAR(0); | 314 PUSHCHAR(0); |
315 | 315 |
316 return result; | 316 return result; |
317 } | 317 } |
318 | 318 |
319 | 319 |
320 BOOL sendUserMsg(const int sock, const char *user, const char *fmt, ...) | 320 BOOL sendUserMsg(const int sock, const char *user, const char *fmt, ...) |
321 { | 321 { |
322 char tmpBuf[SET_BUFSIZE], tmpBuf2[SET_BUFSIZE + 256]; | 322 char tmpBuf[SET_BUFSIZE], tmpBuf2[SET_BUFSIZE + 256]; |
323 int n; | 323 int n; |
324 va_list ap; | 324 va_list ap; |
325 | 325 |
326 va_start(ap, fmt); | 326 va_start(ap, fmt); |
327 n = vsnprintf(tmpBuf, sizeof(tmpBuf), fmt, ap); | 327 n = vsnprintf(tmpBuf, sizeof(tmpBuf), fmt, ap); |
328 va_end(ap); | 328 va_end(ap); |
329 | 329 |
330 if (n < 0) return FALSE; | 330 if (n < 0) return FALSE; |
331 | 331 |
332 snprintf(tmpBuf2, sizeof(tmpBuf2), | 332 snprintf(tmpBuf2, sizeof(tmpBuf2), |
333 "<USER>%s</USER><MESSAGE>%s</MESSAGE>", | 333 "<USER>%s</USER><MESSAGE>%s</MESSAGE>", |
334 user, tmpBuf); | 334 user, tmpBuf); |
335 | 335 |
336 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1); | 336 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1); |
337 } | 337 } |
338 | 338 |
339 | 339 |
340 ringbuf_t * newRingBuf(const size_t size) | 340 ringbuf_t * newRingBuf(const size_t size) |
341 { | 341 { |
342 ringbuf_t *res = th_calloc(1, sizeof(ringbuf_t)); | 342 ringbuf_t *res = th_calloc(1, sizeof(ringbuf_t)); |
343 | 343 |
344 res->data = (char **) th_malloc(size * sizeof(char *)); | 344 res->data = (char **) th_malloc(size * sizeof(char *)); |
345 res->size = size; | 345 res->size = size; |
346 res->n = 0; | 346 res->n = 0; |
347 | 347 |
348 return res; | 348 return res; |
349 } | 349 } |
350 | 350 |
351 | 351 |
352 void freeRingBuf(ringbuf_t *buf) | 352 void freeRingBuf(ringbuf_t *buf) |
353 { | 353 { |
354 size_t i; | 354 size_t i; |
355 | 355 |
356 for (i = 0; i < buf->n; i++) | 356 for (i = 0; i < buf->n; i++) |
357 th_free(buf->data[i]); | 357 th_free(buf->data[i]); |
358 | 358 |
359 th_free(buf->data); | 359 th_free(buf->data); |
360 th_free(buf); | 360 th_free(buf); |
361 } | 361 } |
362 | 362 |
363 | 363 |
364 void addRingBuf(ringbuf_t *buf, const char *str) | 364 void addRingBuf(ringbuf_t *buf, const char *str) |
365 { | 365 { |
366 if (buf->n < buf->size) { | 366 if (buf->n < buf->size) { |
367 buf->data[buf->n] = strdup(str); | 367 buf->data[buf->n] = strdup(str); |
368 buf->n++; | 368 buf->n++; |
369 } else { | 369 } else { |
370 th_free(buf->data[0]); | 370 th_free(buf->data[0]); |
371 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1); | 371 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1); |
372 buf->data[buf->size - 1] = strdup(str); | 372 buf->data[buf->size - 1] = strdup(str); |
373 } | 373 } |
374 } | 374 } |
375 | 375 |
376 | 376 |
377 int writeBuf(editbuf_t *buf, ssize_t pos, int ch) | 377 int writeBuf(editbuf_t *buf, ssize_t pos, int ch) |
378 { | 378 { |
379 /* Check arguments */ | 379 /* Check arguments */ |
380 if (buf->len+1 >= buf->size) return -3; | 380 if (buf->len+1 >= buf->size) return -3; |
381 | 381 |
382 if (pos < 0) | 382 if (pos < 0) |
383 return -1; | 383 return -1; |
384 else if (pos >= buf->len) { | 384 else if (pos >= buf->len) { |
385 buf->data[buf->len++] = ch; | 385 buf->data[buf->len++] = ch; |
386 } else { | 386 } else { |
387 buf->data[pos] = ch; | 387 buf->data[pos] = ch; |
388 } | 388 } |
389 return 0; | 389 return 0; |
390 } | 390 } |
391 | 391 |
392 | 392 |
393 int insertBuf(editbuf_t *buf, ssize_t pos, int ch) | 393 int insertBuf(editbuf_t *buf, ssize_t pos, int ch) |
394 { | 394 { |
395 /* Check arguments */ | 395 /* Check arguments */ |
396 if (buf->len+1 >= buf->size) return -3; | 396 if (buf->len+1 >= buf->size) return -3; |
397 | 397 |
398 if (pos < 0) | 398 if (pos < 0) |
399 return -1; | 399 return -1; |
400 else if (pos >= buf->len) { | 400 else if (pos >= buf->len) { |
401 buf->data[buf->len] = ch; | 401 buf->data[buf->len] = ch; |
402 } else { | 402 } else { |
403 memmove(&(buf->data[pos+1]), &(buf->data[pos]), buf->len - pos + 1); | 403 memmove(&(buf->data[pos+1]), &(buf->data[pos]), buf->len - pos + 1); |
404 buf->data[pos] = ch; | 404 buf->data[pos] = ch; |
405 } | 405 } |
406 buf->len++; | 406 buf->len++; |
407 return 0; | 407 return 0; |
408 } | 408 } |
409 | 409 |
410 | 410 |
411 int deleteBuf(editbuf_t *buf, ssize_t pos) | 411 int deleteBuf(editbuf_t *buf, ssize_t pos) |
412 { | 412 { |
413 /* Check arguments */ | 413 /* Check arguments */ |
414 if (pos < 0) | 414 if (pos < 0) |
415 return -1; | 415 return -1; |
416 else if (pos < buf->len) { | 416 else if (pos < buf->len) { |
417 memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos); | 417 memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos); |
418 buf->len--; | 418 buf->len--; |
419 return 0; | 419 return 0; |
420 } else | 420 } else |
421 return -2; | 421 return -2; |
422 } | 422 } |
423 | 423 |
424 | 424 |
425 void clearBuf(editbuf_t *buf) | 425 void clearBuf(editbuf_t *buf) |
426 { | 426 { |
427 buf->len = 0; | 427 buf->len = 0; |
428 buf->pos = 0; | 428 buf->pos = 0; |
429 } | 429 } |
430 | 430 |
431 | 431 |
432 editbuf_t * newBuf(ssize_t n) | 432 editbuf_t * newBuf(ssize_t n) |
433 { | 433 { |
434 editbuf_t *res = th_calloc(1, sizeof(editbuf_t)); | 434 editbuf_t *res = th_calloc(1, sizeof(editbuf_t)); |
435 | 435 |
436 res->data = (char *) th_malloc(n); | 436 res->data = (char *) th_malloc(n); |
437 res->size = n; | 437 res->size = n; |
438 | 438 |
439 return res; | 439 return res; |
440 } | 440 } |
441 | 441 |
442 | 442 |
443 void freeBuf(editbuf_t *buf) | 443 void freeBuf(editbuf_t *buf) |
444 { | 444 { |
445 if (buf) { | 445 if (buf) { |
446 th_free(buf->data); | 446 th_free(buf->data); |
447 th_free(buf); | 447 th_free(buf); |
448 } | 448 } |
449 } | 449 } |
450 | 450 |
451 | 451 |
452 editbuf_t * copyBuf(editbuf_t *src) | 452 editbuf_t * copyBuf(editbuf_t *src) |
453 { | 453 { |
454 editbuf_t *res; | 454 editbuf_t *res; |
455 | 455 |
456 assert(src != NULL); | 456 assert(src != NULL); |
457 | 457 |
458 if (src == NULL) return NULL; | 458 if (src == NULL) return NULL; |
459 | 459 |
460 if ((res = newBuf(src->size)) == NULL) | 460 if ((res = newBuf(src->size)) == NULL) |
461 return NULL; | 461 return NULL; |
462 | 462 |
463 memcpy(res->data, src->data, src->size); | 463 memcpy(res->data, src->data, src->size); |
464 res->pos = res->len = src->len; | 464 res->pos = res->len = src->len; |
465 | 465 |
466 return res; | 466 return res; |
467 } | 467 } |
468 | 468 |
469 | 469 |
470 void setBufPos(editbuf_t *buf, ssize_t pos) | 470 void setBufPos(editbuf_t *buf, ssize_t pos) |
471 { | 471 { |
472 /* Check arguments */ | 472 /* Check arguments */ |
473 if (pos < 0) | 473 if (pos < 0) |
474 buf->pos = 0; | 474 buf->pos = 0; |
475 else if (pos >= buf->len) | 475 else if (pos >= buf->len) |
476 buf->pos = buf->len; | 476 buf->pos = buf->len; |
477 else | 477 else |
478 buf->pos = pos; | 478 buf->pos = pos; |
479 } | 479 } |
480 | 480 |