dstring.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <cds/dstring.h>
  28. #include <cds/memory.h>
  29. #include <cds/list.h>
  30. #include <cds/logger.h>
  31. #define get_current_buffer(dstr) (dstr)->last
  32. static dstr_buff_t *add_new_buffer(dstring_t *dstr)
  33. {
  34. dstr_buff_t *buff = NULL;
  35. /* e = dlink_element_alloc_pkg(sizeof(dstr_buff_t) + dstr->buff_size); */
  36. /* if (dstr->flags & DSTR_PKG_MEM)
  37. buff = cds_malloc_pkg(sizeof(dstr_buff_t) + dstr->buff_size);
  38. else
  39. buff = cds_malloc(sizeof(dstr_buff_t) + dstr->buff_size);
  40. */
  41. /* buff = cds_malloc(sizeof(dstr_buff_t) + dstr->buff_size);*/
  42. buff = cds_malloc_pkg(sizeof(dstr_buff_t) + dstr->buff_size);
  43. if (buff) {
  44. buff->len = dstr->buff_size;
  45. buff->used = 0;
  46. buff->next = NULL;
  47. LINKED_LIST_ADD(dstr->first, dstr->last, buff);
  48. }
  49. else dstr->error = 1;
  50. return buff;
  51. }
  52. int dstr_append(dstring_t *dstr, const char *s, int len)
  53. {
  54. int size;
  55. dstr_buff_t *buff;
  56. /* if (!dstr) return -1; */
  57. if (dstr->error) return -2;
  58. if (len == 0) return 0; /*append empty string*/
  59. buff = get_current_buffer(dstr);
  60. if (!buff) buff = add_new_buffer(dstr);
  61. while ((len > 0) && (buff)) {
  62. size = buff->len - buff->used;
  63. if (size > len) size = len;
  64. memcpy(buff->data + buff->used, s, size);
  65. buff->used += size;
  66. len -= size;
  67. s += size;
  68. dstr->len += size;
  69. if (len > 0) buff = add_new_buffer(dstr);
  70. }
  71. if (!buff) {
  72. dstr->error = 1;
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. int dstr_append_zt(dstring_t *dstr, const char *s)
  78. {
  79. /* if (!dstr) return -1; */
  80. if (!s) return 0; /*append empty string*/
  81. return dstr_append(dstr, s, strlen(s));
  82. }
  83. int dstr_append_str(dstring_t *dstr, const str_t *s)
  84. {
  85. /* if (!dstr) return -1; */
  86. if (!s) return 0; /*append empty string*/
  87. return dstr_append(dstr, s->s, s->len);
  88. }
  89. /* int dstr_get_data_length(dstring_t *dstr)
  90. {
  91. if (!dstr) return 0;
  92. else return dstr->len;
  93. } */
  94. int dstr_get_data(dstring_t *dstr, char *dst)
  95. {
  96. dstr_buff_t* buff;
  97. /* if (!dstr) return -1; */
  98. if (dstr->error) return -2; /* a previous operation returned error */
  99. buff = dstr->first;
  100. while (buff) {
  101. memcpy(dst, buff->data, buff->used);
  102. dst += buff->used;
  103. buff = buff->next;
  104. }
  105. return 0;
  106. }
  107. int dstr_get_str(dstring_t *dstr, str_t *dst)
  108. {
  109. int res = 0;
  110. if (!dst) return -1;
  111. if (dstr->error) {
  112. dst->s = NULL;
  113. dst->len = 0;
  114. return -2; /* a previous operation returned error */
  115. }
  116. dst->len = dstr_get_data_length(dstr);
  117. if (dst->len > 0) {
  118. dst->s = (char*)cds_malloc(dst->len);
  119. if (!dst->s) {
  120. res = -1;
  121. dst->len = 0;
  122. }
  123. else res = dstr_get_data(dstr, dst->s);
  124. }
  125. else {
  126. dst->s = NULL;
  127. dst->len = 0;
  128. }
  129. return res;
  130. }
  131. int dstr_get_str_pkg(dstring_t *dstr, str_t *dst)
  132. {
  133. int res = 0;
  134. if (!dst) return -1;
  135. if (dstr->error) {
  136. dst->s = NULL;
  137. dst->len = 0;
  138. return -2; /* a previous operation returned error */
  139. }
  140. dst->len = dstr_get_data_length(dstr);
  141. if (dst->len > 0) {
  142. dst->s = (char*)cds_malloc_pkg(dst->len);
  143. if (!dst->s) {
  144. res = -1;
  145. dst->len = 0;
  146. }
  147. else res = dstr_get_data(dstr, dst->s);
  148. }
  149. else {
  150. dst->s = NULL;
  151. dst->len = 0;
  152. }
  153. return res;
  154. }
  155. int dstr_init(dstring_t *dstr, int buff_size)
  156. {
  157. /* if (!dstr) return -1; */
  158. dstr->buff_size = buff_size;
  159. dstr->len = 0;
  160. dstr->error = 0;
  161. dstr->first = 0;
  162. dstr->last = 0;
  163. return 0;
  164. }
  165. int dstr_destroy(dstring_t *dstr)
  166. {
  167. dstr_buff_t *e,*n;
  168. /* if (!dstr) return -1; */
  169. /* dlink_destroy(&dstr->buffers); */
  170. e = dstr->first;
  171. while (e) {
  172. n = e->next;
  173. /* if (dstr->flags & DSTR_PKG_MEM) cds_free_pkg(e);
  174. else cds_free(e);*/
  175. /* cds_free(e);*/
  176. cds_free_pkg(e);
  177. e = n;
  178. }
  179. dstr->first = 0;
  180. dstr->last = 0;
  181. return 0;
  182. }
  183. /* int dstr_error(dstring_t *dstr)
  184. {
  185. if (dstr) return dstr->error;
  186. else return -1;
  187. }
  188. void dstr_clear_error(dstring_t *dstr)
  189. {
  190. if (dstr) dstr->error = 0;
  191. }
  192. */