00001
00006 #include "include/Configuration.h"
00007 #ifndef CONFIGURATION_H_
00008 #include "include/Debug.h"
00009 #endif
00010 #include "include/generic.h"
00011 #include <iostream>
00012 #include <sstream>
00013 #include <fstream>
00014 #include <vector>
00015 #include <string.h>
00016 #include <stdlib.h>
00017 #include <netdb.h>
00018 #include <netinet/in.h>
00019 #include <arpa/inet.h>
00020
00021
00022
00023 using namespace std;
00024
00025
00027
00031 Configuration::Configuration(string cfile, Debug *dbg, vector<string> sections, vector<string> vars)
00032 {
00033
00034 this->sections = sections;
00035 this->vars = vars;
00036 this->dbg = dbg;
00037
00038 ReadFile(cfile);
00039 Parseconf();
00040 }
00041 void Configuration::LoadSysVars()
00042 {
00043 dbg->Prntdbg(CODEAT, 20, "Load system variables");
00044
00045
00046 if( gethostname(hostname, 128) == 0 )
00047 {
00048
00049 struct hostent * pHost;
00050 struct in_addr **addr_list;
00051
00052
00053 pHost = gethostbyname(hostname);
00054
00055 if (!pHost)
00056 {
00057 string err = "Unknown host ";
00058 err += hostname;
00059 dbg->Prnterr(CODEAT, err);
00060 }
00061 else
00062 {
00063 ipaddr = inet_ntoa(*(struct in_addr*)pHost->h_addr);
00064 if (ipaddr.substr(0,3).compare("127") == 0)
00065 {
00066 addr_list = (struct in_addr **)pHost->h_addr_list;
00067 for(int i = 0; addr_list[i] != NULL; i++) {
00068 ipaddr = inet_ntoa(*addr_list[i]);
00069 if (ipaddr.substr(0,3).compare("127") != 0 && ipaddr.compare("") != 0)
00070 break;
00071 }
00072 }
00073 dbg->Prntdbg(CODEAT, 20, "IP address set to " + ipaddr);
00074 }
00075 }
00076 }
00077
00078 string Configuration::GetHostname()
00079 {
00080
00081 string hn = hostname;
00082 return hn;
00083 }
00084 void Configuration::ReadFile(string cfile)
00085 {
00086
00087
00088 ifstream is;
00089 is.exceptions (ifstream::eofbit | ifstream::failbit | ifstream::badbit);
00090
00091 try
00092 {
00093 is.open (cfile.c_str(), ios::binary);
00094 }
00095 catch (ifstream::failure& e) {
00096 dbg->Prnterr(CODEAT, "Error opening file " + cfile);
00097 exit(-1);
00098 }
00099
00100 try
00101 {
00102
00103 is.seekg (0, ios::end);
00104 content_len = is.tellg();
00105 is.seekg (0, ios::beg);
00106 }
00107 catch (ios_base::failure& e)
00108 {
00109 dbg->Prnterr(CODEAT, "Error getting content length");
00110 exit(-1);
00111 }
00112
00113
00114
00115 try {
00116 content = new char [content_len];
00117 }
00118 catch (std::bad_alloc&)
00119 {
00120 dbg->Prnterr(CODEAT, "Error allocating memory");
00121 exit(-1);
00122 }
00123
00124 try
00125 {
00126
00127 is.read (content, content_len);
00128 }
00129 catch (ios_base::failure& e)
00130 {
00131 dbg->Prnterr(CODEAT, "Error reading file content");
00132 exit(-1);
00133 }
00134 is.close();
00135 }
00136
00137 void Configuration::Parseconf()
00138 {
00139 char * line;
00140 string parse, key, value;
00141 int pos;
00142
00143
00144 line = strtok (content,"\n");
00145 while (line != NULL)
00146 {
00147 parse = line;
00148
00149 parse = parse.substr(0,parse.find_first_of("#"));
00150
00151 pos = parse.find_first_not_of(" ");
00152
00153 if (pos >= 0)
00154 {
00155
00156 parse = parse.substr(parse.find_first_not_of(" "), parse.length());
00157 parse = parse.substr(parse.find_first_not_of("\t"), parse.length());
00158
00159 parse = parse.substr(0,parse.find_last_not_of(" ") + 1);
00160 parse = parse.substr(0,parse.find_last_not_of("\t") + 1);
00161
00162
00163 key = parse.substr(0,parse.find_first_of("="));
00164
00165
00166
00167 if (key.find_first_of("[") == 0)
00168 {
00169
00170 key = key.substr(1,key.length()-2);
00171 value = "section";
00172 }
00173 else
00174 {
00175
00176 value = parse.substr(parse.find_first_of("=") + 1, parse.length() - 1);
00177
00178 value = value.substr(value.find_first_not_of(" "), value.length());
00179 value = value.substr(value.find_first_not_of("\t"), value.length());
00180
00181 key = key.substr(0,key.find_last_not_of(" ") + 1);
00182 key = key.substr(0,key.find_last_not_of("\t") + 1);
00183 }
00184 AddConf(key, value);
00185
00186 }
00187 line = strtok (NULL, "\n");
00188 }
00189
00190
00191 delete[] content;
00192 }
00193
00194 void Configuration::AddConf(string key, string value)
00195 {
00196 if (CheckSyntax(key, value))
00197 config.insert(pair<string, string>(key, value));
00198 else
00199 dbg->Prnterr(CODEAT,"Unknown keyword: " + key);
00200 }
00201
00202 bool Configuration::CheckSyntax(string key, string value)
00203 {
00204
00205 bool correct = false;
00206
00207 if (value.compare("section") == 0)
00208 {
00209
00210 for (unsigned int i = 0; i < sections.size(); i++)
00211 if (key.compare(sections[i]) == 0)
00212 correct = true;
00213 }
00214 else
00215 {
00216
00217 for (unsigned int i = 0; i < vars.size(); i++)
00218 if (key.compare(vars[i]) == 0)
00219 correct = true;
00220 }
00221
00222 return correct;
00223 }
00224 string Configuration::GetValue(string key)
00225 {
00226
00227 string val = "Unknown value: " + key;
00228 if (config[key].compare("") != 0)
00229 val = config[key];
00230
00231 return val;
00232 }
00233
00234 Configuration::~Configuration()
00235 {
00236
00237 }