string_buffer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // WINSCP (CodeGuard compatible implementation)
  42. #define string_buffer_append(sb, str, len, all_fit) \
  43. { \
  44. int _sbai = sizeof(sb) - sb##Len - 1; \
  45. if (_sbai > (int)len) \
  46. { \
  47. _sbai = len; \
  48. } \
  49. strncat(sb, str, _sbai); \
  50. sb##Len += _sbai; \
  51. }
  52. // Declare a string multibuffer with the given name of the given maximum size
  53. #define string_multibuffer(name, size) \
  54. char name[size]; \
  55. int name##Size
  56. // Initialize a string_multibuffer
  57. #define string_multibuffer_initialize(smb) \
  58. do { \
  59. smb##Size = 0; \
  60. } while (0)
  61. // Evaluates to the current string within the string_multibuffer
  62. #define string_multibuffer_current(smb) \
  63. &(smb[smb##Size])
  64. // Adds a new string to the string_multibuffer
  65. #define string_multibuffer_add(smb, str, len, all_fit) \
  66. do { \
  67. smb##Size += (snprintf(&(smb[smb##Size]), \
  68. sizeof(smb) - smb##Size, \
  69. "%.*s", (int) (len), str) + 1); \
  70. if (smb##Size > (int) sizeof(smb)) { \
  71. smb##Size = sizeof(smb); \
  72. all_fit = 0; \
  73. } \
  74. else { \
  75. all_fit = 1; \
  76. } \
  77. } while (0)
  78. // Appends to the current string in the string_multibuffer. There must be a
  79. // current string, meaning that string_multibuffer_add must have been called
  80. // at least once for this string_multibuffer.
  81. #define string_multibuffer_append(smb, str, len, all_fit) \
  82. do { \
  83. smb##Size--; \
  84. string_multibuffer_add(smb, str, len, all_fit); \
  85. } while (0)
  86. #endif /* STRING_BUFFER_H */