From 791906e45d3ded91eba11c8544e0e1e6d1ee3170 Mon Sep 17 00:00:00 2001 From: Henrik Andersson Date: Fri, 29 Sep 2017 15:41:56 +0200 Subject: [PATCH] Add function to write fixed length utf16 string to stream --- stream.c | 69 ++++++++++++++++++++++++++++++++++++++++---------------- stream.h | 1 + 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/stream.c b/stream.c index 866697e..4c9b4b0 100644 --- a/stream.c +++ b/stream.c @@ -77,33 +77,19 @@ local_to_utf16() return icv; } -/* Writes a utf16 encoded string into stream including a null - termination. This function assumes that input string is ASCII - compatible, such as UTF-8. -*/ -void -out_utf16s(STREAM s, const char *string) -{ - out_utf16s_no_eos(s, string); - - // append utf16 null termination - out_uint16(s, 0); -} - - /* Writes a utf16 encoded string into stream excluding null termination. This function assumes that input is ASCII compatible, such as UTF-8. */ -void -out_utf16s_no_eos(STREAM s, const char *string) +static inline size_t +_out_utf16s(STREAM s, size_t maxlength, const char *string) { static iconv_t icv_local_to_utf16; - size_t ibl, obl; + size_t bl, ibl, obl; const char *pin; char *pout; if (string == NULL) - return; + return 0; if (!icv_local_to_utf16) { @@ -111,7 +97,7 @@ out_utf16s_no_eos(STREAM s, const char *string) } ibl = strlen(string); - obl = s_left(s); + obl = maxlength ? maxlength : s_left(s); pin = string; pout = (char *) s->p; @@ -121,5 +107,50 @@ out_utf16s_no_eos(STREAM s, const char *string) abort(); } + bl = (unsigned char*)pout - s->p; + s->p = (unsigned char *)pout; + + return bl; +} + +/* Writes a utf16 encoded string into stream including a null + termination. The length is fixed and defined by width. If result is + longer than specified width, the string is truncated. pad, + specifies a character to pad with. +*/ +void +out_utf16s_padded(STREAM s, const char *string, size_t length, unsigned char pad) +{ + int i; + size_t bl; + bl = _out_utf16s(s, length - 2, string); + + // append utf16 null termination + out_uint16(s, 0); + bl += 2; + + for (i = 0; i < (length - bl); i++) + out_uint8(s, pad); +} + +/* Writes a utf16 encoded string into stream including a null + termination. +*/ +void +out_utf16s(STREAM s, const char *string) +{ + _out_utf16s(s, 0, string); + + // append utf16 null termination + out_uint16(s, 0); +} + + +/* Writes a utf16 encoded string into stream excluding null + termination. */ +void +out_utf16s_no_eos(STREAM s, const char *string) +{ + _out_utf16s(s, 0, string); } diff --git a/stream.h b/stream.h index bb4a54f..cbf3606 100644 --- a/stream.h +++ b/stream.h @@ -44,6 +44,7 @@ void s_free(STREAM s); void s_reset(STREAM s); void out_utf16s(STREAM s, const char *string); +void out_utf16s_padded(STREAM s, const char *string, size_t width, unsigned char pad); void out_utf16s_no_eos(STREAM s, const char *string); #define s_push_layer(s,h,n) { (s)->h = (s)->p; (s)->p += n; }