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
master
Tomasz Sowa 4 years ago
parent 8b3ae14d9a
commit da4ed9c1f8

@ -100,35 +100,6 @@ void FileLog::open_file()
}
void FileLog::PutBuffer(PT::WTextStream * buffer, std::ostream & out_stream)
{
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;
PT::WTextStream::const_iterator i = buffer->begin();
while( i != buffer->end() )
{
if( index + utf8_sequence_max_length > buffer_len )
{
out_stream.write(utf8_buffer, index);
index = 0;
}
index += PT::IntToUTF8(*i, utf8_buffer + index, buffer_len - index);
++i;
}
if( index > 0 )
{
out_stream.write(utf8_buffer, index);
}
}
void FileLog::save_log(PT::WTextStream * buffer)
{
if( buffer->empty() )
@ -140,7 +111,7 @@ void FileLog::save_log(PT::WTextStream * buffer)
{
if( log_stdout )
{
PutBuffer(buffer, std::cout);
PT::WideToUTF8(*buffer, std::cout);
}
if( !log_file.empty() )
@ -155,7 +126,7 @@ void FileLog::save_log(PT::WTextStream * buffer)
if( file )
{
PutBuffer(buffer, file);
PT::WideToUTF8(*buffer, file);
file.flush();
}
}

@ -82,7 +82,6 @@ protected:
virtual bool synchro_lock();
virtual void synchro_unlock();
virtual void PutBuffer(PT::WTextStream * buffer, std::ostream & out_stream);
virtual void open_file();
};

