marcelb 1 year ago
commit e4bde2e783
  1. 7
      .vscode/settings.json
  2. 13
      example/config.cfg
  3. 30
      lib/config.hpp
  4. 125
      src/config.cpp
  5. 1
      test/compile.sh
  6. 16
      test/test.cpp
  7. BIN
      test/test.o

@ -0,0 +1,7 @@
{
"files.associations": {
"*.tcc": "cpp",
"compare": "cpp",
"type_traits": "cpp"
}
}

@ -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;

@ -0,0 +1,30 @@
#ifndef _CFG_
#define _CFG_
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
using namespace std;
void clearWhiteSpaces(string &a);
bool clearComments(string &a);
void parseConfigLine(const string a, string &b, string &c);
class config {
public:
vector<string> necessary; // = {"Username", "API", "Domain", "consolePrintLogs", "DinioServer1", "DinioServer2", "DinioGetIPURL", "DinioServer1Port", "DinioServer2Port" };
map<string, string> element;
config(const string _configFilePath, const vector<string> _necessary = {});
void print();
bool init(const string _configFilePath);
bool isHaveNecessary();
};
#endif

@ -0,0 +1,125 @@
#include "../lib/config.hpp"
config::config(const string _configFilePath, const vector<string> _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<necessary.size(); i++) {
if (element[necessary[i]].empty()) {
necessaryHave = false;
break;
}
}
return necessaryHave;
}
/**
* @brief Čišćenje učitanih redova config datoteke od bijelih polja
*
* @param a Prima string reda config datoteke
*/
void clearWhiteSpaces(string &a) {
const char whitespaces[] = {' ', '\t'};
for (int i=0; i<sizeof(whitespaces)/sizeof(const char); i++) {
for (int j=0; j<a.length(); j++) {
if (a[j] == whitespaces[i]) {
a.erase(j, 1);
}
}
}
}
/**
* @brief Funkcija čisti komentare iz linije
*
* @param a string linije config datoteke
* @return true - cijela linija nije komentar
* @return false - cijela je linija komentar
*/
bool clearComments(string &a) {
bool r = a[0] != '#';
size_t commentLocation = a.find('#');
if(commentLocation <= a.length()) {
a = a.substr(0, commentLocation);
}
return r;
}
/**
* @brief Raskopavam konfiguracijsku liniju
*
* @param a String linije
* @param b Ključ
* @param c Vrijednost ključa
*/
void parseConfigLine(const string a, string &b, string &c) {
size_t separatorLocation = a.find('=');
b = a.substr(0, separatorLocation);
string t;
t = a.substr(separatorLocation + 1, a.length()-separatorLocation-1);
separatorLocation = t.find(';');
c = t.substr(0, separatorLocation);
}

@ -0,0 +1 @@
g++ test.cpp ../src/* -o test.o

@ -0,0 +1,16 @@
#include <iostream>
#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;
}

Binary file not shown.
Loading…
Cancel
Save