Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for IDL wstring #2124

Merged
merged 18 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/dynsub/dynsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <locale.h>

#include "dds/dds.h"
#include "dynsub.h"
Expand Down Expand Up @@ -146,6 +147,9 @@ int main (int argc, char **argv)
dds_return_t ret = 0;
dds_entity_t topic = 0;

// for printf("%ls")
setlocale (LC_CTYPE, "");

if (argc != 2)
{
fprintf (stderr, "usage: %s topicname\n", argv[0]);
Expand Down
33 changes: 33 additions & 0 deletions examples/dynsub/print_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <wchar.h>

#include "dds/dds.h"
#include "dds/ddsi/ddsi_xt_typeinfo.h"
Expand Down Expand Up @@ -57,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 All @@ -68,6 +70,7 @@ static bool print_sample1_simple (const unsigned char *sample, const uint8_t dis
case CASE(FLOAT32, float, printf ("%f", *p));
case CASE(FLOAT64, double, printf ("%f", *p));
case CASE(STRING8, char *, printf ("\"%s\"", *p));
case CASE(STRING16, wchar_t *, printf ("\"%ls\"", *p));
#undef CASE
}
return false;
Expand All @@ -92,6 +95,25 @@ static const char *get_string_pointer (const unsigned char *sample, const DDS_XT
}
}

