X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2Funicode.cpp;h=b54d1ed2ddfcf66583006218424663ba50ba2341;hb=990b41af1cefa4f68b2350b94330d8eafc6a56ee;hp=42fc020ecd0ede27a140af6091f149413a4e0233;hpb=652cd172b45f1c52e7add1cb63b71c28bd5c0220;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/unicode.cpp b/xmltooling/unicode.cpp index 42fc020..b54d1ed 100644 --- a/xmltooling/unicode.cpp +++ b/xmltooling/unicode.cpp @@ -1,59 +1,99 @@ -/* - * Copyright 2001-2006 Internet2 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * unicode.cpp - * - * Helper classes and types for manipulating Unicode - */ - -#include "internal.h" -#include "unicode.h" - -#include -#include - -static XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull }; - -char* xmltooling::toUTF8(const XMLCh* src) -{ - unsigned int eaten; - unsigned int srclen=XMLString::stringLen(src); - XMLUTF8Transcoder t(UTF8, srclen*4 + 1); - char* buf=new char[srclen*4 + 1]; - memset(buf,0,srclen*4 + 1); - t.transcodeTo( - src,srclen, - reinterpret_cast(buf),srclen*4, - eaten,XMLTranscoder::UnRep_RepChar); - return buf; -} - -XMLCh* xmltooling::fromUTF8(const char* src) -{ - unsigned int eaten; - unsigned int srclen=strlen(src); - XMLUTF8Transcoder t(UTF8, srclen + 1); - XMLCh* buf=new XMLCh[srclen + 1]; - unsigned char* sizes=new unsigned char[srclen]; - memset(buf,0,(srclen+1)*sizeof(XMLCh)); - t.transcodeFrom( - reinterpret_cast(src),srclen, - buf,srclen, - eaten,sizes); - delete[] sizes; - return buf; -} +/** + * Licensed to the University Corporation for Advanced Internet + * Development, Inc. (UCAID) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + * + * UCAID licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the + * License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the License. + */ + +/** + * unicode.cpp + * + * Helper classes and types for manipulating Unicode + */ + +#include "internal.h" +#include "unicode.h" + +#include +#include + +using namespace xercesc; + +static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull }; + +char* xmltooling::toUTF8(const XMLCh* src, bool use_malloc) +{ + xsecsize_t eaten,factor=1,bufsize; + xsecsize_t srclen=XMLString::stringLen(src); + XMLUTF8Transcoder t(UTF8, 4096); // block size isn't used any more anyway + do { + bufsize = factor*srclen + 10; + char* buf = use_malloc ? reinterpret_cast(malloc(bufsize)) : new char[bufsize]; + memset(buf,0,bufsize); + try { + t.transcodeTo( + src,srclen, + reinterpret_cast(buf),bufsize-1, + eaten, + XMLTranscoder::UnRep_Throw); + } + catch (XMLException&) { + if (use_malloc) + free(buf); + else + delete[] buf; + throw XMLToolingException("Source string contained an unrepresentable character."); + } + if (eaten >= srclen) + return buf; + if (use_malloc) + free(buf); + else + delete[] buf; + factor++; + } while (1); +} + +XMLCh* xmltooling::fromUTF8(const char* src, bool use_malloc) +{ + xsecsize_t eaten; + xsecsize_t srclen=strlen(src); + XMLUTF8Transcoder t(UTF8, 4096); // block size isn't used any more anyway + XMLCh* buf = use_malloc ? reinterpret_cast(malloc((srclen+1)*sizeof(XMLCh))) : new XMLCh[srclen + 1]; + unsigned char* sizes=new unsigned char[srclen]; + memset(buf,0,(srclen+1)*sizeof(XMLCh)); + t.transcodeFrom( + reinterpret_cast(src),srclen, + buf,srclen, + eaten,sizes); + delete[] sizes; + return buf; +} + +std::ostream& xmltooling::operator<<(std::ostream& ostr, const XMLCh* s) +{ + if (s) { + char* p=xmltooling::toUTF8(s); + ostr << p; + delete[] p; + } + return ostr; +} + +std::ostream& xmltooling::operator<<(std::ostream& ostr, const xstring& s) +{ + return ostr << s.c_str(); +}