winix/core/misc.cpp

414 lines
6.4 KiB
C++
Executable File

/*
* This file is a part of CMSLU -- Content Management System like Unix
* and is not publicly distributed
*
* Copyright (c) 2008-2009, Tomasz Sowa
* All rights reserved.
*
*/
#include "misc.h"
#include "log.h"
void ToString(std::string & s, int value)
{
static char buffer[50];
sprintf(buffer, "%d", value);
s = buffer;
}
void ToString(std::string & s, long value)
{
static char buffer[50];
sprintf(buffer, "%ld", value);
s = buffer;
}
bool CorrectUrlChar(char c)
{
if( (c >= 'a' && c <='z') ||
(c >= 'A' && c <='Z') ||
(c >= '0' && c <='9') ||
(c == '(' || c == ')' || c == '.' || c == ',' || c == '_' )
)
{
return true;
}
return false;
}
void CorrectUrl(Item & item)
{
std::string::iterator i;
for(i = item.url.begin(); i!=item.url.end() ; ++i)
{
if( !CorrectUrlChar(*i) )
{
int c = ChangeLocalChar(*i);
if( CorrectUrlChar(c) )
*i = c;
else
*i = '_';
}
}
ToSmall(item.url);
if( item.url.empty() )
item.url = "bez_nazwy"; // !! wrzucic do pliku konfiguracyjnego
else
if( item.url[0] >= '0' && item.url[0] <= '9' )
// url must begin with a letter
item.url.insert(item.url.begin(), '_');
}
void SetUrlFromSubject(Item & item)
{
item.url = item.subject;
CorrectUrl(item);
/*
std::string::iterator i;
item.url.clear();
for(i = item.subject.begin(); i!=item.subject.end() ; ++i)
{
int c = ChangeLocalChar(*i);
if( (c >= 'a' && c <='z') ||
(c >= 'A' && c <='Z') ||
(c >= '0' && c <='9') ||
(c == '(' || c == ')' || c == '.' || c == ',' || c == '_' )
)
{
item.url += c;
}
else
item.url += '_';
}
if( item.url.empty() )
item.url = "bez_nazwy"; // !! wrzucic do pliku konfiguracyjnego
else
if( item.url[0] >= '0' && item.url[0] <= '9' )
// url must begin with a letter
item.url.insert(item.url.begin(), '_');
*/
}
int polish_letters_simple[] =
{ 'a', 'A',
'c', 'C',
's', 'S',
'e', 'E',
'l', 'L',
'o', 'O',
'z', 'Z',
'z', 'Z',
'n', 'N', 0 }; // 0 - terminating
int polish_letters_iso88592[] =
{ 0xb1, 0xa1,
0xe6, 0xc6,
0xb6, 0xa6,
0xea, 0xca,
0xb3, 0xa3,
0xf3, 0xd3,
0xbf, 0xaf,
0xbc, 0xac,
0xf1, 0xd1, 0 };
int ChangeLocalChar(unsigned char c)
{
// if( language == polish_iso88592 )
for(int i = 0 ; polish_letters_iso88592[i] ; ++i)
{
if( polish_letters_simple[i] == 0 )
{
log << log1 << "localization tables don't have the same size" << logend;
return c;
}
if( polish_letters_iso88592[i] == c )
return polish_letters_simple[i];
}
return c;
}
bool HtmlTryChar(std::ostringstream & out, int c)
{
if( c == '<' )
{
out << "&lt;";
return true;
}
else
if( c == '>' )
{
out << "&gt;";
return true;
}
else
if( c == '&' )
{
out << "&amp;";
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
out << "<p>";
for(i = in.begin() ; i != in.end() ; ++i)
{
if( !HtmlTryChar(out, *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>";
out << *i;
was_enter = 0;
}
}
}
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, 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];
--month;
if( month < 0 )
month = 0;
if( month > 11 )
month = 11;
if( year == 0 )
sprintf(buffer, "%s.%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);
// warning: not thread safe (we do not use threads)
return buffer;
}
const char * DateToStr(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(time_t t)
{
tm * ptm = std::localtime(&t);
return DateToStr(ptm);
}
const char * IpToStr(unsigned int ip_)
{
static char buffer[100];
union
{
unsigned int ip;
unsigned char c[4];
} ip;
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]);
return buffer;
}
bool IsWhite(int s)
{
if( s==' ' || s=='\t' || s==13 )
return true;
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);
}
const char * SkipWhite(const char * s)
{
while( IsWhite(*s) )
++s;
return s;
}
void ToSmall(std::string & s)
{
std::string::size_type i;
for(i=0 ; i<s.size() ; ++i)
{
if( s[i]>='A' && s[i]<='Z' )
s[i] = s[i] - 'A' + 'a';
}
}
bool ValidateEmail(const std::string & email)
{
if( email.empty() )
return false;
bool correct = true;
size_t i;
char allowed_chars[] = "!#$%&'*+-/=?^_`{|}~.@";
int at = 0;
for(i=0 ; i<email.length() && correct ; ++i)
{
correct = false;
if( (email[i] >= 'A' && email[i]<='Z') ||
(email[i] >= 'a' && email[i]<='z') ||
(email[i] >= '0' && email[i]<='9') )
{
correct = true;
}
else
{
for(size_t a=0 ; a < sizeof(allowed_chars)-1 ; ++a)
{
if( email[i] == allowed_chars[a] )
{
correct = true;
break;
}
}
}
if( email[i] == '@' )
++at;
}
if( at != 1 )
return false;
return correct;
}