1
0

buf.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * buf.c
  3. *
  4. * This file provides the implementation of a buf class
  5. */
  6. #include "private.h"
  7. #include "lub/argv.h"
  8. #include "lub/string.h"
  9. #include "lub/ctype.h"
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #define KONF_BUF_CHUNK 1024
  16. /*---------------------------------------------------------
  17. * PRIVATE META FUNCTIONS
  18. *--------------------------------------------------------- */
  19. int konf_buf_bt_compare(const void *clientnode, const void *clientkey)
  20. {
  21. const konf_buf_t *this = clientnode;
  22. int keysock;
  23. memcpy(&keysock, clientkey, sizeof(keysock));
  24. return (this->sock - keysock);
  25. }
  26. /*-------------------------------------------------------- */
  27. static void konf_buf_key(lub_bintree_key_t * key,
  28. int sock)
  29. {
  30. memcpy(key, &sock, sizeof(sock));
  31. }
  32. /*-------------------------------------------------------- */
  33. void konf_buf_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  34. {
  35. const konf_buf_t *this = clientnode;
  36. konf_buf_key(key, this->sock);
  37. }
  38. /*---------------------------------------------------------
  39. * PRIVATE METHODS
  40. *--------------------------------------------------------- */
  41. static void
  42. konf_buf_init(konf_buf_t * this, int sock)
  43. {
  44. this->sock = sock;
  45. this->buf = malloc(KONF_BUF_CHUNK);
  46. this->size = KONF_BUF_CHUNK;
  47. this->pos = 0;
  48. this->rpos = 0;
  49. /* Be a good binary tree citizen */
  50. lub_bintree_node_init(&this->bt_node);
  51. }
  52. /*--------------------------------------------------------- */
  53. static void konf_buf_fini(konf_buf_t * this)
  54. {
  55. free(this->buf);
  56. }
  57. /*---------------------------------------------------------
  58. * PUBLIC META FUNCTIONS
  59. *--------------------------------------------------------- */
  60. size_t konf_buf_bt_offset(void)
  61. {
  62. return offsetof(konf_buf_t, bt_node);
  63. }
  64. /*--------------------------------------------------------- */
  65. konf_buf_t *konf_buf_new(int sock)
  66. {
  67. konf_buf_t *this = malloc(sizeof(konf_buf_t));
  68. if (this) {
  69. konf_buf_init(this, sock);
  70. }
  71. return this;
  72. }
  73. /*---------------------------------------------------------
  74. * PUBLIC METHODS
  75. *--------------------------------------------------------- */
  76. void konf_buf_delete(konf_buf_t * this)
  77. {
  78. konf_buf_fini(this);
  79. free(this);
  80. }
  81. /*--------------------------------------------------------- */
  82. konf_buf_t *konf_buftree_find(lub_bintree_t * this,
  83. int sock)
  84. {
  85. lub_bintree_key_t key;
  86. konf_buf_key(&key, sock);
  87. return lub_bintree_find(this, &key);
  88. }
  89. /*--------------------------------------------------------- */
  90. void konf_buftree_remove(lub_bintree_t * this,
  91. int sock)
  92. {
  93. konf_buf_t *tbuf;
  94. if ((tbuf = konf_buftree_find(this, sock)) == NULL)
  95. return;
  96. lub_bintree_remove(this, tbuf);
  97. konf_buf_delete(tbuf);
  98. }
  99. /*--------------------------------------------------------- */
  100. static int konf_buf_realloc(konf_buf_t *buf, int addsize)
  101. {
  102. int chunk = KONF_BUF_CHUNK;
  103. char *tmpbuf;
  104. if (addsize > chunk)
  105. chunk = addsize;
  106. if ((buf->size - buf->pos) < chunk) {
  107. tmpbuf = realloc(buf->buf, buf->size + chunk);
  108. buf->buf = tmpbuf;
  109. buf->size += chunk;
  110. }
  111. return buf->size;
  112. }
  113. /*--------------------------------------------------------- */
  114. int konf_buf_add(konf_buf_t *buf, void *str, size_t len)
  115. {
  116. char *buffer;
  117. konf_buf_realloc(buf, len);
  118. buffer = buf->buf + buf->pos;
  119. memcpy(buffer, str, len);
  120. buf->pos += len;
  121. return len;
  122. }
  123. /*--------------------------------------------------------- */
  124. int konf_buf_read(konf_buf_t *buf)
  125. {
  126. char *buffer;
  127. int buffer_size;
  128. int nbytes;
  129. konf_buf_realloc(buf, 0);
  130. buffer_size = buf->size - buf->pos;
  131. buffer = buf->buf + buf->pos;
  132. nbytes = read(buf->sock, buffer, buffer_size);
  133. if (nbytes > 0)
  134. buf->pos += nbytes;
  135. return nbytes;
  136. }
  137. /*--------------------------------------------------------- */
  138. int konf_buftree_read(lub_bintree_t * this,
  139. int sock)
  140. {
  141. konf_buf_t *buf;
  142. buf = konf_buftree_find(this, sock);
  143. if (!buf)
  144. return -1;
  145. return konf_buf_read(buf);
  146. }
  147. /*--------------------------------------------------------- */
  148. char * konf_buf_string(char *buf, int len)
  149. {
  150. unsigned i;
  151. char *str;
  152. for (i = 0; i < len; i++) {
  153. if (('\0' == buf[i]) ||
  154. ('\n' == buf[i]))
  155. break;
  156. }
  157. if (i >= len)
  158. return NULL;
  159. str = malloc(i + 1);
  160. memcpy(str, buf, i + 1);
  161. str[i] = '\0';
  162. return str;
  163. }
  164. /*--------------------------------------------------------- */
  165. char * konf_buf_parse(konf_buf_t *buf)
  166. {
  167. char * str = NULL;
  168. /* Search the buffer for the string */
  169. str = konf_buf_string(buf->buf, buf->pos);
  170. /* Remove parsed string from the buffer */
  171. if (str) {
  172. int len = strlen(str) + 1;
  173. memmove(buf->buf, &buf->buf[len], buf->pos - len);
  174. buf->pos -= len;
  175. if (buf->rpos >= len)
  176. buf->rpos -= len;
  177. else
  178. buf->rpos = 0;
  179. }
  180. /* Make buffer shorter */
  181. if ((buf->size - buf->pos) > (2 * KONF_BUF_CHUNK)) {
  182. char *tmpbuf;
  183. tmpbuf = realloc(buf->buf, buf->size - KONF_BUF_CHUNK);
  184. buf->buf = tmpbuf;
  185. buf->size -= KONF_BUF_CHUNK;
  186. }
  187. return str;
  188. }
  189. char * konf_buf_preparse(konf_buf_t *buf)
  190. {
  191. char * str = NULL;
  192. str = konf_buf_string(buf->buf + buf->rpos, buf->pos - buf->rpos);
  193. if (str)
  194. buf->rpos += (strlen(str) + 1);
  195. return str;
  196. }
  197. int konf_buf_lseek(konf_buf_t *buf, int newpos)
  198. {
  199. if (newpos > buf->pos)
  200. return -1;
  201. buf->rpos = newpos;
  202. return newpos;
  203. }
  204. /*--------------------------------------------------------- */
  205. char * konf_buftree_parse(lub_bintree_t * this,
  206. int sock)
  207. {
  208. konf_buf_t *buf;
  209. buf = konf_buftree_find(this, sock);
  210. if (!buf)
  211. return NULL;
  212. return konf_buf_parse(buf);
  213. }
  214. /*--------------------------------------------------------- */
  215. int konf_buf__get_sock(const konf_buf_t * this)
  216. {
  217. return this->sock;
  218. }
  219. int konf_buf__get_len(const konf_buf_t *this)
  220. {
  221. return this->pos;
  222. }