PageRenderTime 14ms CodeModel.GetById 9ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/lua/wchar.i

#
Swig | 42 lines | 30 code | 6 blank | 6 comment | 0 complexity | 9dbdaabfd9c607899c19ec2d9d20fa1a MD5 | raw file
 1/* -----------------------------------------------------------------------------
 2 * wchar.i
 3 *
 4 * Typemaps for the wchar_t type
 5 * These are mapped to a Lua string and are passed around by value.
 6 * ----------------------------------------------------------------------------- */
 7
 8// note: only support for pointer right now, not fixed length strings
 9// TODO: determine how long a const wchar_t* is so we can write wstr2str() 
10// & do the output typemap
11
12%{
13#include <stdlib.h>
14	
15wchar_t* str2wstr(const char *str, int len)
16{
17  wchar_t* p;
18  if (str==0 || len<1)  return 0;
19  p=(wchar *)malloc((len+1)*sizeof(wchar_t));
20  if (p==0)	return 0;
21  if (mbstowcs(p, str, len)==-1)
22  {
23    free(p);
24    return 0;
25  }
26  p[len]=0;
27  return p;
28}
29%}
30
31%typemap(in, checkfn="SWIG_lua_isnilstring", fragment="SWIG_lua_isnilstring") wchar_t *
32%{
33$1 = str2wstr(lua_tostring( L, $input ),lua_strlen( L, $input ));
34if ($1==0) {lua_pushfstring(L,"Error in converting to wchar (arg %d)",$input);goto fail;}
35%}
36
37%typemap(freearg) wchar_t *
38%{
39free($1);
40%}
41
42%typemap(typecheck) wchar_t * = char *;