strbuf.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* strbuf - String buffer routines
  2. *
  3. * Copyright (c) 2010-2011 Mark Pulford <[email protected]>
  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 <stdlib.h>
  25. #include <stdarg.h>
  26. /* Size: Total bytes allocated to *buf
  27. * Length: String length, excluding optional NULL terminator.
  28. * Increment: Allocation increments when resizing the string buffer.
  29. * Dynamic: True if created via strbuf_new()
  30. */
  31. typedef struct {
  32. char *buf;
  33. int size;
  34. int length;
  35. int increment;
  36. int dynamic;
  37. int reallocs;
  38. int debug;
  39. } strbuf_t;
  40. #ifndef STRBUF_DEFAULT_SIZE
  41. #define STRBUF_DEFAULT_SIZE 1023
  42. #endif
  43. #ifndef STRBUF_DEFAULT_INCREMENT
  44. #define STRBUF_DEFAULT_INCREMENT -2
  45. #endif
  46. /* Initialise */
  47. extern strbuf_t *strbuf_new(int len);
  48. extern void strbuf_init(strbuf_t *s, int len);
  49. extern void strbuf_set_increment(strbuf_t *s, int increment);
  50. /* Release */
  51. extern void strbuf_free(strbuf_t *s);
  52. extern char *strbuf_free_to_string(strbuf_t *s, int *len);
  53. /* Management */
  54. extern void strbuf_resize(strbuf_t *s, int len);
  55. static int strbuf_empty_length(strbuf_t *s);
  56. static int strbuf_length(strbuf_t *s);
  57. static char *strbuf_string(strbuf_t *s, int *len);
  58. static void strbuf_ensure_empty_length(strbuf_t *s, int len);
  59. /* Update */
  60. extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...);
  61. extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);
  62. static void strbuf_append_mem(strbuf_t *s, const char *c, int len);
  63. extern void strbuf_append_string(strbuf_t *s, const char *str);
  64. static void strbuf_append_char(strbuf_t *s, const char c);
  65. static void strbuf_ensure_null(strbuf_t *s);
  66. /* Reset string for before use */
  67. static inline void strbuf_reset(strbuf_t *s)
  68. {
  69. s->length = 0;
  70. }
  71. static inline int strbuf_allocated(strbuf_t *s)
  72. {
  73. return s->buf != NULL;
  74. }
  75. /* Return bytes remaining in the string buffer
  76. * Ensure there is space for a NULL terminator. */
  77. static inline int strbuf_empty_length(strbuf_t *s)
  78. {
  79. return s->size - s->length - 1;
  80. }
  81. static inline void strbuf_ensure_empty_length(strbuf_t *s, int len)
  82. {
  83. if (len > strbuf_empty_length(s))
  84. strbuf_resize(s, s->length + len);
  85. }
  86. static inline int strbuf_length(strbuf_t *s)
  87. {
  88. return s->length;
  89. }
  90. static inline void strbuf_append_char(strbuf_t *s, const char c)
  91. {
  92. strbuf_ensure_empty_length(s, 1);
  93. s->buf[s->length++] = c;
  94. }
  95. static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
  96. {
  97. s->buf[s->length++] = c;
  98. }
  99. static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)
  100. {
  101. strbuf_ensure_empty_length(s, len);
  102. memcpy(s->buf + s->length, c, len);
  103. s->length += len;
  104. }
  105. static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)
  106. {
  107. memcpy(s->buf + s->length, c, len);
  108. s->length += len;
  109. }
  110. static inline void strbuf_ensure_null(strbuf_t *s)
  111. {
  112. s->buf[s->length] = 0;
  113. }
  114. static inline char *strbuf_string(strbuf_t *s, int *len)
  115. {
  116. if (len)
  117. *len = s->length;
  118. return s->buf;
  119. }
  120. /* vi:ai et sw=4 ts=4:
  121. */