summaryrefslogtreecommitdiff
path: root/src/conf/conf.cpp
blob: 2e5893b82b0dd484d01400fdd283437f6e4b1fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#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 ) : 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);

  shashmap<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++ )
  {
    shashmap<string>::del_elem_insecure(iter->first);
    shashmap<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

      shashmap<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
      shashmap<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);
  }
}

//<<*
string
conf::colored_error_msg(string s_key)
{
  return "<font color=\"#"
         + shashmap<string>::get_elem("chat.html.errorcolor")
         + "\">" + shashmap<string>::get_elem(s_key) + "</font><br>\n";
}
//*>>

int
conf::get_int(string s_key)
{
   return tool::string2int(get_elem(s_key));
}

#endif