static const wchar_t *get_wstring_pointer (const unsigned char *sample, const DDS_XTypes_TypeIdentifier *typeid, struct context *c)
{
uint32_t bound;
if (typeid->_d == DDS_XTypes_TI_STRING16_SMALL)
bound = typeid->_u.string_sdefn.bound;
else
bound = typeid->_u.string_ldefn.bound;
// must always call align for its side effects
if (bound != 0)
{
return align (sample, c, _Alignof (wchar_t), bound + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the size parameter be bound * sizeof (wchar_t) + sizeof (L'\0')?

}
else
{
// if not "valid_data" and not a key field, this'll be a null pointer
return *((const wchar_t **) align (sample, c, _Alignof (wchar_t *), sizeof (wchar_t *)));
}
}

static void print_sample1_ti (const unsigned char *sample, const DDS_XTypes_TypeIdentifier *typeid, uint32_t rank, struct context *c, const char *sep, const char *label, bool is_base_type)
{
if (print_sample1_simple (sample, typeid->_d, c, sep, label, NULL))
Expand All @@ -109,6 +131,17 @@ static void print_sample1_ti (const unsigned char *sample, const DDS_XTypes_Type
}
break;
}
case DDS_XTypes_TI_STRING16_SMALL:
case DDS_XTypes_TI_STRING16_LARGE: {
const wchar_t *p = get_wstring_pointer (sample, typeid, c);
if (c->key || c->valid_data)
{
printf ("%s", sep);
if (label) printf ("\"%s\":", label);
printf ("\"%ls\"", p);
}
break;
}
case DDS_XTypes_TI_PLAIN_SEQUENCE_SMALL:
case DDS_XTypes_TI_PLAIN_SEQUENCE_LARGE: {
const DDS_XTypes_TypeIdentifier *et = (typeid->_d == DDS_XTypes_TI_PLAIN_SEQUENCE_SMALL) ? typeid->_u.seq_sdefn.element_identifier : typeid->_u.seq_ldefn.element_identifier;
Expand Down
6 changes: 6 additions & 0 deletions examples/dynsub/print_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ void ppc_print_ti (struct ppc *ppc, const DDS_XTypes_TypeIdentifier *typeid)
ppc_print (ppc, "STRING8_%s bound=%"PRIu32"\n", (typeid->_d == DDS_XTypes_TI_STRING8_SMALL) ? "SMALL" : "LARGE", bound);
break;
}
case DDS_XTypes_TI_STRING16_SMALL:
case DDS_XTypes_TI_STRING16_LARGE: {
const uint32_t bound = (typeid->_d == DDS_XTypes_TI_STRING16_SMALL) ? typeid->_u.string_sdefn.bound : typeid->_u.string_ldefn.bound;
ppc_print (ppc, "STRING16_%s bound=%"PRIu32"\n", (typeid->_d == DDS_XTypes_TI_STRING16_SMALL) ? "SMALL" : "LARGE", bound);
break;
}
case DDS_XTypes_TI_PLAIN_SEQUENCE_SMALL:
case DDS_XTypes_TI_PLAIN_SEQUENCE_LARGE: {
const DDS_XTypes_PlainCollectionHeader *header;
Expand Down
27 changes: 24 additions & 3 deletions examples/dynsub/type_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <wchar.h>

#include "dds/dds.h"
#include "dds/ddsi/ddsi_xt_typeinfo.h"
Expand Down Expand Up @@ -162,9 +163,9 @@ 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, uint16_t *);
case CASE(STRING16, wchar_t *);
#undef CASE
}
return false;
Expand Down Expand Up @@ -204,7 +205,7 @@ static void build_typecache_ti (const DDS_XTypes_TypeIdentifier *typeid, size_t
case DDS_XTypes_TI_STRING8_SMALL:
case DDS_XTypes_TI_STRING8_LARGE: {
uint32_t bound;
if (typeid->_d == DDS_XTypes_TI_PLAIN_SEQUENCE_SMALL) {
if (typeid->_d == DDS_XTypes_TI_STRING8_SMALL) {
bound = typeid->_u.string_sdefn.bound;
} else {
bound = typeid->_u.string_ldefn.bound;
Expand All @@ -218,6 +219,23 @@ static void build_typecache_ti (const DDS_XTypes_TypeIdentifier *typeid, size_t
}
break;
}
case DDS_XTypes_TI_STRING16_SMALL:
case DDS_XTypes_TI_STRING16_LARGE: {
uint32_t bound;
if (typeid->_d == DDS_XTypes_TI_STRING16_SMALL) {
bound = typeid->_u.string_sdefn.bound;
} else {
bound = typeid->_u.string_ldefn.bound;
}
if (bound == 0) {
*align = _Alignof (wchar_t *);
*size = sizeof (wchar_t *);
} else {
*align = 1;
*size = bound;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, should also use sizeof (wchar_t) and take terminating \0 into account

}
break;
}
case DDS_XTypes_TI_PLAIN_SEQUENCE_SMALL:
case DDS_XTypes_TI_PLAIN_SEQUENCE_LARGE: {
const DDS_XTypes_TypeIdentifier *et;
Expand Down Expand Up @@ -466,6 +484,9 @@ static bool load_deps_ti (dds_entity_t participant, const DDS_XTypes_TypeIdentif
case DDS_XTypes_TI_STRING8_SMALL:
case DDS_XTypes_TI_STRING8_LARGE:
return true;
case DDS_XTypes_TI_STRING16_SMALL:
case DDS_XTypes_TI_STRING16_LARGE:
return true;
case DDS_XTypes_TI_PLAIN_SEQUENCE_SMALL:
return load_deps_ti (participant, typeid->_u.seq_sdefn.element_identifier);
case DDS_XTypes_TI_PLAIN_SEQUENCE_LARGE:
Expand Down
8 changes: 8 additions & 0 deletions examples/dynsub/variouspub.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ static void *samples_M1_O[] = {
NULL
};

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

static struct tpentry {
const char *name;
const dds_topic_descriptor_t *descr;
Expand All @@ -102,6 +109,7 @@ static struct tpentry {
{ "B", &B_desc, samples_b, offsetof (B, a.count) },
{ "C", &C_desc, samples_c, offsetof (C, b.a.count) },
{ "M1::O", &M1_O_desc, samples_M1_O, SIZE_MAX },
{ "D", &D_desc, samples_d, offsetof (D, count) },
{ NULL, NULL, NULL, 0 }
};

Expand Down
6 changes: 6 additions & 0 deletions examples/dynsub/variouspub_types.idl
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ module M1 {
@optional long x;
};
};

struct D {
wstring ws;
wchar wc;
unsigned long count;
};
Loading
Loading