summaryrefslogtreecommitdiff
path: root/inc/string_buffer.h
diff options
context:
space:
mode:
Diffstat (limited to 'inc/string_buffer.h')
-rw-r--r--inc/string_buffer.h105
1 files changed, 105 insertions, 0 deletions
diff --git a/inc/string_buffer.h b/inc/string_buffer.h
new file mode 100644
index 0000000..1deeb51
--- /dev/null
+++ b/inc/string_buffer.h
@@ -0,0 +1,105 @@
+/** **************************************************************************
+ * string_buffer.h
+ *
+ * Copyright 2008 Bryan Ischo <bryan@ischo.com>
+ *
+ * This program 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; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program 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 along
+ * with this program; if not, write to the
+ *
+ * Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ ************************************************************************** **/
+
+#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", len, str); \
+ if (sb##Len > (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", len, str) + 1); \
+ if (smb##Size > 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 */