hathor-cpp-scanner/config.cc

116 lines
3.3 KiB
C++

#include "config.hh"
json parse_file(const string &filename) {
json j_JSON;
ifstream f_JSON;
string f_line;
stringstream s_JSON;
f_JSON.open(filename);
if (f_JSON.is_open()) {
while ( getline (f_JSON,f_line) ) {
s_JSON << f_line;
}
f_JSON.close();
} else {
char * buf;
asprintf(&buf, "Could not read from file : %s", filename.c_str());
perror( buf );
delete buf;
exit(1);
}
try {
j_JSON = json::parse(s_JSON);
} catch(const invalid_argument &e) {
printf( "%s in %s.\n", e.what(), filename.c_str() );
exit(EXIT_FAILURE);
}
f_JSON.close();
return j_JSON;
}
scanner_config hathor_config(json &j_JSON) {
scanner_config opts;
json mysql, scanner, php;
mysql = j_JSON["mysql"];
scanner = j_JSON["scanner"];
php = j_JSON["php"];
opts.mysql.host = mysql["host"].get<string>();
opts.mysql.db = mysql["db"].get<string>();
opts.mysql.user = mysql["user"].get<string>();
opts.mysql.pass = mysql["pass"].is_null() ? "null" : mysql["pass"].get<string>();
opts.scanner.path = scanner["path"].get<string>();
opts.php.root = php["root"].get<string>();
opts.php.user = php["user"].get<string>();
opts.php.email = php["email"].get<string>();
opts.php.pass = php["pass"].is_null() ? "null" : php["pass"].get<string>();
if( mysql["port"].is_number() ) {
opts.mysql.port = mysql["port"];
}
else {
try {
string port = mysql["port"];
opts.mysql.port = stoi( port, nullptr, 0 );
} catch(exception& e) {
opts.mysql.port = 3306;
}
}
if( scanner["throttle"].is_number() ) {
opts.scanner.throttle = scanner["throttle"];
}
else {
try {
string throttle = scanner["throttle"];
opts.scanner.throttle = stoi( throttle,nullptr,0 );
} catch(exception& e) {
opts.scanner.throttle = 0;
} // try
} // else
if( strcmp(opts.mysql.host.c_str(), "null") == 0 ) {
opts.mysql.host = "localhost";
}
if( strcmp(opts.mysql.db.c_str(), "null") == 0 ) {
opts.mysql.db = "hathor";
}
try {
if( strcmp(opts.mysql.user.c_str(), "null") == 0 ) {
throw domain_error("Required values are missing from config.json : 'mysql:{user}'");
}
//else if( strcmp(opts.mysql.pass.c_str(), "null") == 0 ) {
// throw domain_error("Required values are missing from config.json : 'mysql:{pass}'");
//}
else if( strcmp(opts.scanner.path.c_str(), "null") == 0 ) {
throw domain_error("Required values are missing from config.json : 'scanner:{path}'");
}
else if( strcmp(opts.php.user.c_str(), "null") == 0 ) {
throw domain_error("Required values are missing from config.json : 'php:{user}'");
}
//else if( strcmp(opts.php.pass.c_str(), "null") == 0 ) {
// throw domain_error("Required values are missing from config.json : 'php:{pass}'");
// }
else if( strcmp(opts.php.email.c_str(), "null") == 0 ) {
throw domain_error("Required values are missing from config.json : 'php:{email}'");
}
else if( strcmp(opts.php.root.c_str(), "null") == 0 ) {
throw domain_error("Required values are missing from config.json : 'php:{root}'");
} //if
} catch(exception &e) {
cout << e.what() << endl;
} //try
return opts;
} // hathor_config