Skip to content

Commit

Permalink
Initial wchar support
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Boasson <[email protected]>
  • Loading branch information
eboasson committed Nov 19, 2024
1 parent 0552b3c commit 616d1f0
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 57 deletions.
1 change: 1 addition & 0 deletions examples/dynsub/print_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static bool print_sample1_simple (const unsigned char *sample, const uint8_t dis
}
case CASEI(BOOLEAN, uint8_t, printf ("%s", *p ? "true" : "false"));
case CASEI(CHAR8, int8_t, printf ("\"%c\"", (char) *p));
case CASEI(CHAR16, wchar_t, printf ("\"%lc\"", (wchar_t) *p));
case CASEI(INT16, int16_t, printf ("%"PRId16, *p));
case CASEI(INT32, int32_t, printf ("%"PRId32, *p));
case CASEI(INT64, int64_t, printf ("%"PRId64, *p));
Expand Down
2 changes: 1 addition & 1 deletion examples/dynsub/type_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static bool build_typecache_simple (const uint8_t disc, size_t *align, size_t *s
case CASE(INT8, int8_t);
case CASE(UINT8, uint8_t);
case CASE(CHAR8, int8_t);
case CASE(CHAR16, uint16_t);
case CASE(CHAR16, wchar_t);
case CASE(STRING8, unsigned char *);
case CASE(STRING16, wchar_t *);
#undef CASE
Expand Down
6 changes: 3 additions & 3 deletions examples/dynsub/variouspub.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ static void *samples_M1_O[] = {
};

static void *samples_d[] = {
&(D){ L"😀 Een Kruyck gaat soo langh te water tot datse barst.", 0 },
&(D){ L"🙃 Men treckt een Boogh soo lang tot datse stucken knarst.", 0 },
&(D){ L"😊 De Steel-kunst doet zyn Meester de dood vaak verwerven.", 0 },
&(D){ L"😀 Een Kruyck gaat soo langh te water tot datse barst.", L'∆', 0 },
&(D){ L"🙃 Men treckt een Boogh soo lang tot datse stucken knarst.", L'∇', 0 },
&(D){ L"😊 De Steel-kunst doet zyn Meester de dood vaak verwerven.", L'⊥', 0 },
NULL
};

Expand Down
1 change: 1 addition & 0 deletions examples/dynsub/variouspub_types.idl
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ module M1 {

struct D {
wstring ws;
wchar wc;
unsigned long count;
};
177 changes: 136 additions & 41 deletions src/core/cdr/src/dds_cdrstream.c

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/core/cdr/src/dds_cdrstream_keys.part.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ static bool dds_stream_write_keyBO_impl (DDS_OSTREAM_T * __restrict os, const st
case DDS_OP_VAL_WSTR: dds_stream_write_wstringBO (os, allocator, (const wchar_t *) addr); break;
case DDS_OP_VAL_BST: dds_stream_write_stringBO (os, allocator, addr); break;
case DDS_OP_VAL_BWSTR: dds_stream_write_wstringBO (os, allocator, (const wchar_t *) addr); break;
case DDS_OP_VAL_WCHAR:
if (!dds_stream_write_wcharBO (os, allocator, *(wchar_t *) addr))
return false;
break;
case DDS_OP_VAL_ARR: {
const uint32_t num = ops[2];
switch (DDS_OP_SUBTYPE (insn))
Expand Down
39 changes: 38 additions & 1 deletion src/core/cdr/src/dds_cdrstream_write.part.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ static void dds_stream_write_wstringBO (DDS_OSTREAM_T * __restrict os, const str
}
}

ddsrt_attribute_warn_unused_result
static bool dds_stream_write_wcharBO (DDS_OSTREAM_T * __restrict os, const struct dds_cdrstream_allocator * __restrict allocator, const wchar_t val)
{
// surrogates are forbidden
if ((uint32_t) val >= 0xd800 && (uint32_t) val < 0xe000)
return false;
else if ((uint32_t) val > 0xffff) // out-of-range
return false;
dds_os_put2BO (os, allocator, (uint16_t) val);
return true;
}

static bool dds_stream_write_bool_arrBO (DDS_OSTREAM_T * __restrict os, const struct dds_cdrstream_allocator * __restrict allocator, const uint8_t * __restrict addr, uint32_t num)
{
for (uint32_t i = 0; i < num; i++)
Expand Down Expand Up @@ -302,7 +314,15 @@ static const uint32_t *dds_stream_write_seqBO (DDS_OSTREAM_T * __restrict os, co
ops += 3 + bound_op;
break;
}
case DDS_OP_VAL_SEQ: case DDS_OP_VAL_BSQ: case DDS_OP_VAL_ARR: case DDS_OP_VAL_UNI: case DDS_OP_VAL_STU: {
case DDS_OP_VAL_WCHAR: {
const wchar_t *ptr = (const wchar_t *) seq->_buffer;
for (uint32_t i = 0; i < num; i++)
if (!dds_stream_write_wcharBO (os, allocator, ptr[i]))
return NULL;
ops += 2 + bound_op;
break;
}
case DDS_OP_VAL_SEQ: case DDS_OP_VAL_BSQ: case DDS_OP_VAL_ARR: case DDS_OP_VAL_UNI: case DDS_OP_VAL_STU: {
const uint32_t elem_size = ops[2 + bound_op];
const uint32_t jmp = DDS_OP_ADR_JMP (ops[3 + bound_op]);
uint32_t const * const jsr_ops = ops + DDS_OP_ADR_JSR (ops[3 + bound_op]);
Expand Down Expand Up @@ -394,6 +414,14 @@ static const uint32_t *dds_stream_write_arrBO (DDS_OSTREAM_T * __restrict os, co
ops += 5;
break;
}
case DDS_OP_VAL_WCHAR: {
const wchar_t *ptr = (const wchar_t *) addr;
for (uint32_t i = 0; i < num; i++)
if (!dds_stream_write_wcharBO (os, allocator, ptr[i]))
return NULL;
ops += 3;
break;
}
case DDS_OP_VAL_SEQ: case DDS_OP_VAL_BSQ: case DDS_OP_VAL_ARR: case DDS_OP_VAL_UNI: case DDS_OP_VAL_STU: {
const uint32_t * jsr_ops = ops + DDS_OP_ADR_JSR (ops[3]);
const uint32_t jmp = DDS_OP_ADR_JMP (ops[3]);
Expand Down Expand Up @@ -492,6 +520,10 @@ static const uint32_t *dds_stream_write_uniBO (DDS_OSTREAM_T * __restrict os, co
case DDS_OP_VAL_WSTR: dds_stream_write_wstringBO (os, allocator, *(const wchar_t **) valaddr); break;
case DDS_OP_VAL_BST: dds_stream_write_stringBO (os, allocator, (const char *) valaddr); break;
case DDS_OP_VAL_BWSTR: dds_stream_write_wstringBO (os, allocator, (const wchar_t *) valaddr); break;
case DDS_OP_VAL_WCHAR:
if (!dds_stream_write_wcharBO (os, allocator, *((const wchar_t *) valaddr)))
return NULL;
break;
case DDS_OP_VAL_SEQ: case DDS_OP_VAL_BSQ: case DDS_OP_VAL_ARR: case DDS_OP_VAL_UNI: case DDS_OP_VAL_STU: case DDS_OP_VAL_BMK:
if (!dds_stream_write_implBO (os, allocator, valaddr, jeq_op + DDS_OP_ADR_JSR (jeq_op[0]), false, cdr_kind))
return NULL;
Expand Down Expand Up @@ -552,6 +584,11 @@ static const uint32_t *dds_stream_write_adrBO (uint32_t insn, DDS_OSTREAM_T * __
case DDS_OP_VAL_WSTR: dds_stream_write_wstringBO (os, allocator, (const wchar_t *) addr); ops += 2; break;
case DDS_OP_VAL_BST: dds_stream_write_stringBO (os, allocator, (const char *) addr); ops += 3; break;
case DDS_OP_VAL_BWSTR: dds_stream_write_wstringBO (os, allocator, (const wchar_t *) addr); ops += 3; break;
case DDS_OP_VAL_WCHAR:
if (!dds_stream_write_wcharBO (os, allocator, *((const wchar_t *) addr)))
return NULL;
ops += 2;
break;
case DDS_OP_VAL_SEQ: case DDS_OP_VAL_BSQ: ops = dds_stream_write_seqBO (os, allocator, addr, ops, insn, cdr_kind); break;
case DDS_OP_VAL_ARR: ops = dds_stream_write_arrBO (os, allocator, addr, ops, insn, cdr_kind); break;
case DDS_OP_VAL_UNI: ops = dds_stream_write_uniBO (os, allocator, addr, data, ops, insn, cdr_kind); break;
Expand Down
1 change: 1 addition & 0 deletions src/core/ddsc/include/dds/ddsc/dds_data_type_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern "C" {
#define DDS_DATA_TYPE_CONTAINS_EXTERNAL (0x1ull << 11)
#define DDS_DATA_TYPE_CONTAINS_KEY (0x1ull << 12)
#define DDS_DATA_TYPE_CONTAINS_BWSTRING (0x1ull << 13)
#define DDS_DATA_TYPE_CONTAINS_WCHAR (0x1ull << 14)

#define DDS_DATA_TYPE_IS_MEMCPY_SAFE (0x1ull << 63)

Expand Down
15 changes: 9 additions & 6 deletions src/core/ddsc/include/dds/ddsc/dds_opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,9 @@ enum dds_stream_typecode {
DDS_OP_VAL_EXT = 0x0d, /**< field with external definition */
DDS_OP_VAL_BLN = 0x0e, /**< boolean */
DDS_OP_VAL_BMK = 0x0f, /**< bitmask */
DDS_OP_VAL_WSTR = 0x10, /**< wstring */
DDS_OP_VAL_BWSTR = 0x11 /**< bounded wstring */
DDS_OP_VAL_WSTR = 0x10, /**< wstring (UTF-16) */
DDS_OP_VAL_BWSTR = 0x11, /**< bounded wstring (UTF-16) */
DDS_OP_VAL_WCHAR = 0x12 /**< wchar: UTF-16, no surrogates allowed */
};

/**
Expand All @@ -402,8 +403,9 @@ enum dds_stream_typecode_primary {
DDS_OP_TYPE_EXT = DDS_OP_VAL_EXT << 16, /**< field with external definition */
DDS_OP_TYPE_BLN = DDS_OP_VAL_BLN << 16, /**< boolean */
DDS_OP_TYPE_BMK = DDS_OP_VAL_BMK << 16, /**< bitmask */
DDS_OP_TYPE_WSTR = DDS_OP_VAL_WSTR << 16, /**< wstring */
DDS_OP_TYPE_BWSTR = DDS_OP_VAL_BWSTR << 16 /**< bounded wstring */
DDS_OP_TYPE_WSTR = DDS_OP_VAL_WSTR << 16, /**< wstring (UTF-16) */
DDS_OP_TYPE_BWSTR = DDS_OP_VAL_BWSTR << 16, /**< bounded wstring (UTF-16) */
DDS_OP_TYPE_WCHAR = DDS_OP_VAL_WCHAR << 16 /**< wchar: UTF-16, no surrogates allowed */
};

/**
Expand Down Expand Up @@ -441,8 +443,9 @@ enum dds_stream_typecode_subtype {
DDS_OP_SUBTYPE_ENU = DDS_OP_VAL_ENU << 8, /**< enumerated value (long) */
DDS_OP_SUBTYPE_BLN = DDS_OP_VAL_BLN << 8, /**< boolean */
DDS_OP_SUBTYPE_BMK = DDS_OP_VAL_BMK << 8, /**< bitmask */
DDS_OP_SUBTYPE_WSTR = DDS_OP_VAL_WSTR << 8, /**< wstring */
DDS_OP_SUBTYPE_BWSTR = DDS_OP_VAL_BWSTR << 8 /**< bounded wstring */
DDS_OP_SUBTYPE_WSTR = DDS_OP_VAL_WSTR << 8, /**< wstring */
DDS_OP_SUBTYPE_BWSTR = DDS_OP_VAL_BWSTR << 8, /**< bounded wstring */
DDS_OP_SUBTYPE_WCHAR = DDS_OP_VAL_WCHAR << 8 /**< wchar: UTF-16, no surrogates allowed */
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/ddsc/src/dds_dynamic_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ dds_dynamic_type_t dds_dynamic_type_create (dds_entity_t entity, dds_dynamic_typ
case DDS_DYNAMIC_INT8:
case DDS_DYNAMIC_UINT8:
case DDS_DYNAMIC_CHAR8:
case DDS_DYNAMIC_CHAR16:
type.ret = ddsi_dynamic_type_create_primitive (gv, get_dtype_complete_addr (&type), descriptor.kind);
break;
case DDS_DYNAMIC_STRING8:
Expand Down Expand Up @@ -257,7 +258,6 @@ dds_dynamic_type_t dds_dynamic_type_create (dds_entity_t entity, dds_dynamic_typ
break;
}

case DDS_DYNAMIC_CHAR16:
case DDS_DYNAMIC_MAP:
case DDS_DYNAMIC_BITSET:
type.ret = DDS_RETCODE_UNSUPPORTED;
Expand Down
2 changes: 1 addition & 1 deletion src/core/ddsc/tests/dynamic_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ CU_Test (ddsc_dynamic_type, type_create, .init = dynamic_type_init, .fini = dyna
{ { .kind = DDS_DYNAMIC_INT8, .name = "t" }, DDS_RETCODE_OK },
{ { .kind = DDS_DYNAMIC_UINT8, .name = "t" }, DDS_RETCODE_OK },
{ { .kind = DDS_DYNAMIC_CHAR8, .name = "t" }, DDS_RETCODE_OK },
{ { .kind = DDS_DYNAMIC_CHAR16, .name = "t" }, DDS_RETCODE_UNSUPPORTED },
{ { .kind = DDS_DYNAMIC_CHAR16, .name = "t" }, DDS_RETCODE_OK },
{ { .kind = DDS_DYNAMIC_STRING8, .name = "t" }, DDS_RETCODE_OK },
{ { .kind = DDS_DYNAMIC_STRING16, .name = "t" }, DDS_RETCODE_OK },
{ { .kind = DDS_DYNAMIC_ENUMERATION, .name = "t" }, DDS_RETCODE_OK },
Expand Down
23 changes: 20 additions & 3 deletions src/core/ddsi/src/ddsi_typebuilder.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,13 @@ static dds_return_t typebuilder_add_type (struct typebuilder_data *tbd, uint32_t
*align = ALGN (uint16_t, is_ext);
*size = SZ (uint16_t, is_ext);
break;
case DDS_XTypes_TK_CHAR16:
tb_type->type_code = DDS_OP_VAL_WCHAR;
tb_type->args.prim_args.is_signed = false;
tb_type->cdr_align = 2;
*align = ALGN (uint16_t, is_ext);
*size = SZ (uint16_t, is_ext);
break;
case DDS_XTypes_TK_INT32:
case DDS_XTypes_TK_UINT32:
case DDS_XTypes_TK_FLOAT32:
Expand Down Expand Up @@ -578,7 +585,6 @@ static dds_return_t typebuilder_add_type (struct typebuilder_data *tbd, uint32_t
break;
}
case DDS_XTypes_TK_FLOAT128:
case DDS_XTypes_TK_CHAR16:
case DDS_XTypes_TK_ANNOTATION:
case DDS_XTypes_TK_MAP:
case DDS_XTypes_TK_BITSET:
Expand Down Expand Up @@ -932,6 +938,11 @@ static dds_return_t get_ops_type (struct typebuilder_type *tb_type, uint32_t fla
PUSH_ARG (member_offset);
PUSH_ARG (tb_type->args.string_args.max_size);
break;
case DDS_OP_VAL_WCHAR:
flags |= get_type_flags (tb_type);
PUSH_OP (DDS_OP_ADR | DDS_OP_TYPE_WCHAR | flags);
PUSH_ARG (member_offset);
break;
case DDS_OP_VAL_BSQ:
case DDS_OP_VAL_SEQ: {
bool bounded = tb_type->type_code == DDS_OP_VAL_BSQ;
Expand All @@ -946,7 +957,7 @@ static dds_return_t get_ops_type (struct typebuilder_type *tb_type, uint32_t fla
switch (element_type->type_code)
{
case DDS_OP_VAL_1BY: case DDS_OP_VAL_2BY: case DDS_OP_VAL_4BY: case DDS_OP_VAL_8BY:
case DDS_OP_VAL_BLN: case DDS_OP_VAL_STR: case DDS_OP_VAL_WSTR:
case DDS_OP_VAL_BLN: case DDS_OP_VAL_STR: case DDS_OP_VAL_WSTR: case DDS_OP_VAL_WCHAR:
break;
case DDS_OP_VAL_ENU:
PUSH_ARG (element_type->args.enum_args.max);
Expand Down Expand Up @@ -1003,7 +1014,7 @@ static dds_return_t get_ops_type (struct typebuilder_type *tb_type, uint32_t fla
switch (element_type->type_code)
{
case DDS_OP_VAL_1BY: case DDS_OP_VAL_2BY: case DDS_OP_VAL_4BY: case DDS_OP_VAL_8BY:
case DDS_OP_VAL_BLN: case DDS_OP_VAL_STR: case DDS_OP_VAL_WSTR:
case DDS_OP_VAL_BLN: case DDS_OP_VAL_STR: case DDS_OP_VAL_WSTR: case DDS_OP_VAL_WCHAR:
break;
case DDS_OP_VAL_ENU:
PUSH_ARG (element_type->args.enum_args.max);
Expand Down Expand Up @@ -1147,6 +1158,12 @@ static dds_return_t get_ops_union_case (struct typebuilder_type *tb_type, uint32
PUSH_ARG (offset);
PUSH_ARG (0);
break;
case DDS_OP_VAL_WCHAR:
PUSH_OP (DDS_OP_JEQ4 | DDS_OP_TYPE_WCHAR | flags);
PUSH_ARG (disc_value);
PUSH_ARG (offset);
PUSH_ARG (0);
break;
case DDS_OP_VAL_UNI:
case DDS_OP_VAL_STU: {
flags |= get_type_flags (tb_type);
Expand Down
1 change: 1 addition & 0 deletions src/idl/src/descriptor_type_meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ get_plain_typeid (const idl_pstate_t *pstate, struct descriptor_type_meta *dtm,
{
case IDL_BOOL: ti->_d = DDS_XTypes_TK_BOOLEAN; break;
case IDL_CHAR: ti->_d = DDS_XTypes_TK_CHAR8; break;
case IDL_WCHAR: ti->_d = DDS_XTypes_TK_CHAR16; break;
case IDL_OCTET: ti->_d = DDS_XTypes_TK_BYTE; break;
case IDL_INT8: ti->_d = DDS_XTypes_TK_INT8; break;
case IDL_INT16: case IDL_SHORT: ti->_d = DDS_XTypes_TK_INT16; break;
Expand Down
2 changes: 2 additions & 0 deletions src/idl/src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ static int print_base_type(
switch (idl_type(node)) {
case IDL_BOOL: type = "bool"; break;
case IDL_CHAR: type = "char"; break;
case IDL_WCHAR: type = "wchar_t"; break;
case IDL_INT8: type = "int8_t"; break;
case IDL_OCTET:
case IDL_UINT8: type = "uint8_t"; break;
Expand Down Expand Up @@ -224,6 +225,7 @@ static int print_templ_type(
switch (idl_type(type_spec)) {
case IDL_BOOL: type = "bool"; break;
case IDL_CHAR: type = "char"; break;
case IDL_WCHAR: type = "wchar_t"; break;
case IDL_INT8: type = "int8"; break;
case IDL_OCTET: type = "octet"; break;
case IDL_UINT8: type = "uint8"; break;
Expand Down
7 changes: 7 additions & 0 deletions src/tools/idlc/src/libidlc/libidlc__descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,9 @@ static idl_retcode_t add_typecode(const idl_pstate_t *pstate, const idl_type_spe
case IDL_CHAR:
*add_to |= ((uint32_t)DDS_OP_VAL_1BY << shift) | (uint32_t)DDS_OP_FLAG_SGN;
break;
case IDL_WCHAR:
*add_to |= ((uint32_t)DDS_OP_VAL_WCHAR << shift);
break;
case IDL_BOOL:
*add_to |= ((uint32_t)DDS_OP_VAL_BLN << shift);
break;
Expand Down Expand Up @@ -1666,6 +1669,7 @@ static int print_opcode(FILE *fp, const struct instruction *inst)
case DDS_OP_VAL_EXT: vec[len++] = " | DDS_OP_TYPE_EXT"; break;
case DDS_OP_VAL_WSTR: vec[len++] = " | DDS_OP_TYPE_WSTR"; break;
case DDS_OP_VAL_BWSTR: vec[len++] = " | DDS_OP_TYPE_BWSTR"; break;
case DDS_OP_VAL_WCHAR: vec[len++] = " | DDS_OP_TYPE_WCHAR"; break;
}
}

Expand Down Expand Up @@ -1702,6 +1706,7 @@ static int print_opcode(FILE *fp, const struct instruction *inst)
case DDS_OP_VAL_BMK: vec[len++] = " | DDS_OP_SUBTYPE_BMK"; break;
case DDS_OP_VAL_WSTR: vec[len++] = " | DDS_OP_SUBTYPE_WSTR"; break;
case DDS_OP_VAL_BWSTR: vec[len++] = " | DDS_OP_SUBTYPE_BWSTR"; break;
case DDS_OP_VAL_WCHAR: vec[len++] = " | DDS_OP_SUBTYPE_WCHAR"; break;
case DDS_OP_VAL_EXT: abort(); break;
}

Expand Down Expand Up @@ -1992,6 +1997,7 @@ static idl_retcode_t get_ctype_keys_adr(
switch (subtype) {
case DDS_OP_VAL_BLN:
case DDS_OP_VAL_1BY: key->size = key->align = 1; break;
case DDS_OP_VAL_WCHAR:
case DDS_OP_VAL_2BY: key->size = key->align = 2; break;
case DDS_OP_VAL_4BY: key->size = key->align = 4; break;
case DDS_OP_VAL_8BY: key->size = key->align = 8; break;
Expand Down Expand Up @@ -2024,6 +2030,7 @@ static idl_retcode_t get_ctype_keys_adr(
switch (type) {
case DDS_OP_VAL_BLN:
case DDS_OP_VAL_1BY: key->size = key->align = 1; break;
case DDS_OP_VAL_WCHAR:
case DDS_OP_VAL_2BY: key->size = key->align = 2; break;
case DDS_OP_VAL_4BY: key->size = key->align = 4; break;
case DDS_OP_VAL_8BY: key->size = key->align = 8; break;
Expand Down

0 comments on commit 616d1f0

Please sign in to comment.