wchar.i 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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_t *)malloc((len+1)*sizeof(wchar_t));
  17. if (p==0) return 0;
  18. if (mbstowcs(p, str, len)==(size_t)-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_rawlen( L, $input ));
  30. if ($1==0) {SWIG_Lua_pushferrstring(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 *;