diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-05-04 20:40:55 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-05-04 20:40:55 -0700 |
commit | e5186dace2ce5c13c2a178b67507a3f917f4ed25 (patch) | |
tree | 365929fae624706089d854ee9c50cb7dc3378e5c /ffi.c | |
parent | 7246e3eff36f290b207b203c40f90850ddd031e8 (diff) | |
download | txr-e5186dace2ce5c13c2a178b67507a3f917f4ed25.tar.gz txr-e5186dace2ce5c13c2a178b67507a3f917f4ed25.tar.bz2 txr-e5186dace2ce5c13c2a178b67507a3f917f4ed25.zip |
ffi: new bstr type.
The bstr type is like str, but doesn't perform UTF-8
conversion. The C data is assumed to be null terminated byte
strings representing code points U+0000 through U+00FF.
* ffi.c (bstr_s, bstr_d_s): New symbol variables.
(ffi_bstr_put, ffi_bstr_get, ffi_bstr_d_get): New static
functions.
(ffi_init_types): Register bstr and bstr-d types.
(ffi_init): Initialize bstr_s and bstr_d_s.
* ffi.h (bstr_s, bstr_d_s): Declared.
* lib.c (chk_strdup_8bit, string_8bit): New function.
* lib.h (chk_strdup_8bit, string_8bit): Declared.
Diffstat (limited to 'ffi.c')
-rw-r--r-- | ffi.c | 36 |
1 files changed, 35 insertions, 1 deletions
@@ -73,7 +73,7 @@ val array_s, zarray_s; val struct_s; -val str_d_s, wstr_s, wstr_d_s; +val str_d_s, wstr_s, wstr_d_s, bstr_s, bstr_d_s; val buf_d_s; @@ -622,6 +622,32 @@ static val ffi_wstr_d_get(struct txr_ffi_type *tft, mem_t *src, val self) return p ? string_own(p) : nil; } +static void ffi_bstr_put(struct txr_ffi_type *tft, val s, mem_t *dst, + val self) +{ + if (s == nil) { + *coerce(unsigned char **, dst) = 0; + } else { + const wchar_t *ws = c_str(s); + unsigned char *u8s = chk_strdup_8bit(ws); + *coerce(unsigned char **, dst) = u8s; + } +} + +static val ffi_bstr_get(struct txr_ffi_type *tft, mem_t *src, val self) +{ + unsigned char *p = *coerce(unsigned char **, src); + return p ? string_8bit(p) : nil; +} + +static val ffi_bstr_d_get(struct txr_ffi_type *tft, mem_t *src, val self) +{ + unsigned char *p = *coerce(unsigned char **, src); + val ret = p ? string_8bit(p) : nil; + free(p); + return ret; +} + static void ffi_buf_put(struct txr_ffi_type *tft, val buf, mem_t *dst, val self) { @@ -1442,6 +1468,12 @@ static void ffi_init_types(void) ffi_typedef(wstr_d_s, make_ffi_type_builtin(wstr_d_s, str_s, sizeof (mem_t *), &ffi_type_pointer, ffi_wstr_d_put, ffi_wstr_d_get)); + ffi_typedef(bstr_s, make_ffi_type_builtin(bstr_s, str_s, + sizeof (mem_t *), &ffi_type_pointer, + ffi_bstr_put, ffi_bstr_get)); + ffi_typedef(bstr_d_s, make_ffi_type_builtin(bstr_d_s, str_s, + sizeof (mem_t *), &ffi_type_pointer, + ffi_bstr_put, ffi_bstr_d_get)); { val iter; @@ -1802,6 +1834,8 @@ void ffi_init(void) str_d_s = intern(lit("str-d"), user_package); wstr_s = intern(lit("wstr"), user_package); wstr_d_s = intern(lit("wstr-d"), user_package); + bstr_s = intern(lit("bstr"), user_package); + bstr_d_s = intern(lit("bstr-d"), user_package); buf_d_s = intern(lit("buf-d"), user_package); ptr_in_s = intern(lit("ptr-in"), user_package); ptr_out_s = intern(lit("ptr-out"), user_package); |