added support for hex strings and binary strings
added FT::hexadecimal, FT::binary and FT::dont_use_utf8
This commit is contained in:
@@ -291,52 +291,310 @@ void DbConnector::allocate_default_expression_if_needed()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, char & field_value)
|
||||
char DbConnector::unescape_hex_char_part(char hex)
|
||||
{
|
||||
field_value = *value_str;
|
||||
value_str += 1;
|
||||
|
||||
if( *value_str != 0 )
|
||||
if( hex>='0' && hex<='9' )
|
||||
{
|
||||
// value has more than one charater, put some error?
|
||||
return hex - '0';
|
||||
}
|
||||
else
|
||||
if( hex>='a' && hex<='f' )
|
||||
{
|
||||
return hex - 'a' + 10;
|
||||
}
|
||||
else
|
||||
if( hex>='A' && hex<='F' )
|
||||
{
|
||||
return hex - 'A' + 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( log )
|
||||
{
|
||||
(*log) << PT::Log::log2 << "Morm: incorrect character when reading a hex string, char code: " << (int)(unsigned char)hex;
|
||||
|
||||
if( hex >= 32 )
|
||||
{
|
||||
(*log) << " '" << hex << "'";
|
||||
}
|
||||
|
||||
(*log) << PT::Log::logend;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::unescape_hex_string(const char * str, std::string & out)
|
||||
{
|
||||
for(size_t i=0 ; str[i] != 0 ; i+=2 )
|
||||
{
|
||||
out += unescape_hex_char(str[i], str[i+1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::unescape_hex_string(const char * str, std::wstring & out, FT field_type)
|
||||
{
|
||||
if( field_type.use_utf8() )
|
||||
{
|
||||
size_t len;
|
||||
wchar_t c;
|
||||
|
||||
while( *str != 0 && (len = unescape_hex_char(str, c, field_type)) > 0 )
|
||||
{
|
||||
out += c;
|
||||
str += len;
|
||||
}
|
||||
}
|
||||
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])));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::unescape_bin_string(const char * str, std::string & out)
|
||||
{
|
||||
unescape_hex_string(str, out);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::unescape_bin_string(const char * str, std::wstring & out, FT field_type)
|
||||
{
|
||||
unescape_hex_string(str, out, field_type);
|
||||
}
|
||||
|
||||
|
||||
// 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, 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::UTF8ToInt(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, 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, FT field_type)
|
||||
{
|
||||
wchar_t c;
|
||||
|
||||
field_value = 0;
|
||||
get_value(value_str, c, field_type);
|
||||
|
||||
if( field_type.use_utf8() )
|
||||
{
|
||||
if( c <= 127 )
|
||||
{
|
||||
field_value = static_cast<char>(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
if( log )
|
||||
{
|
||||
(*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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, unsigned char & field_value)
|
||||
// CHECKME need to be tested
|
||||
void DbConnector::get_value(const char * value_str, unsigned char & field_value, FT field_type)
|
||||
{
|
||||
field_value = *(const unsigned char*)value_str;
|
||||
value_str += 1;
|
||||
char tmp_char;
|
||||
get_value(value_str, tmp_char, field_type);
|
||||
|
||||
if( *value_str != 0 )
|
||||
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, FT field_type)
|
||||
{
|
||||
field_value = 0;
|
||||
|
||||
if( field_type.is_binary() )
|
||||
{
|
||||
// value has more than one charater, put some error?
|
||||
unescape_bin_char(value_str, field_value, field_type);
|
||||
}
|
||||
else
|
||||
if( field_type.is_hexadecimal() )
|
||||
{
|
||||
unescape_hex_char(value_str, field_value, field_type);
|
||||
}
|
||||
else
|
||||
{
|
||||
if( field_type.use_utf8() )
|
||||
{
|
||||
int value_int;
|
||||
bool is_correct;
|
||||
|
||||
PT::UTF8ToInt(value_str, value_int, is_correct);
|
||||
|
||||
if( is_correct )
|
||||
{
|
||||
field_value = static_cast<wchar_t>(value_int);
|
||||
}
|
||||
else
|
||||
{
|
||||
// report an error?
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
field_value = static_cast<wchar_t>((unsigned char)*value_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, std::wstring & field_value)
|
||||
|
||||
// CHECKME need to be tested
|
||||
void DbConnector::get_value(const char * value_str, std::wstring & field_value, FT field_type)
|
||||
{
|
||||
// CHECKME
|
||||
// what about \0 in val_str?
|
||||
// it is escaped somehow?
|
||||
PT::UTF8ToWide(value_str, field_value);
|
||||
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::UTF8ToWide(value_str, field_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(size_t i=0 ; value_str[i] != 0 ; ++i)
|
||||
{
|
||||
field_value += static_cast<wchar_t>(value_str[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, std::string & field_value)
|
||||
// CHECKME need to be tested
|
||||
void DbConnector::get_value(const char * value_str, std::string & field_value, FT field_type)
|
||||
{
|
||||
field_value = value_str;
|
||||
if( field_type.is_binary() )
|
||||
{
|
||||
unescape_bin_string(value_str, field_value);
|
||||
}
|
||||
else
|
||||
if( field_type.is_hexadecimal() )
|
||||
{
|
||||
unescape_hex_string(value_str, field_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
field_value = value_str;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, bool & field_value)
|
||||
void DbConnector::get_value(const char * value_str, bool & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME
|
||||
// this 't' is locale dependent
|
||||
@@ -344,91 +602,91 @@ void DbConnector::get_value(const char * value_str, bool & field_value)
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, short & field_value)
|
||||
void DbConnector::get_value(const char * value_str, short & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = (short)PT::Toi(value_str, 10);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, unsigned short & field_value)
|
||||
void DbConnector::get_value(const char * value_str, unsigned short & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = (unsigned short)PT::Toui(value_str, 10);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, int & field_value)
|
||||
void DbConnector::get_value(const char * value_str, int & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = PT::Toi(value_str, 10);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, unsigned int & field_value)
|
||||
void DbConnector::get_value(const char * value_str, unsigned int & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = PT::Toui(value_str, 10);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, long & field_value)
|
||||
void DbConnector::get_value(const char * value_str, long & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = PT::Tol(value_str, 10);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, unsigned long & field_value)
|
||||
void DbConnector::get_value(const char * value_str, unsigned long & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = PT::Toul(value_str, 10);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, long long & field_value)
|
||||
void DbConnector::get_value(const char * value_str, long long & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = PT::Toll(value_str, 10);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, unsigned long long & field_value)
|
||||
void DbConnector::get_value(const char * value_str, unsigned long long & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = PT::Toull(value_str, 10);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, float & field_value)
|
||||
void DbConnector::get_value(const char * value_str, float & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = strtof(value_str, 0);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, double & field_value)
|
||||
void DbConnector::get_value(const char * value_str, double & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = strtod(value_str, 0);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, long double & field_value)
|
||||
void DbConnector::get_value(const char * value_str, long double & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some overflow checking
|
||||
field_value = strtold(value_str, 0);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, PT::Date & field_value)
|
||||
void DbConnector::get_value(const char * value_str, PT::Date & field_value, FT field_type)
|
||||
{
|
||||
// IMPROVE ME give some log if parsing failed
|
||||
field_value.Parse(value_str);
|
||||
}
|
||||
|
||||
|
||||
void DbConnector::get_value(const char * value_str, PT::Space & field_value)
|
||||
void DbConnector::get_value(const char * value_str, PT::Space & field_value, FT field_type)
|
||||
{
|
||||
field_value.clear();
|
||||
|
||||
@@ -443,7 +701,7 @@ void DbConnector::get_value(const char * value_str, PT::Space & field_value)
|
||||
|
||||
if( log )
|
||||
{
|
||||
(*log) << PT::Log::log1 << "Morm: I cannot correctly parse the Space struct from the datebase"
|
||||
(*log) << PT::Log::log2 << "Morm: I cannot correctly parse the Space struct from the datebase"
|
||||
<< ", the raw string is: " << value_str << PT::Log::logend;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user