views:

42

answers:

1

Hello all, I'm trying to use tpl to serialize structs that contain wchar_t* strings.

The code I have looks like this, and it's not working:

#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <wchar.h>
#include "tpl.h"

struct chinese_t {
    wchar_t *chars;
};


int main() {

tpl_node *tn;


struct chinese_t cstr;
cstr.chars = L"字符串";

tn = tpl_map("S(s)", &cstr);
tpl_pack( tn, 0 );
tpl_dump(tn, TPL_FILE, "string.tpl");
tpl_free(tn);


struct chinese_t cstr2;

tn = tpl_map( "S(s)", &cstr2);
//tpl_load(tn, TPL_MEM, buffer, len);
tpl_load(tn, TPL_FILE, "string.tpl");
tpl_unpack(tn, 0);
tpl_free(tn);


printf("%ls\n", cstr2.chars);
return;
}

If I replace the Chinese "字符串" string with "1234" it just prints "1" -- if I change the definition so that the struct users a char* (and I only push ASCII characters into it) it works just fine. However I can't figure out how to get it to serialize and deserialize wchar_t* strings properly.

+2  A: 

I haven't used tpl before, but from a quick look at the documentation, it doesn't appear to support wide characters directly. The behavior you're seeing for the string "1234" is consistent with the UTF-16 coded string containing the bytes "1\x002\x003\x004\x00\x00\x00" being treated by tpl as just the NUL-terminated byte string "1\x00".

It looks like your best options are probably:

  • Represent your wide character strings in tpl as arrays of 16-bit integers;
  • Encode your strings as UTF-8 char strings and use the tpl string type; or
  • Modify tpl to include a wide character string type.
llasram
Ah, I missed the part in the docs that talked about how to designate which of the unbounded arrays in the struct you were adding to. Thanks for the answer!
John Biesnecker