diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2010-06-02 20:53:17 +0000 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2010-06-02 20:53:17 +0000 |
commit | 2824b0cbb66e715490e1ef13250bd675d87b32d9 (patch) | |
tree | c3bc8d54c6d73f2b7ce08cac34172dbc9f5e5b95 /trunk/Examples/perl5/xmlstring | |
parent | 289cfef4b4766ff266f3b1bdda8ca3a952e5a047 (diff) | |
download | swig-2.0.0.tar.gz |
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/tags/rel-2.0.0@12089 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'trunk/Examples/perl5/xmlstring')
-rw-r--r-- | trunk/Examples/perl5/xmlstring/Makefile | 23 | ||||
-rw-r--r-- | trunk/Examples/perl5/xmlstring/example.cxx | 1 | ||||
-rw-r--r-- | trunk/Examples/perl5/xmlstring/example.h | 36 | ||||
-rw-r--r-- | trunk/Examples/perl5/xmlstring/example.i | 10 | ||||
-rw-r--r-- | trunk/Examples/perl5/xmlstring/runme.pl | 8 | ||||
-rw-r--r-- | trunk/Examples/perl5/xmlstring/xmlstring.i | 111 |
6 files changed, 189 insertions, 0 deletions
diff --git a/trunk/Examples/perl5/xmlstring/Makefile b/trunk/Examples/perl5/xmlstring/Makefile new file mode 100644 index 000000000..36143fc3a --- /dev/null +++ b/trunk/Examples/perl5/xmlstring/Makefile @@ -0,0 +1,23 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lxerces-c -lxerces-depdom -lm + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' LIBS=$(LIBS) CXX="g++ -g3" perl5_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='myperl' INTERFACE='$(INTERFACE)' perl5_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile perl5_clean + +check: all + + +run: + perl runme.pl diff --git a/trunk/Examples/perl5/xmlstring/example.cxx b/trunk/Examples/perl5/xmlstring/example.cxx new file mode 100644 index 000000000..370dd9ea9 --- /dev/null +++ b/trunk/Examples/perl5/xmlstring/example.cxx @@ -0,0 +1 @@ +#include "example.h" diff --git a/trunk/Examples/perl5/xmlstring/example.h b/trunk/Examples/perl5/xmlstring/example.h new file mode 100644 index 000000000..a1ccf2185 --- /dev/null +++ b/trunk/Examples/perl5/xmlstring/example.h @@ -0,0 +1,36 @@ +#include <xercesc/util/XMLString.hpp> + + +class XMLChTest +{ + XMLCh *_val; + +public: + + XMLChTest() : _val(0) + { + } + + void set(const XMLCh *v) + { + size_t len = XERCES_CPP_NAMESPACE::XMLString::stringLen(v); + delete[] _val; + _val = new XMLCh[len + 1]; + for (int i = 0; i < len; ++i) { + _val[i] = v[i]; + } + _val[len] = 0; + } + + const XMLCh *get() + { + return _val; + } + + XMLCh get_first() + { + return _val[0]; + } + +}; + diff --git a/trunk/Examples/perl5/xmlstring/example.i b/trunk/Examples/perl5/xmlstring/example.i new file mode 100644 index 000000000..8a9e02e40 --- /dev/null +++ b/trunk/Examples/perl5/xmlstring/example.i @@ -0,0 +1,10 @@ +%module example + +%include <xmlstring.i> + +%{ +#include "example.h" +%} + + +%include example.h diff --git a/trunk/Examples/perl5/xmlstring/runme.pl b/trunk/Examples/perl5/xmlstring/runme.pl new file mode 100644 index 000000000..1b223d4ca --- /dev/null +++ b/trunk/Examples/perl5/xmlstring/runme.pl @@ -0,0 +1,8 @@ +use example; + + +$e1 = new example::XMLChTest(); +$e1->set("hello"); +print $e1->get(),"\n"; +print $e1->get_first(),"\n"; + diff --git a/trunk/Examples/perl5/xmlstring/xmlstring.i b/trunk/Examples/perl5/xmlstring/xmlstring.i new file mode 100644 index 000000000..3ef53169d --- /dev/null +++ b/trunk/Examples/perl5/xmlstring/xmlstring.i @@ -0,0 +1,111 @@ +%include <perlstrings.swg> + +%fragment("<XMLCh.h>","header") +%{ +#include <xercesc/util/XMLString.hpp> +#include <xercesc/util/TransService.hpp> +#include <xercesc/util/XMLUTF8Transcoder.hpp> +%} + +%fragment("SWIG_UTF8Transcoder","header",fragment="<XMLCh.h>") { +SWIGINTERN XERCES_CPP_NAMESPACE::XMLTranscoder* +SWIG_UTF8Transcoder() { + using namespace XERCES_CPP_NAMESPACE; + static int init = 0; + static XMLTranscoder* UTF8_TRANSCODER = NULL; + static XMLCh* UTF8_ENCODING = NULL; + if (!init) { + XMLTransService::Codes failReason; + XMLPlatformUtils::Initialize(); // first we must create the transservice + UTF8_ENCODING = XMLString::transcode("UTF-8"); + UTF8_TRANSCODER = XMLPlatformUtils::fgTransService->makeNewTranscoderFor(UTF8_ENCODING, + failReason, + 1024); + init = 1; + } + return UTF8_TRANSCODER; +} +} + +%fragment("SWIG_AsXMLChPtrAndSize","header",fragment="SWIG_AsCharPtrAndSize",fragment="SWIG_UTF8Transcoder") { +SWIGINTERN int +SWIG_AsXMLChPtrAndSize(SV *obj, XMLCh **val, size_t* psize, int *alloc) +{ + if (!val) { + return SWIG_AsCharPtrAndSize(obj, 0, 0, 0); + } else { + size_t size; + char *cptr = 0; + int calloc = SWIG_OLDOBJ; + int res = SWIG_AsCharPtrAndSize(obj, &cptr, &size, &calloc); + if (SWIG_IsOK(res)) { + STRLEN length = size - 1; + if (SvUTF8(obj)) { + unsigned int charsEaten = 0; + unsigned char* sizes = %new_array(size, unsigned char); + *val = %new_array(size, XMLCh); + unsigned int chars_stored = + SWIG_UTF8Transcoder()->transcodeFrom((const XMLByte*) cptr, + (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 = XERCES_CPP_NAMESPACE::XMLString::transcode(cptr); + } + if (psize) *psize = size; + if (alloc) *alloc = SWIG_NEWOBJ; + if (calloc == SWIG_NEWOBJ) %delete_array(cptr); + return SWIG_NEWOBJ; + } else { + return res; + } + } +} +} + +%fragment("SWIG_FromXMLChPtrAndSize","header",fragment="SWIG_UTF8Transcoder") { +SWIGINTERNINLINE SV * +SWIG_FromXMLChPtrAndSize(const XMLCh* input, size_t size) +{ + SV *output = sv_newmortal(); + unsigned int charsEaten = 0; + int length = size; // string length + XMLByte* res = %new_array(length * UTF8_MAXLEN, XMLByte); // output string + unsigned int total_chars = + SWIG_UTF8Transcoder()->transcodeTo((const XMLCh*) input, + (unsigned int) length, + (XMLByte*) res, + (unsigned int) length*UTF8_MAXLEN, + charsEaten, + XERCES_CPP_NAMESPACE::XMLTranscoder::UnRep_Throw + ); + res[total_chars] = '\0'; + sv_setpv((SV*)output, (char *)res ); + SvUTF8_on((SV*)output); + %delete_array(res); + return output; +} +} + +%init { + if (!SWIG_UTF8Transcoder()) { + croak("ERROR: XML::Xerces: INIT: Could not create UTF-8 transcoder"); + } +} + + +%include <typemaps/strings.swg> +%typemaps_string(%checkcode(UNISTRING), %checkcode(UNICHAR), + XMLCh, XMLCh, + SWIG_AsXMLChPtrAndSize, + SWIG_FromXMLChPtrAndSize, + XERCES_CPP_NAMESPACE::XMLString::stringLen, + "<XMLCh.h>", INT_MIN, INT_MAX); + + |