2010-09-18 02:51:12 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-02-12 17:30:49 +01:00
|
|
|
* Copyright (c) 2010-2014, Tomasz Sowa
|
2010-09-18 02:51:12 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2010-09-18 02:51:12 +02:00
|
|
|
*/
|
|
|
|
|
2010-09-30 00:41:34 +02:00
|
|
|
#ifndef headerfile_winix_db_dbtextstream
|
|
|
|
#define headerfile_winix_db_dbtextstream
|
2010-09-18 02:51:12 +02:00
|
|
|
|
2010-10-09 22:27:45 +02:00
|
|
|
#include <ctime>
|
2010-09-18 02:51:12 +02:00
|
|
|
#include "core/textstream.h"
|
2012-10-27 09:44:26 +02:00
|
|
|
#include "textstream/textstream.h"
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
DbTextStream is used as a buffer for creating a database's query
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
By default all operators<< escape its string artuments. If you don't want
|
2010-09-18 02:51:12 +02:00
|
|
|
to escape an argument you should use a helper function R() (raw argument)
|
|
|
|
note: you have to define the function yourself, we do not provide it
|
|
|
|
because such a short name would make a mess in namespaces
|
|
|
|
|
|
|
|
sample:
|
|
|
|
create a helper function R as follows:
|
|
|
|
|
|
|
|
template<class RawType>
|
|
|
|
DbTextStream::RawText<RawType> R(const RawType & par)
|
|
|
|
{
|
|
|
|
return DbTextStream::RawText<RawType>(par);
|
|
|
|
}
|
|
|
|
|
|
|
|
now you can use DbTextStream in an easy way:
|
|
|
|
|
|
|
|
DbTextStream query;
|
|
|
|
std::string key = "some string";
|
|
|
|
query << R("select * from table where key=") << key << R(";");
|
|
|
|
|
|
|
|
in above example only the key is escaped.
|
|
|
|
|
|
|
|
Also with escaping operators<< insert commas between parameters, e.g.:
|
|
|
|
|
|
|
|
query << R("insert into table (key1, key2, key3) values (")
|
|
|
|
<< key1
|
|
|
|
<< key2
|
|
|
|
<< key3
|
|
|
|
<< R(");");
|
|
|
|
|
|
|
|
between key1 key2 and key3 are commas inserted automatically
|
|
|
|
*/
|
2010-11-21 01:19:17 +01:00
|
|
|
class DbTextStream : public TextStream<std::string>
|
2010-09-18 02:51:12 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
a helper struct to select a proper operator<<
|
|
|
|
(for non-escaping versions of these operators)
|
|
|
|
*/
|
|
|
|
template<class RawType>
|
|
|
|
struct RawText
|
|
|
|
{
|
|
|
|
const RawType & par;
|
|
|
|
|
|
|
|
RawText(const RawText<RawType> & p) : par(p.par) {}
|
|
|
|
RawText(const RawType & p) : par(p) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
DbTextStream();
|
|
|
|
|
|
|
|
// extented escaping: adding E character before the first quote e.g. E'string'
|
|
|
|
// default: true
|
|
|
|
void SetExtented(bool ext);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
without escaping
|
|
|
|
*/
|
|
|
|
|
|
|
|
DbTextStream & PutText(const char *);
|
|
|
|
DbTextStream & PutText(const std::string *);
|
|
|
|
DbTextStream & PutText(const std::string &);
|
2010-11-21 01:19:17 +01:00
|
|
|
DbTextStream & PutText(const wchar_t * str);
|
|
|
|
DbTextStream & PutText(const std::wstring * str);
|
|
|
|
DbTextStream & PutText(const std::wstring & str);
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
we need this template operator for such calling:
|
|
|
|
dbtextstream_object << R("some string");
|
|
|
|
"some string" is actually a table (not a pointer)
|
|
|
|
*/
|
|
|
|
template<size_t str_size>
|
2012-05-19 17:14:35 +02:00
|
|
|
DbTextStream & operator<<(RawText<char [str_size]> raw) { return PutText(raw.par); }
|
2010-09-18 02:51:12 +02:00
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
template<size_t str_size>
|
2012-05-19 17:14:35 +02:00
|
|
|
DbTextStream & operator<<(RawText<wchar_t [str_size]> raw) { return PutText(raw.par); }
|
2010-11-21 01:19:17 +01:00
|
|
|
|
2012-05-19 17:14:35 +02:00
|
|
|
DbTextStream & operator<<(RawText<const char*> raw);
|
|
|
|
DbTextStream & operator<<(RawText<const wchar_t*> raw);
|
added: uptime winix function prints how many sessions there are
changed: functions for text/numbers conversions
int Toi(const std::string & str, int base = 10);
int Toi(const std::wstring & str, int base = 10);
int Toi(const char * str, int base = 10);
int Toi(const wchar_t * str, int base = 10);
long Tol(const std::string & str, int base = 10);
long Tol(const std::wstring & str, int base = 10);
long Tol(const char * str, int base = 10);
long Tol(const wchar_t * str, int base = 10);
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10);
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10);
const wchar_t * Toa(unsigned int value, int base = 10);
const wchar_t * Toa(unsigned long value, int base = 10);
const wchar_t * Toa(int value, int base = 10);
const wchar_t * Toa(long value, int base = 10);
void Toa(int value, std::string & res, int base = 10, bool clear = true);
void Toa(long value, std::string & res, int base = 10, bool clear = true);
void Toa(int value, std::wstring & res, int base = 10, bool clear = true);
void Toa(long value, std::wstring & res, int base = 10, bool clear = true);
added: HtmlTextStream class (files htmltextstream.cpp htmltextstream.h in templates)
this is a special stream for automatically escaping html tags
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@682 e52654a7-88a9-db11-a3e9-0013d4bc506e
2010-11-25 02:34:46 +01:00
|
|
|
DbTextStream & operator<<(RawText<const std::string*> raw);
|
|
|
|
DbTextStream & operator<<(RawText<const std::wstring*> raw);
|
2010-09-18 02:51:12 +02:00
|
|
|
DbTextStream & operator<<(RawText<std::string> raw);
|
2010-11-21 01:19:17 +01:00
|
|
|
DbTextStream & operator<<(RawText<std::wstring> raw);
|
changed: when winix demonizes it creates a three new descriptors (0, 1 and 3)
pointing to /dev/null
added: DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
it reads binary (bytea) data
added: DbTextStream can handle 'bool' types now
(is puts 'true' of 'false' to the stream)
changed: now passwords can be stored either as plain text, a hash or can be encrypted
with RSA
currently we have following hashes:
md4, md5, sha1, sha224, sha256, sha384, sha512
we are using openssl to manage them
(look at config options for more info)
changed: winix version to 0.4.7
added: class Run - you can run any program from os and send a buffer to its standard input
and read what the program put on its standard output
added: class Crypt (in System) - calculating hashes, and crypting/decrypting
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@734 e52654a7-88a9-db11-a3e9-0013d4bc506e
2011-06-09 23:22:08 +02:00
|
|
|
DbTextStream & operator<<(RawText<bool> raw);
|
2010-09-18 02:51:12 +02:00
|
|
|
DbTextStream & operator<<(RawText<char> raw);
|
2010-11-21 01:19:17 +01:00
|
|
|
DbTextStream & operator<<(RawText<wchar_t> raw);
|
2010-09-18 02:51:12 +02:00
|
|
|
DbTextStream & operator<<(RawText<int> raw);
|
|
|
|
DbTextStream & operator<<(RawText<long> raw);
|
|
|
|
DbTextStream & operator<<(RawText<unsigned int> raw);
|
|
|
|
DbTextStream & operator<<(RawText<unsigned long> raw);
|
|
|
|
DbTextStream & operator<<(RawText<double> raw);
|
|
|
|
DbTextStream & operator<<(RawText<void*> raw);
|
2021-05-20 20:59:12 +02:00
|
|
|
DbTextStream & operator<<(RawText<pt::Date> date);
|
2010-09-18 02:51:12 +02:00
|
|
|
|
2012-10-27 09:44:26 +02:00
|
|
|
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
DbTextStream & operator<<(RawText<pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> > raw);
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
with escaping
|
|
|
|
*/
|
|
|
|
|
|
|
|
DbTextStream & EBinPutChar(char c);
|
|
|
|
DbTextStream & ETextPutChar(char c);
|
2010-11-21 01:19:17 +01:00
|
|
|
DbTextStream & ETextPutChar(wchar_t c);
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
DbTextStream & EPutText(const char * str);
|
|
|
|
DbTextStream & EPutText(const std::string * str);
|
|
|
|
DbTextStream & EPutText(const std::string & str);
|
2010-11-21 01:19:17 +01:00
|
|
|
DbTextStream & EPutText(const wchar_t * str);
|
|
|
|
DbTextStream & EPutText(const std::wstring * str);
|
|
|
|
DbTextStream & EPutText(const std::wstring & str);
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
DbTextStream & EPutBin(const char * str, size_t len);
|
|
|
|
DbTextStream & EPutBin(const std::string * str);
|
|
|
|
DbTextStream & EPutBin(const std::string & str);
|
|
|
|
|
|
|
|
DbTextStream & operator<<(const char * str);
|
|
|
|
DbTextStream & operator<<(const std::string * str);
|
|
|
|
DbTextStream & operator<<(const std::string & str);
|
2010-11-21 01:19:17 +01:00
|
|
|
DbTextStream & operator<<(const wchar_t * str);
|
|
|
|
DbTextStream & operator<<(const std::wstring * str);
|
|
|
|
DbTextStream & operator<<(const std::wstring & str);
|
changed: when winix demonizes it creates a three new descriptors (0, 1 and 3)
pointing to /dev/null
added: DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
it reads binary (bytea) data
added: DbTextStream can handle 'bool' types now
(is puts 'true' of 'false' to the stream)
changed: now passwords can be stored either as plain text, a hash or can be encrypted
with RSA
currently we have following hashes:
md4, md5, sha1, sha224, sha256, sha384, sha512
we are using openssl to manage them
(look at config options for more info)
changed: winix version to 0.4.7
added: class Run - you can run any program from os and send a buffer to its standard input
and read what the program put on its standard output
added: class Crypt (in System) - calculating hashes, and crypting/decrypting
git-svn-id: svn://ttmath.org/publicrep/winix/trunk@734 e52654a7-88a9-db11-a3e9-0013d4bc506e
2011-06-09 23:22:08 +02:00
|
|
|
DbTextStream & operator<<(bool);
|
2010-09-18 02:51:12 +02:00
|
|
|
DbTextStream & operator<<(char);
|
2010-11-21 01:19:17 +01:00
|
|
|
DbTextStream & operator<<(wchar_t);
|
2010-09-18 02:51:12 +02:00
|
|
|
DbTextStream & operator<<(int);
|
|
|
|
DbTextStream & operator<<(long);
|
|
|
|
DbTextStream & operator<<(unsigned int);
|
|
|
|
DbTextStream & operator<<(unsigned long);
|
|
|
|
DbTextStream & operator<<(double);
|
|
|
|
DbTextStream & operator<<(const void *);
|
2011-12-08 23:01:19 +01:00
|
|
|
DbTextStream & operator<<(const std::vector<long> & tabid);
|
2021-05-20 20:59:12 +02:00
|
|
|
DbTextStream & operator<<(const pt::Space & space);
|
|
|
|
DbTextStream & operator<<(const pt::Date & date);
|
2010-10-09 22:27:45 +02:00
|
|
|
|
2012-10-27 09:44:26 +02:00
|
|
|
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
DbTextStream & operator<<(const pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg);
|
2010-09-18 02:51:12 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool was_param;
|
|
|
|
bool ext_escape;
|
2012-05-27 01:04:49 +02:00
|
|
|
TextStream<std::wstring> tmp_stream;
|
2012-01-08 04:59:51 +01:00
|
|
|
|
2012-09-11 07:19:45 +02:00
|
|
|
char EBinGetHex(char c);
|
|
|
|
|
2010-09-18 02:51:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-27 09:44:26 +02:00
|
|
|
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
DbTextStream & DbTextStream::operator<<(RawText<pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> > raw)
|
2012-10-27 09:44:26 +02:00
|
|
|
{
|
|
|
|
TextStream<std::string>::operator<<(raw.par);
|
2010-09-18 02:51:12 +02:00
|
|
|
|
2012-10-27 09:44:26 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename arg_char_type, size_t arg_stack_size, size_t arg_heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
DbTextStream & DbTextStream::operator<<(const pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size> & arg)
|
2012-10-27 09:44:26 +02:00
|
|
|
{
|
2021-05-20 20:59:12 +02:00
|
|
|
typename pt::TextStreamBase<arg_char_type, arg_stack_size, arg_heap_block_size>::const_iterator i;
|
2012-10-27 09:44:26 +02:00
|
|
|
|
|
|
|
if( was_param )
|
|
|
|
buffer += ", ";
|
|
|
|
|
|
|
|
if( ext_escape )
|
|
|
|
buffer += 'E';
|
2010-09-18 02:51:12 +02:00
|
|
|
|
2012-10-27 09:44:26 +02:00
|
|
|
buffer += '\'';
|
|
|
|
|
|
|
|
for(i=arg.begin() ; i != arg.end() ; ++i)
|
|
|
|
ETextPutChar(*i);
|
|
|
|
|
|
|
|
buffer += '\'';
|
|
|
|
was_param = true;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
} // namespace Winix
|
|
|
|
|
|
|
|
|
2012-10-27 09:44:26 +02:00
|
|
|
|
|
|
|
#endif
|