diff options
| author | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:41 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2013-04-06 13:14:41 +0200 |
| commit | 9cd3ccffd5372dfde3af478e3f832f18db4be3f1 (patch) | |
| tree | 631c295a4a4a16b57502b847626763a279bf6df7 /ychat-0.7.0/src/conf | |
| parent | 13aaf70af703748fe096e0664c305cd202637ad2 (diff) | |
tagging tags
Diffstat (limited to 'ychat-0.7.0/src/conf')
| -rwxr-xr-x | ychat-0.7.0/src/conf/conf.cpp | 155 | ||||
| -rwxr-xr-x | ychat-0.7.0/src/conf/conf.h | 27 | ||||
| -rwxr-xr-x | ychat-0.7.0/src/conf/lang.cpp | 67 | ||||
| -rwxr-xr-x | ychat-0.7.0/src/conf/lang.h | 23 |
4 files changed, 272 insertions, 0 deletions
diff --git a/ychat-0.7.0/src/conf/conf.cpp b/ychat-0.7.0/src/conf/conf.cpp new file mode 100755 index 0000000..6670f66 --- /dev/null +++ b/ychat-0.7.0/src/conf/conf.cpp @@ -0,0 +1,155 @@ +// class conf implementation. + +#ifndef CONF_CPP +#define CONF_CPP + +#include <fstream> +#include "conf.h" + +using namespace std; + +conf::conf( string s_conf, map<string,string>* p_start_params ) : nmap<string,string>::nmap(HMAPOCC), name::name( s_conf ) +{ + string s_check[] = { + get_name(), + string(getenv("HOME"))+string("/.ychat/") + get_name(), + string("./etc/") + get_name(), + string("/etc/") + get_name(), + string(PREFIX+string("etc/")+get_name()) }; + + string s_config; + + for ( int i = 0; i<4; i++ ) + { + cout << "Checking for " << s_check[i]; + ifstream if_check( s_check[i].c_str() ); + if( if_check ) + { + s_config = s_check[i]; + if_check.close(); + cout << "... ok!" << endl; + break; + } + cout << "... not ok!" << endl; + } + + if ( s_config.empty() ) + { + cout << CFILEFA << endl; + exit(1); + } + + else + { + cout << CFILEOK << "..." << endl; + } + + p_xml = new TiXmlDocument(s_config.c_str()); + TiXmlBase::SetCondenseWhiteSpace( false ); + + if ( !p_xml->LoadFile() ) + { + cout << XMLER1 << endl; + exit(1); + } + + vector<string> vec_string; + parse_xml(p_xml, &vec_string); + + nmap<string,string>::add_elem_insecure(tool::ychat_version(), "ychat.version"); + + // Overrides ychat.conf values with command line options (ychat -o key1 value1 -o key2 value2 ...) + map<string,string>::iterator iter; + for ( iter = p_start_params->begin(); iter != p_start_params->end(); iter++ ) + { + nmap<string,string>::del_elem_insecure(iter->first); + nmap<string,string>::add_elem_insecure(iter->second, iter->first); + } + + delete p_xml; +} + +void +conf::parse_xml(TiXmlNode* p_node, vector<string>* p_vec) +{ + if ( p_node->NoChildren() ) + return; + + for ( TiXmlNode* p_child = p_node->FirstChild(); p_child; p_child = p_child->NextSibling() ) + { + //cout << p_vec->size() << ": (Value:" << p_child->Value() << ") (Type:" << p_child->Type() << ")" << endl; + + if ( strcmp(p_child->Value(),"category") == 0) + { + p_vec->push_back(p_child->ToElement()->Attribute("name")); + parse_xml(p_child, p_vec); + p_vec->pop_back(); + } + + else if ( strcmp(p_child->Value(),"option") == 0) + { + string s_option_name = ""; + + for ( vector<string>::iterator iter = p_vec->begin(); iter != p_vec->end(); ++iter ) + s_option_name.append(*iter + "."); + + TiXmlElement* p_element = p_child->ToElement(); + exit_if_xml_error(); + + s_option_name.append(p_element->Attribute("name")); + +#ifdef VERBOSE + cout << XMLREAD << s_option_name; +#endif + + TiXmlNode *p_node_value = p_element->FirstChild("value"); + TiXmlNode *p_node_descr = p_element->FirstChild("descr"); + + TiXmlText* p_text_value = NULL; + TiXmlText* p_text_descr = NULL; + + if ( p_node_value && p_node_value->FirstChild() ) + p_text_value = p_node_value->FirstChild()->ToText(); + + if ( p_node_descr && p_node_descr->FirstChild() ) + p_text_descr = p_node_descr->FirstChild()->ToText(); + + if ( !p_text_value ) + continue; + +#ifdef VERBOSE + cout << " := " << p_text_value->Value() << endl; +#endif + + nmap<string,string>::add_elem_insecure(p_text_value->Value(), s_option_name); + + if ( !p_text_descr ) + continue; + + s_option_name.append(".descr"); +#ifdef VERBOSE + cout << XMLREAD << s_option_name << endl; +#endif + nmap<string,string>::add_elem_insecure(p_text_descr->Value(), s_option_name); + } + } + + exit_if_xml_error(); +} + +conf::~conf() +{ + delete p_xml; +} + +void +conf::exit_if_xml_error() const +{ + if ( p_xml->Error() ) + { + cout << XMLERR << p_xml->ErrorDesc() << endl; + exit(1); + } +} + +#endif diff --git a/ychat-0.7.0/src/conf/conf.h b/ychat-0.7.0/src/conf/conf.h new file mode 100755 index 0000000..7508872 --- /dev/null +++ b/ychat-0.7.0/src/conf/conf.h @@ -0,0 +1,27 @@ +// class conf declaration. this class parses the server config file. + +#ifndef CONF_H +#define CONF_H + +class conf; + +#include "../incl.h" +#include "../maps/nmap.h" +#include "../name.h" +#include "../contrib/xml/tinyxml.h" + +using namespace std; + +class conf : public nmap<string,string>, name +{ +private: + TiXmlDocument* p_xml; + void exit_if_xml_error() const; + void parse_xml( TiXmlNode* p_node, vector<string>* p_vec); +public: + //conf ( string s_conf ); + conf ( string s_conf, map<string,string>* p_start_params ); + ~conf(); +}; + +#endif diff --git a/ychat-0.7.0/src/conf/lang.cpp b/ychat-0.7.0/src/conf/lang.cpp new file mode 100755 index 0000000..04dd13b --- /dev/null +++ b/ychat-0.7.0/src/conf/lang.cpp @@ -0,0 +1,67 @@ +#ifndef LANG_CPP +#define LANG_CPP + +#include <fstream> +#include "lang.h" + +using namespace std; + +lang::lang( string s_lang = "en" ) : nmap<string,string>::nmap(HMAPOCC), name::name( s_lang ) +{ + parse( ); // parse the config file. +} + +lang::~lang() +{} + +void +lang::parse() +{ + // wrap::system_message( CFILEOK + get_name() ); + + string filename(wrap::CONF->get_elem("LANGUAGE_DIR")); + filename.append(get_name()+".txt"); + + ifstream fs_conf( filename.c_str() ); + + if ( ! fs_conf ) + { + // wrap::system_message( CFILEOFF + get_name() ); + return; + } + + char c_buf[READBUF]; + + while( fs_conf.getline( c_buf, READBUF ) ) + { + string s_token( c_buf ); + unsigned int ui_pos = s_token.find( "#", 0 ); + + // if line is commented out: + if ( ui_pos == 0 ) + continue; + + ui_pos = s_token.find( ";", 0 ); + + // if token has not been found. + if ( ui_pos == string::npos ) + continue; + + s_token = s_token.substr( 0 , --ui_pos ); + ui_pos = s_token.find ( "\"", 0 ); + + if ( ui_pos == string::npos ) + continue; + + string s_val = s_token.substr( ui_pos+1, s_token.length() ); + string s_key = s_token.substr( 0 , --ui_pos ); + + // fill the map. + nmap<string,string>::add_elem(s_val, s_key ); + } + + fs_conf.close(); + fs_conf.~ifstream(); +} + +#endif diff --git a/ychat-0.7.0/src/conf/lang.h b/ychat-0.7.0/src/conf/lang.h new file mode 100755 index 0000000..6230e2f --- /dev/null +++ b/ychat-0.7.0/src/conf/lang.h @@ -0,0 +1,23 @@ +#include "../incl.h" + +#ifndef LANG_H +#define LANG_H + +#include "../name.h" +#include "../maps/nmap.h" + +using namespace std; + +class lang : public nmap<string,string>, name +{ +private: + +public: + // public methods: + lang ( string s_lang ); // standard constructor. + ~lang(); // standard destructor. + + virtual void parse( ); // parses the config file. +}; + +#endif |
