From b7e0b79e7ce53bf467b1bcb8d60e86fac509f776 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 4 Nov 2011 03:02:16 +0200 Subject: config.cfg parser --- client/Config.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 4 deletions(-) (limited to 'client/Config.cpp') diff --git a/client/Config.cpp b/client/Config.cpp index b3c66a4..429d615 100644 --- a/client/Config.cpp +++ b/client/Config.cpp @@ -1,13 +1,12 @@ #include #include -#include "client.h" +#include +#include #include "Config.h" -#include "Proxy.h" using namespace std; - Config* Config::self = NULL; Config *Config::CurrentConfig() @@ -19,6 +18,74 @@ Config *Config::CurrentConfig() Config::Config(): StaticProxySpeedLow(50), configValid(0) { + Logger::Info("Parsing config.cfg to determine initial configuration\n"); + ifstream configFile("config.cfg", std::ios::in); + if (!configFile) + { + Logger::Fatal("Can't open file: config.cfg\n"); + return; + } + + const int str_size = 512; + char str[str_size] = {0}; + while (!configFile.eof()) + { + configFile.getline(str, str_size, ';'); + configFile.ignore(2, '\n'); + if (configFile.eof()) + break; + string entry = str; + size_t eqpos = entry.find('='); + if (eqpos == string::npos) + { + Logger::Warn("No '=' at the config line: %s\n", entry.c_str()); + continue; + } + string key = entry.substr(0, eqpos); + string value = entry.substr(eqpos+1); + Logger::Trace("Found option: %s = %s\n", key.c_str(), value.c_str()); + if (key.compare("config_update_interval") == 0) + { + updateInterval = atoi(value.c_str()); + } + else if (key.compare("client_update_interval") == 0) + { + ClientUpdateInterval = atoi(value.c_str()); + } + else if (key.compare("server") == 0) + { + ServerEntry server(value); + servers.push_back(server); + } + else if (key.compare("welcome_msg") == 0) + { + WelcomeMsg = value; + } + else if (key.compare("config_downloaded_msg") == 0) + { + ConfigLoadedMsg = value; + } + else if (key.compare("top_panel_text") == 0) + { + TopPanelText = value; + } + else if (key.compare("bottom_panel_text") == 0) + { + BottomPanelText = value; + } + else if (key.compare("speed_visibility") == 0) + { + IsSpeedVisible = false; + if (value.compare("true")) + { + IsSpeedVisible = true; + } + } + else + { + Logger::Warn("Unrecognized config option: %s\n", key.c_str()); + } + } } void Config::AcquireConfig() @@ -138,7 +205,6 @@ vector Config::GetStaticProxyGuiLine(unsigned line) swap(staticProxyLine[j], staticProxyLine[j-1]); } } - return staticProxyLine; } @@ -207,4 +273,28 @@ int Config::ReadStaticProxy() } proxyFile.close(); return 0; +} + + +/*** + * Nested class section + */ +Config::ServerEntry::ServerEntry(): host("127.0.0.1"), timeout(120), retry(60) +{ +} + +Config::ServerEntry::ServerEntry(string entry) +{ + ServerEntry(); + + /* processing string: "8.8.8.8 600 60" */ + size_t start = 0, end = 0; + end = entry.find(' '); + host = entry.substr(start, end); + start = end+1; + end = entry.find(' '); + timeout = atoi(entry.substr(start, end).c_str()); + start = end+1; + end = entry.find(' '); + retry = atoi(entry.substr(start, end).c_str()); } \ No newline at end of file -- cgit v1.2.3