changed: now we do not use std::string and char* in the Winix API
everywhere we are using std::wstring and wchar_t* (std::string and char* is used only locally in some places especially when creating a path to OS file system etc.) added: to the special thread when winix closes: a write function for curl: FetchPageOnExitCurlCallback() without this function the curl library will print the page's content to the standart output changed: TextStream<> class from core can make UTF8<->wide strings conversions removed: from config: utf8 option now winix expects UTF8 from the user's input (html forms, url-es) and outputs strings in the UTF8 format git-svn-id: svn://ttmath.org/publicrep/winix/trunk@965 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
359
core/misc.cpp
359
core/misc.cpp
@@ -57,9 +57,6 @@ namespace misc_private
|
||||
0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x2028,
|
||||
0x2029, 0x202F, 0x205F, 0x3000 };
|
||||
|
||||
std::ifstream get_file_content;
|
||||
std::string get_file_content_ansi;
|
||||
|
||||
PT::WTextStream tmp_qencode;
|
||||
}
|
||||
|
||||
@@ -177,7 +174,7 @@ return buffer;
|
||||
|
||||
void Toa(int value, std::string & res, int base, bool clear)
|
||||
{
|
||||
static char buffer[50];
|
||||
static char buffer[50]; // !! IMPROVE ME this 'static' is not needed here?
|
||||
size_t len = sizeof(buffer) / sizeof(char);
|
||||
|
||||
if( clear )
|
||||
@@ -190,7 +187,7 @@ size_t len = sizeof(buffer) / sizeof(char);
|
||||
|
||||
void Toa(long value, std::string & res, int base, bool clear)
|
||||
{
|
||||
static char buffer[50];
|
||||
static char buffer[50]; // !! IMPROVE ME the same as above
|
||||
size_t len = sizeof(buffer) / sizeof(char);
|
||||
|
||||
if( clear )
|
||||
@@ -203,7 +200,7 @@ size_t len = sizeof(buffer) / sizeof(char);
|
||||
|
||||
void Toa(int value, std::wstring & res, int base, bool clear)
|
||||
{
|
||||
static wchar_t buffer[50];
|
||||
static wchar_t buffer[50]; // !!
|
||||
size_t len = sizeof(buffer) / sizeof(wchar_t);
|
||||
|
||||
if( clear )
|
||||
@@ -216,7 +213,7 @@ size_t len = sizeof(buffer) / sizeof(wchar_t);
|
||||
|
||||
void Toa(long value, std::wstring & res, int base, bool clear)
|
||||
{
|
||||
static wchar_t buffer[50];
|
||||
static wchar_t buffer[50]; // !!
|
||||
size_t len = sizeof(buffer) / sizeof(wchar_t);
|
||||
|
||||
if( clear )
|
||||
@@ -231,142 +228,6 @@ size_t len = sizeof(buffer) / sizeof(wchar_t);
|
||||
|
||||
|
||||
|
||||
|
||||
void AssignString(const char * src, size_t len, std::wstring & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
if( dst.capacity() < dst.size() + len )
|
||||
dst.reserve(dst.size() + len + 128);
|
||||
|
||||
for(size_t i=0 ; i<len ; ++i )
|
||||
dst += static_cast<unsigned char>(src[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AssignString(const char * src, std::wstring & dst, bool clear)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for(len=0 ; src[len] ; ++len){}
|
||||
|
||||
AssignString(src, len, dst, clear);
|
||||
}
|
||||
|
||||
|
||||
void AssignString(const std::string & src, std::wstring & dst, bool clear)
|
||||
{
|
||||
AssignString(src.c_str(), src.size(), dst, clear);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void AssignString(const wchar_t * src, size_t len, std::string & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
if( dst.capacity() < dst.size() + len )
|
||||
dst.reserve(dst.size() + len + 128);
|
||||
|
||||
for(size_t i=0 ; i<len ; ++i)
|
||||
dst += static_cast<char>(src[i]);
|
||||
}
|
||||
|
||||
|
||||
void AssignString(const wchar_t * src, std::string & dst, bool clear)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for(len=0 ; src[len] ; ++len){}
|
||||
|
||||
AssignString(src, len, dst, clear);
|
||||
}
|
||||
|
||||
|
||||
void AssignString(const std::wstring & src, std::string & dst, bool clear)
|
||||
{
|
||||
AssignString(src.c_str(), src.size(), dst, clear);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AssignString(const char * src, size_t len, std::string & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
// we suppose that append is smart enough and we don't have to use reserve()
|
||||
dst.append(src, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AssignString(const char * src, std::string & dst, bool clear)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for(len=0 ; src[len] ; ++len){}
|
||||
|
||||
AssignString(src, len, dst, clear);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void AssignString(const std::string & src, std::string & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
dst.append(src);
|
||||
}
|
||||
|
||||
|
||||
void AssignString(const wchar_t * src, size_t len, std::wstring & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
// we suppose that append is smart enough and we don't have to use reserve()
|
||||
dst.append(src, len);
|
||||
}
|
||||
|
||||
|
||||
void AssignString(const wchar_t * src, std::wstring & dst, bool clear)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for(len=0 ; src[len] ; ++len){}
|
||||
|
||||
AssignString(src, len, dst, clear);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AssignString(const std::wstring & src, std::wstring & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
dst.append(src);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool CorrectUrlChar(wchar_t c)
|
||||
{
|
||||
return (c >= 'a' && c <='z') ||
|
||||
@@ -689,6 +550,18 @@ return path[path.size()-1] == '/';
|
||||
|
||||
|
||||
|
||||
void Overwrite(std::string & str)
|
||||
{
|
||||
for(char & c : str)
|
||||
c = 0;
|
||||
}
|
||||
|
||||
void Overwrite(std::wstring & str)
|
||||
{
|
||||
for(wchar_t & c : str)
|
||||
c = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const char * SkipWhite(const char * s)
|
||||
@@ -792,11 +665,12 @@ bool ValidateEmail(const std::wstring & email)
|
||||
bool IsFile(const wchar_t * file)
|
||||
{
|
||||
struct stat sb;
|
||||
static std::string afile;
|
||||
char file_name[WINIX_OS_PATH_SIZE];
|
||||
|
||||
PT::WideToUTF8(file, afile);
|
||||
if( !WideToUTF8(file, file_name, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
return (stat(afile.c_str(), &sb) == 0);
|
||||
return (stat(file_name, &sb) == 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -811,19 +685,20 @@ bool IsFile(const std::wstring & file)
|
||||
*/
|
||||
bool CreateDir(const wchar_t * dir, int priv, int group)
|
||||
{
|
||||
static std::string adir;
|
||||
char dir_name[WINIX_OS_PATH_SIZE];
|
||||
|
||||
if( !IsFile(dir) )
|
||||
{
|
||||
PT::WideToUTF8(dir, adir);
|
||||
if( !WideToUTF8(dir, dir_name, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
if( mkdir(adir.c_str(), 0777) < 0 )
|
||||
if( mkdir(dir_name, 0777) < 0 )
|
||||
{
|
||||
log << log1 << "Can't create a directory on fs: " << adir << logend;
|
||||
log << log1 << "Can't create a directory on fs: " << dir << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
return SetPriv(adir, priv, group);
|
||||
return SetPriv(dir, priv, group);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -841,7 +716,7 @@ bool CreateDir(const std::wstring & dir, int priv, int group)
|
||||
// 'dirs' can begin with a slash (will be skipped)
|
||||
bool CreateDirs(const wchar_t * base_dir, const wchar_t * dirs, int priv, int group, bool skip_last)
|
||||
{
|
||||
static std::wstring temp;
|
||||
static std::wstring temp; // !! IMPROVE ME change to char[WINIX_OS_PATH_SIZE] or just remove 'static'
|
||||
const wchar_t * p = dirs;
|
||||
|
||||
temp = base_dir; // we start creating from 'base_dir'
|
||||
@@ -883,15 +758,19 @@ bool CreateDirs(const std::wstring & base_dir, const std::wstring & dirs, int pr
|
||||
|
||||
|
||||
|
||||
int GetGroupId(const char * name)
|
||||
int GetGroupId(const wchar_t * name)
|
||||
{
|
||||
struct group gr;
|
||||
struct group * result;
|
||||
char group_name[WINIX_OS_USERNAME_SIZE];
|
||||
char buffer[512];
|
||||
|
||||
if( getgrnam_r(name, &gr, buffer, sizeof(buffer)/sizeof(char), &result) != 0 )
|
||||
if( !WideToUTF8(name, group_name, WINIX_OS_USERNAME_SIZE) )
|
||||
return -1;
|
||||
|
||||
if( getgrnam_r(group_name, &gr, buffer, sizeof(buffer)/sizeof(char), &result) != 0 )
|
||||
{
|
||||
log << log1 << "I cannot get the group_id for group name: " << name << logend;
|
||||
log << log1 << "Misc: I cannot get the group_id for group name: " << name << logend;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -900,7 +779,7 @@ char buffer[512];
|
||||
*/
|
||||
if( result == 0 )
|
||||
{
|
||||
log << log1 << "There is no a group with name: " << name << logend;
|
||||
log << log1 << "Misc: There is no a group with name: " << name << logend;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -908,7 +787,7 @@ return gr.gr_gid;
|
||||
}
|
||||
|
||||
|
||||
int GetGroupId(const std::string & name)
|
||||
int GetGroupId(const std::wstring & name)
|
||||
{
|
||||
return GetGroupId(name.c_str());
|
||||
}
|
||||
@@ -918,19 +797,25 @@ int GetGroupId(const std::string & name)
|
||||
* setting priveleges and a group id on a file or on a directory
|
||||
* group can be -1 (it is not used then)
|
||||
*/
|
||||
bool SetPriv(const char * name, int priv, int group)
|
||||
bool SetPriv(const wchar_t * name, int priv, int group)
|
||||
{
|
||||
if( chmod(name, priv) < 0 )
|
||||
char file_name[WINIX_OS_PATH_SIZE];
|
||||
|
||||
if( !WideToUTF8(name, file_name, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
if( chmod(file_name, priv) < 0 )
|
||||
{
|
||||
log << log1 << "Can't set proper fs privileges on: " << name << logend;
|
||||
log << log1 << "Misc: Can't set proper fs privileges on: " << name << logend;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( group != -1 )
|
||||
{
|
||||
if( chown(name, geteuid(), group) < 0 )
|
||||
if( chown(file_name, geteuid(), group) < 0 )
|
||||
{
|
||||
log << log1 << "Can't set proper fs group on: " << name << logend;
|
||||
log << log1 << "Can't set proper fs group on: " << name
|
||||
<< ", group id was: " << group << logend;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -939,7 +824,7 @@ return true;
|
||||
}
|
||||
|
||||
|
||||
bool SetPriv(const std::string & name, int priv, int group)
|
||||
bool SetPriv(const std::wstring & name, int priv, int group)
|
||||
{
|
||||
return SetPriv(name.c_str(), priv, group);
|
||||
}
|
||||
@@ -970,18 +855,22 @@ return true;
|
||||
|
||||
bool CopyFile(const wchar_t * src, const wchar_t * dst)
|
||||
{
|
||||
static std::string asrc, adst;
|
||||
char src_name[WINIX_OS_PATH_SIZE];
|
||||
char dst_name[WINIX_OS_PATH_SIZE];
|
||||
FILE * in, * out;
|
||||
|
||||
PT::WideToUTF8(src, asrc);
|
||||
PT::WideToUTF8(dst, adst);
|
||||
if( !WideToUTF8(src, src_name, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
in = fopen(asrc.c_str(), "rb");
|
||||
if( !WideToUTF8(dst, dst_name, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
in = fopen(src_name, "rb");
|
||||
|
||||
if( !in )
|
||||
return false;
|
||||
|
||||
out = fopen(adst.c_str(), "wb");
|
||||
out = fopen(dst_name, "wb");
|
||||
|
||||
if( !out )
|
||||
{
|
||||
@@ -998,7 +887,7 @@ FILE * in, * out;
|
||||
res = false;
|
||||
|
||||
if( !res )
|
||||
remove(adst.c_str());
|
||||
remove(dst_name);
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -1013,11 +902,12 @@ bool CopyFile(const std::wstring & src, const std::wstring & dst)
|
||||
|
||||
bool RemoveFile(const wchar_t * file)
|
||||
{
|
||||
static std::string afile;
|
||||
char file_name[WINIX_OS_PATH_SIZE];
|
||||
|
||||
PT::WideToUTF8(file, afile);
|
||||
if( !WideToUTF8(file, file_name, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
return unlink(afile.c_str()) == 0;
|
||||
return unlink(file_name) == 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1030,12 +920,16 @@ bool RemoveFile(const std::wstring & file)
|
||||
|
||||
bool RenameFile(const wchar_t * from, const wchar_t * to)
|
||||
{
|
||||
static std::string afrom, ato;
|
||||
char from_name[WINIX_OS_PATH_SIZE];
|
||||
char to_name[WINIX_OS_PATH_SIZE];
|
||||
|
||||
PT::WideToUTF8(from, afrom);
|
||||
PT::WideToUTF8(to, ato);
|
||||
if( !WideToUTF8(from, from_name, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
return rename(afrom.c_str(), ato.c_str()) == 0;
|
||||
if( !WideToUTF8(to, to_name, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
return rename(from_name, to_name) == 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1048,19 +942,25 @@ bool RenameFile(const std::wstring & from, const std::wstring & to)
|
||||
|
||||
|
||||
|
||||
bool GetUTF8File(const char * file_path, std::wstring & content, bool clear_content)
|
||||
bool GetUTF8File(const wchar_t * file_path, std::wstring & content, bool clear_content)
|
||||
{
|
||||
using namespace misc_private;
|
||||
char file[WINIX_OS_PATH_SIZE];
|
||||
std::ifstream get_file_content;
|
||||
|
||||
if( clear_content )
|
||||
content.clear();
|
||||
|
||||
get_file_content.clear();
|
||||
get_file_content.open(file_path, std::ios_base::in | std::ios_base::binary);
|
||||
if( !WideToUTF8(file_path, file, WINIX_OS_PATH_SIZE) )
|
||||
return false;
|
||||
|
||||
get_file_content.open(file, std::ios_base::in | std::ios_base::binary);
|
||||
|
||||
if( !get_file_content )
|
||||
return false;
|
||||
|
||||
/*
|
||||
* we don't report any errors when converting from UTF8 to wide characters here
|
||||
*/
|
||||
PT::UTF8ToWide(get_file_content, content);
|
||||
get_file_content.close();
|
||||
|
||||
@@ -1068,21 +968,6 @@ return true;
|
||||
}
|
||||
|
||||
|
||||
bool GetUTF8File(const wchar_t * file_path, std::wstring & content, bool clear_content)
|
||||
{
|
||||
using namespace misc_private;
|
||||
|
||||
PT::WideToUTF8(file_path, get_file_content_ansi);
|
||||
return GetUTF8File(get_file_content_ansi.c_str(), content, clear_content);
|
||||
}
|
||||
|
||||
|
||||
bool GetUTF8File(const std::string & file_path, std::wstring & content, bool clear_content)
|
||||
{
|
||||
return GetUTF8File(file_path.c_str(), content, clear_content);
|
||||
}
|
||||
|
||||
|
||||
bool GetUTF8File(const std::wstring & file_path, std::wstring & content, bool clear_content)
|
||||
{
|
||||
return GetUTF8File(file_path.c_str(), content, clear_content);
|
||||
@@ -1222,6 +1107,69 @@ void UrlEncode(const std::wstring & in, std::wstring & out, bool clear_out)
|
||||
|
||||
|
||||
|
||||
bool UrlDecodeFromHex(int c, int & out)
|
||||
{
|
||||
if( c>='0' && c<='9' )
|
||||
{
|
||||
out = c - '0';
|
||||
return true;
|
||||
}
|
||||
else
|
||||
if( c>='a' && c<='f' )
|
||||
{
|
||||
out = c - 'a' + 10;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
if( c>='A' && c<='F' )
|
||||
{
|
||||
out = c - 'A' + 10;
|
||||
return true;
|
||||
}
|
||||
|
||||
out = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool UrlDecode(const char * url, std::wstring & out, bool clear_out)
|
||||
{
|
||||
char url_utf8[WINIX_URL_MAX_SIZE];
|
||||
size_t index = 0;
|
||||
int c1, c2;
|
||||
|
||||
if( clear_out )
|
||||
out.clear();
|
||||
|
||||
while( *url && index < WINIX_URL_MAX_SIZE-1 )
|
||||
{
|
||||
if( *url == '%' && *(url+1) && *(url+2) &&
|
||||
UrlDecodeFromHex(*(url+1), c1) && UrlDecodeFromHex(*(url+2), c2) )
|
||||
{
|
||||
url_utf8[index++] = (c1 << 4) + c2;
|
||||
url += 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
url_utf8[index++] = *url;
|
||||
url += 1;
|
||||
}
|
||||
}
|
||||
|
||||
url_utf8[index] = 0;
|
||||
|
||||
return PT::UTF8ToWide(url_utf8, out, false);
|
||||
}
|
||||
|
||||
|
||||
bool UrlDecode(const std::string & url, std::wstring & out, bool clear_out)
|
||||
{
|
||||
return UrlDecode(url.c_str(), out, clear_out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void QEncode(const std::wstring & in, std::string & out, bool clear)
|
||||
{
|
||||
@@ -1254,6 +1202,27 @@ void RemovePostFileTmp(PostFileTab & post_file_tab)
|
||||
}
|
||||
|
||||
|
||||
bool WideToUTF8(const wchar_t * wide_string, char * utf8, size_t utf8_size)
|
||||
{
|
||||
bool res = PT::WideToUTF8(wide_string, utf8, utf8_size);
|
||||
|
||||
if( !res )
|
||||
{
|
||||
/*
|
||||
* either the 'utf8' buffer is too small or there was an error when converting
|
||||
*/
|
||||
log << log1 << "Misc: I cannot convert from a wide string to an UTF-8 string, original string was: "
|
||||
<< wide_string << logend;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool WideToUTF8(const std::wstring & wide_string, char * utf8, size_t utf8_size)
|
||||
{
|
||||
return WideToUTF8(wide_string.c_str(), utf8, utf8_size);
|
||||
}
|
||||
|
||||
|
||||
} // namespace Winix
|
||||
|
Reference in New Issue
Block a user