From 59943c87cd54cf63c3a269de6deee48ddc151e64 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 7 Jan 2010 14:45:00 +0000 Subject: [PATCH] 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 --- core/config.cpp | 98 +++++++++++++++++++++++++---------------- core/config.h | 12 ++++- core/data.h | 7 --- core/misc.cpp | 15 +++++++ core/misc.h | 2 +- templates/templates.cpp | 2 +- 6 files changed, 86 insertions(+), 50 deletions(-) diff --git a/core/config.cpp b/core/config.cpp index 3c87bd6..5461693 100755 --- a/core/config.cpp +++ b/core/config.cpp @@ -16,6 +16,7 @@ Config::Config() { + default_str.clear(); default_int = 0; default_bool = false; @@ -94,21 +95,20 @@ 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_chmod = Int("fcgi_socket_chmod", 0770); data.fcgi_socket_user = Text("fcgi_socket_user"); 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 ) - data.log_stdout = Bool("log_stdout"); + data.log_stdout = Bool("log_stdout", false); else 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.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"); @@ -127,80 +127,100 @@ void Config::AssignValues() 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.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.priv_no_user = Text("priv_no_user", "-- no user --"); + data.priv_no_group = Text("priv_no_group", "-- no group --"); - data.session_max_iddle = Int("session_max_iddle"); - data.session_remember_max_iddle = Int("session_remember_max_iddle"); + data.session_max_iddle = Int("session_max_iddle", 10800); // 3h + data.session_remember_max_iddle = Int("session_remember_max_iddle", 16070400); // 3 months data.session_file = Text("session_file"); - data.compression = Bool("compression"); + data.compression = Bool("compression", true); std::string p = Text("plugin"); 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 -std::string & Config::Text(const char * name) +const 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); 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 def; return i->second; } -// !! mozna dodac drugi argument -- wartosc domyslna 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); 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; - } + return def; 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) +{ + 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); - 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; - } + if( i == conf_parser.table.end() || i->second.empty() ) + return def; - bool res = default_bool; - - if( i->second == "true" || i->second == "1" || i->second == "yes" ) + bool res = false; + + if( EqualNoCase(i->second.c_str(), "true") || + EqualNoCase(i->second.c_str(), "yes") || + EqualNoCase(i->second.c_str(), "1") + ) res = true; - - log << log3 << "Config: " << name << "=" << (res?"true":"false") << logend; return res; } diff --git a/core/config.h b/core/config.h index 1bfaaf7..5a9f8d4 100755 --- a/core/config.h +++ b/core/config.h @@ -33,12 +33,20 @@ private: 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 * name, int def); + int Int(const std::string & name, int def); + bool Bool(const char *); + bool Bool(const char * name, bool def); + bool Bool(const std::string & name, bool def); std::string default_str; - int default_int; + int default_int; bool default_bool; diff --git a/core/data.h b/core/data.h index fde4ff1..5b674c1 100755 --- a/core/data.h +++ b/core/data.h @@ -59,7 +59,6 @@ public: std::string fcgi_socket_group; std::string templates; - std::string default_index; std::string db_database; 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. 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 std::string priv_no_user; std::string priv_no_group; diff --git a/core/misc.cpp b/core/misc.cpp index 7af0f0f..d9a5913 100755 --- a/core/misc.cpp +++ b/core/misc.cpp @@ -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) { if( email.empty() ) diff --git a/core/misc.h b/core/misc.h index b82a138..4f45d2a 100755 --- a/core/misc.h +++ b/core/misc.h @@ -55,7 +55,7 @@ const char * ToStr(int value); bool IsSubString(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); diff --git a/templates/templates.cpp b/templates/templates.cpp index 3a4ed4f..75d8a6c 100755 --- a/templates/templates.cpp +++ b/templates/templates.cpp @@ -417,7 +417,7 @@ void Templates::ReadTemplates() using namespace TemplatesFunctions; 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.ParseFile("err_404.html");