PageRenderTime 73ms CodeModel.GetById 19ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. // note: only support for pointer right now, not fixed length strings
  8. // TODO: determine how long a const wchar_t* is so we can write wstr2str()
  9. // & do the output typemap
  10. %{
  11. #include <stdlib.h>
  12. wchar_t* str2wstr(const char *str, int len)
  13. {
  14. wchar_t* p;
  15. if (str==0 || len<1) return 0;
  16. p=(wchar *)malloc((len+1)*sizeof(wchar_t));
  17. if (p==0) return 0;
  18. if (mbstowcs(p, str, len)==-1)
  19. {
  20. free(p);
  21. return 0;
  22. }
  23. p[len]=0;
  24. return p;
  25. }
  26. %}
  27. %typemap(in, checkfn="SWIG_lua_isnilstring", fragment="SWIG_lua_isnilstring") wchar_t *
  28. %{
  29. $1 = str2wstr(lua_tostring( L, $input ),lua_strlen( L, $input ));
  30. if ($1==0) {lua_pushfstring(L,"Error in converting to wchar (arg %d)",$input);goto fail;}
  31. %}
  32. %typemap(freearg) wchar_t *
  33. %{
  34. free($1);
  35. %}
  36. %typemap(typecheck) wchar_t * = char *;