commit e4bde2e7838fa6bdbfe19798335def000027d0f4 Author: marcelb Date: Sun May 21 09:06:21 2023 +0200 Tested diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0bd7bcb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "*.tcc": "cpp", + "compare": "cpp", + "type_traits": "cpp" + } +} \ No newline at end of file diff --git a/example/config.cfg b/example/config.cfg new file mode 100644 index 0000000..7a98b7f --- /dev/null +++ b/example/config.cfg @@ -0,0 +1,13 @@ +Username=ivanm; +API=ZahgxFbYZTNheEQxvupTR1bt1eRC820GVStWXJhMHJ0xsBmMNBNQ3v22TNEwvSg; +Domain=itasel.ml; + +# dont touch this +consolePrintLogs=true; +DinioServer1=localhost; #ns-hl.ddns.net +DinioServer2=localhost; #lab-it.ddns.net; +DinioServer1Port=5000; +DinioServer2Port=4048; +DinioGetIPURL=http://lab-it.ddns.net/getip/index.php; #ovo ide na više A recorda i više servera pod istim domenom +# DinioGetIPURL2=http://ns-hl.ddns.net/getip/index.php; + diff --git a/lib/config.hpp b/lib/config.hpp new file mode 100644 index 0000000..c4de4c0 --- /dev/null +++ b/lib/config.hpp @@ -0,0 +1,30 @@ +#ifndef _CFG_ +#define _CFG_ + +#include +#include +#include +#include +#include + +using namespace std; + +void clearWhiteSpaces(string &a); +bool clearComments(string &a); +void parseConfigLine(const string a, string &b, string &c); + +class config { + public: + vector necessary; // = {"Username", "API", "Domain", "consolePrintLogs", "DinioServer1", "DinioServer2", "DinioGetIPURL", "DinioServer1Port", "DinioServer2Port" }; + map element; + + config(const string _configFilePath, const vector _necessary = {}); + + void print(); + bool init(const string _configFilePath); + bool isHaveNecessary(); + +}; + + +#endif \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..fec1f8c --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,125 @@ +#include "../lib/config.hpp" + + +config::config(const string _configFilePath, const vector _necessary) { + necessary = _necessary; + if(!init(_configFilePath)) { + cout << "ERROR init config file" << endl; + exit(1); + } + if (!isHaveNecessary()) { + cout << "ERROR Konfiguracijska datoteka nema sva nužna polja!" << endl; + exit(2); + } +} + + +/** +* Otvaram config.cfg datoteku, čitam je i inicijaliziram map kontenjer configs prema cnf_fields +*/ + +bool config::init(const string _configFilePath) { + + ifstream configFile; + configFile.open(_configFilePath, ios::in); + if(!configFile) { + printf ("[CRITICAL ERROR] Nema konfiguracijske datoteke!"); + return false; + } + + else { + for (string line; getline(configFile, line); ) { + clearWhiteSpaces(line); + if(clearComments(line) && !line.empty()) { // samo ako nije komentar + string key, value; + parseConfigLine(line, key, value); + element[key] = value; + } + } + } + + configFile.close(); + return true; +} + + +/** + * @brief Funkcija ispisuje configs kontejner - KORISTIMO SAMO ZA RAZVOJ I DEBUG + * + */ +void config::print() { + for(auto i : element) { + cout << i.first << " " << i.second << "\n"; + } + +} + +/** + * +*/ + +bool config::isHaveNecessary() { + bool necessaryHave = true; + for (int i=0; i + +#include "../lib/config.hpp" + +using namespace std; + +int main() { + + config mycfg ("../example/config.cfg", {"Username", "API", "Domain" }); + + // cout << "HAloooo" << endl; + cout << mycfg.element["consolePrintLogs"]; + + + return 0; +} \ No newline at end of file diff --git a/test/test.o b/test/test.o new file mode 100755 index 0000000..50856ad Binary files /dev/null and b/test/test.o differ