1 /** **************************************************************************
4 * Copyright 2008 Bryan Ischo <bryan@ischo.com>
6 * This file is part of libs3.
8 * libs3 is free software: you can redistribute it and/or modify it under the
9 * terms of the GNU General Public License as published by the Free Software
10 * Foundation, version 3 of the License.
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.
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
21 * You should have received a copy of the GNU General Public License version 3
22 * along with libs3, in a file named COPYING. If not, see
23 * <http://www.gnu.org/licenses/>.
25 ************************************************************************** **/
27 #ifndef STRING_BUFFER_H
28 #define STRING_BUFFER_H
33 // Declare a string_buffer with the given name of the given maximum length
34 #define string_buffer(name, len) \
39 // Initialize a string_buffer
40 #define string_buffer_initialize(sb) \
47 // Append [len] bytes of [str] to [sb], setting [all_fit] to 1 if it fit, and
49 #define string_buffer_append(sb, str, len, all_fit) \
51 sb##Len += snprintf(&(sb[sb##Len]), sizeof(sb) - sb##Len - 1, \
52 "%.*s", (int) (len), str); \
53 if (sb##Len > (int) (sizeof(sb) - 1)) { \
54 sb##Len = sizeof(sb) - 1; \
63 // Declare a string multibuffer with the given name of the given maximum size
64 #define string_multibuffer(name, size) \
69 // Initialize a string_multibuffer
70 #define string_multibuffer_initialize(smb) \
76 // Evaluates to the current string within the string_multibuffer
77 #define string_multibuffer_current(smb) \
81 // Adds a new string to the string_multibuffer
82 #define string_multibuffer_add(smb, str, len, all_fit) \
84 smb##Size += (snprintf(&(smb[smb##Size]), \
85 sizeof(smb) - smb##Size, \
86 "%.*s", (int) (len), str) + 1); \
87 if (smb##Size > (int) sizeof(smb)) { \
88 smb##Size = sizeof(smb); \
97 // Appends to the current string in the string_multibuffer. There must be a
98 // current string, meaning that string_multibuffer_add must have been called
99 // at least once for this string_multibuffer.
100 #define string_multibuffer_append(smb, str, len, all_fit) \
103 string_multibuffer_add(smb, str, len, all_fit); \
107 #endif /* STRING_BUFFER_H */