aboutsummaryrefslogtreecommitdiff
path: root/sprinter-text.c
diff options
context:
space:
mode:
authorAustin Clements <amdragon@MIT.EDU>2012-08-02 21:14:49 -0400
committerDavid Bremner <bremner@debian.org>2012-08-03 20:21:29 -0300
commit14883b07003b9ed4223cd8f2c03b301fddae07bd (patch)
tree216ab7263c82a84aff4a03aa1ce759148cc07080 /sprinter-text.c
parent624d1897ce70fde8a41f2ea245db2a5e27f16ce1 (diff)
sprinter: Add a string_len method
This method allows callers to output strings with specific lengths. It's useful both for strings with embedded NULs (which JSON can represent, though parser support is apparently spotty), and non-terminated strings.
Diffstat (limited to 'sprinter-text.c')
-rw-r--r--sprinter-text.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/sprinter-text.c b/sprinter-text.c
index b208840..dfa54b5 100644
--- a/sprinter-text.c
+++ b/sprinter-text.c
@@ -25,14 +25,20 @@ struct sprinter_text {
};
static void
-text_string (struct sprinter *sp, const char *val)
+text_string_len (struct sprinter *sp, const char *val, size_t len)
{
struct sprinter_text *sptxt = (struct sprinter_text *) sp;
if (sptxt->current_prefix != NULL)
fprintf (sptxt->stream, "%s:", sptxt->current_prefix);
- fputs(val, sptxt->stream);
+ fwrite (val, len, 1, sptxt->stream);
+}
+
+static void
+text_string (struct sprinter *sp, const char *val)
+{
+ text_string_len (sp, val, strlen (val));
}
static void
@@ -105,6 +111,7 @@ sprinter_text_create (const void *ctx, FILE *stream)
.begin_list = text_begin_list,
.end = text_end,
.string = text_string,
+ .string_len = text_string_len,
.integer = text_integer,
.boolean = text_boolean,
.null = text_null,