PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/perl5/xmlstring/xmlstring.i

#
Swig | 111 lines | 102 code | 9 blank | 0 comment | 0 complexity | 46902afae5f472358b4cad8b99819095 MD5 | raw file
  1%include <perlstrings.swg>
  2
  3%fragment("<XMLCh.h>","header") 
  4%{
  5#include <xercesc/util/XMLString.hpp>
  6#include <xercesc/util/TransService.hpp>
  7#include <xercesc/util/XMLUTF8Transcoder.hpp>
  8%}
  9
 10%fragment("SWIG_UTF8Transcoder","header",fragment="<XMLCh.h>") {
 11SWIGINTERN XERCES_CPP_NAMESPACE::XMLTranscoder* 
 12SWIG_UTF8Transcoder() {
 13  using namespace XERCES_CPP_NAMESPACE;
 14  static int init = 0;
 15  static XMLTranscoder* UTF8_TRANSCODER  = NULL;
 16  static XMLCh* UTF8_ENCODING = NULL;  
 17  if (!init) {
 18    XMLTransService::Codes failReason;
 19    XMLPlatformUtils::Initialize(); // first we must create the transservice
 20    UTF8_ENCODING = XMLString::transcode("UTF-8");
 21    UTF8_TRANSCODER = XMLPlatformUtils::fgTransService->makeNewTranscoderFor(UTF8_ENCODING,
 22									     failReason,
 23									     1024);
 24    init = 1;
 25  }
 26  return UTF8_TRANSCODER;
 27}
 28}
 29   
 30%fragment("SWIG_AsXMLChPtrAndSize","header",fragment="SWIG_AsCharPtrAndSize",fragment="SWIG_UTF8Transcoder") {
 31SWIGINTERN int
 32SWIG_AsXMLChPtrAndSize(SV *obj, XMLCh **val, size_t* psize, int *alloc)
 33{
 34  if (!val) {
 35    return SWIG_AsCharPtrAndSize(obj, 0,  0, 0);
 36  } else {
 37    size_t size;
 38    char *cptr = 0;
 39    int calloc = SWIG_OLDOBJ;
 40    int res = SWIG_AsCharPtrAndSize(obj, &cptr, &size, &calloc);
 41    if (SWIG_IsOK(res)) {
 42      STRLEN length = size - 1;
 43      if (SvUTF8(obj)) {
 44	unsigned int charsEaten = 0;
 45	unsigned char* sizes = %new_array(size, unsigned char);
 46	*val = %new_array(size, XMLCh);
 47	unsigned int chars_stored = 
 48	  SWIG_UTF8Transcoder()->transcodeFrom((const XMLByte*) cptr,
 49					       (unsigned int) length,
 50					       (XMLCh*) *val, 
 51					       (unsigned int) length,
 52					       charsEaten,
 53					       (unsigned char*)sizes
 54					       );
 55	%delete_array(sizes);
 56	// indicate the end of the string
 57	(*val)[chars_stored] = '\0';
 58      } else {
 59	*val = XERCES_CPP_NAMESPACE::XMLString::transcode(cptr);
 60      }
 61      if (psize) *psize = size;
 62      if (alloc) *alloc = SWIG_NEWOBJ;
 63      if (calloc == SWIG_NEWOBJ) %delete_array(cptr);
 64      return SWIG_NEWOBJ;    
 65    } else {
 66      return res;
 67    }
 68  }
 69}
 70}
 71
 72%fragment("SWIG_FromXMLChPtrAndSize","header",fragment="SWIG_UTF8Transcoder") {
 73SWIGINTERNINLINE SV *
 74SWIG_FromXMLChPtrAndSize(const XMLCh* input, size_t size)
 75{
 76  SV *output = sv_newmortal();
 77  unsigned int charsEaten = 0;
 78  int length  = size;                                        // string length
 79  XMLByte* res = %new_array(length * UTF8_MAXLEN, XMLByte);          // output string
 80  unsigned int total_chars =
 81    SWIG_UTF8Transcoder()->transcodeTo((const XMLCh*) input, 
 82				       (unsigned int) length,
 83				       (XMLByte*) res,
 84				       (unsigned int) length*UTF8_MAXLEN,
 85				       charsEaten,
 86				       XERCES_CPP_NAMESPACE::XMLTranscoder::UnRep_Throw
 87				       );
 88  res[total_chars] = '\0';
 89  sv_setpv((SV*)output, (char *)res );
 90  SvUTF8_on((SV*)output);
 91  %delete_array(res);
 92  return output;
 93}
 94}
 95
 96%init {
 97  if (!SWIG_UTF8Transcoder()) {
 98    croak("ERROR: XML::Xerces: INIT: Could not create UTF-8 transcoder");
 99  }
100}
101
102
103%include <typemaps/strings.swg>
104%typemaps_string(%checkcode(UNISTRING), %checkcode(UNICHAR),
105		 XMLCh, XMLCh,
106		 SWIG_AsXMLChPtrAndSize, 
107		 SWIG_FromXMLChPtrAndSize,
108		 XERCES_CPP_NAMESPACE::XMLString::stringLen,
109		 "<XMLCh.h>", INT_MIN, INT_MAX);
110
111