1e50c9c402e3b651e9b9171cf0e6212fb4cb6116
[shibboleth/cpp-xmltooling.git] / xmltooling / unicode.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * unicode.cpp
19  * 
20  * Helper classes and types for manipulating Unicode 
21  */
22
23 #include "internal.h"
24 #include "unicode.h"
25
26 #include <xercesc/util/XMLUTF8Transcoder.hpp>
27 #include <xercesc/util/XMLUniDefs.hpp>
28
29 using namespace xercesc;
30
31 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
32
33 char* xmltooling::toUTF8(const XMLCh* src, bool use_malloc)
34 {
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
38     do {
39         bufsize = factor*srclen + 10;
40         char* buf = use_malloc ? reinterpret_cast<char*>(malloc(bufsize)) : new char[bufsize];
41         memset(buf,0,bufsize);
42         try {
43             t.transcodeTo(
44                 src,srclen,
45                 reinterpret_cast<XMLByte*>(buf),bufsize-1,
46                 eaten,
47                 XMLTranscoder::UnRep_Throw);
48         }
49         catch (XMLException&) {
50             if (use_malloc)
51                 free(buf);
52             else
53                 delete[] buf;
54             throw XMLToolingException("Source string contained an unrepresentable character.");
55         }
56         if (eaten >= srclen)
57             return buf;
58         if (use_malloc)
59             free(buf);
60         else
61             delete[] buf;
62         factor++;
63     } while (1);
64 }
65
66 XMLCh* xmltooling::fromUTF8(const char* src, bool use_malloc)
67 {
68     xsecsize_t eaten;
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));
74     t.transcodeFrom(
75         reinterpret_cast<const XMLByte*>(src),srclen,
76         buf,srclen,
77         eaten,sizes);
78     delete[] sizes;
79     return buf;
80 }
81
82 std::ostream& xmltooling::operator<<(std::ostream& ostr, const XMLCh* s)
83 {
84     if (s) {
85         char* p=xmltooling::toUTF8(s);
86         ostr << p;
87         delete[] p;
88     }
89     return ostr;
90 }
91
92 std::ostream& xmltooling::operator<<(std::ostream& ostr, const xstring& s)
93 {
94     return ostr << s.c_str();
95 }