comparison libnnchat.c @ 65:e763ef5cfd53

Move more functions to libnnchat.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 11 Nov 2008 23:57:49 +0200
parents 6a3a917303e4
children 3a23c2adfb78
comparison
equal deleted inserted replaced
64:6a3a917303e4 65:e763ef5cfd53
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
340 ringbuf_t * newRingBuf(const size_t size)
341 {
342 ringbuf_t *res = th_calloc(1, sizeof(ringbuf_t));
343
344 res->data = (char **) th_malloc(size * sizeof(char *));
345 res->size = size;
346 res->n = 0;
347
348 return res;
349 }
350
351
352 void freeRingBuf(ringbuf_t *buf)
353 {
354 size_t i;
355
356 for (i = 0; i < buf->n; i++)
357 th_free(buf->data[i]);
358
359 th_free(buf->data);
360 th_free(buf);
361 }
362
363
364 void addRingBuf(ringbuf_t *buf, const char *str)
365 {
366 if (buf->n < buf->size) {
367 buf->data[buf->n] = strdup(str);
368 buf->n++;
369 } else {
370 th_free(buf->data[0]);
371 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1);
372 buf->data[buf->size - 1] = strdup(str);
373 }
374 }
375
376
377 int writeBuf(editbuf_t *buf, ssize_t pos, int ch)
378 {
379 /* Check arguments */
380 if (buf->len+1 >= buf->size) return -3;
381
382 if (pos < 0)
383 return -1;
384 else if (pos >= buf->len) {
385 buf->data[buf->len++] = ch;
386 } else {
387 buf->data[pos] = ch;
388 }
389 return 0;
390 }
391
392
393 int insertBuf(editbuf_t *buf, ssize_t pos, int ch)
394 {
395 /* Check arguments */
396 if (buf->len+1 >= buf->size) return -3;
397
398 if (pos < 0)
399 return -1;
400 else if (pos >= buf->len) {
401 buf->data[buf->len] = ch;
402 } else {
403 memmove(&(buf->data[pos+1]), &(buf->data[pos]), buf->len - pos + 1);
404 buf->data[pos] = ch;
405 }
406 buf->len++;
407 return 0;
408 }
409
410
411 int deleteBuf(editbuf_t *buf, ssize_t pos)
412 {
413 /* Check arguments */
414 if (pos < 0)
415 return -1;
416 else if (pos < buf->len) {
417 memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos);
418 buf->len--;
419 return 0;
420 } else
421 return -2;
422 }
423
424
425 void clearBuf(editbuf_t *buf)
426 {
427 buf->len = 0;
428 buf->pos = 0;
429 }
430
431
432 editbuf_t * newBuf(ssize_t n)
433 {
434 editbuf_t *res = th_calloc(1, sizeof(editbuf_t));
435
436 res->data = (char *) th_malloc(n);
437 res->size = n;
438
439 return res;
440 }
441
442
443 void freeBuf(editbuf_t *buf)
444 {
445 if (buf) {
446 th_free(buf->data);
447 th_free(buf);
448 }
449 }
450
451
452 editbuf_t * copyBuf(editbuf_t *src)
453 {
454 editbuf_t *res;
455
456 assert(src != NULL);
457
458 if (src == NULL) return NULL;
459
460 if ((res = newBuf(src->size)) == NULL)
461 return NULL;
462
463 memcpy(res->data, src->data, src->size);
464 res->pos = res->len = src->len;
465
466 return res;
467 }
468
469
470 void setBufPos(editbuf_t *buf, ssize_t pos)
471 {
472 /* Check arguments */
473 if (pos < 0)
474 buf->pos = 0;
475 else if (pos >= buf->len)
476 buf->pos = buf->len;
477 else
478 buf->pos = pos;
479 }
480