184 lines
3.7 KiB
C++
Executable File
184 lines
3.7 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 "config.h"
|
|
|
|
|
|
Config::Config()
|
|
{
|
|
default_int = 0;
|
|
default_bool = false;
|
|
|
|
errors_to_stdout = true;
|
|
}
|
|
|
|
|
|
|
|
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 << "cant open a config file: " << data.config_file << std::endl;
|
|
|
|
log << log1 << "cant open a config file: " << data.config_file << logend;
|
|
break;
|
|
|
|
case ConfParser::syntax_error:
|
|
if( errors_to_stdout )
|
|
std::cout << "syntax error a config file: line " << conf_parser.line << std::endl;
|
|
|
|
log << log1 << "config file: 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 << "name of the config file is empty" << logend;
|
|
return false;
|
|
}
|
|
|
|
log << log2 << "reading 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.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");
|
|
data.one_item_is_showed = Bool("one_item_is_showed");
|
|
data.dirs.root.default_item = Int("dir.root.default_item");
|
|
|
|
data.priv_no_user = Text("priv_no_user");
|
|
data.priv_no_group = Text("priv_no_group");
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string & Config::Text(const char * name)
|
|
{
|
|
ConfParser::Table::iterator i = conf_parser.table.find(name);
|
|
|
|
if( i == conf_parser.table.end() )
|
|
{
|
|
log << log2 << "warning: " << name << " is not defined in the config, default will be: \"" << default_str << "\"" << logend;
|
|
return default_str;
|
|
}
|
|
|
|
//log << log3 << name << "=" << i->second << logend;
|
|
|
|
return i->second;
|
|
}
|
|
|
|
|
|
|
|
|
|
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 << "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 << name << "=" << res << logend;
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
bool Config::Bool(const char * name)
|
|
{
|
|
ConfParser::Table::iterator i = conf_parser.table.find(name);
|
|
|
|
if( i == conf_parser.table.end() )
|
|
{
|
|
log << log2 << "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 << name << "=" << (res?"true":"false") << logend;
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|