1
0

string_buffer.h 5.1 KB

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