PageRenderTime 466ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

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