summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-06-11 17:00:43 -0700
committerVito Caputo <vcaputo@pengaru.com>2023-06-12 16:16:12 -0700
commit3398e156c561981ce30f4847ac56458c875f8316 (patch)
treef45b8a8aae6b332f2e3dee45ee3f6015cbd1fc26 /src
parent7f7272b5b87d3baf8051a6856507b4b2618765c9 (diff)
til_str: add preliminary growable string type
Preparation for eliminating open_memstream() usage...
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am2
-rw-r--r--src/til_str.c180
-rw-r--r--src/til_str.h14
3 files changed, 195 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index b5f4e62..5da7150 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,7 +1,7 @@
SUBDIRS = libs modules
noinst_LTLIBRARIES = libtil.la
-libtil_la_SOURCES = til.c til.h til_args.c til_args.h til_fb.c til_fb.h til_jenkins.c til_jenkins.h til_limits.h til_module_context.c til_module_context.h til_settings.h til_settings.c til_setup.c til_setup.h til_stream.c til_stream.h til_tap.h til_threads.c til_threads.h til_util.c til_util.h
+libtil_la_SOURCES = til.c til.h til_args.c til_args.h til_fb.c til_fb.h til_jenkins.c til_jenkins.h til_limits.h til_module_context.c til_module_context.h til_settings.h til_settings.c til_setup.c til_setup.h til_str.c til_str.h til_stream.c til_stream.h til_tap.h til_threads.c til_threads.h til_util.c til_util.h
libtil_la_CPPFLAGS = -I@top_srcdir@/src
libtil_la_LIBADD = modules/blinds/libblinds.la modules/checkers/libcheckers.la modules/compose/libcompose.la modules/drizzle/libdrizzle.la modules/flui2d/libflui2d.la modules/julia/libjulia.la modules/meta2d/libmeta2d.la modules/mixer/libmixer.la modules/moire/libmoire.la modules/montage/libmontage.la modules/pixbounce/libpixbounce.la modules/plasma/libplasma.la modules/plato/libplato.la modules/ray/libray.la modules/rkt/librkt.la modules/roto/libroto.la modules/rtv/librtv.la modules/shapes/libshapes.la modules/snow/libsnow.la modules/sparkler/libsparkler.la modules/spiro/libspiro.la modules/stars/libstars.la modules/strobe/libstrobe.la modules/submit/libsubmit.la modules/swab/libswab.la modules/swarm/libswarm.la modules/voronoi/libvoronoi.la libs/grid/libgrid.la libs/puddle/libpuddle.la libs/ray/libray.la libs/rocket/librocket.la libs/sig/libsig.la libs/txt/libtxt.la libs/ascii/libascii.la libs/din/libdin.la
diff --git a/src/til_str.c b/src/til_str.c
new file mode 100644
index 0000000..442e923
--- /dev/null
+++ b/src/til_str.c
@@ -0,0 +1,180 @@
+#include <assert.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "til_str.h"
+#include "til_util.h"
+
+/* This implements very rudimentary growable strings, and hasn't been optimized at all.
+ *
+ * The impetus for adding this is to get rid of the open_memstream() usage which is mostly
+ * just being used as a convenient way to build up a buffer from format strings. So this
+ * basically implements just that ability using variadic functions...
+ *
+ * Why no open_memstream()? Because Windows, that's why. For some reason even mingw doesn't
+ * have open_memstream(). FILE* I keep forgetting that you're dead to me.
+ */
+
+struct til_str_t {
+ struct {
+ size_t allocated, used; /* used is length, including '\0' terminator */
+ } size;
+ char *buf;
+};
+
+
+#define TIL_STR_MIN_SIZE 64
+
+
+/* alloc always returns a buf w/nul terminator present */
+static til_str_t * til_str_nulstr(size_t minsize)
+{
+ til_str_t *str;
+
+ str = calloc(1, sizeof(*str));
+ if (!str)
+ return NULL;
+
+ str->size.used = 1;
+ str->size.allocated = MAX(minsize, TIL_STR_MIN_SIZE);
+
+ str->buf = calloc(1, str->size.allocated);
+ if (!str->buf) {
+ free(str);
+ return NULL;
+ }
+
+ return str;
+}
+
+
+/* allocate a new til_str, starting with a dup of seed, just use "" for an empty str, there is no NULL str */
+til_str_t * til_str_new(const char *seed)
+{
+ til_str_t *str;
+ size_t len;
+
+ assert(seed);
+
+ len = strlen(seed);
+ str = til_str_nulstr(len + 1);
+ if (!str)
+ return NULL;
+
+ memcpy(str->buf, seed, len);
+ str->size.used += len;
+
+ return str;
+}
+
+
+void * til_str_free(til_str_t *str)
+{
+ if (str) {
+ free(str->buf);
+ free(str);
+ }
+
+ return NULL;
+}
+
+
+/* allocate a new til_str from a fmt string + args */
+til_str_t * til_str_newf(const char *format, ...)
+{
+ til_str_t *str;
+ va_list ap;
+
+ assert(format);
+
+ va_start(ap, format);
+
+ str = til_str_nulstr(vsnprintf(NULL, 0, format, ap) + 1);
+ if (!str)
+ return NULL;
+
+ str->size.used += vsnprintf(str->buf, str->size.allocated, format, ap);
+
+ va_end(ap);
+
+ assert(str->size.used <= str->size.allocated);
+
+ return str;
+}
+
+
+/* append to an existing til_str_t from a fmt string + args */
+int til_str_appendf(til_str_t *str, const char *format, ...)
+{
+ size_t len;
+ va_list ap;
+
+ assert(str);
+ assert(format);
+
+ va_start(ap, format);
+ len = vsnprintf(NULL, 0, format, ap);
+ va_end(ap);
+
+ if (str->size.used + len > str->size.allocated) {
+ char *new;
+
+ new = realloc(str->buf, str->size.used + len);
+ if (!new)
+ return -ENOMEM;
+
+ str->buf = new;
+ str->size.allocated = str->size.used + len;
+ }
+
+ va_start(ap, format);
+ str->size.used += vsnprintf(&str->buf[str->size.used - 1],
+ str->size.allocated - (str->size.used - 1),
+ format, ap);
+ va_end(ap);
+
+ assert(str->size.used <= str->size.allocated);
+
+ return 0;
+}
+
+
+/* strdup() the /used/ contents of str */
+char * til_str_strdup(const til_str_t *str)
+{
+ assert(str);
+
+ return strdup(str->buf);
+}
+
+
+/* a valid \0-terminated string is _always_ maintained @ str->buf so callers can just use it as a string..
+ * but must not hang onto that pointer across more til_str() calls on the same str.
+ */
+const char * til_str_buf(const til_str_t *str, size_t *res_len)
+{
+ assert(str);
+
+ if (res_len)
+ *res_len = str->size.used;
+
+ return str->buf;
+}
+
+
+/* for when you just want the buf without the dup overhead, and don't need the str anymore */
+char * til_str_to_buf(til_str_t *str, size_t *res_len)
+{
+ char *buf;
+
+ assert(str);
+
+ if (res_len)
+ *res_len = str->size.used;
+
+ buf = str->buf;
+ free(str);
+
+ return buf;
+}
diff --git a/src/til_str.h b/src/til_str.h
new file mode 100644
index 0000000..e653884
--- /dev/null
+++ b/src/til_str.h
@@ -0,0 +1,14 @@
+#ifndef _TIL_STR_H
+#define _TIL_STR_H
+
+typedef struct til_str_t til_str_t;
+
+til_str_t * til_str_new(const char *seed);
+void * til_str_free(til_str_t *str);
+til_str_t * til_str_newf(const char *format, ...);
+int til_str_appendf(til_str_t *str, const char *format, ...);
+char * til_str_strdup(const til_str_t *str);
+const char * til_str_buf(const til_str_t *str, size_t *res_len);
+char * til_str_to_buf(til_str_t *str, size_t *res_len);
+
+#endif
© All Rights Reserved