winix/core/misc.cpp

211 lines
3.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, Tomasz Sowa
* All rights reserved.
*
*/
#include "misc.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 = '_';
}
}
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;
}
void HtmlEscape(std::ostringstream & out, const std::string & in)
{
std::string::const_iterator i;
for(i = in.begin() ; i != in.end() ; ++i)
{
if( *i == '<' )
out << "&lt;";
else
if( *i == '>' )
out << "&gt;";
else
if( *i == '&' )
out << "&amp;";
else
out << *i;
}
}
std::string HtmlEscape(const std::string & in)
{
std::ostringstream out;
HtmlEscape(out, in);
return out.str();
}
const char * DateToStr(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;
sprintf(buffer, "%s.%02d %02d:%02d:%02d", month_letter[month], day, hour, min, sec);
// warning: not thread safe (we do not use threads)
return buffer;
}