// file : xsd/cxx/xml/string.ixx // author : Boris Kolpackov // copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC // license : GNU GPL v2 + exceptions; see accompanying LICENSE file #ifndef XSD_CXX_XML_STRING_IXX #define XSD_CXX_XML_STRING_IXX #include #include // std::memcpy #include #include // We sometimes need this functionality even if we are building for // wchar_t. // namespace xsd { namespace cxx { namespace xml { #ifndef XSD_USE_LCP namespace bits { // UTF-16 to/from UTF-8 transcoder. // template struct char_transcoder { static std::basic_string to (const XMLCh* s, std::size_t length); static XMLCh* from (const C* s, std::size_t length); private: static const unsigned char first_byte_mask_[5]; }; } #endif template <> inline std::basic_string transcode (const XMLCh* s) { if (s == 0) return std::basic_string (); #ifndef XSD_USE_LCP return bits::char_transcoder::to ( s, xercesc::XMLString::stringLen (s)); #else // Use Xerces-C++ local code page transcoding. // std_memory_manager mm; auto_array r ( xercesc::XMLString::transcode (s, &mm), mm); return std::basic_string (r.get ()); #endif } template <> inline std::basic_string transcode (const XMLCh* s, std::size_t len) { if (s == 0 || len == 0) return std::basic_string (); #ifndef XSD_USE_LCP // Convert UTF-16 to UTF-8 // return bits::char_transcoder::to (s, len); #else // Use Xerces-C++ local code page transcoding. // auto_array tmp (new XMLCh[len + 1]); std::memcpy (tmp.get (), s, len * sizeof (XMLCh)); tmp[len] = XMLCh (0); std_memory_manager mm; auto_array r ( xercesc::XMLString::transcode (tmp.get (), &mm), mm); tmp.reset (); return std::basic_string (r.get ()); #endif } template <> inline XMLCh* transcode_to_xmlch (const char* s) { #ifndef XSD_USE_LCP // Convert UTF-8 to UTF-16 // return bits::char_transcoder::from ( s, std::char_traits::length (s)); #else // Use Xerces-C++ local code page transcoding. // std_memory_manager mm; return xercesc::XMLString::transcode (s, &mm); #endif } template <> inline XMLCh* transcode_to_xmlch (const std::basic_string& s) { #ifndef XSD_USE_LCP // Convert UTF-8 to UTF-16 // return bits::char_transcoder::from ( s.c_str (), s.length ()); #else // Use Xerces-C++ local code page transcoding. // std_memory_manager mm; return xercesc::XMLString::transcode (s.c_str (), &mm); #endif } } } } #endif // XSD_CXX_XML_STRING_IXX #if defined(XSD_USE_CHAR) || !defined(XSD_USE_WCHAR) #ifndef XSD_CXX_XML_STRING_IXX_CHAR #define XSD_CXX_XML_STRING_IXX_CHAR #endif // XSD_CXX_XML_STRING_IXX_CHAR #endif // XSD_USE_CHAR #if defined(XSD_USE_WCHAR) || !defined(XSD_USE_CHAR) #ifndef XSD_CXX_XML_STRING_IXX_WCHAR #define XSD_CXX_XML_STRING_IXX_WCHAR namespace xsd { namespace cxx { namespace xml { namespace bits { template struct wchar_transcoder; // Specialization for 2-byte wchar_t (resulting encoding is UTF-16). // template struct wchar_transcoder { static std::basic_string to (const XMLCh* s, std::size_t length); static XMLCh* from (const W* s, std::size_t length); }; // Specialization for 4-byte wchar_t (resulting encoding is UCS-4). // template struct wchar_transcoder { static std::basic_string to (const XMLCh* s, std::size_t length); static XMLCh* from (const W* s, std::size_t length); }; } template <> inline std::basic_string transcode (const XMLCh* s) { if (s == 0) return std::basic_string (); return bits::wchar_transcoder::to ( s, xercesc::XMLString::stringLen (s)); } template <> inline std::basic_string transcode (const XMLCh* s, std::size_t len) { if (s == 0 || len == 0) return std::basic_string (); return bits::wchar_transcoder::to ( s, len); } template <> inline XMLCh* transcode_to_xmlch (const wchar_t* s) { return bits::wchar_transcoder::from ( s, std::char_traits::length (s)); } template <> inline XMLCh* transcode_to_xmlch (const std::basic_string& s) { return bits::wchar_transcoder::from ( s.c_str (), s.length ()); } } } } #endif // XSD_CXX_XML_STRING_IXX_WCHAR #endif // XSD_USE_WCHAR