@ -115,7 +115,7 @@ Log & Log::operator<<(const char * s)
{
if( buffer && file_log && s && current_level <= file_log->get_log_level() )
{
(*buffer) << s;
PT::UTF8ToWide(s, *buffer, false);
}
return *this;
@ -127,7 +127,7 @@ Log & Log::operator<<(const std::string & s)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
{
(*buffer) << s;
PT::UTF8ToWide(s, *buffer, false);
}
return *this;
@ -139,7 +139,7 @@ Log & Log::operator<<(const std::string * s)
{
if( buffer && file_log && current_level <= file_log->get_log_level() )
{
(*buffer) << *s;
PT::UTF8ToWide(*s, *buffer, false);
}
return *this;

@ -1,4 +1,7 @@
# DO NOT DELETE
mainspaceparser.o: mainspaceparser.h ../space/space.h ../textstream/types.h
mainspaceparser.o: ../utf8/utf8.h
mainspaceparser.o: ../utf8/utf8.h ../textstream/textstream.h ../date/date.h
mainspaceparser.o: ../convert/convert.h ../convert/inttostr.h
mainspaceparser.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h
mainspaceparser.o: ../membuffer/membuffer.h ../textstream/types.h

@ -1,11 +1,19 @@
# DO NOT DELETE
jsontospaceparser.o: jsontospaceparser.h space.h ../textstream/types.h
jsontospaceparser.o: ../utf8/utf8.h
jsontospaceparser.o: ../utf8/utf8.h ../textstream/textstream.h
jsontospaceparser.o: ../space/space.h ../date/date.h ../convert/convert.h
jsontospaceparser.o: ../convert/inttostr.h ../convert/strtoint.h
jsontospaceparser.o: ../convert/text.h ../convert/misc.h
jsontospaceparser.o: ../membuffer/membuffer.h ../textstream/types.h
space.o: space.h ../textstream/types.h ../utf8/utf8.h
space.o: ../textstream/textstream.h ../space/space.h ../date/date.h
space.o: ../convert/convert.h ../convert/inttostr.h ../convert/strtoint.h
space.o: ../convert/text.h ../convert/misc.h ../membuffer/membuffer.h
space.o: ../textstream/types.h
spaceparser.o: spaceparser.h space.h ../textstream/types.h ../utf8/utf8.h
spaceparser.o: ../textstream/textstream.h ../space/space.h ../date/date.h
spaceparser.o: ../convert/convert.h ../convert/inttostr.h
spaceparser.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h
spaceparser.o: ../membuffer/membuffer.h ../textstream/types.h
spacetojson.o: spacetojson.h space.h ../textstream/types.h

@ -1,3 +1,6 @@
# DO NOT DELETE
utf8.o: utf8.h
utf8.o: utf8.h ../textstream/textstream.h ../space/space.h
utf8.o: ../textstream/types.h ../date/date.h ../convert/convert.h
utf8.o: ../convert/inttostr.h ../convert/strtoint.h ../convert/text.h
utf8.o: ../convert/misc.h ../membuffer/membuffer.h ../textstream/types.h

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2014, Tomasz Sowa
* Copyright (c) 2010-2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -38,6 +38,7 @@
#include "utf8.h"
namespace PT
{
@ -264,6 +265,7 @@ static void IntToWide(int c, std::wstring & 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);
@ -275,36 +277,31 @@ static void IntToWide(int c, std::wstring & res)
}
static void IntToWide(int c, WTextStream & 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);
}
}
/*!
this function converts an utf8 string into wide string (std::wstring)
input:
utf8 - an input utf8 string
utf8_len - size of the input string
mode - what to do with errors when converting
0: skip an invalid character
1: put U+FFFD "replacement character" istead of the invalid character (default)
output:
res - an output wide string
the function returns false if there were some errors when converting
*/
bool UTF8ToWide(const char * utf8, size_t utf8_len, std::wstring & res, bool clear, int mode)
static bool UTF8ToWideGeneric(const char * utf8, size_t utf8_len, int mode, auto convert_function)
{
int z;
size_t len;
bool correct, was_error = false;
if( clear )
res.clear();
while( utf8_len > 0 )
{
if( (unsigned char)*utf8 <= 0x7f )
if( (unsigned char)*utf8 <= 0x7f )
{
// small optimization
len = 1;
@ -319,13 +316,13 @@ bool correct, was_error = false;
if( !correct )
{
if( mode == 1 )
res += 0xFFFD; // U+FFFD "replacement character"
convert_function(0xFFFD); // U+FFFD "replacement character"
was_error = true;
}
else
{
IntToWide(z, res);
convert_function(z);
}
utf8 += len;
@ -336,6 +333,36 @@ return !was_error;
}
/*!
this function converts an utf8 string into wide string (std::wstring)
input:
utf8 - an input utf8 string
utf8_len - size of the input string
mode - what to do with errors when converting
0: skip an invalid character
1: put U+FFFD "replacement character" istead of the invalid character (default)
output:
res - an output wide string
the function returns false if there were some errors when converting
*/
bool UTF8ToWide(const char * utf8, size_t utf8_len, std::wstring & res, bool clear, int mode)
{
if( clear )
res.clear();
bool status = UTF8ToWideGeneric(utf8, utf8_len, mode, [&res](int c) {
IntToWide(c, res);
});
return status;
}
/*!
this function converts an utf8 string into wide string (std::wstring)
@ -425,6 +452,67 @@ return !was_error;
}
// need to be tested
bool UTF8ToWide(const char * utf8, size_t utf8_len, WTextStream & res, bool clear, int mode)
{
if( clear )
res.clear();
bool status = UTF8ToWideGeneric(utf8, utf8_len, mode, [&res](int c) {
IntToWide(c, res);
});
return status;
}
// need to be tested
bool UTF8ToWide(const char * utf8, WTextStream & res, bool clear, int mode)
{
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
bool UTF8ToWide(const std::string & utf8, WTextStream & res, bool clear, int mode)
{
return UTF8ToWide(utf8.c_str(), utf8.size(), res, clear, mode);
}
// need to be tested
bool UTF8ToWide(std::istream & utf8, WTextStream & res, bool clear, int mode)
{
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
{
IntToWide(z, res);
}
}
return !was_error;
}
/*!
this function converts one wide character into UTF-8 sequence
@ -1174,6 +1262,58 @@ return res;
// not tested
// FIX ME it is not using surrogate pairs from input stream
// and mode parameter
static void WideToUTF8Generic(PT::WTextStream & buffer, int mode, auto 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;
PT::WTextStream::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);
}
}
// not tested
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear, int mode)
{
if( clear )
utf8.clear();
WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
utf8.append(utf8_buffer, buffer_len);
});
}
// not tested
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode)
{
WideToUTF8Generic(buffer, mode, [&utf8](const char * utf8_buffer, std::size_t buffer_len){
utf8.write(utf8_buffer, buffer_len);
});
}
} // namespace

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2010-2014, Tomasz Sowa
* Copyright (c) 2010-2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -40,11 +40,14 @@
#include <fstream>
#include <string>
#include "textstream/textstream.h"
namespace PT
{
/*!
UTF-8, a transformation format of ISO 10646
http://tools.ietf.org/html/rfc3629
@ -83,6 +86,16 @@ bool UTF8ToWide(const std::string & utf8, std::wstring & res, bool cle
bool UTF8ToWide(std::istream & utf8, std::wstring & res, bool clear = true, int mode = 1);
/*!
converting UTF-8 string to a WTextStream stream
(need to be tested)
*/
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);
/*!
converting one int character to UTF-8
*/
@ -97,18 +110,22 @@ size_t IntToUTF8(int z, std::ostream & utf8);
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);
void WideToUTF8(PT::WTextStream & buffer, std::string & utf8, bool clear = true, int mode = 1);// not tested
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);
void WideToUTF8(PT::WTextStream & buffer, std::ostream & utf8, int mode = 1);// not tested
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);
// implement void WideToUTF8(PT::WTextStream & buffer, 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);
// implement void WideToUTF8(PT::WTextStream & buffer, char * utf8, size_t utf8_len, int mode = 1);

Loading…
Cancel
Save