Re: ffi : use of wchar_t, size_t

  • From: Justin Cormack <justin@xxxxxxxxxxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 11 Apr 2014 10:03:17 +0100

On Fri, Apr 11, 2014 at 9:25 AM, Alain Meunier <deco33@xxxxxxxxxx> wrote:
> Hello,
>
> I have this useless - but meaningful and working - code in c :
>
> #include <stdio.h>
> #include <wchar.h>
> #include <locale.h>
>
> int main(int argc, char **argv)
> {
>     setlocale(LC_ALL, "");
>
>     wchar_t po[] = L"sé";
>
>     wprintf(L"%lc\n",po[1]);
>     wprintf(L"%ls \n", L"éáèï");
>
>     size_t s1 = 5;
>
>     wprintf(L"%zd",s1);
>
>
>     return 0;
> }
>
> Using luajit + ffi, I would like to be able to use wchar to manipulate the
> accents and so on.
> The following code leads to errors :
> bad argument #1 to 'cast' (invalid C type) or bad argument #1 to 'string'
> (cannot convert 'int' to 'const char *')..
>
> The only thing I understand is that I do things wrong.
>
> ffi.cdef[[
>     wchar_t so[10];
>     int wprintf (const wchar_t* format, ...);
> ]]
>
> --[[
> local a = ffi.new("char[4]","sae")
> local gou = ffi.new("so",a)
> ]]
> local gou = ffi.cast("wchar_t","joé")
> ffi.C.wprintf(gou[1])

You want to do ffi.new("wchar_t[10]") or similar, you are casting to a
wchar_t not an array.

But there is no built in conversion from strings to wchar_t, wchar_t
is just an integer, would will have to parse them with something like
mbrtowc, the luajit program text is UTF-8.

Basically wchar is obsolete unless you need to talk to Windows system
calls, I don't recommend using it.

Justin

Other related posts: