dstring.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #ifndef __DSTRING_H
  26. #define __DSTRING_H
  27. #include <cds/sstr.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /** \ingroup cds
  32. * @{
  33. *
  34. * \defgroup cds_dstring Dynamic strings
  35. *
  36. * Dynamic strings were introduced to satisfy needs of presence
  37. * modules when building presence documents.
  38. *
  39. * Dynamic string uses a list of buffers holding data.
  40. * Buffers are allocated when needed - when there is not enough
  41. * space in the last buffer. The whole result can be copied into one
  42. * destination buffer with \ref dstr_get_data, \ref dstr_get_str
  43. * or \ref dstr_get_str_pkg function.
  44. *
  45. * \todo Function with sprintf syntax which will help with
  46. * readibility of code using dynamic strings.
  47. * @{
  48. * */
  49. /** Buffer used by dynamic string.
  50. *
  51. * \todo 'len' and 'used' can be replaced by 'unused' member
  52. * but it doesn't save too much */
  53. typedef struct _dstr_buff_t {
  54. int len; /**< the buffer length */
  55. int used; /**< already used bytes from buffer */
  56. struct _dstr_buff_t *next; /**< pointer to next buffer in the list*/
  57. char data[1]; /** buffer data */
  58. } dstr_buff_t;
  59. /** Dynamic string structure. It is used
  60. * for muliple appends of any strings.
  61. *
  62. * \note There was an attempt to add flags for SHM/PKG memory using, ...
  63. * but it shows that it slows down, thus they were removed and only the
  64. * "most quick" version is used (rather two functions than one with param) */
  65. typedef struct _dstring_t {
  66. /** pointer to the first buffer in the list */
  67. dstr_buff_t *first;
  68. /** pointer to the last buffer in the list */
  69. dstr_buff_t *last;
  70. /** the length of whole string */
  71. int len;
  72. /** predefined buffer size */
  73. int buff_size;
  74. /** a operation on this string was unsuccesfull ->
  75. * all other operations will produce error */
  76. int error;
  77. } dstring_t;
  78. /** Appends zero terminated string to dynamic string.
  79. * \retval 0 if successful
  80. * \retval negative on error */
  81. int dstr_append_zt(dstring_t *dstr, const char *s);
  82. /** Appends string with given length to dynamic string.
  83. * \retval 0 if successful
  84. * \retval negative on error */
  85. int dstr_append(dstring_t *dstr, const char *s, int len);
  86. /** Appends string to dynamic string.
  87. * \retval 0 if successful
  88. * \retval negative on error */
  89. int dstr_append_str(dstring_t *dstr, const str_t *s);
  90. /* int dstr_get_data_length(dstring_t *dstr); */
  91. /** Returns data stored in dynamic string. It does NOT allocate
  92. * space for them - it expects that the buffer is already allocated.
  93. * \retval 0 if successful
  94. * \retval negative on error */
  95. int dstr_get_data(dstring_t *dstr, char *dst);
  96. /** Returns data stored in dynamic string. It allocates space for
  97. * them with cds_malloc (SER's shared memory).
  98. * \retval 0 if successful
  99. * \retval negative on error */
  100. int dstr_get_str(dstring_t *dstr, str_t *dst);
  101. /** Returns data stored in dynamic string. It allocates space for
  102. * them with cds_malloc_pkg (SER's package memory).
  103. * \retval 0 if successful
  104. * \retval negative on error */
  105. int dstr_get_str_pkg(dstring_t *dstr, str_t *dst);
  106. /** Initializes dynamic string.
  107. * \param dstr dynamic string to be initialized
  108. * \param buff_size size of buffer used with this dynamic string
  109. * \retval 0 if successful
  110. * \retval negative on error */
  111. int dstr_init(dstring_t *dstr, int buff_size);
  112. /** Destroys dynamic string. It frees all allocated buffers. */
  113. int dstr_destroy(dstring_t *dstr);
  114. /* returns nozero if error !!! */
  115. /* int dstr_error(dstring_t *dstr);
  116. void dstr_clear_error(dstring_t *dstr); */
  117. /** Macro returning length of data stored in dynamic string. */
  118. #define dstr_get_data_length(dstr) (dstr)->len
  119. /** Macro pointing to error in dynamic string. If set
  120. * there was an error during a previous operation with
  121. * this dynamic string. */
  122. #define dstr_error(dstr) (dstr)->error
  123. /** Macro for cleaning error flag in dynamic string. */
  124. #define dstr_clear_error(dstr) (dstr)->error = 0
  125. /** @}
  126. @} */
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. #endif