winix/core/config.cpp

215 lines
4.6 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 "config.h"
#include "log.h"
#include "data.h"
#include "plugin.h"
Config::Config()
{
default_int = 0;
default_bool = false;
errors_to_stdout = true;
}
//!! czy tu w ogole mozemy uzywac log << ?
//!! przeciez jeszcze nie zostal przetworzony plik konfiguracyjny
void Config::ShowError()
{
switch( conf_parser.status )
{
case ConfParser::ok:
log << log2 << "Config: syntax ok" << logend;
break;
case ConfParser::cant_open_file:
if( errors_to_stdout )
std::cout << "Config: cant open a config file: " << data.config_file << std::endl;
log << log1 << "Config: cant open a config file: " << data.config_file << logend;
break;
case ConfParser::syntax_error:
if( errors_to_stdout )
std::cout << "Config: syntax error, line: " << conf_parser.line << std::endl;
log << log1 << "Config: syntax error, line: " << conf_parser.line << logend;
break;
}
}
bool Config::ReadConfig(bool errors_to_stdout_)
{
errors_to_stdout = errors_to_stdout_;
if( data.config_file.empty() )
{
log << log2 << "Config: name of the config file is empty" << logend;
return false;
}
log << log2 << "Config: reading a config file" << logend;
ConfParser::Status status = conf_parser.Parse( data.config_file.c_str() );
if( status == ConfParser::ok )
{
AssignValues();
data.SetAdditionalVariables();
return true;
}
else
{
ShowError();
return false;
}
}
void Config::AssignValues()
{
data.log_file = Text("log_file");
data.log_notify_file = Text("log_notify_file");
data.fcgi_socket = Text("fcgi_socket");
data.fcgi_socket_chmod = Int("fcgi_socket_chmod");
data.fcgi_socket_user = Text("fcgi_socket_user");
data.fcgi_socket_group = Text("fcgi_socket_group");
data.log_level = Int("log_level");
if( !data.stdout_is_closed )
data.log_stdout = Bool("log_stdout");
else
data.log_stdout = false;
data.templates = Text("templates");
data.default_index = Text("default_index");
data.http_session_id_name = Text("http_session_id_name");
data.db_database = Text("db_database");
data.db_user = Text("db_user");
data.db_pass = Text("db_pass");
data.base_url = Text("base_url");
NoLastSlash(data.base_url);
data.one_item_is_showed = Bool("one_item_is_showed");
data.priv_no_user = Text("priv_no_user");
data.priv_no_group = Text("priv_no_group");
data.session_max_iddle = Int("session_max_iddle");
data.session_remember_max_iddle = Int("session_remember_max_iddle");
data.session_file = Text("session_file");
data.compression = Bool("compression");
std::string p = Text("plugin");
data.plugin_file.push_back(p);
}
// !! mozna dodac drugi argument -- wartosc domyslna
std::string & Config::Text(const char * name)
{
ConfParser::Table::iterator i = conf_parser.table.find(name);
if( i == conf_parser.table.end() )
{
log << log2 << "Config: warning: " << name << " is not defined in the config, default will be: \"" << default_str << "\"" << logend;
return default_str;
}
log << log3 << "Config: " << name << "=" << i->second << logend;
return i->second;
}
// !! mozna dodac drugi argument -- wartosc domyslna
int Config::Int(const char * name)
{
ConfParser::Table::iterator i = conf_parser.table.find(name);
if( i == conf_parser.table.end() || i->second.empty() )
{
log << log2 << "Config: warning: " << name << " is not defined in the config, default will be: " << default_int << logend;
return default_int;
}
long res = (i->second[0] == '0')? strtol(i->second.c_str() + 1, 0, 8) : strtol(i->second.c_str(), 0, 10);
log << log3 << "Config: " << name << "=" << res << logend;
return res;
}
// !! mozna dodac drugi argument -- wartosc domyslna
bool Config::Bool(const char * name)
{
ConfParser::Table::iterator i = conf_parser.table.find(name);
if( i == conf_parser.table.end() )
{
log << log2 << "Config: warning: " << name << " is not defined in the config, default will be: " << (default_bool?"true":"false") << logend;
return default_int;
}
bool res = default_int;
if( i->second == "true" || i->second == "1" || i->second == "yes" )
res = true;
log << log3 << "Config: " << name << "=" << (res?"true":"false") << logend;
return res;
}
void Config::NoLastSlash(std::string & s)
{
if( s.empty() )
return;
log << log2 << "Config: removing the last slash from: " << s << logend;
if( *(--s.end()) == '/' )
s.erase(--s.end());
}