added: to misc: UrlEncode which takes PT::TextStreamBase as an argument

added: to misc: QEncode which takes PT::TextStreamBase as an argument
added: to Locale: methods: IsKey, Get which takes PT::TextStreamBase as an argument




git-svn-id: svn://ttmath.org/publicrep/winix/trunk@860 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2012-07-10 16:50:59 +00:00
parent f76a0ca3e9
commit 9830b0a50f
16 changed files with 2413 additions and 1705 deletions

View File

@@ -18,6 +18,9 @@
#include "item.h"
#include "requesttypes.h"
#include "date/date.h"
#include "textstream/textstream.h"
#include "utf8/utf8.h"
@@ -708,8 +711,14 @@ int SelectFileType(const std::wstring & file_name);
template<class StringType>
void UrlEncode(char c, StringType & out, bool clear_out = true)
// thread safe
template<typename char_type, size_t stack_size, size_t heap_block_size>
void UrlEncode(char c,
PT::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear_out = true)
{
char buffer[10];
size_t buflen = sizeof(buffer)/sizeof(char);
@@ -722,21 +731,89 @@ size_t buflen = sizeof(buffer)/sizeof(char);
(c >= '0' && c <= '9') ||
c == '.' || c == ',' || c == '-' || c == '_' || c == '(' || c == ')' )
{
out += c;
out << c;
}
else
{
Toa(static_cast<unsigned char>(c), buffer, buflen, 16);
out += '%';
out << '%';
if( buffer[1] == 0 )
out += '0'; // there is only one character in the buffer
out << '0'; // there is only one character in the buffer
for(size_t i=0 ; buffer[i] != 0 ; ++i)
out += buffer[i];
out << buffer;
}
}
// thread safe
template<typename char_type, size_t stack_size, size_t heap_block_size>
void UrlEncode(const char * in,
PT::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear_out = true)
{
if( clear_out )
out.clear();
for(size_t i=0 ; in[i] != 0 ; ++i)
UrlEncode(in[i], out, false);
}
// thread safe
template<typename char_type, size_t stack_size, size_t heap_block_size>
void UrlEncode(const std::string & in,
PT::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear_out = true)
{
UrlEncode(in.c_str(), out, clear_out);
}
// not thread safe
template<typename char_type, size_t stack_size, size_t heap_block_size>
void UrlEncode(const wchar_t * in,
PT::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear_out = true)
{
static std::string ain;
PT::WideToUTF8(in, ain);
if( clear_out )
out.clear();
for(size_t i=0 ; i < ain.size() ; ++i)
UrlEncode(ain[i], out, false);
}
// not thread safe
template<typename char_type, size_t stack_size, size_t heap_block_size>
void UrlEncode(const std::wstring & in,
PT::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear_out = true)
{
UrlEncode(in.c_str(), out, clear_out);
}
// no thread safe
template<class StringType>
void UrlEncode(char c, StringType & out, bool clear_out = true)
{
static PT::TextStream tmp;
UrlEncode(c, tmp);
tmp.to_string(out, clear_out);
}
// !! IMROVE ME we need some UrlEncode methods with PT::TextBuffer instead of std::string
void UrlEncode(const char * in, std::string & out, bool clear_out = true);
void UrlEncode(const std::string & in, std::string & out, bool clear_out = true);
void UrlEncode(const wchar_t * in, std::string & out, bool clear_out = true);
@@ -745,8 +822,95 @@ void UrlEncode(const wchar_t * in, std::wstring & out, bool clear_out = true);
void UrlEncode(const std::wstring & in, std::wstring & out, bool clear_out = true);
template<typename char_type, size_t stack_size, size_t heap_block_size>
void QEncodeAddChar(char_type c, PT::TextStreamBase<char_type, stack_size, heap_block_size> & out)
{
if( (c>='A' && c<='Z') ||
(c>='a' && c<='z') ||
(c>='0' && c<='9') )
{
out << c;
}
else
{
char buf1[10];
char buf2[10];
size_t len1 = sizeof(buf1) / sizeof(char);
size_t len2 = sizeof(buf2) / sizeof(char);
size_t len = PT::IntToUTF8(int(c), buf1, len1);
for(size_t i=0 ; i<len ; ++i)
{
// make sure that it produces *capital* letters (ABC...)
Toa((unsigned long)(unsigned char)buf1[i], buf2, len2, 16);
out << '=';
if( buf2[1] == 0 )
out << '0';
out << buf2;
}
}
}
/*
this encoding is used in mails headers
encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
http://www.faqs.org/rfcs/rfc1522.html
we have:
charset = UTF-8
encoding = Q
current limitation:
we do not support checking the maximum length:
"An encoded-word may not be more than 75 characters long, including
charset, encoding, encoded-text, and delimiters."
*/
template<typename char_type, size_t stack_size, size_t heap_block_size>
void QEncode(const wchar_t * in, PT::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear = true)
{
if( clear )
out.clear();
out << "=?UTF-8?Q?";
for( ; *in ; ++in)
QEncodeAddChar(*in, out);
out << "?=";
}
template<typename char_type, size_t stack_size, size_t heap_block_size>
void QEncode(const std::wstring & in, PT::TextStreamBase<char_type, stack_size, heap_block_size> & out,
bool clear = true)
{
if( clear )
out.clear();
out << "=?UTF-8?Q?";
// do not use QEncode(in.c_str()) as 'in' can have a zero byte
for(size_t i=0 ; i<in.size() ; ++i)
QEncodeAddChar(in[i], out);
out << "?=";
}
void QEncode(const std::wstring & in, std::string & out, bool clear = true);
void RemovePostFileTmp(PostFileTab & post_file_tab);