2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
24 * Helper classes and types for manipulating Unicode
30 #include <xercesc/util/XMLUTF8Transcoder.hpp>
31 #include <xercesc/util/XMLUniDefs.hpp>
33 using namespace xercesc;
35 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
37 char* xmltooling::toUTF8(const XMLCh* src, bool use_malloc)
39 xsecsize_t eaten,factor=1,bufsize;
40 xsecsize_t srclen=XMLString::stringLen(src);
41 XMLUTF8Transcoder t(UTF8, 4096); // block size isn't used any more anyway
43 bufsize = factor*srclen + 10;
44 char* buf = use_malloc ? reinterpret_cast<char*>(malloc(bufsize)) : new char[bufsize];
45 memset(buf,0,bufsize);
49 reinterpret_cast<XMLByte*>(buf),bufsize-1,
51 XMLTranscoder::UnRep_Throw);
53 catch (XMLException&) {
58 throw XMLToolingException("Source string contained an unrepresentable character.");
70 XMLCh* xmltooling::fromUTF8(const char* src, bool use_malloc)
73 xsecsize_t srclen=strlen(src);
74 XMLUTF8Transcoder t(UTF8, 4096); // block size isn't used any more anyway
75 XMLCh* buf = use_malloc ? reinterpret_cast<XMLCh*>(malloc((srclen+1)*sizeof(XMLCh))) : new XMLCh[srclen + 1];
76 unsigned char* sizes=new unsigned char[srclen];
77 memset(buf,0,(srclen+1)*sizeof(XMLCh));
79 reinterpret_cast<const XMLByte*>(src),srclen,
86 std::ostream& xmltooling::operator<<(std::ostream& ostr, const XMLCh* s)
89 char* p=xmltooling::toUTF8(s);
96 std::ostream& xmltooling::operator<<(std::ostream& ostr, const xstring& s)
98 return ostr << s.c_str();