add methods: try_esc_to_json(wchar_t val, stream) try_esc_to_xml(...) try_esc_to_csv(...)
Those methods return true if the val character was escaped and put to the out stream. If the character is invalid for such a stream they only return true without putting it to the stream.master
parent
3b9b464bb7
commit
ac3c59323b
|
@ -73,48 +73,70 @@ void esc_to_json_uformat(wchar_t val, Stream & out)
|
|||
}
|
||||
|
||||
|
||||
void esc_to_json(wchar_t val, Stream & out)
|
||||
/*
|
||||
* return true if the val character was escaped and put to the out stream
|
||||
* if the character is invalid for such a stream then only return true
|
||||
* but not put it to the stream
|
||||
*/
|
||||
bool try_esc_to_json(wchar_t val, Stream & out)
|
||||
{
|
||||
bool status = false;
|
||||
|
||||
if( val == '\r' )
|
||||
{
|
||||
out << '\\' << 'r';
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == '\n' )
|
||||
{
|
||||
out << '\\' << 'n';
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == '\t' )
|
||||
{
|
||||
out << '\\' << 't';
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == 0x08 )
|
||||
{
|
||||
out << '\\' << 'b';
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == 0x0c )
|
||||
{
|
||||
out << '\\' << 'f';
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == '\\' )
|
||||
{
|
||||
out << '\\' << '\\';
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == '"' )
|
||||
{
|
||||
out << '\\' << '\"';
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val < 32 )
|
||||
{
|
||||
esc_to_json_uformat(val, out);
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
void esc_to_json(wchar_t val, Stream & out)
|
||||
{
|
||||
if( !try_esc_to_json(val, out) )
|
||||
{
|
||||
out << val;
|
||||
}
|
||||
|
@ -123,7 +145,10 @@ void esc_to_json(wchar_t val, Stream & out)
|
|||
|
||||
void esc_to_json(char val, Stream & out)
|
||||
{
|
||||
esc_to_json((wchar_t)(unsigned char)val, out);
|
||||
if( !try_esc_to_json((wchar_t)(unsigned char)val, out) )
|
||||
{
|
||||
out << val;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -177,44 +202,66 @@ void esc_to_json(const std::wstring & in, Stream & out)
|
|||
|
||||
|
||||
|
||||
/*
|
||||
* return true if the val character was escaped and put to the out stream
|
||||
* if the character is invalid for such a stream then only return true
|
||||
* but not put it to the stream
|
||||
*/
|
||||
bool try_esc_to_xml(wchar_t val, Stream & out)
|
||||
{
|
||||
bool status = false;
|
||||
|
||||
if( val == 0 )
|
||||
{
|
||||
// null character is invalid in XML 1.0 and 1.1
|
||||
// https://en.wikipedia.org/wiki/Valid_characters_in_XML
|
||||
// return true but not put the char to the out stream
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == '<')
|
||||
{
|
||||
out << "<";
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == '>')
|
||||
{
|
||||
out << ">";
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == '&')
|
||||
{
|
||||
out << "&";
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == '"')
|
||||
{
|
||||
out << """;
|
||||
status = true;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
void esc_to_xml(wchar_t val, Stream & out)
|
||||
{
|
||||
switch(val)
|
||||
if( !try_esc_to_xml(val, out) )
|
||||
{
|
||||
case 0:
|
||||
// null character is invalid in XML 1.0 and 1.1
|
||||
// https://en.wikipedia.org/wiki/Valid_characters_in_XML
|
||||
break;
|
||||
|
||||
case '<':
|
||||
out << "<";
|
||||
break;
|
||||
|
||||
case '>':
|
||||
out << ">";
|
||||
break;
|
||||
|
||||
case '&':
|
||||
out << "&";
|
||||
break;
|
||||
|
||||
case '"':
|
||||
out << """;
|
||||
break;
|
||||
|
||||
default:
|
||||
out << val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void esc_to_xml(char val, Stream & out)
|
||||
{
|
||||
esc_to_xml((wchar_t)(unsigned char)val, out);
|
||||
if( !try_esc_to_xml((wchar_t)(unsigned char)val, out) )
|
||||
{
|
||||
out << val;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -269,28 +316,47 @@ void esc_to_xml(const std::wstring & in, Stream & out)
|
|||
|
||||
|
||||
|
||||
void esc_to_csv(wchar_t c, pt::Stream & out)
|
||||
/*
|
||||
* return true if the val character was escaped and put to the out stream
|
||||
* if the character is invalid for such a stream then only return true
|
||||
* but not put it to the stream
|
||||
*/
|
||||
bool try_esc_to_csv(wchar_t val, pt::Stream & out)
|
||||
{
|
||||
switch(c)
|
||||
bool status = false;
|
||||
|
||||
if( val == 0 )
|
||||
{
|
||||
case 0:
|
||||
// null characters are invalid in text files
|
||||
break;
|
||||
|
||||
case '"':
|
||||
// return true but not put to the out stream
|
||||
status = true;
|
||||
}
|
||||
else
|
||||
if( val == '"' )
|
||||
{
|
||||
out << "\"\"";
|
||||
break;
|
||||
status = true;
|
||||
}
|
||||
|
||||
default:
|
||||
out << c;
|
||||
break;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
void esc_to_csv(wchar_t val, pt::Stream & out)
|
||||
{
|
||||
if( !try_esc_to_csv(val, out) )
|
||||
{
|
||||
out << val;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void esc_to_csv(char val, Stream & out)
|
||||
{
|
||||
esc_to_csv((wchar_t)(unsigned char)val, out);
|
||||
if( !try_esc_to_csv((wchar_t)(unsigned char)val, out) )
|
||||
{
|
||||
out << val;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -50,6 +50,7 @@ namespace pt
|
|||
|
||||
void SetOverflow(bool * was_overflow, bool val);
|
||||
|
||||
bool try_esc_to_json(wchar_t val, Stream & out);
|
||||
void esc_to_json(wchar_t val, Stream & out);
|
||||
void esc_to_json(char val, Stream & out);
|
||||
void esc_to_json(const char * c, pt::Stream & out);
|
||||
|
@ -59,6 +60,7 @@ void esc_to_json(const wchar_t * c, size_t len, pt::Stream & out);
|
|||
void esc_to_json(const std::string & in, Stream & out);
|
||||
void esc_to_json(const std::wstring & in, Stream & out);
|
||||
|
||||
bool try_esc_to_xml(wchar_t val, Stream & out);
|
||||
void esc_to_xml(wchar_t c, pt::Stream & out);
|
||||
void esc_to_xml(char c, pt::Stream & out);
|
||||
void esc_to_xml(const char * c, pt::Stream & out);
|
||||
|
@ -68,6 +70,7 @@ void esc_to_xml(const wchar_t * c, size_t len, pt::Stream & out);
|
|||
void esc_to_xml(const std::string & in, Stream & out);
|
||||
void esc_to_xml(const std::wstring & in, Stream & out);
|
||||
|
||||
bool try_esc_to_csv(wchar_t val, pt::Stream & out);
|
||||
void esc_to_csv(wchar_t val, Stream & out);
|
||||
void esc_to_csv(char c, pt::Stream & out);
|
||||
void esc_to_csv(const char * c, std::size_t len, Stream & out);
|
||||
|
|
Loading…
Reference in New Issue