src/utils_strbuf.[ch]: Add string buffer utility.
[collectd.git] / src / utils_strbuf.h
diff --git a/src/utils_strbuf.h b/src/utils_strbuf.h
new file mode 100644 (file)
index 0000000..95f3276
--- /dev/null
@@ -0,0 +1,52 @@
+/**
+ * collectd - src/utils_strbuf.h
+ * Copyright (C) 2018  Florian Forster
+ *
+ * MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Authors:
+ *   Florian Forster <octo at collectd.org>
+ **/
+
+#ifndef UTILS_STRBUF_H
+#define UTILS_STRBUF_H 1
+
+#include <sys/types.h> /* for size_t */
+
+struct strbuf_s {
+  char *buffer;
+  size_t
+      free; /* available chars in the buffer, including the terminating nil. */
+  size_t used; /* used chars in the buffer, excluding the terminating nil. */
+};
+typedef struct strbuf_s strbuf_t;
+
+strbuf_t *strbuf_create(size_t sz);
+void strbuf_destroy(strbuf_t *buf);
+
+void strbuf_reset(strbuf_t *buf);
+
+int strbuf_add(strbuf_t *buf, char const *str);
+
+__attribute__((format(printf, 2, 3))) int strbuf_addf(strbuf_t *buf,
+                                                      char const *format, ...);
+
+#endif /* UTILS_STRBUF_H */