CPPCHECK: Change arguments to references, add some consts

sql-rework
Tomasz Golinski 2020-02-11 17:34:23 +01:00
rodzic 74efb8fa89
commit 2ab560f172
12 zmienionych plików z 57 dodań i 57 usunięć

4
api.h
Wyświetl plik

@ -11,10 +11,10 @@ public:
// virtual Cache get_cache(std::string code) = 0; // virtual Cache get_cache(std::string code) = 0;
// virtual Caches get_caches(std::vector<std::string> codes) = 0; // virtual Caches get_caches(std::vector<std::string> codes) = 0;
virtual Caches get_user_caches(std::string uuid, int count = 0) = 0; virtual Caches get_user_caches(const std::string& uuid, int count = 0) const = 0;
protected: protected:
Position get_lat_lon(std::string loc) { Position get_lat_lon(std::string loc) const {
int pos = loc.find("|"); int pos = loc.find("|");
return { stof(loc.substr(0, pos)), stof(loc.substr(pos + 1)) }; return { stof(loc.substr(0, pos)), stof(loc.substr(pos + 1)) };
} }

Wyświetl plik

@ -35,7 +35,7 @@ void htmlencode(std::string& data) {
data.swap(tmp); data.swap(tmp);
} }
void show_histogram(const Caches& cc, std::string Cache::*ptr, std::string caption, bool html, bool sort_by_val) { void show_histogram(const Caches& cc, std::string Cache::*ptr, const std::string& caption, bool html, bool sort_by_val) {
std::map<std::string, int> histogram; std::map<std::string, int> histogram;
std::vector<std::pair<std::string, int>> pairs; std::vector<std::pair<std::string, int>> pairs;

Wyświetl plik

@ -4,6 +4,6 @@
void htmlencode(std::string& data); void htmlencode(std::string& data);
void show_histogram(const Caches& cc, std::string Cache::*ptr, std::string caption, bool html = 0, bool sort_by_val = 1); 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); int find_streak(const std::multimap<std::time_t, const Cache*>& cc, std::tm& start);

Wyświetl plik

@ -30,7 +30,7 @@ public:
} }
}; };
GPX::GPX(std::string path) { GPX::GPX(const std::string& path) {
std::ifstream stream(path); std::ifstream stream(path);
if (stream.is_open()) { if (stream.is_open()) {
ReportDebug report; ReportDebug report;
@ -49,7 +49,7 @@ GPX::~GPX() {
delete root; delete root;
} }
Caches GPX::get_user_caches(__attribute__((unused)) std::string uuid, __attribute__((unused)) int count) { Caches GPX::get_user_caches(__attribute__((unused)) const std::string& uuid, __attribute__((unused)) int count) const {
Caches list; Caches list;
for (auto& el : items) { for (auto& el : items) {

6
gpx.h
Wyświetl plik

@ -16,9 +16,9 @@ private:
gpx::GPX* root; gpx::GPX* root;
public: public:
explicit GPX(std::string path); explicit GPX(const std::string& path);
~GPX(); ~GPX();
Cache get_cache(std::string code); Cache get_cache(const std::string& code) const;
Caches get_user_caches(std::string uuid = "", int count = 0); Caches get_user_caches(const std::string& uuid = "", int count = 0) const;
}; };

Wyświetl plik

@ -18,7 +18,7 @@ Heat::Heat(const Map* m) : mp(m) {
#endif #endif
} }
void Heat::generate(std::string filename, Caches& points, int stamp_size, std::string theme) { void Heat::generate(const std::string& filename, const Caches& points, int stamp_size, const std::string& theme) {
heatmap_t* hm = heatmap_new(mp->size_x, mp->size_y); heatmap_t* hm = heatmap_new(mp->size_x, mp->size_y);
heatmap_stamp_t* stamp = heatmap_stamp_gen(stamp_size); heatmap_stamp_t* stamp = heatmap_stamp_gen(stamp_size);
std::vector<unsigned char> image(mp->size_x * mp->size_y * 4); std::vector<unsigned char> image(mp->size_x * mp->size_y * 4);
@ -46,7 +46,7 @@ void Heat::generate(std::string filename, Caches& points, int stamp_size, std::s
// heatmap.write("geostat_heat.png"); // heatmap.write("geostat_heat.png");
} }
void Heat::generate_path(std::string filename, const Date_Caches& sorted) { void Heat::generate_path(const std::string& filename, const Date_Caches& sorted) {
Magick::Image contour(mp->map_file); Magick::Image contour(mp->map_file);
contour.strokeColor("black"); contour.strokeColor("black");

4
heat.h
Wyświetl plik

@ -13,6 +13,6 @@ private:
public: public:
explicit Heat(const Map* m); explicit Heat(const Map* m);
void generate(std::string filename, Caches& points, int stamp_size, std::string theme = "soft"); void generate(const std::string& filename, const Caches& points, int stamp_size, const std::string& theme = "soft");
void generate_path(std::string filename, const Date_Caches& sorted); void generate_path(const std::string& filename, const Date_Caches& sorted);
}; };

2
maps.h
Wyświetl plik

@ -13,7 +13,7 @@ public:
const std::string map_file; const std::string map_file;
Map(int sx, int sy, float lat_mi, float lat_ma, float lon_mi, float lon_ma, std::string file) : size_x(sx), size_y(sy), lat_max(lat_ma), lat_min(lat_mi), lon_max(lon_ma), lon_min(lon_mi), map_file(file){}; Map(int sx, int sy, float lat_mi, float lat_ma, float lon_mi, float lon_ma, const std::string& file) : size_x(sx), size_y(sy), lat_max(lat_ma), lat_min(lat_mi), lon_max(lon_ma), lon_min(lon_mi), map_file(file){};
bool contains(Position pos) const { bool contains(Position pos) const {
return (pos.lon >= lon_min && pos.lon <= lon_max && pos.lat >= lat_min && pos.lat <= lat_max); return (pos.lon >= lon_min && pos.lon <= lon_max && pos.lat >= lat_min && pos.lat <= lat_max);

Wyświetl plik

@ -11,7 +11,7 @@
using json = nlohmann::json; using json = nlohmann::json;
// using namespace std::literals::string_literals; // using namespace std::literals::string_literals;
OCdb::OCdb(std::string db_file) { OCdb::OCdb(const std::string& db_file) {
int res = 0; int res = 0;
res = sqlite3_open(db_file.c_str(), &db); res = sqlite3_open(db_file.c_str(), &db);
@ -32,7 +32,7 @@ OCdb::~OCdb() {
sqlite3_close(db); sqlite3_close(db);
} }
bool OCdb::request(std::string req) { bool OCdb::request(const std::string& req) const {
int res; int res;
res = sqlite3_prepare_v2(db, req.c_str(), req.length() + 1, &stmt, NULL); res = sqlite3_prepare_v2(db, req.c_str(), req.length() + 1, &stmt, NULL);
@ -51,7 +51,7 @@ bool OCdb::request(std::string req) {
return 1; return 1;
} }
bool OCdb::init(std::string dump_path) { bool OCdb::init(const std::string& dump_path) {
request("DELETE FROM caches;"); request("DELETE FROM caches;");
request("DELETE FROM logs;"); request("DELETE FROM logs;");
request("DELETE FROM revision;"); request("DELETE FROM revision;");
@ -70,7 +70,7 @@ bool OCdb::init(std::string dump_path) {
return 1; return 1;
} }
bool OCdb::init_part(std::string json_file) { bool OCdb::init_part(const std::string& json_file) {
Debug(2) << "Processing file: " << json_file << '\n'; Debug(2) << "Processing file: " << json_file << '\n';
std::ifstream file(json_file); std::ifstream file(json_file);
@ -84,7 +84,7 @@ bool OCdb::init_part(std::string json_file) {
return 1; return 1;
} }
bool OCdb::update(Okapi& oc) { bool OCdb::update(const Okapi& oc) {
if (revision == 0) { if (revision == 0) {
Debug(1) << "Cannot update database, you need to init it first with a full dump.\n"; Debug(1) << "Cannot update database, you need to init it first with a full dump.\n";
throw 1; throw 1;
@ -104,7 +104,7 @@ bool OCdb::update(Okapi& oc) {
return 1; return 1;
} }
bool OCdb::parse_item(json j) { bool OCdb::parse_item(const json& j) {
if (j.count("object_type") > 0 && j["object_type"] == "geocache") { if (j.count("object_type") > 0 && j["object_type"] == "geocache") {
if (j.count("change_type") > 0 && j["change_type"] == "replace") { if (j.count("change_type") > 0 && j["change_type"] == "replace") {
Debug(5) << "Inserting/updating cache " << j["object_key"]["code"].get<std::string>() << ".\n"; Debug(5) << "Inserting/updating cache " << j["object_key"]["code"].get<std::string>() << ".\n";
@ -136,7 +136,7 @@ bool OCdb::parse_item(json j) {
return 1; return 1;
} }
bool OCdb::update_cache(json j) { bool OCdb::update_cache(const json& j) {
// (code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status TEXT, size TEXT, difficulty REAL, terrain REAL, country TEXT, region TEXT, owner TEXT) // (code TEXT PRIMARY KEY, name TEXT, location TEXT, type TEXT, status TEXT, size TEXT, difficulty REAL, terrain REAL, country TEXT, region TEXT, owner TEXT)
std::map<std::string, std::string> fields; std::map<std::string, std::string> fields;
std::map<std::string, float> fields2; std::map<std::string, float> fields2;
@ -214,7 +214,7 @@ bool OCdb::update_cache(json j) {
return 1; return 1;
} }
bool OCdb::update_log(json j) { bool OCdb::update_log(const json& j) {
// logs (uuid TEXT PRIMARY KEY, cache_code TEXT, date TEXT, user TEXT, type TEXT);")) // logs (uuid TEXT PRIMARY KEY, cache_code TEXT, date TEXT, user TEXT, type TEXT);"))
std::map<std::string, std::string> fields; std::map<std::string, std::string> fields;
int res; int res;
@ -289,7 +289,7 @@ bool OCdb::read_revision() {
return 1; return 1;
} }
Caches OCdb::get_user_caches_not_found(std::string uuid) { Caches OCdb::get_user_caches_not_found(const std::string& uuid) const {
int res; int res;
Caches cc; Caches cc;
Cache c; Cache c;
@ -321,7 +321,7 @@ Caches OCdb::get_user_caches_not_found(std::string uuid) {
return cc; return cc;
} }
Caches OCdb::get_user_caches(std::string uuid, __attribute__((unused)) int count) { Caches OCdb::get_user_caches(const std::string& uuid, __attribute__((unused)) int count) const {
int res; int res;
Caches cc; Caches cc;
Cache c; Cache c;
@ -360,7 +360,7 @@ Caches OCdb::get_user_caches(std::string uuid, __attribute__((unused)) int count
return cc; return cc;
} }
int OCdb::get_revision() { int OCdb::get_revision() const {
return revision; return revision;
} }

24
ocdb.h
Wyświetl plik

@ -15,25 +15,25 @@ using json = nlohmann::json;
class OCdb : public Api { class OCdb : public Api {
private: private:
sqlite3* db; sqlite3* db;
sqlite3_stmt* stmt; mutable sqlite3_stmt* stmt;
int revision; int revision;
bool request(std::string req); bool request(const std::string& req) const;
bool init_part(std::string json_file); // read db dump bool init_part(const std::string& json_file); // read db dump
bool parse_item(json j); bool parse_item(const json& j);
bool update_cache(json j); bool update_cache(const json& j);
bool update_log(json j); bool update_log(const json& j);
bool read_revision(); bool read_revision();
public: public:
explicit OCdb(std::string db_file); explicit OCdb(const std::string& db_file);
~OCdb(); ~OCdb();
bool init(std::string json_file); // read db dump bool init(const std::string& json_file); // read db dump
bool update(Okapi& oc); // apply changelog bool update(const Okapi& oc); // apply changelog
int get_revision(); int get_revision() const;
void set_revision(int rev); void set_revision(int rev);
Caches get_user_caches_not_found(std::string uuid); Caches get_user_caches_not_found(const std::string& uuid) const;
Caches get_user_caches(std::string uuid, int count = 0); Caches get_user_caches(const std::string& uuid, int count = 0) const;
}; };

Wyświetl plik

@ -17,7 +17,7 @@ static const std::string OKAPI_username = "services/users/by_username";
static const std::string OKAPI_user = "services/users/user"; static const std::string OKAPI_user = "services/users/user";
static const std::string OKAPI_changelog = "services/replicate/changelog"; static const std::string OKAPI_changelog = "services/replicate/changelog";
Okapi::Okapi(std::string server_url, std::string consumer_key) : url(server_url), key(consumer_key) { Okapi::Okapi(const std::string& server_url, const std::string& consumer_key) : url(server_url), key(consumer_key) {
if (url.find(".pl/") != std::string::npos) if (url.find(".pl/") != std::string::npos)
service = "OC.pl"; service = "OC.pl";
if (url.find(".de/") != std::string::npos) if (url.find(".de/") != std::string::npos)
@ -40,7 +40,7 @@ size_t Okapi::write_cb(char* ptr, __attribute__((unused)) size_t size, size_t nm
return nmemb; return nmemb;
} }
std::string Okapi::curl_post(std::string url, std::string post) { std::string Okapi::curl_post(const std::string& url, const std::string& post) const {
CURL* curl; CURL* curl;
CURLcode res; CURLcode res;
@ -76,7 +76,7 @@ std::string Okapi::curl_post(std::string url, std::string post) {
return output; return output;
} }
std::string Okapi::get_user_caches_json(std::string uuid, int count, int offset) { std::string Okapi::get_user_caches_json(const std::string& uuid, int count, int offset) const {
std::string service = url + OKAPI_logs; std::string service = url + OKAPI_logs;
std::string query = "consumer_key=" + key + "&user_uuid=" + uuid + "&fields=cache_code|type|date&limit=" + std::to_string(count) + "&offset=" + std::to_string(offset); std::string query = "consumer_key=" + key + "&user_uuid=" + uuid + "&fields=cache_code|type|date&limit=" + std::to_string(count) + "&offset=" + std::to_string(offset);
return curl_post(service, query); return curl_post(service, query);
@ -88,7 +88,7 @@ std::string Okapi::get_user_caches_json(std::string uuid, int count, int offset)
// return curl_post(service, query); // return curl_post(service, query);
// } // }
std::string Okapi::get_caches_json(std::string codes) { std::string Okapi::get_caches_json(const std::string& codes) const {
std::string service = url + OKAPI_caches; std::string service = url + OKAPI_caches;
std::string query = "consumer_key=" + key + "&cache_codes=" + codes + "&fields=code|name|location|type|status|difficulty|terrain|owner|region|size2|date_hidden|recommendations|founds"; std::string query = "consumer_key=" + key + "&cache_codes=" + codes + "&fields=code|name|location|type|status|difficulty|terrain|owner|region|size2|date_hidden|recommendations|founds";
return curl_post(service, query); return curl_post(service, query);
@ -117,11 +117,11 @@ std::string Okapi::get_caches_json(std::string codes) {
// return c; // return c;
// } // }
Caches Okapi::get_caches(std::map<std::string, std::tm> codes) { Caches Okapi::get_caches(const std::map<std::string, std::tm>& codes) const {
Cache c; Cache c;
Caches cc; Caches cc;
uint k = 0; uint k;
std::string codes_list; std::string codes_list;
codes_list.reserve(500 * 7); // maximum of 500 codes, 6 chars per code plus a separator codes_list.reserve(500 * 7); // maximum of 500 codes, 6 chars per code plus a separator
@ -174,7 +174,7 @@ Caches Okapi::get_caches(std::map<std::string, std::tm> codes) {
return cc; return cc;
} }
Caches Okapi::get_user_caches(std::string uuid, int count) { Caches Okapi::get_user_caches(const std::string& uuid, int count) const {
Caches cc; Caches cc;
std::map<std::string, std::tm> codes; std::map<std::string, std::tm> codes;
json j; json j;
@ -219,21 +219,21 @@ Caches Okapi::get_user_caches(std::string uuid, int count) {
return cc; return cc;
} }
std::string Okapi::get_uuid(std::string username) { std::string Okapi::get_uuid(const std::string& username) const {
std::string service = url + OKAPI_username; std::string service = url + OKAPI_username;
std::string query = "consumer_key=" + key + "&username=" + username + "&fields=uuid"; std::string query = "consumer_key=" + key + "&username=" + username + "&fields=uuid";
json j = json::parse(curl_post(service, query)); json j = json::parse(curl_post(service, query));
return j["uuid"]; return j["uuid"];
} }
std::string Okapi::get_profile_url(std::string uuid) { std::string Okapi::get_profile_url(const std::string& uuid) const {
std::string service = url + OKAPI_user; std::string service = url + OKAPI_user;
std::string query = "consumer_key=" + key + "&user_uuid=" + uuid + "&fields=profile_url"; std::string query = "consumer_key=" + key + "&user_uuid=" + uuid + "&fields=profile_url";
json j = json::parse(curl_post(service, query)); json j = json::parse(curl_post(service, query));
return j["profile_url"]; return j["profile_url"];
} }
std::string Okapi::get_changelog_json(int revision) { std::string Okapi::get_changelog_json(int revision) const {
std::string service = url + OKAPI_changelog; std::string service = url + OKAPI_changelog;
std::string query = "consumer_key=" + key + "&since=" + std::to_string(revision); std::string query = "consumer_key=" + key + "&since=" + std::to_string(revision);
return curl_post(service, query); return curl_post(service, query);

20
okapi.h
Wyświetl plik

@ -14,19 +14,19 @@ private:
std::string service; std::string service;
static size_t write_cb(char* ptr, size_t size, size_t nmemb, void* userdata); static size_t write_cb(char* ptr, size_t size, size_t nmemb, void* userdata);
std::string curl_post(std::string url, std::string post); std::string curl_post(const std::string& url, const std::string& post) const;
std::string get_user_caches_json(std::string uuid, int count = 0, int offset = 0); std::string get_user_caches_json(const std::string& uuid, int count = 0, int offset = 0) const;
// std::string get_cache_json(std::string code); // std::string get_cache_json(std::string code) const;
std::string get_caches_json(std::string codes); std::string get_caches_json(const std::string& codes) const;
public: public:
Okapi(std::string server_url, std::string consumer_key); Okapi(const std::string& server_url, const std::string& consumer_key);
// Cache get_cache(std::string code); // Cache get_cache(std::string code);
Caches get_caches(std::map<std::string, std::tm> codes); Caches get_caches(const std::map<std::string, std::tm>& codes) const;
Caches get_user_caches(std::string uuid, int count = 0); Caches get_user_caches(const std::string& uuid, int count = 0) const;
std::string get_changelog_json(int revision); std::string get_changelog_json(int revision) const;
std::string get_uuid(std::string username); std::string get_uuid(const std::string& username) const;
std::string get_profile_url(std::string uuid); std::string get_profile_url(const std::string& uuid) const;
}; };