strbuf.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* strbuf - String buffer routines
  2. *
  3. * Copyright (c) 2010-2012 Mark Pulford <mark@kyne.com.au>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #include "c_stdlib.h"
  25. #include "c_stdarg.h"
  26. #include "user_config.h"
  27. /* Size: Total bytes allocated to *buf
  28. * Length: String length, excluding optional NULL terminator.
  29. * Increment: Allocation increments when resizing the string buffer.
  30. * Dynamic: True if created via strbuf_new()
  31. */
  32. typedef struct {
  33. char *buf;
  34. int size;
  35. int length;
  36. int increment;
  37. int dynamic;
  38. int reallocs;
  39. int debug;
  40. } strbuf_t;
  41. #ifndef STRBUF_DEFAULT_SIZE
  42. #define STRBUF_DEFAULT_SIZE 1023
  43. #endif
  44. #ifndef STRBUF_DEFAULT_INCREMENT
  45. #define STRBUF_DEFAULT_INCREMENT -2
  46. #endif
  47. /* Initialise */
  48. extern strbuf_t *strbuf_new(int len);
  49. extern int strbuf_init(strbuf_t *s, int len);
  50. extern int strbuf_set_increment(strbuf_t *s, int increment);
  51. /* Release */
  52. extern void strbuf_free(strbuf_t *s);
  53. extern char *strbuf_free_to_string(strbuf_t *s, int *len);
  54. /* Management */
  55. extern int strbuf_resize(strbuf_t *s, int len);
  56. static int strbuf_empty_length(strbuf_t *s);
  57. static int strbuf_length(strbuf_t *s);
  58. static char *strbuf_string(strbuf_t *s, int *len);
  59. static void strbuf_ensure_empty_length(strbuf_t *s, int len);
  60. static char *strbuf_empty_ptr(strbuf_t *s);
  61. static void strbuf_extend_length(strbuf_t *s, int len);
  62. /* Update */
  63. extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...);
  64. extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);
  65. static void strbuf_append_mem(strbuf_t *s, const char *c, int len);
  66. extern void strbuf_append_string(strbuf_t *s, const char *str);
  67. static void strbuf_append_char(strbuf_t *s, const char c);
  68. static void strbuf_ensure_null(strbuf_t *s);
  69. /* Reset string for before use */
  70. static inline void strbuf_reset(strbuf_t *s)
  71. {
  72. s->length = 0;
  73. }
  74. static inline int strbuf_allocated(strbuf_t *s)
  75. {
  76. return s->buf != NULL;
  77. }
  78. /* Return bytes remaining in the string buffer
  79. * Ensure there is space for a NULL terminator. */
  80. static inline int strbuf_empty_length(strbuf_t *s)
  81. {
  82. return s->size - s->length - 1;
  83. }
  84. static inline void strbuf_ensure_empty_length(strbuf_t *s, int len)
  85. {
  86. if (len > strbuf_empty_length(s))
  87. strbuf_resize(s, s->length + len);
  88. }
  89. static inline char *strbuf_empty_ptr(strbuf_t *s)
  90. {
  91. return s->buf + s->length;
  92. }
  93. static inline void strbuf_extend_length(strbuf_t *s, int len)
  94. {
  95. s->length += len;
  96. }
  97. static inline int strbuf_length(strbuf_t *s)
  98. {
  99. return s->length;
  100. }
  101. static inline void strbuf_append_char(strbuf_t *s, const char c)
  102. {
  103. strbuf_ensure_empty_length(s, 1);
  104. s->buf[s->length++] = c;
  105. }
  106. static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
  107. {
  108. s->buf[s->length++] = c;
  109. }
  110. static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)
  111. {
  112. strbuf_ensure_empty_length(s, len);
  113. c_memcpy(s->buf + s->length, c, len);
  114. s->length += len;
  115. }
  116. static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)
  117. {
  118. c_memcpy(s->buf + s->length, c, len);
  119. s->length += len;
  120. }
  121. static inline void strbuf_ensure_null(strbuf_t *s)
  122. {
  123. s->buf[s->length] = 0;
  124. }
  125. static inline char *strbuf_string(strbuf_t *s, int *len)
  126. {
  127. if (len)
  128. *len = s->length;
  129. return s->buf;
  130. }
  131. /* vi:ai et sw=4 ts=4:
  132. */