From d4aaf7a08ad2717d9cbc71877455915955ace64d Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Wed, 4 Jan 2006 17:06:45 +0000 Subject: [PATCH] add initial xmlstring support git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@8211 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Lib/perl5/xmlstring.i | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 SWIG/Lib/perl5/xmlstring.i diff --git a/SWIG/Lib/perl5/xmlstring.i b/SWIG/Lib/perl5/xmlstring.i new file mode 100644 index 000000000..6782c993d --- /dev/null +++ b/SWIG/Lib/perl5/xmlstring.i @@ -0,0 +1,79 @@ +%include +%include + +%{ +/* include here XMLCh/XMString declaration */ +%} + + +%fragment("SWIG_AsPtrXMLChPtr","header",fragment="SWIG_AsCharPtrAndSize") { +SWIGINTERN int +SWIG_AsPtrXMLChPtr(SV *obj, XMLCh **val) +{ + if (!val) { + return SWIG_AsCharPtrAndSize(obj, 0, 0, 0); + } else { + size_t size; + char *ptr = 0; + int alloc = SWIG_OLDOBJ; + int res = SWIG_AsCharPtrAndSize(obj, &ptr, &size, &alloc); + if (SWIG_IsOK(res)) { + STRLEN length = size - 1; + if (SvUTF8(input)) { + unsigned int charsEaten = 0; + unsigned char* sizes = %new_array(size, unsigned char); + *val = %new_array(size, XMLCh); + unsigned int chars_stored = + UTF8_TRANSCODER->transcodeFrom((const XMLByte*) ptr, + (unsigned int) length, + (XMLCh*) *val, + (unsigned int) length, + charsEaten, + (unsigned char*)sizes + ); + %delete_array(sizes); + // indicate the end of the string + (*val)[chars_stored] = '\0'; + } else { + *val = XMLChPtr::transcode(ptr); + } + if (SWIG_IsNewObj(res)) %delete_array(ptr); + return SWIG_NEWOBJ; + } else { + return res; + } + } +} +} + +%fragment("SWIG_FromXMLChPtr","header") { +SWIGINTERNINLINE SV * +SWIG_FromXMLChPtr(XMLCh* input) +{ + SV *output = sv_newmortal(); + unsigned int charsEaten = 0; + int length = XMLChPtr::stringLen(input); // string length + XMLByte* res = new XMLByte[length * UTF8_MAXLEN]; // output string + unsigned int total_chars = + UTF8_TRANSCODER->transcodeTo((const XMLCh*) input, + (unsigned int) length, + (XMLByte*) res, + (unsigned int) length*UTF8_MAXLEN, + charsEaten, + XMLTranscoder::UnRep_Throw + ); + res[total_chars] = '\0'; + sv_setpv((SV*)output, (char *)res ); + SvUTF8_on((SV*)output); + delete[] res; + return output; +} +} + + +%typemaps_asptrfrom(%checkcode(STRING), + SWIG_AsPtrXMLChPtr, + SWIG_FromXMLChPtr, + "SWIG_AsPtrXMLChPtr", + "SWIG_FromXMLChPtr", + XMLCh *);