Back to index

nux  3.0.0
NUnicode.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "NuxCore.h"
00024 #include "NUnicode.h"
00025 
00026 
00027 namespace nux
00028 {
00029 
00030   ANSICHAR *UnicharToAnsicharConvertion::Convert (const UNICHAR *Source)
00031   {
00032     std::wstring utf16string (Source);
00033     size_t utf16size = utf16string.length();
00034     size_t utf8size = 6 * utf16size;
00035     ANSICHAR *utf8string = new ANSICHAR[utf8size+1];
00036 
00037     const wchar_t *source_start = utf16string.c_str();
00038     const wchar_t *source_end = source_start + utf16size;
00039     unsigned char *target_start = reinterpret_cast<unsigned char *> (utf8string);
00040     unsigned char *target_end = target_start + utf8size;
00041 
00042     ConversionResult res = ConvertUTF16toUTF8 (&source_start, source_end, &target_start, target_end, lenientConversion);
00043 
00044     if (res != conversionOK)
00045     {
00046       delete [] utf8string;
00047       utf8string = 0;
00048     }
00049 
00050     // mark end of string
00051     *target_start = 0;
00052     return utf8string;
00053   }
00054 
00055   UNICHAR *AnsicharToUnicharConvertion::Convert (const ANSICHAR *Source)
00056   {
00057     std::string utf8string (Source);
00058     size_t utf8size = utf8string.length();
00059     size_t utf16size = utf8size;
00060     UNICHAR *utf16string = new UNICHAR[utf16size+1];
00061 
00062     const unsigned char *source_start = reinterpret_cast<const unsigned char *> (utf8string.c_str() );
00063     const unsigned char *source_end = source_start + utf8size;
00064     wchar_t *target_start = reinterpret_cast<wchar_t *> (utf16string);
00065     wchar_t *target_end = target_start + utf16size;
00066 
00067     ConversionResult res = ConvertUTF8toUTF16 (&source_start, source_end, &target_start, target_end, lenientConversion);
00068 
00069     if (res != conversionOK)
00070     {
00071       delete[] utf16string;
00072       utf16string = 0;
00073     }
00074 
00075     // mark end of string
00076     *target_start = 0;
00077     return utf16string;
00078   }
00079 
00080 }