fix: put 'char' type directly to the output stream
Char type was converted to wchar_t and then was serialized as utf-8 stream. Let char type will always be one char, of course it need to be a valid utf-8 sequence. Let FT::dont_use_utf8 apply only to wchar_t and std::wstring but ignore it if FT::hexadecimal or FT::binary are defined. Now we have bool BaseExpression::esc_char(wchar_t val, pt::TextStream & stream) method which (in most cases) will be used in derived classes. Let wchar_t (and std::wstring) will be stored as 8 hex digits when using FT::hexadecimal or FT::binary (and ignore FT::dont_use_utf8 in such a case).
This commit is contained in:
parent
48d515ea64
commit
0bdabfc7b4
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -381,6 +381,38 @@ void BaseExpression::char_to_hex(char c, pt::TextStream & stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void BaseExpression::char_to_hex(wchar_t c, pt::TextStream & stream)
|
||||||
|
{
|
||||||
|
unsigned int z = static_cast<unsigned int>(c);
|
||||||
|
|
||||||
|
char_to_hex((char)(unsigned char)(z >> 24), stream);
|
||||||
|
char_to_hex((char)(unsigned char)(z >> 16), stream);
|
||||||
|
char_to_hex((char)(unsigned char)(z >> 8), stream);
|
||||||
|
char_to_hex((char)(unsigned char)(z), stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* return true if the val character was escaped and put (or ignored) to the stream
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
bool BaseExpression::esc_char(char val, pt::TextStream & stream)
|
||||||
|
{
|
||||||
|
return esc_char((wchar_t)(unsigned char)val, stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* return true if the val character was escaped and put (or ignored) to the stream
|
||||||
|
*
|
||||||
|
* in most caces you have to provide your own esc_char(wchar_t val, pt::TextStream & stream) method
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
bool BaseExpression::esc_char(wchar_t val, pt::TextStream & stream)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void BaseExpression::esc(char val, pt::TextStream & stream, const FT & field_type)
|
void BaseExpression::esc(char val, pt::TextStream & stream, const FT & field_type)
|
||||||
|
@ -390,9 +422,12 @@ void BaseExpression::esc(char val, pt::TextStream & stream, const FT & field_typ
|
||||||
char_to_hex(val, stream);
|
char_to_hex(val, stream);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
if( !esc_char(val, stream) )
|
||||||
{
|
{
|
||||||
stream << val;
|
stream << val;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -404,48 +439,46 @@ void BaseExpression::esc(unsigned char val, pt::TextStream & stream, const FT &
|
||||||
|
|
||||||
void BaseExpression::esc(wchar_t val, pt::TextStream & stream, const FT & field_type)
|
void BaseExpression::esc(wchar_t val, pt::TextStream & stream, const FT & field_type)
|
||||||
{
|
{
|
||||||
|
if( field_type.is_binary() || field_type.is_hexadecimal() )
|
||||||
|
{
|
||||||
|
char_to_hex(val, stream);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if( field_type.use_utf8() )
|
if( field_type.use_utf8() )
|
||||||
{
|
{
|
||||||
char utf8_buf[10];
|
if( !esc_char(val, stream) )
|
||||||
|
|
||||||
// FIXME surrogate pairs are not used
|
|
||||||
size_t utf8_len = pt::int_to_utf8((int)val, utf8_buf, sizeof(utf8_buf));
|
|
||||||
|
|
||||||
for(size_t a = 0 ; a < utf8_len ; ++a)
|
|
||||||
{
|
{
|
||||||
esc(utf8_buf[a], stream, field_type);
|
stream << val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
esc(static_cast<char>(val), stream, field_type);
|
char val_char = (char)(unsigned char)val;
|
||||||
|
|
||||||
|
if( !esc_char(val_char, stream) )
|
||||||
|
{
|
||||||
|
stream << val_char;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, pt::TextStream & stream, const FT & field_type)
|
void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, pt::TextStream & stream, const FT & field_type)
|
||||||
{
|
{
|
||||||
if( field_type.use_utf8() )
|
|
||||||
{
|
|
||||||
char utf8_buf[10];
|
|
||||||
|
|
||||||
for(size_t i = 0 ; has_known_length ? (i < len) : val[i] != 0 ; ++i)
|
for(size_t i = 0 ; has_known_length ? (i < len) : val[i] != 0 ; ++i)
|
||||||
{
|
{
|
||||||
// FIXME surrogate pairs are not used
|
esc(val[i], stream, field_type);
|
||||||
size_t utf8_len = pt::int_to_utf8((int)val[i], utf8_buf, sizeof(utf8_buf));
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for(size_t a = 0 ; a < utf8_len ; ++a)
|
|
||||||
{
|
void BaseExpression::esc(const char * val, bool has_known_length, size_t len, pt::TextStream & stream, const FT & field_type)
|
||||||
esc(utf8_buf[a], stream, field_type);
|
{
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for(size_t i = 0 ; has_known_length ? (i < len) : val[i] != 0 ; ++i)
|
for(size_t i = 0 ; has_known_length ? (i < len) : val[i] != 0 ; ++i)
|
||||||
{
|
{
|
||||||
esc(static_cast<char>(val[i]), stream, field_type);
|
esc(val[i], stream, field_type);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -464,19 +497,13 @@ void BaseExpression::esc(const wchar_t * val, pt::TextStream & stream, const FT
|
||||||
|
|
||||||
void BaseExpression::esc(const std::string & val, pt::TextStream & stream, const FT & field_type)
|
void BaseExpression::esc(const std::string & val, pt::TextStream & stream, const FT & field_type)
|
||||||
{
|
{
|
||||||
for(size_t i = 0 ; i < val.size() ; ++i)
|
esc(val.c_str(), true, val.size(), stream, field_type);
|
||||||
{
|
|
||||||
esc(val[i], stream, field_type);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BaseExpression::esc(const char * val, pt::TextStream & stream, const FT & field_type)
|
void BaseExpression::esc(const char * val, pt::TextStream & stream, const FT & field_type)
|
||||||
{
|
{
|
||||||
for(size_t i = 0 ; val[i] != 0 ; ++i)
|
esc(val, false, 0, stream, field_type);
|
||||||
{
|
|
||||||
esc(val[i], stream, field_type);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -275,9 +275,11 @@ public:
|
||||||
* esc for: signed char, wchar_t, char16_t, char32_t
|
* esc for: signed char, wchar_t, char16_t, char32_t
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
virtual bool esc_char(char val, pt::TextStream & stream);
|
||||||
|
virtual bool esc_char(wchar_t val, pt::TextStream & stream);
|
||||||
|
|
||||||
virtual void esc(char val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
virtual void esc(char val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
||||||
virtual void esc(unsigned char val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
virtual void esc(unsigned char val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
||||||
|
|
||||||
virtual void esc(wchar_t val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
virtual void esc(wchar_t val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
||||||
|
|
||||||
virtual void esc(const std::wstring & val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
virtual void esc(const std::wstring & val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
||||||
|
@ -298,7 +300,6 @@ public:
|
||||||
virtual void esc(float val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
virtual void esc(float val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
||||||
virtual void esc(double val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
virtual void esc(double val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
||||||
virtual void esc(long double val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
virtual void esc(long double val, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
||||||
//virtual void esc(void* val, pt::TextStream & stream);
|
|
||||||
|
|
||||||
virtual void esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
virtual void esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
||||||
virtual void esc(const pt::TextStream & val,pt::TextStream & stream, const FT & field_type = FT::default_type);
|
virtual void esc(const pt::TextStream & val,pt::TextStream & stream, const FT & field_type = FT::default_type);
|
||||||
|
@ -628,8 +629,10 @@ protected:
|
||||||
|
|
||||||
char char_to_hex_part(char c);
|
char char_to_hex_part(char c);
|
||||||
void char_to_hex(char c, pt::TextStream & stream);
|
void char_to_hex(char c, pt::TextStream & stream);
|
||||||
|
void char_to_hex(wchar_t c, pt::TextStream & stream);
|
||||||
|
|
||||||
void esc(const wchar_t * val, bool has_known_length, size_t len, pt::TextStream & stream, const FT & field_type = FT::default_type);
|
void esc(const wchar_t * val, bool has_known_length, size_t len, pt::TextStream & stream, const FT & field_type);
|
||||||
|
void esc(const char * val, bool has_known_length, size_t len, pt::TextStream & stream, const FT & field_type);
|
||||||
|
|
||||||
bool is_empty_field(const wchar_t * value);
|
bool is_empty_field(const wchar_t * value);
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -313,7 +313,7 @@ void DbConnector::allocate_default_expression_if_needed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char DbConnector::unescape_hex_char_part(char hex)
|
unsigned int DbConnector::unescape_hex_char_part(char hex)
|
||||||
{
|
{
|
||||||
if( hex>='0' && hex<='9' )
|
if( hex>='0' && hex<='9' )
|
||||||
{
|
{
|
||||||
|
@ -348,43 +348,79 @@ char DbConnector::unescape_hex_char_part(char hex)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char DbConnector::unescape_hex_char(char char1, char char2)
|
|
||||||
{
|
|
||||||
int c1 = unescape_hex_char_part(char1);
|
|
||||||
int c2 = unescape_hex_char_part(char2);
|
|
||||||
|
|
||||||
return static_cast<char>(((c1 << 4) | c2));
|
const char * DbConnector::unescape_hex_char(const char * str, size_t len, unsigned int & res)
|
||||||
|
{
|
||||||
|
unsigned int c;
|
||||||
|
res = 0;
|
||||||
|
|
||||||
|
for(size_t i = 0 ; i < len ; ++i)
|
||||||
|
{
|
||||||
|
if( *str != 0 )
|
||||||
|
{
|
||||||
|
c = unescape_hex_char_part(*str);
|
||||||
|
str += 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
c = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
res = (res << 4) | c;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DbConnector::unescape_hex_char(const char * str, char & c)
|
||||||
|
{
|
||||||
|
unsigned int res = 0;
|
||||||
|
unescape_hex_char(str, sizeof(char) * 2, res);
|
||||||
|
c = (char)res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DbConnector::unescape_hex_char(const char * str, wchar_t & c)
|
||||||
|
{
|
||||||
|
unsigned int res = 0;
|
||||||
|
unescape_hex_char(str, sizeof(wchar_t) * 2, res);
|
||||||
|
c = (wchar_t)res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DbConnector::unescape_bin_char(const char * str, char & c)
|
||||||
|
{
|
||||||
|
unescape_hex_char(str, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DbConnector::unescape_bin_char(const char * str, wchar_t & c)
|
||||||
|
{
|
||||||
|
unescape_hex_char(str, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void DbConnector::unescape_hex_string(const char * str, std::string & out)
|
void DbConnector::unescape_hex_string(const char * str, std::string & out)
|
||||||
{
|
{
|
||||||
for(size_t i=0 ; str[i] != 0 ; i+=2 )
|
unsigned int c = 0;
|
||||||
|
|
||||||
|
while( *str != 0 )
|
||||||
{
|
{
|
||||||
out += unescape_hex_char(str[i], str[i+1]);
|
str = unescape_hex_char(str, sizeof(char) * 2, c);
|
||||||
|
out += (char)c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void DbConnector::unescape_hex_string(const char * str, std::wstring & out, const FT & field_type)
|
void DbConnector::unescape_hex_string(const char * str, std::wstring & out)
|
||||||
{
|
{
|
||||||
if( field_type.use_utf8() )
|
unsigned int c = 0;
|
||||||
{
|
|
||||||
size_t len;
|
|
||||||
wchar_t c;
|
|
||||||
|
|
||||||
while( *str != 0 && (len = unescape_hex_char(str, c, field_type)) > 0 )
|
while( *str != 0 )
|
||||||
{
|
{
|
||||||
out += c;
|
str = unescape_hex_char(str, sizeof(wchar_t) * 2, c);
|
||||||
str += len;
|
out += (wchar_t)c;
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for(size_t i=0 ; str[i] != 0 ; i+=2 )
|
|
||||||
{
|
|
||||||
out += static_cast<wchar_t>(static_cast<unsigned char>(unescape_hex_char(str[i], str[i+1])));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -395,151 +431,53 @@ void DbConnector::unescape_bin_string(const char * str, std::string & out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void DbConnector::unescape_bin_string(const char * str, std::wstring & out, const FT & field_type)
|
void DbConnector::unescape_bin_string(const char * str, std::wstring & out)
|
||||||
{
|
{
|
||||||
unescape_hex_string(str, out, field_type);
|
unescape_hex_string(str, out);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// returns how many characters have been provided to utf8_str buffer
|
|
||||||
// min size of utf8_str should be 5 bytes (max 4 bytes for utf8 sequence + terminating null)
|
|
||||||
size_t DbConnector::unescape_hex_char(const char * value_str, char * utf8_str, size_t utf8_str_max_len)
|
|
||||||
{
|
|
||||||
size_t value_str_index = 0;
|
|
||||||
size_t utf8_str_index = 0;
|
|
||||||
|
|
||||||
utf8_str[0] = 0;
|
|
||||||
|
|
||||||
while( utf8_str_index + 1 < utf8_str_max_len )
|
|
||||||
{
|
|
||||||
if( value_str[value_str_index] != 0 && value_str[value_str_index+1] != 0 )
|
|
||||||
{
|
|
||||||
utf8_str[utf8_str_index] = unescape_hex_char(value_str[value_str_index], value_str[value_str_index+1]);
|
|
||||||
utf8_str[utf8_str_index+1] = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
value_str_index += 2;
|
|
||||||
utf8_str_index += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return utf8_str_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// CHECKME need to be tested
|
|
||||||
// returns how many characters were used from value_str
|
|
||||||
size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_value, const FT & field_type)
|
|
||||||
{
|
|
||||||
size_t len = 0;
|
|
||||||
|
|
||||||
if( field_type.use_utf8() )
|
|
||||||
{
|
|
||||||
char utf8_str[4 + 1]; // max utf8 sequence length + terminating zero
|
|
||||||
size_t utf8_str_len = unescape_hex_char(value_str, utf8_str, sizeof(utf8_str) / sizeof(char));
|
|
||||||
|
|
||||||
int value_int;
|
|
||||||
bool is_correct;
|
|
||||||
len = pt::utf8_to_int(utf8_str, utf8_str_len, value_int, is_correct);
|
|
||||||
len = len * 2;
|
|
||||||
|
|
||||||
if( is_correct )
|
|
||||||
{
|
|
||||||
field_value = static_cast<wchar_t>(value_int);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( log )
|
|
||||||
{
|
|
||||||
(*log) << pt::Log::log2 << "Morm: incorrect utf-8 sequence (ignoring)" << pt::Log::logend;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( value_str[0] != 0 && value_str[1] != 0 )
|
|
||||||
{
|
|
||||||
field_value = static_cast<wchar_t>(static_cast<unsigned char>(unescape_hex_char(value_str[0], value_str[1])));
|
|
||||||
len = 2;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( log )
|
|
||||||
{
|
|
||||||
(*log) << pt::Log::log2 << "Morm: unexpected end of string (ignoring)" << pt::Log::logend;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
size_t DbConnector::unescape_bin_char(const char * value_str, wchar_t & field_value, const FT & field_type)
|
|
||||||
{
|
|
||||||
return unescape_hex_char(value_str, field_value, field_type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// CHECKME need to be tested
|
|
||||||
void DbConnector::get_value(const char * value_str, char & field_value, const FT & field_type)
|
void DbConnector::get_value(const char * value_str, char & field_value, const FT & field_type)
|
||||||
{
|
{
|
||||||
wchar_t c;
|
if( field_type.is_hexadecimal() )
|
||||||
|
|
||||||
field_value = 0;
|
|
||||||
get_value(value_str, c, field_type);
|
|
||||||
|
|
||||||
if( field_type.use_utf8() )
|
|
||||||
{
|
{
|
||||||
if( c <= 127 )
|
unescape_hex_char(value_str, field_value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if( field_type.is_binary() )
|
||||||
{
|
{
|
||||||
field_value = static_cast<char>(c);
|
unescape_bin_char(value_str, field_value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if( log )
|
field_value = *value_str;
|
||||||
{
|
|
||||||
(*log) << pt::Log::log2 << "Morm: a character greater than 127 cannot be stored in char type, code point: "
|
|
||||||
<< (int)c << " '" << c << "'" << pt::Log::logend;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
field_value = static_cast<char>(c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// CHECKME need to be tested
|
|
||||||
void DbConnector::get_value(const char * value_str, unsigned char & field_value, const FT & field_type)
|
void DbConnector::get_value(const char * value_str, unsigned char & field_value, const FT & field_type)
|
||||||
{
|
{
|
||||||
char tmp_char;
|
char tmp_char;
|
||||||
get_value(value_str, tmp_char, field_type);
|
get_value(value_str, tmp_char, field_type);
|
||||||
|
|
||||||
field_value = static_cast<unsigned char>(tmp_char);
|
field_value = static_cast<unsigned char>(tmp_char);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// CHECKME need to be tested
|
|
||||||
void DbConnector::get_value(const char * value_str, wchar_t & field_value, const FT & field_type)
|
void DbConnector::get_value(const char * value_str, wchar_t & field_value, const FT & field_type)
|
||||||
{
|
{
|
||||||
field_value = 0;
|
field_value = 0;
|
||||||
|
|
||||||
if( field_type.is_binary() )
|
|
||||||
{
|
|
||||||
unescape_bin_char(value_str, field_value, field_type);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
if( field_type.is_hexadecimal() )
|
if( field_type.is_hexadecimal() )
|
||||||
{
|
{
|
||||||
unescape_hex_char(value_str, field_value, field_type);
|
unescape_hex_char(value_str, field_value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if( field_type.is_binary() )
|
||||||
|
{
|
||||||
|
unescape_bin_char(value_str, field_value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -556,7 +494,7 @@ void DbConnector::get_value(const char * value_str, wchar_t & field_value, const
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// report an error?
|
field_value = 0xFFFD; // U+FFFD "replacement character";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -568,48 +506,18 @@ void DbConnector::get_value(const char * value_str, wchar_t & field_value, const
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// CHECKME need to be tested
|
|
||||||
void DbConnector::get_value(const char * value_str, std::wstring & field_value, const FT & field_type)
|
|
||||||
{
|
|
||||||
if( field_type.is_binary() )
|
|
||||||
{
|
|
||||||
unescape_bin_string(value_str, field_value, field_type);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
if( field_type.is_hexadecimal() )
|
|
||||||
{
|
|
||||||
unescape_hex_string(value_str, field_value, field_type);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if( field_type.use_utf8() )
|
|
||||||
{
|
|
||||||
pt::utf8_to_wide(value_str, field_value);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for(size_t i=0 ; value_str[i] != 0 ; ++i)
|
|
||||||
{
|
|
||||||
field_value += static_cast<wchar_t>(value_str[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// CHECKME need to be tested
|
|
||||||
void DbConnector::get_value(const char * value_str, std::string & field_value, const FT & field_type)
|
void DbConnector::get_value(const char * value_str, std::string & field_value, const FT & field_type)
|
||||||
{
|
{
|
||||||
if( field_type.is_binary() )
|
|
||||||
{
|
|
||||||
unescape_bin_string(value_str, field_value);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
if( field_type.is_hexadecimal() )
|
if( field_type.is_hexadecimal() )
|
||||||
{
|
{
|
||||||
unescape_hex_string(value_str, field_value);
|
unescape_hex_string(value_str, field_value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
if( field_type.is_binary() )
|
||||||
|
{
|
||||||
|
unescape_bin_string(value_str, field_value);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
field_value = value_str;
|
field_value = value_str;
|
||||||
}
|
}
|
||||||
|
@ -622,6 +530,34 @@ void DbConnector::get_value(const char * value_str, std::string_view & field_val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DbConnector::get_value(const char * value_str, std::wstring & field_value, const FT & field_type)
|
||||||
|
{
|
||||||
|
if( field_type.is_hexadecimal() )
|
||||||
|
{
|
||||||
|
unescape_hex_string(value_str, field_value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if( field_type.is_binary() )
|
||||||
|
{
|
||||||
|
unescape_bin_string(value_str, field_value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if( field_type.use_utf8() )
|
||||||
|
{
|
||||||
|
pt::utf8_to_wide(value_str, field_value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for(size_t i=0 ; value_str[i] != 0 ; ++i)
|
||||||
|
{
|
||||||
|
field_value += static_cast<wchar_t>((unsigned char)value_str[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void DbConnector::get_value(const char * value_str, bool & field_value, const FT & field_type)
|
void DbConnector::get_value(const char * value_str, bool & field_value, const FT & field_type)
|
||||||
{
|
{
|
||||||
// IMPROVE ME
|
// IMPROVE ME
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -149,20 +149,23 @@ protected:
|
||||||
|
|
||||||
virtual const char * query_last_sequence(const wchar_t * sequence_table_name);
|
virtual const char * query_last_sequence(const wchar_t * sequence_table_name);
|
||||||
|
|
||||||
|
virtual void unescape_hex_char(const char * str, char & c);
|
||||||
|
virtual void unescape_hex_char(const char * str, wchar_t & c);
|
||||||
|
|
||||||
|
virtual void unescape_bin_char(const char * str, char & c);
|
||||||
|
virtual void unescape_bin_char(const char * str, wchar_t & c);
|
||||||
|
|
||||||
virtual void unescape_hex_string(const char * str, std::string & out);
|
virtual void unescape_hex_string(const char * str, std::string & out);
|
||||||
virtual void unescape_hex_string(const char * str, std::wstring & out, const FT & field_type);
|
virtual void unescape_hex_string(const char * str, std::wstring & out);
|
||||||
|
|
||||||
virtual void unescape_bin_string(const char * str, std::string & out);
|
virtual void unescape_bin_string(const char * str, std::string & out);
|
||||||
virtual void unescape_bin_string(const char * str, std::wstring & out, const FT & field_type);
|
virtual void unescape_bin_string(const char * str, std::wstring & out);
|
||||||
|
|
||||||
virtual size_t unescape_hex_char(const char * value_str, char * utf8_str, size_t utf8_str_max_len);
|
|
||||||
virtual size_t unescape_hex_char(const char * value_str, wchar_t & field_value, const FT & field_type);
|
|
||||||
virtual size_t unescape_bin_char(const char * value_str, wchar_t & field_value, const FT & field_type);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
char unescape_hex_char_part(char hex);
|
unsigned int unescape_hex_char_part(char hex);
|
||||||
char unescape_hex_char(char char1, char char2);
|
const char * unescape_hex_char(const char * str, size_t len, unsigned int & res);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
4
src/ft.h
4
src/ft.h
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, Tomasz Sowa
|
* Copyright (c) 2021-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -58,7 +58,7 @@ public:
|
||||||
no_fetchable = 32, /* not supported yet */
|
no_fetchable = 32, /* not supported yet */
|
||||||
no_removable = 64,
|
no_removable = 64,
|
||||||
raw_field_name = 128,
|
raw_field_name = 128,
|
||||||
dont_use_utf8 = 256,
|
dont_use_utf8 = 256, /* used only with wchar_t and std::wstring, ignored if binary or hexadecimal flags are used */
|
||||||
hexadecimal = 512,
|
hexadecimal = 512,
|
||||||
binary = 1024,
|
binary = 1024,
|
||||||
json = 2048,
|
json = 2048,
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -135,19 +135,13 @@ void JSONExpression::after_field_value_list()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void JSONExpression::esc(char val, pt::TextStream & stream, const FT & field_type)
|
bool JSONExpression::esc_char(wchar_t val, pt::TextStream & stream)
|
||||||
{
|
{
|
||||||
if( field_type.is_hexadecimal() || field_type.is_binary() )
|
return pt::try_esc_to_json(val, stream);
|
||||||
{
|
|
||||||
char_to_hex(val, stream);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
pt::esc_to_json(val, stream);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void JSONExpression::esc(const pt::Space & space, pt::TextStream & stream, const FT & field_type)
|
void JSONExpression::esc(const pt::Space & space, pt::TextStream & stream, const FT & field_type)
|
||||||
{
|
{
|
||||||
bool pretty_print = field_type.is_pretty_print();
|
bool pretty_print = field_type.is_pretty_print();
|
||||||
|
@ -160,6 +154,7 @@ void JSONExpression::esc(const pt::Space & space, pt::TextStream & stream, const
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// when serializing as json put it directly without escaping
|
||||||
space.serialize_to_json_stream(stream, pretty_print);
|
space.serialize_to_json_stream(stream, pretty_print);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -66,7 +66,7 @@ protected:
|
||||||
// 'morm::JSONExpression::esc' hides overloaded virtual function [-Woverloaded-virtual]
|
// 'morm::JSONExpression::esc' hides overloaded virtual function [-Woverloaded-virtual]
|
||||||
using FlatExpression::esc;
|
using FlatExpression::esc;
|
||||||
|
|
||||||
void esc(char val, pt::TextStream & stream, const FT & field_type);
|
bool esc_char(wchar_t val, pt::TextStream & stream);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -559,17 +559,28 @@ void PostgreSQLConnector::log_unsupported_bin_format()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PostgreSQLConnector::unescape_bin_char(const char * str, char & field_value)
|
||||||
size_t PostgreSQLConnector::unescape_bin_char(const char * str, wchar_t & field_value, const FT & field_type)
|
|
||||||
{
|
{
|
||||||
if( str[0]!='\\' || str[1]!='x' )
|
if( str[0]!='\\' || str[1]!='x' )
|
||||||
{
|
{
|
||||||
log_unsupported_bin_format();
|
log_unsupported_bin_format();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return unescape_hex_char(str + 2, field_value, field_type);
|
DbConnector::unescape_bin_char(str + 2, field_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PostgreSQLConnector::unescape_bin_char(const char * str, wchar_t & field_value)
|
||||||
|
{
|
||||||
|
if( str[0]!='\\' || str[1]!='x' )
|
||||||
|
{
|
||||||
|
log_unsupported_bin_format();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DbConnector::unescape_bin_char(str + 2, field_value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -583,12 +594,12 @@ void PostgreSQLConnector::unescape_bin_string(const char * str, std::string & ou
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
unescape_hex_string(str + 2, out);
|
DbConnector::unescape_bin_string(str + 2, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PostgreSQLConnector::unescape_bin_string(const char * str, std::wstring & out, const FT & field_type)
|
void PostgreSQLConnector::unescape_bin_string(const char * str, std::wstring & out)
|
||||||
{
|
{
|
||||||
if( str[0]!='\\' || str[1]!='x' )
|
if( str[0]!='\\' || str[1]!='x' )
|
||||||
{
|
{
|
||||||
|
@ -596,7 +607,7 @@ void PostgreSQLConnector::unescape_bin_string(const char * str, std::wstring & o
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
unescape_hex_string(str + 2, out, field_type);
|
DbConnector::unescape_bin_string(str + 2, out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -100,9 +100,11 @@ protected:
|
||||||
|
|
||||||
void log_unsupported_bin_format();
|
void log_unsupported_bin_format();
|
||||||
|
|
||||||
size_t unescape_bin_char(const char * str, wchar_t & field_value, const FT & field_type);
|
void unescape_bin_char(const char * str, char & field_value);
|
||||||
|
void unescape_bin_char(const char * str, wchar_t & field_value);
|
||||||
|
|
||||||
void unescape_bin_string(const char * str, std::string & out);
|
void unescape_bin_string(const char * str, std::string & out);
|
||||||
void unescape_bin_string(const char * str, std::wstring & out, const FT & field_type);
|
void unescape_bin_string(const char * str, std::wstring & out);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -63,26 +63,27 @@ void PostgreSQLExpression::after_field_value_string(const FT & field_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool PostgreSQLExpression::esc_char(wchar_t val, pt::TextStream & stream)
|
||||||
void PostgreSQLExpression::esc(char val, pt::TextStream & stream, const FT & field_type)
|
|
||||||
{
|
{
|
||||||
if( field_type.is_hexadecimal() || field_type.is_binary() )
|
if( val == '\\' )
|
||||||
{
|
{
|
||||||
char_to_hex(val, stream);
|
stream << "\\\\";
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
if( val == '\'' )
|
||||||
{
|
{
|
||||||
switch( val )
|
stream << "\\\'"; // don't use "''" because we use the method for PQconnectdb too
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if( val == 0 )
|
||||||
{
|
{
|
||||||
case '\\': stream << "\\\\"; break;
|
// may put the replacement character to the stream?
|
||||||
case '\'': stream << "\\\'"; break; // don't use "''" because we use the method for PQconnectdb too
|
return true;
|
||||||
default:
|
|
||||||
if( val != 0 )
|
|
||||||
{
|
|
||||||
stream << val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2021, Tomasz Sowa
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -45,7 +45,6 @@ class PostgreSQLExpression : public DbExpression
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void esc(char val, pt::TextStream & stream, const FT & field_type);
|
|
||||||
void esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type);
|
void esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type);
|
||||||
|
|
||||||
DbExpression & page(pt::TextStream & stream, size_t page_number, size_t page_size);
|
DbExpression & page(pt::TextStream & stream, size_t page_number, size_t page_size);
|
||||||
|
@ -61,6 +60,7 @@ private:
|
||||||
void before_field_value_string(const FT & field_type);
|
void before_field_value_string(const FT & field_type);
|
||||||
void after_field_value_string(const FT & field_type);
|
void after_field_value_string(const FT & field_type);
|
||||||
|
|
||||||
|
bool esc_char(wchar_t val, pt::TextStream & stream);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue