comparison libnnchat.c @ 277:2cff949abbc5

Remove ringbuf implementation, it's been moved to th-libs.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 09 Jun 2011 11:39:33 +0300
parents 1211b740fbda
children b765f15f9895
comparison
equal deleted inserted replaced
276:1807059fb8f2 277:2cff949abbc5
537 } else 537 } else
538 return FALSE; 538 return FALSE;
539 } 539 }
540 540
541 541
542 nn_ringbuf_t * nn_ringbuf_new(const size_t size)
543 {
544 nn_ringbuf_t *res = th_calloc(1, sizeof(nn_ringbuf_t));
545
546 res->data = (char **) th_malloc(size * sizeof(char *));
547 res->size = size;
548 res->n = 0;
549
550 return res;
551 }
552
553
554 void nn_ringbuf_free(nn_ringbuf_t *buf)
555 {
556 size_t i;
557
558 for (i = 0; i < buf->n; i++)
559 th_free(buf->data[i]);
560
561 th_free(buf->data);
562 th_free(buf);
563 }
564
565
566 void nn_ringbuf_add(nn_ringbuf_t *buf, const char *str)
567 {
568 if (buf->n < buf->size) {
569 buf->data[buf->n] = th_strdup(str);
570 buf->n++;
571 } else {
572 th_free(buf->data[0]);
573 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1);
574 buf->data[buf->size - 1] = th_strdup(str);
575 }
576 }
577
578
579 int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch) 542 int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch)
580 { 543 {
581 if (buf->len+1 >= buf->size) return -3; 544 if (buf->len+1 >= buf->size) return -3;
582 545
583 if (pos < 0) 546 if (pos < 0)