2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * Helper classes and types for manipulating Unicode
26 #include <xercesc/util/XMLUTF8Transcoder.hpp>
27 #include <xercesc/util/XMLUniDefs.hpp>
29 using namespace xercesc;
31 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
33 char* xmltooling::toUTF8(const XMLCh* src, bool use_malloc)
35 xsecsize_t eaten,factor=1,bufsize;
36 xsecsize_t srclen=XMLString::stringLen(src);
37 XMLUTF8Transcoder t(UTF8, 4096); // block size isn't used any more anyway
39 bufsize = factor*srclen + 10;
40 char* buf = use_malloc ? reinterpret_cast<char*>(malloc(bufsize)) : new char[bufsize];
41 memset(buf,0,bufsize);
45 reinterpret_cast<XMLByte*>(buf),bufsize-1,
47 XMLTranscoder::UnRep_Throw);
49 catch (XMLException&) {
54 throw XMLToolingException("Source string contained an unrepresentable character.");
66 XMLCh* xmltooling::fromUTF8(const char* src, bool use_malloc)
69 xsecsize_t srclen=strlen(src);
70 XMLUTF8Transcoder t(UTF8, 4096); // block size isn't used any more anyway
71 XMLCh* buf = use_malloc ? reinterpret_cast<XMLCh*>(malloc((srclen+1)*sizeof(XMLCh))) : new XMLCh[srclen + 1];
72 unsigned char* sizes=new unsigned char[srclen];
73 memset(buf,0,(srclen+1)*sizeof(XMLCh));
75 reinterpret_cast<const XMLByte*>(src),srclen,
82 std::ostream& xmltooling::operator<<(std::ostream& ostr, const XMLCh* s)
85 char* p=xmltooling::toUTF8(s);
92 std::ostream& xmltooling::operator<<(std::ostream& ostr, const xstring& s)
94 return ostr << s.c_str();