string_buffer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /** **************************************************************************
  2. * string_buffer.h
  3. *
  4. * Copyright 2008 Bryan Ischo <[email protected]>
  5. *
  6. * This file is part of libs3.
  7. *
  8. * libs3 is free software: you can redistribute it and/or modify it under the
  9. * terms of the GNU Lesser General Public License as published by the Free
  10. * Software Foundation, version 3 of the License.
  11. *
  12. * In addition, as a special exception, the copyright holders give
  13. * permission to link the code of this library and its programs with the
  14. * OpenSSL library, and distribute linked combinations including the two.
  15. *
  16. * libs3 is distributed in the hope that it will be useful, but WITHOUT ANY
  17. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  18. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * version 3 along with libs3, in a file named COPYING. If not, see
  23. * <http://www.gnu.org/licenses/>.
  24. *
  25. ************************************************************************** **/
  26. #ifndef STRING_BUFFER_H
  27. #define STRING_BUFFER_H
  28. #include <stdio.h>
  29. // Declare a string_buffer with the given name of the given maximum length
  30. #define string_buffer(name, len) \
  31. char name[len + 1]; \
  32. int name##Len
  33. // Initialize a string_buffer
  34. #define string_buffer_initialize(sb) \
  35. do { \
  36. sb[0] = 0; \
  37. sb##Len = 0; \
  38. } while (0)
  39. // Append [len] bytes of [str] to [sb], setting [all_fit] to 1 if it fit, and
  40. // 0 if it did not
  41. #define string_buffer_append(sb, str, len, all_fit) \
  42. do { \
  43. sb##Len += snprintf(&(sb[sb##Len]), sizeof(sb) - sb##Len - 1, \
  44. "%.*s", (int) (len), str); \
  45. if (sb##Len > (int) (sizeof(sb) - 1)) { \
  46. sb##Len = sizeof(sb) - 1; \
  47. all_fit = 0; \
  48. } \
  49. else { \
  50. all_fit = 1; \
  51. } \
  52. } while (0)
  53. // Declare a string multibuffer with the given name of the given maximum size
  54. #define string_multibuffer(name, size) \
  55. char name[size]; \
  56. int name##Size
  57. // Initialize a string_multibuffer
  58. #define string_multibuffer_initialize(smb) \
  59. do { \
  60. smb##Size = 0; \
  61. } while (0)
  62. // Evaluates to the current string within the string_multibuffer
  63. #define string_multibuffer_current(smb) \
  64. &(smb[smb##Size])
  65. // Adds a new string to the string_multibuffer
  66. #define string_multibuffer_add(smb, str, len, all_fit) \
  67. do { \
  68. smb##Size += (snprintf(&(smb[smb##Size]), \
  69. sizeof(smb) - smb##Size, \
  70. "%.*s", (int) (len), str) + 1); \
  71. if (smb##Size > (int) sizeof(smb)) { \
  72. smb##Size = sizeof(smb); \
  73. all_fit = 0; \
  74. } \
  75. else { \
  76. all_fit = 1; \
  77. } \
  78. } while (0)
  79. // Appends to the current string in the string_multibuffer. There must be a
  80. // current string, meaning that string_multibuffer_add must have been called
  81. // at least once for this string_multibuffer.
  82. #define string_multibuffer_append(smb, str, len, all_fit) \
  83. do { \
  84. smb##Size--; \
  85. string_multibuffer_add(smb, str, len, all_fit); \
  86. } while (0)
  87. #endif /* STRING_BUFFER_H */