added: default values for variables read from the config file

git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@547 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2010-01-07 14:45:00 +00:00
parent 3f9a46b1f7
commit 59943c87cd
6 changed files with 86 additions and 50 deletions

View File

@ -16,6 +16,7 @@
Config::Config() Config::Config()
{ {
default_str.clear();
default_int = 0; default_int = 0;
default_bool = false; default_bool = false;
@ -94,21 +95,20 @@ void Config::AssignValues()
data.log_file = Text("log_file"); data.log_file = Text("log_file");
data.log_notify_file = Text("log_notify_file"); data.log_notify_file = Text("log_notify_file");
data.fcgi_socket = Text("fcgi_socket"); data.fcgi_socket = Text("fcgi_socket");
data.fcgi_socket_chmod = Int("fcgi_socket_chmod"); data.fcgi_socket_chmod = Int("fcgi_socket_chmod", 0770);
data.fcgi_socket_user = Text("fcgi_socket_user"); data.fcgi_socket_user = Text("fcgi_socket_user");
data.fcgi_socket_group = Text("fcgi_socket_group"); data.fcgi_socket_group = Text("fcgi_socket_group");
data.log_level = Int("log_level"); data.log_level = Int("log_level", 1);
if( !data.stdout_is_closed ) if( !data.stdout_is_closed )
data.log_stdout = Bool("log_stdout"); data.log_stdout = Bool("log_stdout", false);
else else
data.log_stdout = false; data.log_stdout = false;
data.post_file_max = Int("post_file_max"); data.post_file_max = Int("post_file_max", 8388608); // 8 MB
data.static_auth_dir = Text("static_auth_dir"); data.static_auth_dir = Text("static_auth_dir");
data.templates = Text("templates"); data.templates = Text("templates");
data.default_index = Text("default_index");
data.http_session_id_name = Text("http_session_id_name"); data.http_session_id_name = Text("http_session_id_name");
data.db_database = Text("db_database"); data.db_database = Text("db_database");
data.db_user = Text("db_user"); data.db_user = Text("db_user");
@ -127,80 +127,100 @@ void Config::AssignValues()
data.base_url_static = data.base_url_static_prefix + data.base_server; data.base_url_static = data.base_url_static_prefix + data.base_server;
data.base_url_static_auth = data.base_url_static_auth_prefix + data.base_server; data.base_url_static_auth = data.base_url_static_auth_prefix + data.base_server;
data.one_item_is_showed = Bool("one_item_is_showed"); data.priv_no_user = Text("priv_no_user", "-- no user --");
data.priv_no_group = Text("priv_no_group", "-- no group --");
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_max_iddle = Int("session_max_iddle", 10800); // 3h
data.session_remember_max_iddle = Int("session_remember_max_iddle"); data.session_remember_max_iddle = Int("session_remember_max_iddle", 16070400); // 3 months
data.session_file = Text("session_file"); data.session_file = Text("session_file");
data.compression = Bool("compression"); data.compression = Bool("compression", true);
std::string p = Text("plugin"); std::string p = Text("plugin");
data.plugin_file.push_back(p); data.plugin_file.push_back(p);
data.html_filter = Bool("html_filter"); data.html_filter = Bool("html_filter", true);
} }
// !! mozna dodac drugi argument -- wartosc domyslna const std::string & Config::Text(const char * name)
std::string & Config::Text(const char * name) {
return Text(std::string(name), default_str);
}
const std::string & Config::Text(const char * name, const char * def)
{
return Text(std::string(name), std::string(def));
}
const std::string & Config::Text(const std::string & name, const std::string & def)
{ {
ConfParser::Table::iterator i = conf_parser.table.find(name); ConfParser::Table::iterator i = conf_parser.table.find(name);
if( i == conf_parser.table.end() ) if( i == conf_parser.table.end() )
{ return def;
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; return i->second;
} }
// !! mozna dodac drugi argument -- wartosc domyslna
int Config::Int(const char * name) int Config::Int(const char * name)
{
return Int(std::string(name), default_int);
}
int Config::Int(const char * name, int def)
{
return Int(std::string(name), def);
}
int Config::Int(const std::string & name, int def)
{ {
ConfParser::Table::iterator i = conf_parser.table.find(name); ConfParser::Table::iterator i = conf_parser.table.find(name);
if( i == conf_parser.table.end() || i->second.empty() ) if( i == conf_parser.table.end() || i->second.empty() )
{ return def;
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); 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; return res;
} }
// !! mozna dodac drugi argument -- wartosc domyslna
bool Config::Bool(const char * name) bool Config::Bool(const char * name)
{
return Bool(std::string(name), default_bool);
}
bool Config::Bool(const char * name, bool def)
{
return Bool(std::string(name), def);
}
bool Config::Bool(const std::string & name, bool def)
{ {
ConfParser::Table::iterator i = conf_parser.table.find(name); ConfParser::Table::iterator i = conf_parser.table.find(name);
if( i == conf_parser.table.end() ) if( i == conf_parser.table.end() || i->second.empty() )
{ return def;
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_bool; bool res = false;
if( i->second == "true" || i->second == "1" || i->second == "yes" ) if( EqualNoCase(i->second.c_str(), "true") ||
EqualNoCase(i->second.c_str(), "yes") ||
EqualNoCase(i->second.c_str(), "1")
)
res = true; res = true;
log << log3 << "Config: " << name << "=" << (res?"true":"false") << logend;
return res; return res;
} }

View File

@ -33,12 +33,20 @@ private:
void AssignValues(); void AssignValues();
std::string & Text(const char *); const std::string & Text(const char *);
const std::string & Text(const char * name, const char * def);
const std::string & Text(const std::string & name, const std::string & def);
int Int(const char *); int Int(const char *);
int Int(const char * name, int def);
int Int(const std::string & name, int def);
bool Bool(const char *); bool Bool(const char *);
bool Bool(const char * name, bool def);
bool Bool(const std::string & name, bool def);
std::string default_str; std::string default_str;
int default_int; int default_int;
bool default_bool; bool default_bool;

View File

@ -59,7 +59,6 @@ public:
std::string fcgi_socket_group; std::string fcgi_socket_group;
std::string templates; std::string templates;
std::string default_index;
std::string db_database; std::string db_database;
std::string db_user; std::string db_user;
@ -77,12 +76,6 @@ public:
// it's useful when you want to redirect from 'mydomain.tld' into 'www.mydomain.tld' etc. // it's useful when you want to redirect from 'mydomain.tld' into 'www.mydomain.tld' etc.
bool base_url_redirect; bool base_url_redirect;
// if there is one item in a directory
// it will be showed
// (instead of showing directory contents)
// !! wywalic to, nie bedzie uzywane
bool one_item_is_showed;
// string used in a place where is a user (or group) selected // string used in a place where is a user (or group) selected
std::string priv_no_user; std::string priv_no_user;
std::string priv_no_group; std::string priv_no_group;

View File

@ -481,6 +481,21 @@ return false;
} }
bool EqualNoCase(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 && *long_str == 0 )
return true;
return false;
}
bool ValidateEmail(const std::string & email) bool ValidateEmail(const std::string & email)
{ {
if( email.empty() ) if( email.empty() )

View File

@ -55,7 +55,7 @@ const char * ToStr(int value);
bool IsSubString(const char * short_str, const char * long_str); bool IsSubString(const char * short_str, const char * long_str);
bool IsSubStringNoCase(const char * short_str, const char * long_str); bool IsSubStringNoCase(const char * short_str, const char * long_str);
bool EqualNoCase(const char * short_str, const char * long_str);
bool ValidateEmail(const std::string & email); bool ValidateEmail(const std::string & email);

View File

@ -417,7 +417,7 @@ void Templates::ReadTemplates()
using namespace TemplatesFunctions; using namespace TemplatesFunctions;
pat_index.Directory(data.templates); pat_index.Directory(data.templates);
pat_index.ParseFile(data.default_index); pat_index.ParseFile("index.html");
pat_err_404.Directory(data.templates); pat_err_404.Directory(data.templates);
pat_err_404.ParseFile("err_404.html"); pat_err_404.ParseFile("err_404.html");