From 3462cdf8278083fe8b6b76a8bd4285df53e465c3 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 11 Dec 2008 02:48:25 +0000 Subject: [PATCH] I forgot to add config.h and config.cpp into the repository git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@464 e52654a7-88a9-db11-a3e9-0013d4bc506e --- core/config.cpp | 178 ++++++++++++++++++++++++++++++++++++++++++++++++ core/config.h | 50 ++++++++++++++ 2 files changed, 228 insertions(+) create mode 100755 core/config.cpp create mode 100755 core/config.h diff --git a/core/config.cpp b/core/config.cpp new file mode 100755 index 0000000..6b7db51 --- /dev/null +++ b/core/config.cpp @@ -0,0 +1,178 @@ +/* + * 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(); + 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.dir.root.default_item = Int("dir.root.default_item"); +} + + + + +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; +} + + + + + + + + + + + + + + diff --git a/core/config.h b/core/config.h new file mode 100755 index 0000000..e51ec8f --- /dev/null +++ b/core/config.h @@ -0,0 +1,50 @@ +/* + * 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. + * + */ + +#ifndef headerfileconfig +#define headerfileconfig + + +#include "../confparser/confparser.h" +#include "error.h" +#include "data.h" +#include "log.h" + + +class Config +{ + +public: + Config(); + bool ReadConfig(bool errors_to_stdout_); + + + + + +private: + ConfParser conf_parser; + void ShowError(); + void AssignValues(); + + + std::string & Text(const char *); + int Int(const char *); + bool Bool(const char *); + + std::string default_str; + int default_int; + bool default_bool; + + + bool errors_to_stdout; +}; + + +#endif