CPPXT-104 - Add exception handling to integer conversions
[shibboleth/cpp-xmltooling.git] / xmltooling / unicode.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * unicode.cpp
23  * 
24  * Helper classes and types for manipulating Unicode 
25  */
26
27 #include "internal.h"
28 #include "unicode.h"
29
30 #include <xercesc/util/XMLUTF8Transcoder.hpp>
31 #include <xercesc/util/XMLUniDefs.hpp>
32
33 using namespace xercesc;
34
35 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
36
37 char* xmltooling::toUTF8(const XMLCh* src, bool use_malloc)
38 {
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
42     do {
43         bufsize = factor*srclen + 10;
44         char* buf = use_malloc ? reinterpret_cast<char*>(malloc(bufsize)) : new char[bufsize];
45         memset(buf,0,bufsize);
46         try {
47             t.transcodeTo(
48                 src,srclen,
49                 reinterpret_cast<XMLByte*>(buf),bufsize-1,
50                 eaten,
51                 XMLTranscoder::UnRep_Throw);
52         }
53         catch (XMLException&) {
54             if (use_malloc)
55                 free(buf);
56             else
57                 delete[] buf;
58             throw XMLToolingException("Source string contained an unrepresentable character.");
59         }
60         if (eaten >= srclen)
61             return buf;
62         if (use_malloc)
63             free(buf);
64         else
65             delete[] buf;
66         factor++;
67     } while (1);
68 }
69
70 XMLCh* xmltooling::fromUTF8(const char* src, bool use_malloc)
71 {
72     xsecsize_t eaten;
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));
78     t.transcodeFrom(
79         reinterpret_cast<const XMLByte*>(src),srclen,
80         buf,srclen,
81         eaten,sizes);
82     delete[] sizes;
83     return buf;
84 }
85
86 std::ostream& xmltooling::operator<<(std::ostream& ostr, const XMLCh* s)
87 {
88     if (s) {
89         char* p=xmltooling::toUTF8(s);
90         ostr << p;
91         delete[] p;
92     }
93     return ostr;
94 }
95
96 std::ostream& xmltooling::operator<<(std::ostream& ostr, const xstring& s)
97 {
98     return ostr << s.c_str();
99 }