2012-01-11 12:15:20 +01:00
|
|
|
/*
|
2012-01-12 03:46:16 +01:00
|
|
|
* This file is a part of PikoTools
|
2012-01-11 12:15:20 +01:00
|
|
|
* and is distributed under the (new) BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
* Copyright (c) 2010-2018, Tomasz Sowa
|
2012-01-11 12:15:20 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
|
|
|
* project may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2012-01-12 03:46:16 +01:00
|
|
|
#ifndef headerfile_picotools_utf8_utf8
|
|
|
|
#define headerfile_picotools_utf8_utf8
|
2012-01-11 12:15:20 +01:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
#include "textstream/textstream.h"
|
2012-01-11 12:15:20 +01:00
|
|
|
|
|
|
|
|
2012-01-12 03:46:16 +01:00
|
|
|
namespace PT
|
2012-01-11 12:15:20 +01:00
|
|
|
{
|
|
|
|
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
|
|
|
|
|
2012-01-11 12:15:20 +01:00
|
|
|
/*!
|
|
|
|
UTF-8, a transformation format of ISO 10646
|
|
|
|
http://tools.ietf.org/html/rfc3629
|
|
|
|
|
|
|
|
when wchar_t is 4 bytes length we use UTF-32
|
|
|
|
when wchar_t is 2 bytes length we use UTF-16 (with surrogate pairs)
|
|
|
|
|
|
|
|
UTF-16
|
|
|
|
http://www.ietf.org/rfc/rfc2781.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
returns true if 'c' is a correct unicode character
|
|
|
|
*/
|
|
|
|
bool UTF8_CheckRange(int c);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
converting one character from UTF-8 to an int
|
|
|
|
*/
|
|
|
|
size_t UTF8ToInt(const char * utf8, size_t utf8_len, int & res, bool & correct);
|
|
|
|
size_t UTF8ToInt(const char * utf8, int & res, bool & correct);
|
|
|
|
size_t UTF8ToInt(const std::string & utf8, int & res, bool & correct);
|
|
|
|
size_t UTF8ToInt(std::istream & utf8, int & res, bool & correct);
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
converting UTF-8 string to a wide string
|
|
|
|
*/
|
|
|
|
bool UTF8ToWide(const char * utf8, size_t utf8_len, std::wstring & res, bool clear = true, int mode = 1);
|
|
|
|
bool UTF8ToWide(const char * utf8, std::wstring & res, bool clear = true, int mode = 1);
|
|
|
|
bool UTF8ToWide(const std::string & utf8, std::wstring & res, bool clear = true, int mode = 1);
|
|
|
|
bool UTF8ToWide(std::istream & utf8, std::wstring & res, bool clear = true, int mode = 1);
|
|
|
|
|
|
|
|
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
/*!
|
|
|
|
converting UTF-8 string to a WTextStream stream
|
|
|
|
(need to be tested)
|
|
|
|
*/
|
2018-11-24 18:09:25 +01:00
|
|
|
/*
|
|
|
|
implemented as templates below
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
|
|
|
|
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
|
|
|
|
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
|
|
|
|
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
|
2018-11-24 18:09:25 +01:00
|
|
|
*/
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
|
2012-01-11 12:15:20 +01:00
|
|
|
/*!
|
|
|
|
converting one int character to UTF-8
|
|
|
|
*/
|
|
|
|
size_t IntToUTF8(int z, char * utf8, size_t utf8_max_len);
|
|
|
|
size_t IntToUTF8(int z, std::string & utf8, bool clear = true );
|
|
|
|
size_t IntToUTF8(int z, std::ostream & utf8);
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
converting a wide string to UTF-8 string
|
|
|
|
*/
|
|
|
|
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, std::string & utf8, bool clear = true, int mode = 1);
|
|
|
|
bool WideToUTF8(const wchar_t * wide_string, std::string & utf8, bool clear = true, int mode = 1);
|
|
|
|
bool WideToUTF8(const std::wstring & wide_string, std::string & utf8, bool clear = true, int mode = 1);
|
2018-11-24 18:09:25 +01:00
|
|
|
|
|
|
|
// implemented as a template below
|
|
|
|
//void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);// not tested
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
|
2012-01-11 12:15:20 +01:00
|
|
|
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, std::ostream & utf8, int mode = 1);
|
|
|
|
bool WideToUTF8(const wchar_t * wide_string, std::ostream & utf8, int mode = 1);
|
|
|
|
bool WideToUTF8(const std::wstring & wide_string, std::ostream & utf8, int mode = 1);
|
2018-11-24 18:09:25 +01:00
|
|
|
|
|
|
|
// implemented as a template below
|
|
|
|
//void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);// not tested
|
2012-01-11 12:15:20 +01:00
|
|
|
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
|
|
|
|
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
|
|
|
|
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
// implement void WideToUTF8(PT::WTextStream & buffer, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
|
fixed: in static size_t WideToInt(const wchar_t * wide_string, size_t string_len, int & z, bool & correct)
we didn't test UTF8_CheckRange()
added: functions for converting from a wide string into an utf8 c-string:
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, size_t & utf8_written, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@962 e52654a7-88a9-db11-a3e9-0013d4bc506e
2014-10-04 13:18:33 +02:00
|
|
|
|
|
|
|
bool WideToUTF8(const wchar_t * wide_string, size_t string_len, char * utf8, size_t utf8_len, int mode = 1);
|
|
|
|
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_len, int mode = 1);
|
|
|
|
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_len, int mode = 1);
|
added to utf8:
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear = true, int mode = 1);
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);
these functions need some testing yet
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1156 e52654a7-88a9-db11-a3e9-0013d4bc506e
2018-11-24 14:30:45 +01:00
|
|
|
// implement void WideToUTF8(PT::WTextStream & buffer, char * utf8, size_t utf8_len, int mode = 1);
|
2012-01-11 12:15:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-24 18:09:25 +01:00
|
|
|
namespace private_namespace
|
|
|
|
{
|
|
|
|
template<typename function_type>
|
|
|
|
bool UTF8ToWideGeneric(const char * utf8, size_t utf8_len, int mode, function_type convert_function)
|
|
|
|
{
|
|
|
|
int z;
|
|
|
|
size_t len;
|
|
|
|
bool correct, was_error = false;
|
|
|
|
|
|
|
|
while( utf8_len > 0 )
|
|
|
|
{
|
|
|
|
if( (unsigned char)*utf8 <= 0x7f )
|
|
|
|
{
|
|
|
|
// small optimization
|
|
|
|
len = 1;
|
|
|
|
correct = true;
|
|
|
|
z = static_cast<unsigned char>(*utf8);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = UTF8ToInt(utf8, utf8_len, z, correct); // the len will be different from zero
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !correct )
|
|
|
|
{
|
|
|
|
if( mode == 1 )
|
|
|
|
convert_function(0xFFFD); // U+FFFD "replacement character"
|
|
|
|
|
|
|
|
was_error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
convert_function(z);
|
|
|
|
}
|
|
|
|
|
|
|
|
utf8 += len;
|
|
|
|
utf8_len -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !was_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
void IntToWide(int c, TextStreamBase<char_type, stack_size, heap_block_size> & res)
|
|
|
|
{
|
|
|
|
if( sizeof(wchar_t)==2 && c>0xffff )
|
|
|
|
{
|
|
|
|
// UTF16 surrogate pairs
|
|
|
|
c -= 0x10000;
|
|
|
|
res << static_cast<wchar_t>(((c >> 10) & 0x3FF) + 0xD800);
|
|
|
|
res << static_cast<wchar_t>((c & 0x3FF) + 0xDC00);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res << static_cast<wchar_t>(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// not tested
|
|
|
|
// FIX ME it is not using surrogate pairs from input stream
|
|
|
|
// and mode parameter
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size, typename function_type>
|
|
|
|
void WideToUTF8Generic(TextStreamBase<char_type, stack_size, heap_block_size> & buffer, int mode, function_type write_function)
|
|
|
|
{
|
|
|
|
char utf8_buffer[256];
|
|
|
|
std::size_t buffer_len = sizeof(utf8_buffer) / sizeof(char);
|
|
|
|
std::size_t utf8_sequence_max_length = 10;
|
|
|
|
std::size_t index = 0;
|
|
|
|
|
|
|
|
typename TextStreamBase<char_type, stack_size, heap_block_size>::const_iterator i = buffer.begin();
|
|
|
|
|
|
|
|
while( i != buffer.end() )
|
|
|
|
{
|
|
|
|
if( index + utf8_sequence_max_length > buffer_len )
|
|
|
|
{
|
|
|
|
write_function(utf8_buffer, index);
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
index += PT::IntToUTF8(*i, utf8_buffer + index, buffer_len - index);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( index > 0 )
|
|
|
|
{
|
|
|
|
write_function(utf8_buffer, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// need to be tested
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
bool UTF8ToWide(const char * utf8, size_t utf8_len, TextStreamBase<char_type, stack_size, heap_block_size> & res, bool clear = true, int mode = 1)
|
|
|
|
{
|
|
|
|
if( clear )
|
|
|
|
res.clear();
|
|
|
|
|
|
|
|
bool status = private_namespace::UTF8ToWideGeneric(utf8, utf8_len, mode, [&res](int c) {
|
|
|
|
private_namespace::IntToWide(c, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// need to be tested
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
bool UTF8ToWide(const char * utf8, TextStreamBase<char_type, stack_size, heap_block_size> & res, bool clear = true, int mode = 1)
|
|
|
|
{
|
|
|
|
size_t utf8_len = 0;
|
|
|
|
|
|
|
|
while( utf8[utf8_len] != 0 )
|
|
|
|
utf8_len += 1;
|
|
|
|
|
|
|
|
return UTF8ToWide(utf8, utf8_len, res, clear, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// need to be tested
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
bool UTF8ToWide(const std::string & utf8, TextStreamBase<char_type, stack_size, heap_block_size> & res, bool clear = true, int mode = 1)
|
|
|
|
{
|
|
|
|
return UTF8ToWide(utf8.c_str(), utf8.size(), res, clear, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// need to be tested
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
bool UTF8ToWide(std::istream & utf8, TextStreamBase<char_type, stack_size, heap_block_size> & res, bool clear = true, int mode = 1)
|
|
|
|
{
|
|
|
|
int z;
|
|
|
|
bool correct, was_error = false;
|
|
|
|
|
|
|
|
if( clear )
|
|
|
|
res.clear();
|
|
|
|
|
|
|
|
while( UTF8ToInt(utf8, z, correct) > 0 )
|
|
|
|
{
|
|
|
|
if( !correct )
|
|
|
|
{
|
|
|
|
if( mode == 1 )
|
|
|
|
res << 0xFFFD; // U+FFFD "replacement character"
|
|
|
|
|
|
|
|
was_error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
private_namespace::IntToWide(z, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !was_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// not tested
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
void WideToUTF8(TextStreamBase<char_type, stack_size, heap_block_size> & buffer, std::string & utf8, bool clear = true, int mode = 1)
|
|
|
|
{
|
|
|
|
if( clear )
|
|
|
|
utf8.clear();
|
|
|
|
|
|
|
|
private_namespace::WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
|
|
|
|
utf8.append(utf8_buffer, buffer_len);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// not tested
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
void WideToUTF8(TextStreamBase<char_type, stack_size, heap_block_size> & buffer, std::ostream & utf8, int mode = 1)
|
|
|
|
{
|
|
|
|
private_namespace::WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
|
|
|
|
utf8.write(utf8_buffer, buffer_len);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-12 03:46:16 +01:00
|
|
|
} // namespace
|
2012-01-11 12:15:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|