diff options
author | Alex Borisov <borisov.alexandr@rambler.ru> | 2011-11-22 01:32:14 +0200 |
---|---|---|
committer | Alex Borisov <borisov.alexandr@rambler.ru> | 2011-11-22 01:32:14 +0200 |
commit | 17d203d2fcb93673163cde0eda6e28b38007f910 (patch) | |
tree | 1016b09a1b6f22d65856987a7992db4c1ea46675 /client/Config.cpp | |
parent | e555b3102f7be659f53bc24e81bcba90d97aad81 (diff) |
DOwnload/Delete file list request and parsing. FIX bug while reading other configs
Diffstat (limited to 'client/Config.cpp')
-rw-r--r-- | client/Config.cpp | 141 |
1 files changed, 107 insertions, 34 deletions
diff --git a/client/Config.cpp b/client/Config.cpp index a4a6c07..ba62ee0 100644 --- a/client/Config.cpp +++ b/client/Config.cpp @@ -11,7 +11,7 @@ using namespace std; /* - * Firewall Entry class + * FirewallEntry nested class section */ void Config::FirewallEntry::Parse(string entry) { @@ -19,7 +19,7 @@ void Config::FirewallEntry::Parse(string entry) size_t lp1 = 0, lp2 = 0; //extract action part - lp2 = entry.find(" ", lp1); + lp2 = entry.find(' ', lp1); string action = entry.substr(lp1, lp2-lp1); if (action.compare("block") == 0) { @@ -34,14 +34,65 @@ void Config::FirewallEntry::Parse(string entry) Logger::Error("Unknown firewall action! Skipping.\n"); return; } - //extract host part + //extract host part lp1 = lp2 + 1; host.clear(); host = entry.substr(lp1); } + +/* + * FileEntry nested class section + */ +void Config::FileEntry::Parse(string entry, ActionType action) +{ + Logger::Trace("Parsing file action %s\n", entry.c_str()); + + size_t start = 0, end = 0; + if (action == DeleteAction) + { + start = 1; + end = entry.find('"', start); + path = entry.substr(start, end - 1); + } + else if (action == DownloadAction) + { + end = entry.find(' ', start); + path = entry.substr(start, end); + start = end + 1; + md5 = entry.substr(start); + } + else + { + Logger::Error("Unknown FileEntry action!\n"); + } + + Logger::Debug("path: %s, md5: %s\n", path.c_str(), md5.c_str()); +} + + /* - * Config class + * ServerEntry nested class section + */ +Config::ServerEntry::ServerEntry(string entry) +{ + ServerEntry(); + + /* processing server entry e.g.: "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(' '); + retryTimeout = atoi(entry.substr(start, end).c_str()); +} + + +/* + * Config nested class section */ Config::Config(): StaticProxySpeedLow(50) { @@ -270,8 +321,6 @@ void Config::ParseGenericProxies(string data) { proxies.getline(str, str_size, ';'); proxies.ignore(2, '\n'); - if (proxies.eof()) - break; string entry = str; ProxyEntryGeneric proxy; proxy.Parse(entry); @@ -291,8 +340,6 @@ void Config::ParseStaticPorxies(string data) { proxies.getline(str, str_size, ';'); proxies.ignore(2, '\n'); - if (proxies.eof()) - break; string entry = str; ProxyEntryStatic proxy; proxy.Parse(entry); @@ -311,8 +358,6 @@ void Config::ParseFirewalls(string data) { rules.getline(str, str_size, ';'); rules.ignore(2, '\n'); - if (rules.eof()) - break; string entry = str; FirewallEntry rule; rule.Parse(entry); @@ -320,6 +365,58 @@ void Config::ParseFirewalls(string data) } } +void Config::ParseDeleteList(string data) +{ + Logger::Debug("Delete list:\n%s", data.c_str()); + + //delete all entries with DeleteAction + for (unsigned i = 0; i < fileActions.size(); i++) + { + if (fileActions[i].action == FileEntry::DeleteAction) + { + fileActions.erase(fileActions.begin() + i); + } + } + + stringstream files(data, ios_base::in); + const int str_size = 512; + char str[str_size] = {0}; + while (! files.eof()) + { + files.getline(str, str_size, ';'); + files.ignore(2, '\n'); + string entry = str; + FileEntry file; + file.Parse(entry, FileEntry::DeleteAction); + fileActions.push_back(file); + } +} + +void Config::ParseDownloadList(string data) +{ + //delete all entries with DownloadAction + for (unsigned i = 0; i < fileActions.size(); i++) + { + if (fileActions[i].action == FileEntry::DownloadAction) + { + fileActions.erase(fileActions.begin() + i); + } + } + + stringstream files(data, ios_base::in); + const int str_size = 512; + char str[str_size] = {0}; + while (! files.eof()) + { + files.getline(str, str_size, ';'); + //there is no \n or \r\n + string entry = str; + FileEntry file; + file.Parse(entry, FileEntry::DownloadAction); + fileActions.push_back(file); + } +} + /** * @deprecated left for testing purposes, will be removed @@ -374,27 +471,3 @@ void Config::ReadStaticProxy() ParseStaticPorxies(proxies); delete[] buffer; } - - -/*** - * Nested class section - */ -Config::ServerEntry::ServerEntry(): host("127.0.0.1"), timeout(120), retryTimeout(60) -{ -} - -Config::ServerEntry::ServerEntry(string entry) -{ - ServerEntry(); - - /* processing server entry e.g.: "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(' '); - retryTimeout = atoi(entry.substr(start, end).c_str()); -} |