Split out simple function to check and convert a c-string to a number

sql-rework
Tomasz Golinski 2020-02-23 01:41:15 +01:00
rodzic f4c861867a
commit d1fa8363d5
4 zmienionych plików z 23 dodań i 40 usunięć

Wyświetl plik

@ -130,3 +130,18 @@ int find_streak(const std::multimap<std::time_t, const Cache*>& cc, std::tm& sta
return max_str;
}
long int get_num(char c, char* opt) {
try {
if (std::stoi(opt) > 0) {
return std::stoi(opt);
} else {
std::cout << "Option \"-" << c << "\" requires a valid number as an argument\n";
std::exit(EXIT_FAILURE);
}
}
catch (...) {
std::cout << "Option \"-" << c << "\" requires a valid number as an argument\n";
std::exit(EXIT_FAILURE);
}
}

Wyświetl plik

@ -7,3 +7,5 @@ void htmlencode(std::string& data);
void show_histogram(const Caches& cc, std::string Cache::*ptr, const std::string& caption, bool html = 0, bool sort_by_val = 1);
int find_streak(const std::multimap<std::time_t, const Cache*>& cc, std::tm& start);
long int get_num(char c, char* opt);

Wyświetl plik

@ -80,17 +80,8 @@ int main(int argc, char** argv) {
while ((o = getopt(argc, argv, "qNQg:o::p:d:u:n:r:k:H:s:m:eth?")) != -1)
switch (o) {
// case 'd':
// try {
// if (std::stoi(optarg) > 0) {
// // Debug(1) << "Setting debug level to " << optarg;
// Debug::set_debug_level(std::stoi(optarg));
// }
// }
// catch (...) {
// std::cout << "Option \"-d\" requires a valid number as an argument\n";
// std::exit(EXIT_FAILURE);
// }
// break;
// Debug::set_debug_level(get_num('d',optarg));
// break;
case 'g':
gpx_file = optarg;
break;
@ -136,15 +127,7 @@ int main(int argc, char** argv) {
heat_file = optarg;
break;
case 's':
try {
if (std::stoi(optarg) > 0) {
heat_stamp_size = std::stoi(optarg);
}
}
catch (...) {
std::cout << "Option \"-s\" requires a valid number as an argument\n";
std::exit(EXIT_FAILURE);
}
heat_stamp_size = get_num('s',optarg);
break;
case 'm':
heat_map = optarg;

Wyświetl plik

@ -85,17 +85,8 @@ int main(int argc, char** argv) {
while ((o = getopt(argc, argv, "qNg:o::p:d:u:n:r:k:MDCOH:s:m:etLTYh?")) != -1)
switch (o) {
// case 'd':
// try {
// if (std::stoi(optarg) > 0) {
// // Debug(1) << "Setting debug level to " << optarg;
// Debug::set_debug_level(std::stoi(optarg));
// }
// }
// catch (...) {
// std::cout << "Option \"-d\" requires a valid number as an argument\n";
// std::exit(EXIT_FAILURE);
// }
// break;
// Debug::set_debug_level(get_num('d',optarg));
// break;
case 'g':
gpx_file = optarg;
break;
@ -153,15 +144,7 @@ int main(int argc, char** argv) {
heat_file = optarg;
break;
case 's':
try {
if (std::stoi(optarg) > 0) {
heat_stamp_size = std::stoi(optarg);
}
}
catch (...) {
std::cout << "Option \"-s\" requires a valid number as an argument\n";
std::exit(EXIT_FAILURE);
}
heat_stamp_size = get_num('s',optarg);
break;
case 'm':
heat_map = optarg;