UnicodeConverter.cpp
Go to the documentation of this file.
00001 //
00002 // UnicodeConverter.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/UnicodeConverter.cpp#2 $
00005 //
00006 // Library: Foundation
00007 // Package: Text
00008 // Module:  UnicodeConverter
00009 //
00010 // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
00034 //
00035 
00036 
00037 #ifndef POCO_NO_WSTRING
00038 
00039 
00040 #include "Poco/UnicodeConverter.h"
00041 #include "Poco/TextConverter.h"
00042 #include "Poco/TextIterator.h"
00043 #include "Poco/UTF8Encoding.h"
00044 #include "Poco/UTF16Encoding.h"
00045 #include <cstring>
00046 #include <wchar.h>
00047 
00048 
00049 namespace Poco {
00050 
00051 
00052 void UnicodeConverter::toUTF16(const std::string& utf8String, std::wstring& utf16String)
00053 {
00054         utf16String.clear();
00055         UTF8Encoding utf8Encoding;
00056         TextIterator it(utf8String, utf8Encoding);
00057         TextIterator end(utf8String);
00058         while (it != end) utf16String += (wchar_t) *it++;
00059 }
00060 
00061 
00062 void UnicodeConverter::toUTF16(const char* utf8String, int length, std::wstring& utf16String)
00063 {
00064         poco_check_ptr (utf8String);
00065 
00066         utf16String.clear();
00067 
00068         UTF8Encoding utf8Encoding;
00069         UTF16Encoding utf16Encoding;
00070         const unsigned char* it  = (const unsigned char*) utf8String;
00071         const unsigned char* end = (const unsigned char*) utf8String + length;
00072         
00073         while (it < end)
00074         {
00075                 unsigned char c = *it;
00076                 int n = utf8Encoding.characterMap()[c];
00077                 int uc = '?';
00078                 if (n == -1) 
00079                 {
00080                         ++it;
00081                 }
00082                 else if (n >= 0)
00083                 {
00084                         uc = n;
00085                         ++it;
00086                 }
00087                 else
00088                 {
00089                         if (it - n <= end)
00090                         {
00091                                 uc = utf8Encoding.convert(it);
00092                                 if (uc == -1) uc = '?';
00093                         }
00094                         it -= n;
00095                 }
00096                 utf16String += (wchar_t) uc; // TODO: surrogates
00097         }
00098 }
00099 
00100 
00101 void UnicodeConverter::toUTF16(const char* utf8String, std::wstring& utf16String)
00102 {
00103         toUTF16(utf8String, (int) std::strlen(utf8String), utf16String);
00104 }
00105 
00106 
00107 void UnicodeConverter::toUTF8(const std::wstring& utf16String, std::string& utf8String)
00108 {
00109         utf8String.clear();
00110         UTF8Encoding utf8Encoding;
00111         UTF16Encoding utf16Encoding;
00112         TextConverter converter(utf16Encoding, utf8Encoding);
00113         converter.convert(utf16String.data(), (int) utf16String.length()*sizeof(wchar_t), utf8String);
00114 }
00115 
00116 
00117 void UnicodeConverter::toUTF8(const wchar_t* utf16String, int length, std::string& utf8String)
00118 {
00119         utf8String.clear();
00120         UTF8Encoding utf8Encoding;
00121         UTF16Encoding utf16Encoding;
00122         TextConverter converter(utf16Encoding, utf8Encoding);
00123         converter.convert(utf16String, (int) length*sizeof(wchar_t), utf8String);
00124 }
00125 
00126 
00127 void UnicodeConverter::toUTF8(const wchar_t* utf16String, std::string& utf8String)
00128 {
00129         toUTF8(utf16String, (int) wcslen(utf16String), utf8String);
00130 }
00131 
00132 
00133 } // namespace Poco
00134 
00135 
00136 #endif // POCO_NO_WSTRING


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19