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:
630
core/misc.cpp
630
core/misc.cpp
@@ -11,10 +11,177 @@
|
||||
#include <sys/stat.h>
|
||||
#include "misc.h"
|
||||
#include "log.h"
|
||||
#include "templates/templates.h"
|
||||
|
||||
|
||||
int Atoi(const std::string & str, int base)
|
||||
{
|
||||
return Atoi(str.c_str(), base);
|
||||
}
|
||||
|
||||
int Atoi(const std::wstring & str, int base)
|
||||
{
|
||||
return Atoi(str.c_str(), base);
|
||||
}
|
||||
|
||||
int Atoi(const char * str, int base)
|
||||
{
|
||||
return static_cast<int>(strtol(str, 0, base));
|
||||
}
|
||||
|
||||
int Atoi(const wchar_t * str, int base)
|
||||
{
|
||||
return static_cast<int>(wcstol(str, 0, base));
|
||||
}
|
||||
|
||||
|
||||
|
||||
long Atol(const std::string & str, int base)
|
||||
{
|
||||
return Atol(str.c_str(), base);
|
||||
}
|
||||
|
||||
long Atol(const std::wstring & str, int base)
|
||||
{
|
||||
return Atol(str.c_str(), base);
|
||||
}
|
||||
|
||||
long Atol(const char * str, int base)
|
||||
{
|
||||
return strtol(str, 0, base);
|
||||
}
|
||||
|
||||
long Atol(const wchar_t * str, int base)
|
||||
{
|
||||
return wcstol(str, 0, base);
|
||||
}
|
||||
|
||||
|
||||
const wchar_t * Itoa(int value, int base)
|
||||
{
|
||||
return Ltoa(value, base);
|
||||
}
|
||||
|
||||
|
||||
const wchar_t * Ltoa(long value, int base)
|
||||
{
|
||||
static wchar_t digits[] = L"0123456789ABCDEF";
|
||||
static wchar_t buffer_[50];
|
||||
wchar_t * buffer = buffer_;
|
||||
size_t i1, i2;
|
||||
long rest;
|
||||
|
||||
i1 = i2 = 0;
|
||||
|
||||
if( base < 2 )
|
||||
base = 2;
|
||||
|
||||
if( base > 16 )
|
||||
base = 16;
|
||||
|
||||
if( value < 0 )
|
||||
{
|
||||
buffer[0] = '-';
|
||||
buffer += 1;
|
||||
value = -value;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
rest = value % base;
|
||||
value = value / base;
|
||||
buffer[i2++] = digits[rest];
|
||||
}
|
||||
while(value != 0);
|
||||
|
||||
buffer[i2--] = 0;
|
||||
|
||||
for( ; i1 < i2 ; ++i1, --i2)
|
||||
{
|
||||
wchar_t temp = buffer[i1];
|
||||
buffer[i1] = buffer[i2];
|
||||
buffer[i2] = temp;
|
||||
}
|
||||
|
||||
return buffer_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void AssignString(const char * src, std::wstring & dst, bool clear)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
for(len=0 ; src[len] ; ++len){}
|
||||
dst.reserve(len);
|
||||
|
||||
for( ; *src ; ++src )
|
||||
dst += static_cast<unsigned char>(*src);
|
||||
}
|
||||
|
||||
|
||||
void AssignString(const std::string & src, std::wstring & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
dst.reserve(src.size());
|
||||
|
||||
for(size_t i=0 ; i<src.size() ; ++i )
|
||||
dst += static_cast<unsigned char>(src[i]);
|
||||
}
|
||||
|
||||
|
||||
void AssignString(const wchar_t * src, std::string & dst, bool clear)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
for(len=0 ; src[len] ; ++len){}
|
||||
dst.reserve(len);
|
||||
|
||||
for( ; *src ; ++src )
|
||||
dst += static_cast<char>(*src);
|
||||
}
|
||||
|
||||
|
||||
void AssignString(const std::wstring & src, std::string & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
dst.reserve(src.size());
|
||||
|
||||
for(size_t i=0 ; i<src.size() ; ++i )
|
||||
dst += static_cast<char>(src[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AssignString(const std::string & src, std::string & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
dst += src;
|
||||
}
|
||||
|
||||
|
||||
void AssignString(const std::wstring & src, std::wstring & dst, bool clear)
|
||||
{
|
||||
if( clear )
|
||||
dst.clear();
|
||||
|
||||
dst += src;
|
||||
}
|
||||
|
||||
// !! skasowac, jest juz Itoa
|
||||
void ToString(std::string & s, int value)
|
||||
{
|
||||
static char buffer[50];
|
||||
@@ -33,27 +200,38 @@ static char buffer[50];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool CorrectUrlChar(char c)
|
||||
void ToString(std::wstring & s, int value)
|
||||
{
|
||||
if( (c >= 'a' && c <='z') ||
|
||||
(c >= 'A' && c <='Z') ||
|
||||
(c >= '0' && c <='9') ||
|
||||
(c == '(' || c == ')' || c == '.' || c == ',' || c == '_' )
|
||||
)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
static wchar_t buffer[50];
|
||||
|
||||
swprintf(buffer, sizeof(buffer)/sizeof(wchar_t), L"%d", value);
|
||||
s = buffer;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
void ToString(std::wstring & s, long value)
|
||||
{
|
||||
static wchar_t buffer[50];
|
||||
|
||||
swprintf(buffer, sizeof(buffer)/sizeof(wchar_t), L"%ld", value);
|
||||
s = buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool CorrectUrlChar(wchar_t c)
|
||||
{
|
||||
return (c >= 'a' && c <='z') ||
|
||||
(c >= 'A' && c <='Z') ||
|
||||
(c >= '0' && c <='9') ||
|
||||
c == '(' || c == ')' || c == '.' || c == ',' || c == '_' || c == '-';
|
||||
}
|
||||
|
||||
|
||||
|
||||
// this function checks how many dots there are in the url
|
||||
// if there are more than one (last) dot then the first dots will be changed into '_'
|
||||
void CorrectUrlDots(std::string & url)
|
||||
void CorrectUrlDots(std::wstring & url)
|
||||
{
|
||||
size_t i = url.size();
|
||||
bool was_dot = false;
|
||||
@@ -64,45 +242,49 @@ bool was_dot = false;
|
||||
{
|
||||
if( was_dot )
|
||||
// only one dot is allowed
|
||||
url[i] = '_';
|
||||
url[i] = '_'; // !! do konfiga
|
||||
|
||||
was_dot = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CorrectUrlChars(std::string & url)
|
||||
// !! pomyslec o lepszej nazwie
|
||||
void CorrectUrlChars(std::wstring & url)
|
||||
{
|
||||
std::string::iterator i;
|
||||
std::wstring::iterator i;
|
||||
|
||||
for(i=url.begin(); i != url.end() ; ++i)
|
||||
{
|
||||
if( !CorrectUrlChar(*i) )
|
||||
{
|
||||
int c = ChangeLocalChar(*i);
|
||||
//wchar_t c = ChangeLocalChar(*i);
|
||||
wchar_t c = TemplatesFunctions::locale.Subst(*i);
|
||||
|
||||
if( CorrectUrlChar(c) )
|
||||
*i = c;
|
||||
else
|
||||
*i = '_';
|
||||
*i = '_'; // !! dodac do konfiga
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CorrectUrlOnlyAllowedChar(std::string & url)
|
||||
// !! pomyslec o lepszej nazwie
|
||||
// przerzucic do funkcji - tam gdzie jest PrepareUrl()
|
||||
// bo tutaj nie mamy wskaznika na config
|
||||
void CorrectUrlOnlyAllowedChar(std::wstring & url)
|
||||
{
|
||||
CorrectUrlDots(url);
|
||||
CorrectUrlChars(url);
|
||||
ToSmall(url);
|
||||
Trim(url, '_');
|
||||
|
||||
if( url.empty() || url == "." )
|
||||
if( url.empty() || url == L"." )
|
||||
{
|
||||
// !! brakuje config->
|
||||
//if( config->item_url_empty.empty() )
|
||||
url = "unnamed";
|
||||
url = L"unnamed";
|
||||
//else
|
||||
//{
|
||||
// url = config->item_url_empty;
|
||||
@@ -121,7 +303,7 @@ void CorrectUrlOnlyAllowedChar(std::string & url)
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
int polish_letters_simple[] =
|
||||
{ 'a', 'A',
|
||||
'c', 'C',
|
||||
@@ -165,110 +347,14 @@ int ChangeLocalChar(unsigned char c)
|
||||
|
||||
return c;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
bool HtmlTryChar(std::ostringstream & out, int c)
|
||||
|
||||
const wchar_t * DateToStr(int year, int month, int day)
|
||||
{
|
||||
if( c == '<' )
|
||||
{
|
||||
out << "<";
|
||||
return true;
|
||||
}
|
||||
else
|
||||
if( c == '>' )
|
||||
{
|
||||
out << ">";
|
||||
return true;
|
||||
}
|
||||
else
|
||||
if( c == '&' )
|
||||
{
|
||||
out << "&";
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void HtmlEscape(std::ostringstream & out, const std::string & in)
|
||||
{
|
||||
std::string::const_iterator i;
|
||||
|
||||
for(i = in.begin() ; i != in.end() ; ++i)
|
||||
{
|
||||
if( !HtmlTryChar(out, *i) )
|
||||
out << *i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string HtmlEscape(const std::string & in)
|
||||
{
|
||||
std::ostringstream out;
|
||||
|
||||
HtmlEscape(out, in);
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
|
||||
void HtmlEscapeFormTxt(std::ostringstream & out, const std::string & in)
|
||||
{
|
||||
std::string::const_iterator i;
|
||||
int was_enter = 0; // how many enteres there were before
|
||||
|
||||
if( in.empty() )
|
||||
return;
|
||||
|
||||
out << "<p>";
|
||||
|
||||
// skipping first new line characters
|
||||
for(i = in.begin() ; i != in.end() && (*i==13 || *i==10) ; ++i);
|
||||
|
||||
for( ; i != in.end() ; ++i )
|
||||
{
|
||||
if( *i == 13 ) // skipping stupid characters (\r\n\ in dos mode)
|
||||
continue;
|
||||
|
||||
if( *i == 10 )
|
||||
{
|
||||
++was_enter;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( was_enter == 1 )
|
||||
out << "<br>\n";
|
||||
else
|
||||
if( was_enter > 1 )
|
||||
out << "</p>\n<p>";
|
||||
|
||||
was_enter = 0;
|
||||
}
|
||||
|
||||
if( !HtmlTryChar(out, *i) )
|
||||
out << *i;
|
||||
}
|
||||
|
||||
out << "</p>\n";
|
||||
}
|
||||
|
||||
|
||||
std::string HtmlEscapeFormTxt(const std::string & in)
|
||||
{
|
||||
std::ostringstream out;
|
||||
|
||||
HtmlEscapeFormTxt(out, in);
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
|
||||
const char * DateToStr(int year, int month, int day)
|
||||
{
|
||||
static const char * month_letter[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII" };
|
||||
static char buffer[100];
|
||||
static const wchar_t * month_letter[] = { L"I", L"II", L"III", L"IV", L"V", L"VI", L"VII", L"VIII", L"IX", L"X", L"XI", L"XII" };
|
||||
static wchar_t buffer[100];
|
||||
|
||||
--month;
|
||||
|
||||
@@ -279,19 +365,19 @@ static char buffer[100];
|
||||
month = 11;
|
||||
|
||||
if( year == 0 )
|
||||
sprintf(buffer, "%s %02d", month_letter[month], day);
|
||||
swprintf(buffer, sizeof(buffer)/sizeof(wchar_t), L"%ls %02d", month_letter[month], day);
|
||||
else
|
||||
sprintf(buffer, "%02d %s %02d", year, month_letter[month], day);
|
||||
swprintf(buffer, sizeof(buffer)/sizeof(wchar_t), L"%02d %ls %02d", year, month_letter[month], day);
|
||||
|
||||
// warning: not thread safe (we do not use threads)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
const char * DateToStr(int year, int month, int day, int hour, int min, int sec)
|
||||
const wchar_t * DateToStr(int year, int month, int day, int hour, int min, int sec)
|
||||
{
|
||||
static const char * month_letter[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII" };
|
||||
static char buffer[100];
|
||||
static const wchar_t * month_letter[] = { L"I", L"II", L"III", L"IV", L"V", L"VI", L"VII", L"VIII", L"IX", L"X", L"XI", L"XII" };
|
||||
static wchar_t buffer[100];
|
||||
|
||||
--month;
|
||||
|
||||
@@ -302,28 +388,28 @@ static char buffer[100];
|
||||
month = 11;
|
||||
|
||||
if( year == 0 )
|
||||
sprintf(buffer, "%s %02d %02d:%02d:%02d", month_letter[month], day, hour, min, sec);
|
||||
swprintf(buffer, sizeof(buffer)/sizeof(wchar_t), L"%ls %02d %02d:%02d:%02d", month_letter[month], day, hour, min, sec);
|
||||
else
|
||||
sprintf(buffer, "%02d %s %02d %02d:%02d:%02d", year, month_letter[month], day, hour, min, sec);
|
||||
swprintf(buffer, sizeof(buffer)/sizeof(wchar_t), L"%02d %ls %02d %02d:%02d:%02d", year, month_letter[month], day, hour, min, sec);
|
||||
|
||||
// warning: not thread safe (we do not use threads)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
const char * DateToStr(const tm * ptm)
|
||||
const wchar_t * DateToStr(const tm * ptm)
|
||||
{
|
||||
return DateToStr(ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
|
||||
}
|
||||
|
||||
|
||||
const char * DateToStr(const tm & rtm)
|
||||
const wchar_t * DateToStr(const tm & rtm)
|
||||
{
|
||||
return DateToStr(rtm.tm_year + 1900, rtm.tm_mon+1, rtm.tm_mday, rtm.tm_hour, rtm.tm_min, rtm.tm_sec);
|
||||
}
|
||||
|
||||
|
||||
const char * DateToStr(time_t t)
|
||||
const wchar_t * DateToStr(time_t t)
|
||||
{
|
||||
tm rtm = Time(t);
|
||||
|
||||
@@ -331,19 +417,19 @@ return DateToStr(rtm);
|
||||
}
|
||||
|
||||
|
||||
const char * DateToStrWithoutHours(const tm * ptm)
|
||||
const wchar_t * DateToStrWithoutHours(const tm * ptm)
|
||||
{
|
||||
return DateToStr(ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday);
|
||||
}
|
||||
|
||||
|
||||
const char * DateToStrWithoutHours(const tm & rtm)
|
||||
const wchar_t * DateToStrWithoutHours(const tm & rtm)
|
||||
{
|
||||
return DateToStr(rtm.tm_year + 1900, rtm.tm_mon+1, rtm.tm_mday);
|
||||
}
|
||||
|
||||
|
||||
const char * DateToStrWithoutHours(time_t t)
|
||||
const wchar_t * DateToStrWithoutHours(time_t t)
|
||||
{
|
||||
tm rtm = Time(t);
|
||||
|
||||
@@ -392,9 +478,9 @@ return DateToStrCookie(rtm);
|
||||
}
|
||||
|
||||
|
||||
const char * IpToStr(unsigned int ip_)
|
||||
const wchar_t * IpToStr(unsigned int ip_)
|
||||
{
|
||||
static char buffer[100];
|
||||
static wchar_t buffer[100];
|
||||
|
||||
union
|
||||
{
|
||||
@@ -404,23 +490,23 @@ static char buffer[100];
|
||||
|
||||
ip.ip = ip_;
|
||||
|
||||
sprintf(buffer, "%u.%u.%u.%u", (int)ip.c[0], (int)ip.c[1], (int)ip.c[2], (int)ip.c[3]);
|
||||
swprintf(buffer, sizeof(buffer)/sizeof(wchar_t), L"%u.%u.%u.%u", (int)ip.c[0], (int)ip.c[1], (int)ip.c[2], (int)ip.c[3]);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
const char * ToStr(int value)
|
||||
const wchar_t * ToStr(int value)
|
||||
{
|
||||
static char buffer[100];
|
||||
static wchar_t buffer[100];
|
||||
|
||||
sprintf(buffer, "%d", value);
|
||||
swprintf(buffer, sizeof(buffer)/sizeof(wchar_t), L"%d", value);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
bool IsWhite(int s)
|
||||
bool IsWhite(wchar_t s)
|
||||
{
|
||||
if( s==' ' || s=='\t' || s==13 || s==160 )
|
||||
return true;
|
||||
@@ -430,64 +516,8 @@ return false;
|
||||
|
||||
|
||||
|
||||
void TrimWhite(std::string & s)
|
||||
{
|
||||
std::string::size_type i;
|
||||
|
||||
if( s.empty() )
|
||||
return;
|
||||
|
||||
// looking for white characters at the end
|
||||
for(i=s.size()-1 ; i>0 && IsWhite(s[i]) ; --i);
|
||||
|
||||
if( i==0 && IsWhite(s[i]) )
|
||||
{
|
||||
// the whole string has white characters
|
||||
s.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// deleting white characters at the end
|
||||
if( i != s.size() - 1 )
|
||||
s.erase(i+1, std::string::npos);
|
||||
|
||||
// looking for white characters at the beginning
|
||||
for(i=0 ; i<s.size() && IsWhite(s[i]) ; ++i);
|
||||
|
||||
// deleting white characters at the beginning
|
||||
if( i != 0 )
|
||||
s.erase(0, i);
|
||||
}
|
||||
|
||||
|
||||
void Trim(std::string & s, char c)
|
||||
{
|
||||
std::string::size_type i;
|
||||
|
||||
if( s.empty() )
|
||||
return;
|
||||
|
||||
// looking for the 'c' characters at the end
|
||||
for(i=s.size()-1 ; i>0 && s[i]==c ; --i);
|
||||
|
||||
if( i==0 && s[i]==c )
|
||||
{
|
||||
// the whole string has the 'c' characters
|
||||
s.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// deleting 'c' characters at the end
|
||||
if( i != s.size() - 1 )
|
||||
s.erase(i+1, std::string::npos);
|
||||
|
||||
// looking for the 'c' characters at the beginning
|
||||
for(i=0 ; i<s.size() && s[i]==c ; ++i);
|
||||
|
||||
// deleting the 'c' characters at the beginning
|
||||
if( i != 0 )
|
||||
s.erase(0, i);
|
||||
}
|
||||
|
||||
|
||||
const char * SkipWhite(const char * s)
|
||||
@@ -499,7 +529,16 @@ return s;
|
||||
}
|
||||
|
||||
|
||||
int ToSmall(int c)
|
||||
const wchar_t * SkipWhite(const wchar_t * s)
|
||||
{
|
||||
while( IsWhite(*s) )
|
||||
++s;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
wchar_t ToSmall(wchar_t c)
|
||||
{
|
||||
if( c>='A' && c<='Z' )
|
||||
c = c - 'A' + 'a';
|
||||
@@ -508,87 +547,24 @@ return c;
|
||||
}
|
||||
|
||||
|
||||
void ToSmall(std::string & s)
|
||||
void ToSmall(std::wstring & s)
|
||||
{
|
||||
std::string::size_type i;
|
||||
std::wstring::size_type i;
|
||||
|
||||
for(i=0 ; i<s.size() ; ++i)
|
||||
s[i] = ToSmall(s[i]);
|
||||
}
|
||||
|
||||
|
||||
bool IsSubString(const char * short_str, const char * long_str)
|
||||
{
|
||||
while( *short_str && *long_str && *short_str == *long_str )
|
||||
{
|
||||
++short_str;
|
||||
++long_str;
|
||||
}
|
||||
|
||||
if( *short_str == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool IsSubString(const std::string & short_str, const std::string & long_str)
|
||||
{
|
||||
return IsSubString(short_str.c_str(), long_str.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool IsSubStringNoCase(const char * short_str, const char * long_str)
|
||||
{
|
||||
while( *short_str && *long_str && ToSmall(*short_str) == ToSmall(*long_str) )
|
||||
{
|
||||
++short_str;
|
||||
++long_str;
|
||||
}
|
||||
|
||||
if( *short_str == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool IsSubStringNoCase(const std::string & short_str, const std::string & long_str)
|
||||
{
|
||||
return IsSubStringNoCase(short_str.c_str(), long_str.c_str());
|
||||
}
|
||||
|
||||
|
||||
bool EqualNoCase(const char * str1, const char * str2)
|
||||
{
|
||||
while( *str1 && *str2 && ToSmall(*str1) == ToSmall(*str2) )
|
||||
{
|
||||
++str1;
|
||||
++str2;
|
||||
}
|
||||
|
||||
if( *str1 == 0 && *str2 == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool EqualNoCase(const std::string & str1, const std::string & str2)
|
||||
{
|
||||
return EqualNoCase(str1.c_str(), str2.c_str());
|
||||
}
|
||||
|
||||
|
||||
bool ValidateEmail(const std::string & email)
|
||||
bool ValidateEmail(const std::wstring & email)
|
||||
{
|
||||
if( email.empty() )
|
||||
return false;
|
||||
|
||||
bool correct = true;
|
||||
size_t i;
|
||||
char allowed_chars[] = "!#$%&'*+-/=?^_`{|}~.@";
|
||||
wchar_t allowed_chars[] = L"!#$%&'*+-/=?^_`{|}~.@";
|
||||
int at = 0;
|
||||
|
||||
for(i=0 ; i<email.length() && correct ; ++i)
|
||||
@@ -625,26 +601,39 @@ return correct;
|
||||
|
||||
|
||||
|
||||
bool IsFile(const char * file)
|
||||
bool IsFile(const wchar_t * file)
|
||||
{
|
||||
struct stat sb;
|
||||
static std::string afile;
|
||||
|
||||
return (stat(file, &sb) == 0);
|
||||
AssignString(file, afile); // or it can be UTF-8 used
|
||||
|
||||
return (stat(afile.c_str(), &sb) == 0);
|
||||
}
|
||||
|
||||
bool IsFile(const std::string & file)
|
||||
bool IsFile(const std::wstring & file)
|
||||
{
|
||||
return IsFile(file.c_str());
|
||||
}
|
||||
|
||||
|
||||
bool CreateDir(const char * dir, int priv)
|
||||
bool CreateDir(const wchar_t * dir, int priv)
|
||||
{
|
||||
static std::string adir;
|
||||
|
||||
if( !IsFile(dir) )
|
||||
{
|
||||
if( mkdir(dir, priv) < 0 )
|
||||
AssignString(dir, adir);
|
||||
|
||||
if( mkdir(adir.c_str(), priv) < 0 )
|
||||
{
|
||||
log << log1 << "Can't create a directory on fs: " << dir << logend;
|
||||
log << log1 << "Can't create a directory on fs: " << adir;
|
||||
|
||||
if( !Equal(dir, adir.c_str()) )
|
||||
log << " original name was: " << dir;
|
||||
|
||||
log << logend;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -653,7 +642,7 @@ return true;
|
||||
}
|
||||
|
||||
|
||||
bool CreateDir(const std::string & dir, int priv)
|
||||
bool CreateDir(const std::wstring & dir, int priv)
|
||||
{
|
||||
return CreateDir(dir.c_str(), priv);
|
||||
}
|
||||
@@ -662,10 +651,10 @@ bool CreateDir(const std::string & dir, int priv)
|
||||
|
||||
// creating directories (can be more than one)
|
||||
// 'dirs' can begin with a slash (will be skipped)
|
||||
bool CreateDirs(const char * base_dir, const char * dirs, int priv)
|
||||
bool CreateDirs(const wchar_t * base_dir, const wchar_t * dirs, int priv)
|
||||
{
|
||||
static std::string temp;
|
||||
const char * p = dirs;
|
||||
static std::wstring temp;
|
||||
const wchar_t * p = dirs;
|
||||
|
||||
temp = base_dir; // we start creating from 'base_dir'
|
||||
|
||||
@@ -698,7 +687,7 @@ return true;
|
||||
|
||||
|
||||
|
||||
bool CreateDirs(const std::string & base_dir, const std::string & dirs, int priv)
|
||||
bool CreateDirs(const std::wstring & base_dir, const std::wstring & dirs, int priv)
|
||||
{
|
||||
return CreateDirs(base_dir.c_str(), dirs.c_str(), priv);
|
||||
}
|
||||
@@ -727,16 +716,20 @@ return true;
|
||||
}
|
||||
|
||||
|
||||
bool CopyFile(const char * src, const char * dst)
|
||||
bool CopyFile(const wchar_t * src, const wchar_t * dst)
|
||||
{
|
||||
static std::string asrc, adst;
|
||||
FILE * in, * out;
|
||||
|
||||
in = fopen(src, "rb");
|
||||
AssignString(src, asrc);
|
||||
AssignString(dst, adst);
|
||||
|
||||
in = fopen(asrc.c_str(), "rb");
|
||||
|
||||
if( !in )
|
||||
return false;
|
||||
|
||||
out = fopen(dst, "wb");
|
||||
out = fopen(adst.c_str(), "wb");
|
||||
|
||||
if( !out )
|
||||
{
|
||||
@@ -753,20 +746,55 @@ FILE * in, * out;
|
||||
res = false;
|
||||
|
||||
if( !res )
|
||||
remove(dst);
|
||||
remove(adst.c_str());
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool CopyFile(const std::string & src, const std::string & dst)
|
||||
bool CopyFile(const std::wstring & src, const std::wstring & dst)
|
||||
{
|
||||
return CopyFile(src.c_str(), dst.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool RemoveFile(const wchar_t * file)
|
||||
{
|
||||
static std::string afile;
|
||||
|
||||
AssignString(file, afile);
|
||||
|
||||
return unlink(afile.c_str()) == 0;
|
||||
}
|
||||
|
||||
|
||||
bool RemoveFile(const std::wstring & file)
|
||||
{
|
||||
return RemoveFile(file.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool RenameFile(const wchar_t * from, const wchar_t * to)
|
||||
{
|
||||
static std::string afrom, ato;
|
||||
|
||||
AssignString(from, afrom);
|
||||
AssignString(to, ato);
|
||||
|
||||
return rename(afrom.c_str(), ato.c_str()) == 0;
|
||||
}
|
||||
|
||||
|
||||
bool RenameFile(const std::wstring & from, const std::wstring & to)
|
||||
{
|
||||
return RenameFile(from.c_str(), to.c_str());
|
||||
}
|
||||
|
||||
|
||||
// if there is not an extension it returns a pointer to the last '\0' character
|
||||
const char * GetFileExt(const char * name)
|
||||
const wchar_t * GetFileExt(const wchar_t * name)
|
||||
{
|
||||
size_t i, ilast;
|
||||
|
||||
@@ -792,28 +820,28 @@ return name + i + 1;
|
||||
}
|
||||
|
||||
|
||||
Item::Auth SelectFileType(const char * file_name)
|
||||
Item::Auth SelectFileType(const wchar_t * file_name)
|
||||
{
|
||||
const char * ext = GetFileExt(file_name);
|
||||
const wchar_t * ext = GetFileExt(file_name);
|
||||
|
||||
// as an image we're using only those types which can be rendered
|
||||
// by a web browser
|
||||
if( EqualNoCase(ext, "jpg") ||
|
||||
EqualNoCase(ext, "jpeg") ||
|
||||
EqualNoCase(ext, "jpe") ||
|
||||
EqualNoCase(ext, "pic") ||
|
||||
EqualNoCase(ext, "tga") ||
|
||||
EqualNoCase(ext, "gif") ||
|
||||
EqualNoCase(ext, "bmp") ||
|
||||
EqualNoCase(ext, "png") )
|
||||
if( EqualNoCase(ext, L"jpg") ||
|
||||
EqualNoCase(ext, L"jpeg") ||
|
||||
EqualNoCase(ext, L"jpe") ||
|
||||
EqualNoCase(ext, L"pic") ||
|
||||
EqualNoCase(ext, L"tga") ||
|
||||
EqualNoCase(ext, L"gif") ||
|
||||
EqualNoCase(ext, L"bmp") ||
|
||||
EqualNoCase(ext, L"png") )
|
||||
return Item::auth_image;
|
||||
|
||||
if( EqualNoCase(ext, "pdf") ||
|
||||
EqualNoCase(ext, "doc") ||
|
||||
EqualNoCase(ext, "xls") ||
|
||||
EqualNoCase(ext, "txt") ||
|
||||
EqualNoCase(ext, "ods") ||
|
||||
EqualNoCase(ext, "odt") )
|
||||
if( EqualNoCase(ext, L"pdf") ||
|
||||
EqualNoCase(ext, L"doc") ||
|
||||
EqualNoCase(ext, L"xls") ||
|
||||
EqualNoCase(ext, L"txt") ||
|
||||
EqualNoCase(ext, L"ods") ||
|
||||
EqualNoCase(ext, L"odt") )
|
||||
return Item::auth_document;
|
||||
|
||||
return Item::auth_other;
|
||||
|
Reference in New Issue
Block a user