A mail formatter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

205 lines
4.2 KiB

  1. /*
  2. * sbuffer - Incenp.org Notch Library: string buffer module
  3. * Copyright (C) 2011,2018 Damien Goutte-Gattat
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <sbuffer.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <xmem.h>
  26. #define AVAILABLE_SIZE(s) ((s)->size - (s)->len)
  27. #define CURSOR(s) ((s)->buffer + (s)->len)
  28. #define GROW(s,l) \
  29. do { \
  30. size_t ns = ((((s)->len + (l)) / (s)->block) + 1) * (s)->block; \
  31. char *np = xrealloc((s)->buffer, ns); \
  32. if ( ! np ) \
  33. (s)->error = errno; \
  34. else { \
  35. (s)->buffer = np; \
  36. (s)->size = ns; \
  37. } \
  38. } while ( 0 )
  39. int
  40. sb_init(string_buffer_t *s, size_t block_size)
  41. {
  42. if ( ! s ) {
  43. errno = EINVAL;
  44. return -1;
  45. }
  46. s->buffer = NULL;
  47. s->len = s->size = 0;
  48. s->block = block_size ? block_size : 128;
  49. s->error = 0;
  50. return 0;
  51. }
  52. const char *
  53. sb_get(string_buffer_t *s)
  54. {
  55. const char *str = NULL;
  56. if ( ! s )
  57. errno = EINVAL;
  58. else if ( s->error )
  59. errno = s->error;
  60. else
  61. str = s->buffer;
  62. return str;
  63. }
  64. char *
  65. sb_get_copy(string_buffer_t *s)
  66. {
  67. char *str = NULL;
  68. if ( ! s )
  69. errno = EINVAL;
  70. else if ( s->error )
  71. errno = s->error;
  72. else if ( (str = xmalloc(s->len + 1)) )
  73. strncpy(str, s->buffer, s->len + 1);
  74. return str;
  75. }
  76. int
  77. sb_empty(string_buffer_t *s)
  78. {
  79. if ( ! s ) {
  80. errno = EINVAL;
  81. return -1;
  82. }
  83. s->len = 0;
  84. if ( s->buffer )
  85. s->buffer[0] = '\0';
  86. return 0;
  87. }
  88. int
  89. sb_addc(string_buffer_t *s, char c)
  90. {
  91. if ( ! s ) {
  92. errno = EINVAL;
  93. return -1;
  94. }
  95. if ( AVAILABLE_SIZE(s) < 2 )
  96. GROW(s, 2);
  97. if ( ! s->error ) {
  98. *(s->buffer + s->len++) = c;
  99. *(s->buffer + s->len) = '\0';
  100. }
  101. return 0;
  102. }
  103. int
  104. sb_add(string_buffer_t *s, const char *buffer)
  105. {
  106. if ( ! buffer ) {
  107. errno = EINVAL;
  108. return -1;
  109. }
  110. return sb_addn(s, buffer, strlen(buffer));
  111. }
  112. int
  113. sb_addn(string_buffer_t *s, const char *buffer, size_t len)
  114. {
  115. if ( ! s || ! buffer ) {
  116. errno = EINVAL;
  117. return -1;
  118. }
  119. if ( AVAILABLE_SIZE(s) < len + 1)
  120. GROW(s, len + 1);
  121. if ( ! s->error ) {
  122. strncat(CURSOR(s), buffer, len);
  123. s->len += len;
  124. }
  125. return 0;
  126. }
  127. int
  128. sb_vaddf(string_buffer_t *s, const char *fmt, va_list ap)
  129. {
  130. int written;
  131. va_list aq;
  132. if ( ! s || ! fmt ) {
  133. errno = EINVAL;
  134. return -1;
  135. }
  136. if ( ! s->size )
  137. GROW(s, s->block);
  138. if ( s->error )
  139. return 0;
  140. va_copy(aq, ap);
  141. written = vsnprintf(CURSOR(s), AVAILABLE_SIZE(s), fmt, aq);
  142. va_end(aq);
  143. if ( written >= (int)AVAILABLE_SIZE(s) ) {
  144. GROW(s, written);
  145. if ( s->error )
  146. return 0;
  147. vsprintf(CURSOR(s), fmt, ap);
  148. }
  149. s->len += written;
  150. return 0;
  151. }
  152. int
  153. sb_addf(string_buffer_t *s, const char *fmt, ...)
  154. {
  155. int ret = 0;
  156. va_list ap;
  157. if ( ! s || ! fmt ) {
  158. errno = EINVAL;
  159. return -1;
  160. }
  161. if ( ! s->error ) {
  162. va_start(ap, fmt);
  163. ret = sb_vaddf(s, fmt, ap);
  164. va_end(ap);
  165. }
  166. return ret;
  167. }