From: <von...@am...> - 2009-05-11 07:23:57
|
Hello Dan, I faced the same issue some time ago. The problem is that Ruby does not know too much about wide characters, however it can works well with UTF-8 encoding. Since there are no conversion routines in Ruby C interface which would allowed you to convert wide chars to UTF-8 encoded characters, you have to provide this conversion by yourself at now. I have placed following implementation (which mimics Python implementation of the same swig fragments) on top of my .i file: %fragment("SWIG_AsWCharPtrAndSize","header",fragment="<wchar.h>",fragment="SWIG_pwchar_descriptor") { SWIGINTERN int SWIG_AsWCharPtrAndSize(VALUE obj, wchar_t **cptr, size_t *psize, int *alloc) { if (TYPE(obj) == T_STRING) { %#if defined(StringValuePtr) char *cstr = StringValuePtr(obj); %#else char *cstr = STR2CSTR(obj); %#endif std::wstring tempStr = WcsServices::aswcs(cstr, WcsServices::cp_utf8); size_t size = tempStr.size() + 1; if (cptr) { if (alloc) { *cptr = %new_copy_array(tempStr.c_str(), size, wchar_t); *alloc = SWIG_NEWOBJ; } } if (psize) *psize = size; return SWIG_OK; } else { swig_type_info* pwchar_descriptor = SWIG_pwchar_descriptor(); if (pwchar_descriptor) { void* vptr = 0; if (SWIG_ConvertPtr(obj, &vptr, pwchar_descriptor, 0) == SWIG_OK) { if (cptr) *cptr = (wchar_t *)vptr; if (psize) *psize = vptr ? (wcslen((wchar_t*)vptr) + 1) : 0; if (alloc) *alloc = SWIG_OLDOBJ; return SWIG_OK; } } } return SWIG_TypeError; } } %fragment("SWIG_FromWCharPtrAndSize","header",fragment="<wchar.h>",fragment="SWIG_pwchar_descriptor") { SWIGINTERNINLINE VALUE SWIG_FromWCharPtrAndSize(const wchar_t * carray, size_t size) { if (carray) { std::string tempStr(WcsServices::asmbs(carray, WcsServices::cp_utf8)); if (tempStr.size() > LONG_MAX) { swig_type_info* pwchar_descriptor = SWIG_pwchar_descriptor(); return pwchar_descriptor ? SWIG_NewPointerObj(%const_cast(carray,wchar_t *), pwchar_descriptor, 0) : Qnil; } else { return rb_str_new(tempStr.c_str(), %numeric_cast(tempStr.size(),long)); } } else { return Qnil; } } } %include <typemaps/cwstring.swg> %include <typemaps/std_wstring.swg> Note that WcsServices class is the class which is responsible for conversions between wide chars and chars. You have replace it by your own code. Also note that you should not include %include <cwstring.i> and %include <std_wstring.i> since they are useless yet. Anyway I feel this is ugly workaround, so I would be glad to see some better solution soon. Hopefully Ruby 2.0 will bring more light into this issue. Vit "Macumber, Daniel" <Dan...@nr...> 08.05.2009 20:25 Komu <swi...@li...> Kopie Předmět [Swig-user] wstring in Ruby I am able to SWIG, compile, and successfully run the test case "li_std_string.i" for Ruby 1.8 using SWIG 1.3.39. However, when I try to do this with "li_std_wstring.i" I get the following errors: 1>c:\working\test\li_std_wstring_wrap.cxx(2834) : error C3861: 'SWIG_AsCharPtrAndSize': identifier not found 1>c:\working\test\li_std_wstring_wrap.cxx(2915) : error C3861: 'SWIG_FromCharPtrAndSize': identifier not found I noticed that there is a "li_std_string_runme.rb" in the test-suite\ruby directory but no "li_std_wstring_runme.rb". Does this imply that wstring is not supported in Ruby? There is a "std_wstring.i" in the Lib\ruby directory which made me think wstring should be supported? Thanks a lot, Dan ------------------------------------------------------------------------------ The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your production scanning environment may not be a perfect world - but thanks to Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700 Series Scanner you'll get full speed at 300 dpi even with all image processing features enabled. http://p.sf.net/sfu/kodak-com _______________________________________________ Swig-user mailing list Swi...@li... https://lists.sourceforge.net/lists/listinfo/swig-user |