b8deeb32912d14ae6bda0fab1e87c1aae43bf388
[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 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
30
31 char* xmltooling::toUTF8(const XMLCh* src, bool use_malloc)
32 {
33     unsigned int eaten,factor=1,bufsize;
34     unsigned int srclen=XMLString::stringLen(src);
35     XMLUTF8Transcoder t(UTF8, 4096);    // block size isn't used any more anyway
36     do {
37         bufsize = factor*srclen + 10;
38         char* buf = use_malloc ? reinterpret_cast<char*>(malloc(bufsize)) : new char[bufsize];
39         memset(buf,0,bufsize);
40         try {
41             t.transcodeTo(
42                 src,srclen,
43                 reinterpret_cast<XMLByte*>(buf),bufsize-1,
44                 eaten,
45                 XMLTranscoder::UnRep_Throw);
46         }
47         catch (XMLException&) {
48             if (use_malloc)
49                 free(buf);
50             else
51                 delete[] buf;
52             throw XMLToolingException("Source string contained an unrepresentable character.");
53         }
54         if (eaten >= srclen)
55             return buf;
56         if (use_malloc)
57             free(buf);
58         else
59             delete[] buf;
60         factor++;
61     } while (1);
62 }
63
64 XMLCh* xmltooling::fromUTF8(const char* src, bool use_malloc)
65 {
66     unsigned int eaten;
67     unsigned int srclen=strlen(src);
68     XMLUTF8Transcoder t(UTF8, 4096);    // block size isn't used any more anyway
69     XMLCh* buf = use_malloc ? reinterpret_cast<XMLCh*>(malloc((srclen+1)*sizeof(XMLCh))) : new XMLCh[srclen + 1];
70     unsigned char* sizes=new unsigned char[srclen];
71     memset(buf,0,(srclen+1)*sizeof(XMLCh));
72     t.transcodeFrom(
73         reinterpret_cast<const XMLByte*>(src),srclen,
74         buf,srclen,
75         eaten,sizes);
76     delete[] sizes;
77     return buf;
78 }
79
80 std::ostream& xmltooling::operator<<(std::ostream& ostr, const XMLCh* s)
81 {
82     if (s) {
83         char* p=xmltooling::toUTF8(s);
84         ostr << p;
85         delete[] p;
86     }
87     return ostr;
88 }