added support for UTF-8

now the UTF-8 is a default charset


git-svn-id: svn://ttmath.org/publicrep/winix/trunk@677 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2010-11-21 00:19:17 +00:00
parent f1f0fa34cb
commit 8e72a820dd
153 changed files with 4270 additions and 2784 deletions

View File

@@ -1,16 +1,26 @@
# DO NOT DELETE
db.o: db.h dbbase.h dbconn.h dbtextstream.h ../core/textstream.h
db.o: ../core/error.h ../core/log.h dbitemquery.h ../core/item.h
db.o: dbitemcolumns.h ../core/user.h ../core/group.h ../core/thread.h
db.o: ../core/dircontainer.h ../core/item.h ../core/ugcontainer.h
db.o: ../core/log.h ../core/misc.h
db.o: ../core/misc.h ../core/item.h ../core/error.h ../core/log.h
db.o: ../core/textstream.h dbitemquery.h ../core/item.h dbitemcolumns.h
db.o: ../core/user.h ../core/group.h ../core/thread.h ../core/dircontainer.h
db.o: ../core/ugcontainer.h ../core/log.h ../core/misc.h
dbbase.o: dbbase.h dbconn.h dbtextstream.h ../core/textstream.h
dbbase.o: ../core/error.h ../core/log.h ../core/log.h
dbconn.o: dbconn.h dbtextstream.h ../core/textstream.h ../core/log.h
dbconn.o: ../core/error.h ../core/log.h
dbbase.o: ../core/misc.h ../core/item.h ../core/error.h ../core/log.h
dbbase.o: ../core/textstream.h ../core/log.h ../../ezc/src/ezc.h
dbbase.o: ../../ezc/src/utf8.h ../../ezc/src/generator.h
dbbase.o: ../../ezc/src/pattern.h ../../ezc/src/stringconv.h
dbbase.o: ../../ezc/src/functions.h ../../ezc/src/funinfo.h
dbconn.o: dbconn.h dbtextstream.h ../core/textstream.h ../core/misc.h
dbconn.o: ../core/item.h ../core/log.h ../core/error.h ../core/log.h
dbconn.o: ../core/textstream.h
dbitemcolumns.o: dbitemcolumns.h ../core/item.h dbbase.h dbconn.h
dbitemcolumns.o: dbtextstream.h ../core/textstream.h ../core/error.h
dbitemcolumns.o: ../core/log.h
dbitemcolumns.o: dbtextstream.h ../core/textstream.h ../core/misc.h
dbitemcolumns.o: ../core/item.h ../core/error.h ../core/log.h
dbitemcolumns.o: ../core/textstream.h
dbitemquery.o: dbitemquery.h ../core/item.h
dbtextstream.o: dbtextstream.h ../core/textstream.h
dbtextstream.o: dbtextstream.h ../core/textstream.h ../core/misc.h
dbtextstream.o: ../core/item.h ../../ezc/src/ezc.h ../../ezc/src/utf8.h
dbtextstream.o: ../../ezc/src/generator.h ../../ezc/src/pattern.h
dbtextstream.o: ../../ezc/src/stringconv.h ../../ezc/src/functions.h
dbtextstream.o: ../../ezc/src/funinfo.h

View File

