From d78034230ab787043350acd5fd6bd9f08cceab56 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 4 May 2017 21:19:13 -0700 Subject: ffi: map (array n bchar) to Lisp string. * ffi.c (struct txr_ffi_type): New bitfield flag, bchar_conv. (ffi_array_in, ffi_array_get): Handle bchar_conv. (ffi_type_compile): Set bchar_conv flag for array of bchar. * lib.c (string_8bit_size): New function. * lib.h (string_8bit_size): Declared. --- ffi.c | 26 ++++++++++++++++++++++++++ lib.c | 9 +++++++++ lib.h | 1 + 3 files changed, 36 insertions(+) diff --git a/ffi.c b/ffi.c index 0cccd46e..a6c872e9 100644 --- a/ffi.c +++ b/ffi.c @@ -96,6 +96,7 @@ struct txr_ffi_type { unsigned null_term : 1; unsigned char_conv : 1; unsigned wchar_conv : 1; + unsigned bchar_conv : 1; void (*put)(struct txr_ffi_type *, val obj, mem_t *dst, val self); val (*get)(struct txr_ffi_type *, mem_t *src, val self); val (*in)(struct txr_ffi_type *, mem_t *src, val obj, val self); @@ -959,6 +960,19 @@ static val ffi_array_in(struct txr_ffi_type *tft, mem_t *src, val vec, } } vec = if3(vec, replace(vec, str, zero, t), str); + } else if (tft->bchar_conv) { + val str; + + if (nelem == 0) { + str = null_string; + } else { + const unsigned char *chptr = coerce(const unsigned char *, src); + if (chptr[tft->size - 1] == 0) + str = string_8bit(chptr); + else + str = string_8bit_size(chptr, tft->size); + } + vec = if3(vec, replace(vec, str, zero, t), str); } else { ucnum offs = 0; struct txr_ffi_type *etft = ffi_type_struct(eltype); @@ -1060,6 +1074,16 @@ static val ffi_array_get(struct txr_ffi_type *tft, mem_t *src, val self) return init_str(ustr, wchptr); } } + } else if (tft->bchar_conv) { + if (nelem == 0) { + return null_string; + } else { + const unsigned char *chptr = coerce(const unsigned char *, src); + if (chptr[tft->size - 1] == 0) + return string_8bit(chptr); + else + return string_8bit_size(chptr, tft->size); + } } else { cnum znelem = if3(tft->null_term && nelem > 0, nelem - 1, nelem); val vec = vector(num_fast(znelem), nil); @@ -1295,6 +1319,8 @@ val ffi_type_compile(val syntax) tft->char_conv = 1; else if (eltype_syntax == wchar_s) tft->wchar_conv = 1; + else if (eltype_syntax == bchar_s) + tft->bchar_conv = 1; return type; } } else if (sym == ptr_in_s) { diff --git a/lib.c b/lib.c index 86485e3c..95a93c01 100644 --- a/lib.c +++ b/lib.c @@ -3300,6 +3300,15 @@ val string_8bit(const unsigned char *str) return string_own(wstr); } +val string_8bit_size(const unsigned char *str, size_t sz) +{ + size_t i; + wchar_t *wstr = chk_wmalloc(sz); + for (i = 0; i < sz; i++) + wstr[i] = str[i]; + return string_own(wstr); +} + val mkstring(val len, val ch) { size_t l = c_num(len); diff --git a/lib.h b/lib.h index 4875ef2a..814b0621 100644 --- a/lib.h +++ b/lib.h @@ -726,6 +726,7 @@ val string_own(wchar_t *str); val string(const wchar_t *str); val string_utf8(const char *str); val string_8bit(const unsigned char *str); +val string_8bit_size(const unsigned char *str, size_t sz); val mkstring(val len, val ch); val mkustring(val len); /* must initialize immediately with init_str! */ val init_str(val str, const wchar_t *); -- cgit v1.2.3