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