-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
73 lines (63 loc) · 1.79 KB
/
utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef UTILS_H
#define UTILS_H
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
#include "lib/json.hpp"
using json = nlohmann::json;
using namespace std;
string exec(string cmd) {
array<char, 128> buffer;
string result;
shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) throw runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
result += buffer.data();
}
return result;
}
json get_json(string url) {
auto raw_json = exec("curl -s --header 'User-Agent: Kanttiinit CLI' '" + url + "'");
return json::parse(raw_json);
}
json get(string endpoint) {
auto raw_json = exec("curl -sS 'https://kitchen.kanttiinit.fi/" + endpoint + "'");
return json::parse(raw_json);
}
struct geo_location {
double latitude;
double longitude;
};
pair<bool, struct geo_location> get_location(string address) {
auto response = get_json("http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" + address);
vector<json> results = response["results"];
if (results.size()) {
auto location = results[0]["geometry"]["location"];
double lat = location["lat"];
double lon = location["lng"];
return make_pair(true, geo_location { lat, lon });
}
return make_pair(false, geo_location {});
}
string to_lower_case(string input) {
locale loc;
string output = "";
for (string::size_type i = 0; i < input.length(); ++i) {
output += tolower(input[i], loc);
}
return output;
}
// from https://stackoverflow.com/a/4654718/1810160
bool is_number(const string& s) {
string::const_iterator it = s.begin();
if (*it == '-') {
it++;
}
while (it != s.end() && isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
#endif