@@ -15,7 +15,7 @@
bool Db::CheckUser(const std::string & login, const std::string & password, long & user_id)
bool Db::CheckUser(const std::wstring & login, const std::wstring & password, long & user_id)
{
PGresult * r = 0;
bool user_ok = false;
@@ -60,7 +60,7 @@ return user_ok;
Error Db::AddUser(User & user, const std::string & password)
Error Db::AddUser(User & user, const std::wstring & password)
{
PGresult * r = 0;
Error status = WINIX_ERR_OK;
@@ -107,7 +107,8 @@ bool is_that_url;
PGresult * r = 0;
int index = 1;
const int max_index = 99;
char appendix[20];
wchar_t appendix[20];
size_t appendix_len = sizeof(appendix) / sizeof(wchar_t);
appendix[0] = 0;
try
@@ -131,7 +132,7 @@ appendix[0] = 0;
if( Rows(r) != 0 )
{
sprintf(appendix, "_(%d)", ++index);
swprintf(appendix, appendix_len, L"_(%d)", ++index);
is_that_url = true;
}
else
@@ -213,8 +214,8 @@ void Db::CheckAllUrlSubject()
for(int i = 0 ; i<rows ; ++i)
{
item.id = atol( AssertValue(r, i, cid) );
item.subject = AssertValue(r, i, csubject);
item.id = AssertValueLong(r, i, cid);
item.subject = AssertValueWide(r, i, csubject);
CheckAllUrlSubjectModifyItem(item);
}
@@ -573,7 +574,7 @@ return result;
}
Error Db::EditTemplateItemById(long id, const std::string & new_html_template)
Error Db::EditTemplateItemById(long id, const std::wstring & new_html_template)
{
PGresult * r = 0;
Error result = WINIX_ERR_OK;
@@ -810,7 +811,7 @@ void Db::GetItem(std::vector<Item> & item_tab, long id)
// !! nowy interfejs
Error Db::GetItem(long parent_id, const std::string & url, Item & item)
Error Db::GetItem(long parent_id, const std::wstring & url, Item & item)
{
PGresult * r = 0;
Error result = WINIX_ERR_OK;
@@ -883,7 +884,7 @@ return result;
long Db::GetItemId(long parent_id, const std::string & url, Item::Type type)
long Db::GetItemId(long parent_id, const std::wstring & url, Item::Type type)
{
PGresult * r = 0;
long result = -1;
@@ -916,13 +917,13 @@ return result;
long Db::GetFileId(long parent_id, const std::string & url)
long Db::GetFileId(long parent_id, const std::wstring & url)
{
return GetItemId(parent_id, url, Item::file);
}
long Db::GetDirId(long parent_id, const std::string & url)
long Db::GetDirId(long parent_id, const std::wstring & url)
{
return GetItemId(parent_id, url, Item::dir);
}
@@ -1321,9 +1322,9 @@ void Db::GetUsers(UGContainer<User> & user_tab)
if( u.id != last_id )
{
u.name = AssertValue(r, i, cname);
u.super_user = static_cast<bool>( atoi( AssertValue(r, i, csuper_user) ) );
u.email = AssertValue(r, i, cemail);
u.name = AssertValueWide(r, i, cname);
u.super_user = static_cast<bool>(AssertValueInt(r, i, csuper_user));
u.email = AssertValueWide(r, i, cemail);
u.cms_notify = AssertValueInt(r, i, ccms_notify);
u.thread_notify = AssertValueInt(r, i, cthread_notify);
@@ -1384,7 +1385,7 @@ void Db::GetGroups(UGContainer<Group> & group_tab)
if( g.id != last_id )
{
g.name = AssertValue(r, i, cname);
g.name = AssertValueWide(r, i, cname);
log << log3 << "Db: get group, id:" << g.id << ", group:" << g.name << logend;
iter = group_tab.PushBack( g );
@@ -1510,7 +1511,7 @@ Error Db::GetThreads(long parent_id, std::vector<Thread> & thread_tab)
thread.last_item.id = AssertValueLong(r, i, clast_item);
thread.last_item.date_modification = AssertValueTm(r, i, cdate_modification);
thread.last_item.user_id = AssertValueLong(r, i, cuser_id);
thread.last_item.guest_name = AssertValue(r, i, cguest_name);
thread.last_item.guest_name = AssertValueWide(r, i, cguest_name);
thread_tab.push_back(thread);
}

16
db/db.h
View File

@@ -42,8 +42,8 @@ public:
bool CheckUser(const std::string & login, const std::string & password, long & user_id);
Error AddUser(User & user, const std::string & password);
bool CheckUser(const std::wstring & login, const std::wstring & password, long & user_id);
Error AddUser(User & user, const std::wstring & password);
Error AddItem(Item & item);
Error EditItemById(Item & item, bool with_url = true);
@@ -78,13 +78,13 @@ public:
long Size(long parent_id, Item::Type type = Item::none);
Error GetItemById(long item_id, Item & item);
Error GetItem(long parent_id, const std::string & url, Item & item);
Error GetItem(long parent_id, const std::wstring & url, Item & item);
Error EditDefaultItem(long id, long new_default_item);
Error EditTemplateItemById(long id, const std::string & new_html_template);
Error EditTemplateItemById(long id, const std::wstring & new_html_template);
long GetItemId(long parent_id, const std::string & url, Item::Type type);
long GetFileId(long parent_id, const std::string & url);
long GetDirId(long parent_id, const std::string & url);
long GetItemId(long parent_id, const std::wstring & url, Item::Type type);
long GetFileId(long parent_id, const std::wstring & url);
long GetDirId(long parent_id, const std::wstring & url);
@@ -100,7 +100,7 @@ public:
protected:
DbTextStream query, query_create_url;
std::string temp_url;
std::wstring temp_url;
bool AddItemCreateUrlSubject(Item & item);

View File

@@ -7,13 +7,13 @@
*
*/
#include "dbbase.h"
#include "core/log.h"
#include "core/error.h"
#include <stdlib.h>
#include <limits.h>
#include <limits>
#include "dbbase.h"
#include "core/log.h"
#include "core/error.h"
#include "ezc.h"
DbBase::DbBase()
@@ -86,7 +86,7 @@ return r;
}
PGresult * DbBase::AssertQuery(const std::string & q)
PGresult * DbBase::AssertQuery(const std::wstring & q)
{
return AssertQuery(q.c_str());
}
@@ -117,7 +117,6 @@ int DbBase::AssertColumn(PGresult * r, const char * column_name)
if( c == -1 )
{
log << log1 << "Db: there is no column: " << column_name << logend;
throw Error(WINIX_ERR_DB_NO_COLUMN);
}
@@ -132,7 +131,6 @@ const char * DbBase::AssertValue(PGresult * r, int row, int col)
if( !res )
{
log << log1 << "Db: there is no such an item in the result, row:" << row << ", col:" << col << logend;
throw Error(WINIX_ERR_NO_ITEM);
}
@@ -140,6 +138,24 @@ return res;
}
const std::wstring & DbBase::AssertValueWide(PGresult * r, int row, int col)
{
const char * res = AssertValue(r, row, col);
static std::wstring temp_wide_value;
Ezc::UTF8ToWide(res, temp_wide_value);
return temp_wide_value;
}
void DbBase::AssertValueWide(PGresult * r, int row, int col, std::wstring & result)
{
const char * res = AssertValue(r, row, col);
Ezc::UTF8ToWide(res, result);
}
long DbBase::AssertValueLong(PGresult * r, int row, int col)
{
return strtol( AssertValue(r, row, col), 0, 10 );
@@ -300,9 +316,10 @@ return buffer;
void DbBase::CreateIdList(const std::vector<long> & id_tab, std::string & list, bool add_parentheses)
void DbBase::CreateIdList(const std::vector<long> & id_tab, std::wstring & list, bool add_parentheses)
{
char buffer[50];
wchar_t buffer[50];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
list.clear();
@@ -311,7 +328,7 @@ char buffer[50];
for(size_t i=0 ; i<id_tab.size() ; ++i)
{
sprintf(buffer, "%lu", (unsigned long)id_tab[i]);
swprintf(buffer, buffer_len, L"%lu", (unsigned long)id_tab[i]);
list += buffer;
if( i+1 < id_tab.size() )

View File

@@ -31,12 +31,14 @@ public:
void LogQueries(bool log_q);
PGresult * AssertQuery(const char * query);
PGresult * AssertQuery(const std::string & query);
PGresult * AssertQuery(const std::wstring & query);
PGresult * AssertQuery(const DbTextStream & query);
void AssertResult(PGresult * r, ExecStatusType t);
static int AssertColumn(PGresult * r, const char * column_name);
static const char * AssertValue(PGresult * r, int row, int col);
static const std::wstring & AssertValueWide(PGresult * r, int row, int col); // warning: this method uses a static buffer
static void AssertValueWide(PGresult * r, int row, int col, std::wstring & result);
static long AssertValueLong(PGresult * r, int row, int col);
static int AssertValueInt(PGresult * r, int row, int col);
static unsigned long AssertValueULong(PGresult * r, int row, int col);
@@ -53,7 +55,7 @@ public:
static tm ConvertTime(const char * str);
static const char * ConvertTime(const tm & t); // warning: it uses its own static buffer
void CreateIdList(const std::vector<long> & id_tab, std::string & list, bool add_parentheses = true);
void CreateIdList(const std::vector<long> & id_tab, std::wstring & list, bool add_parentheses = true);
Error DoCommand(const char * command);
Error BeginTrans();
@@ -76,6 +78,7 @@ private:
DbTextStream bquery;
};

View File

@@ -147,7 +147,7 @@ bool was_connection = true;
void DbConn::SetDbParameters()
{
if( PQsetClientEncoding(pg_conn, "LATIN2") == -1 )
if( PQsetClientEncoding(pg_conn, "UTF8") == -1 )
log << log1 << "Db: Can't set the proper client encoding" << logend;
}

View File

@@ -46,18 +46,18 @@ void DbItemColumns::SetItem(PGresult * r, long row, Item & item)
if( privileges != -1 ) item.privileges = DbBase::AssertValueInt(r, row, privileges);
if( date_creation != -1 ) item.date_creation = DbBase::AssertValueTm(r, row, date_creation);
if( date_modification != -1 ) item.date_modification = DbBase::AssertValueTm(r, row, date_modification);
if( url != -1 ) item.url = DbBase::AssertValue(r, row, url);
if( url != -1 ) item.url = DbBase::AssertValueWide(r, row, url);
if( type != -1 ) item.type = static_cast<Item::Type>( DbBase::AssertValueInt(r, row, type) );
if( parent_id != -1 ) item.parent_id = DbBase::AssertValueLong(r, row, parent_id);
if( content_id != -1 ) item.content_id = DbBase::AssertValueLong(r, row, content_id);
if( default_item != -1 ) item.default_item = DbBase::AssertValueLong(r, row, default_item);
if( subject != -1 ) item.subject = DbBase::AssertValue(r, row, subject);
if( content != -1 ) item.content = DbBase::AssertValue(r, row, content);
if( subject != -1 ) item.subject = DbBase::AssertValueWide(r, row, subject);
if( content != -1 ) item.content = DbBase::AssertValueWide(r, row, content);
if( content_type != -1 ) item.content_type = static_cast<Item::ContentType>( DbBase::AssertValueInt(r, row, content_type) );
if( guest_name != -1 ) item.guest_name = DbBase::AssertValue(r, row, guest_name);
if( guest_name != -1 ) item.guest_name = DbBase::AssertValueWide(r, row, guest_name);
if( auth != -1 ) item.auth = static_cast<Item::Auth>( DbBase::AssertValueInt(r, row, auth) );
if( auth_path != -1 ) item.auth_path = DbBase::AssertValue(r, row, auth_path);
if( html_template != -1 ) item.html_template = DbBase::AssertValue(r, row, html_template);
if( auth_path != -1 ) item.auth_path = DbBase::AssertValueWide(r, row, auth_path);
if( html_template != -1 ) item.html_template = DbBase::AssertValueWide(r, row, html_template);
if( modification_user_id != -1 ) item.modification_user_id = DbBase::AssertValueLong(r, row, modification_user_id);
}

View File

@@ -8,7 +8,7 @@
*/
#include "dbtextstream.h"
#include "ezc.h"
DbTextStream::DbTextStream()
@@ -54,12 +54,39 @@ DbTextStream & DbTextStream::PutText(const std::string & str)
DbTextStream & DbTextStream::PutText(const wchar_t * str)
{
Ezc::WideToUTF8(str, buffer, false);
was_param = false;
return *this;
}
DbTextStream & DbTextStream::PutText(const std::wstring * str)
{
return PutText(str->c_str());
}
DbTextStream & DbTextStream::PutText(const std::wstring & str)
{
return PutText(str.c_str());
}
DbTextStream & DbTextStream::operator<<(const RawText<const char*> & raw)
{
return PutText(raw.par);
}
DbTextStream & DbTextStream::operator<<(const RawText<const wchar_t*> & raw)
{
return PutText(raw.par);
}
DbTextStream & DbTextStream::operator<<(RawText<std::string> raw)
@@ -68,9 +95,24 @@ DbTextStream & DbTextStream::operator<<(RawText<std::string> raw)
}
DbTextStream & DbTextStream::operator<<(RawText<std::wstring> raw)
{
return PutText(raw.par.c_str());
}
DbTextStream & DbTextStream::operator<<(RawText<char> raw)
{
TextStream::operator<<(raw.par);
TextStream<std::string>::operator<<(raw.par);
was_param = false;
return *this;
}
DbTextStream & DbTextStream::operator<<(RawText<wchar_t> raw)
{
TextStream<std::string>::operator<<(raw.par);
was_param = false;
return *this;
@@ -79,7 +121,7 @@ return *this;
DbTextStream & DbTextStream::operator<<(RawText<int> raw)
{
TextStream::operator<<(raw.par);
TextStream<std::string>::operator<<(raw.par);
was_param = false;
return *this;
@@ -88,7 +130,7 @@ return *this;
DbTextStream & DbTextStream::operator<<(RawText<long> raw)
{
TextStream::operator<<(raw.par);
TextStream<std::string>::operator<<(raw.par);
was_param = false;
return *this;
@@ -97,7 +139,7 @@ return *this;
DbTextStream & DbTextStream::operator<<(RawText<unsigned int> raw)
{
TextStream::operator<<(raw.par);
TextStream<std::string>::operator<<(raw.par);
was_param = false;
return *this;
@@ -106,7 +148,7 @@ return *this;
DbTextStream & DbTextStream::operator<<(RawText<unsigned long> raw)
{
TextStream::operator<<(raw.par);
TextStream<std::string>::operator<<(raw.par);
was_param = false;
return *this;
@@ -115,7 +157,7 @@ return *this;
DbTextStream & DbTextStream::operator<<(RawText<double> raw)
{
TextStream::operator<<(raw.par);
TextStream<std::string>::operator<<(raw.par);
was_param = false;
return *this;
@@ -124,7 +166,7 @@ return *this;
DbTextStream & DbTextStream::operator<<(RawText<void*> raw)
{
TextStream::operator<<(raw.par);
TextStream<std::string>::operator<<(raw.par);
was_param = false;
return *this;
@@ -185,6 +227,20 @@ return *this;
}
DbTextStream & DbTextStream::ETextPutChar(wchar_t c)
{
if( c == '\\' )
buffer += "\\\\";
else
if( c == '\'' )
buffer += "\\\'"; // don't use "''" because we use the method for PQconnectdb too
else
if( c != 0 )
Ezc::IntToUTF8(int(c), buffer, false);
return *this;
}
DbTextStream & DbTextStream::EPutText(const char * str)
{
@@ -218,6 +274,39 @@ DbTextStream & DbTextStream::EPutText(const std::string & str)
}
DbTextStream & DbTextStream::EPutText(const wchar_t * str)
{
if( was_param )
buffer += ", ";
if( ext_escape )
buffer += 'E';
buffer += '\'';
for( ; *str ; ++str )
ETextPutChar(*str);
buffer += '\'';
was_param = true;
return *this;
}
DbTextStream & DbTextStream::EPutText(const std::wstring * str)
{
return EPutText(str->c_str());
}
DbTextStream & DbTextStream::EPutText(const std::wstring & str)
{
return EPutText(str.c_str());
}
// this method can escaped 0 in the middle of the string
DbTextStream & DbTextStream::EPutBin(const char * str, size_t len)
{
@@ -273,6 +362,25 @@ DbTextStream & DbTextStream::operator<<(const std::string & str)
DbTextStream & DbTextStream::operator<<(const wchar_t * str)
{
return EPutText(str);
}
DbTextStream & DbTextStream::operator<<(const std::wstring * str)
{
return EPutText(str);
}
DbTextStream & DbTextStream::operator<<(const std::wstring & str)
{
return EPutText(str);
}
DbTextStream & DbTextStream::operator<<(char v)
{
@@ -291,12 +399,29 @@ return *this;
}
DbTextStream & DbTextStream::operator<<(wchar_t v)
{
if( was_param )
buffer += ", ";
if( ext_escape )
buffer += 'E';
buffer += '\'';
ETextPutChar(v);
buffer += '\'';
was_param = true;
return *this;
}
DbTextStream & DbTextStream::operator<<(int v)
{
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
TextStream<std::string>::operator<<(v);
was_param = true;
return *this;
@@ -308,7 +433,7 @@ DbTextStream & DbTextStream::operator<<(long v)
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
TextStream<std::string>::operator<<(v);
was_param = true;
return *this;
@@ -320,7 +445,7 @@ DbTextStream & DbTextStream::operator<<(unsigned int v)
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
TextStream<std::string>::operator<<(v);
was_param = true;
return *this;
@@ -332,7 +457,7 @@ DbTextStream & DbTextStream::operator<<(unsigned long v)
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
TextStream<std::string>::operator<<(v);
was_param = true;
return *this;
@@ -344,7 +469,7 @@ DbTextStream & DbTextStream::operator<<(double v)
if( was_param )
buffer += ", ";
TextStream::operator<<(v);
TextStream<std::string>::operator<<(v);
was_param = true;
return *this;
@@ -357,7 +482,7 @@ DbTextStream & DbTextStream::operator<<(const void * v)
buffer += ", ";
buffer += '\''; // !! not needed here?
TextStream::operator<<(v);
TextStream<std::string>::operator<<(v);
buffer += '\'';
was_param = true;

View File

@@ -49,7 +49,7 @@
between key1 key2 and key3 are commas inserted automatically
*/
class DbTextStream : public TextStream
class DbTextStream : public TextStream<std::string>
{
public:
@@ -83,6 +83,9 @@ public:
DbTextStream & PutText(const char *);
DbTextStream & PutText(const std::string *);
DbTextStream & PutText(const std::string &);
DbTextStream & PutText(const wchar_t * str);
DbTextStream & PutText(const std::wstring * str);
DbTextStream & PutText(const std::wstring & str);
/*
we need this template operator for such calling:
@@ -92,9 +95,15 @@ public:
template<size_t str_size>
DbTextStream & operator<<(const RawText<char [str_size]> & raw) { return PutText(raw.par); }
DbTextStream & operator<<(const RawText<const char*> & raw);
template<size_t str_size>
DbTextStream & operator<<(const RawText<wchar_t [str_size]> & raw) { return PutText(raw.par); }
DbTextStream & operator<<(const RawText<const char*> & raw); // !! ? bez const tutaj? bo jest const w szablonie
DbTextStream & operator<<(const RawText<const wchar_t*> & raw);
DbTextStream & operator<<(RawText<std::string> raw);
DbTextStream & operator<<(RawText<std::wstring> raw);
DbTextStream & operator<<(RawText<char> raw);
DbTextStream & operator<<(RawText<wchar_t> raw);
DbTextStream & operator<<(RawText<int> raw);
DbTextStream & operator<<(RawText<long> raw);
DbTextStream & operator<<(RawText<unsigned int> raw);
@@ -110,10 +119,14 @@ public:
DbTextStream & EBinPutChar(char c);
DbTextStream & ETextPutChar(char c);
DbTextStream & ETextPutChar(wchar_t c);
DbTextStream & EPutText(const char * str);
DbTextStream & EPutText(const std::string * str);
DbTextStream & EPutText(const std::string & str);
DbTextStream & EPutText(const wchar_t * str);
DbTextStream & EPutText(const std::wstring * str);
DbTextStream & EPutText(const std::wstring & str);
DbTextStream & EPutBin(const char * str, size_t len);
DbTextStream & EPutBin(const std::string * str);
@@ -122,7 +135,11 @@ public:
DbTextStream & operator<<(const char * str);
DbTextStream & operator<<(const std::string * str);
DbTextStream & operator<<(const std::string & str);
DbTextStream & operator<<(const wchar_t * str);
DbTextStream & operator<<(const std::wstring * str);
DbTextStream & operator<<(const std::wstring & str);
DbTextStream & operator<<(char);
DbTextStream & operator<<(wchar_t);
DbTextStream & operator<<(int);
DbTextStream & operator<<(long);
DbTextStream & operator<<(unsigned int);