code reformat

This commit is contained in:
max mustermann 2019-05-08 19:31:49 +02:00
parent 31819abcaf
commit c738a01ce4
9 changed files with 35 additions and 28 deletions

1
.gitignore vendored
View File

@ -20,6 +20,7 @@
.idea/**/dynamic.xml .idea/**/dynamic.xml
.idea/**/uiDesigner.xml .idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml .idea/**/dbnavigator.xml
.idea/**/*.xml
# Gradle # Gradle
.idea/**/gradle.xml .idea/**/gradle.xml

View File

@ -11,7 +11,7 @@
void Logger::safeip(std::string ip) { void Logger::safeip(std::string ip) {
std::ofstream out; std::ofstream out;
out.open("ip.txt",std::ios::out); out.open("ip.txt", std::ios::out);
out << ip; out << ip;
@ -20,7 +20,7 @@ void Logger::safeip(std::string ip) {
std::string Logger::readip() { std::string Logger::readip() {
std::ifstream in; std::ifstream in;
in.open("ip.txt",std::ios::in); in.open("ip.txt", std::ios::in);
std::string ip; std::string ip;
@ -31,15 +31,15 @@ std::string Logger::readip() {
void Logger::logToLogfile(std::string text) { void Logger::logToLogfile(std::string text) {
std::ofstream out; std::ofstream out;
out.open("dynurefresher.log",std::ios::out | std::ios::app); out.open("dynurefresher.log", std::ios::out | std::ios::app);
std::time_t t = std::time(0); // get time now std::time_t t = std::time(0); // get time now
std::tm* now = std::localtime(&t); std::tm *now = std::localtime(&t);
std::stringstream logline; std::stringstream logline;
logline << "[ "<< (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday logline << "[ " << (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday
<< "_" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << " ] " << '\t' << text << std::endl; << "_" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << " ] " << '\t' << text << std::endl;

View File

@ -9,7 +9,9 @@
class Logger { class Logger {
public: public:
void logToLogfile(std::string text); void logToLogfile(std::string text);
void safeip(std::string ip); void safeip(std::string ip);
std::string readip(); std::string readip();
}; };

View File

@ -12,13 +12,14 @@
std::string API::request(std::string myurl) { std::string API::request(std::string myurl) {
Hashmap<std::string,std::string> map; Hashmap<std::string, std::string> map;
std::vector<std::string> str; std::vector<std::string> str;
return request(std::move(myurl),false,map,str); return request(std::move(myurl), false, map, str);
} }
std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::string> &map, std::vector<std::string> &headers) { std::string
API::request(std::string myurl, bool post, Hashmap<std::string, std::string> &map, std::vector<std::string> &headers) {
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
@ -40,12 +41,12 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
if(post){ if (post) {
std::stringstream poststring; std::stringstream poststring;
poststring << "{"; poststring << "{";
for (int i = 0; i < map.size(); i++) { for (int i = 0; i < map.size(); i++) {
poststring << "\""+map.getKey(i) << "\": \""+map.getValue(i) << "\""; poststring << "\"" + map.getKey(i) << "\": \"" + map.getValue(i) << "\"";
if(i < map.size()-1){ if (i < map.size() - 1) {
poststring << ", "; poststring << ", ";
} }
} }
@ -53,20 +54,19 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, poststring.str().c_str()); curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, poststring.str().c_str());
}else{ } else {
std::string getstring; std::string getstring;
for(int i =0; i< map.size();i++){ for (int i = 0; i < map.size(); i++) {
getstring+=map.getKey(i)+"="+map.getValue(i); getstring += map.getKey(i) + "=" + map.getValue(i);
if(i < map.size()-1){ if (i < map.size() - 1) {
getstring+="&"; getstring += "&";
} }
} }
myurl+="?"+getstring; myurl += "?" + getstring;
} }
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readString); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readString);
@ -78,6 +78,6 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
} }
size_t API::write_data(void *contents, size_t size, size_t nmemb, FILE *stream) { size_t API::write_data(void *contents, size_t size, size_t nmemb, FILE *stream) {
((std::string*)stream)->append((char*)contents, size * nmemb); ((std::string *) stream)->append((char *) contents, size * nmemb);
return size * nmemb; return size * nmemb;
} }

View File

@ -12,7 +12,8 @@
class API { class API {
public: public:
std::string request(std::string myurl); std::string request(std::string myurl);
std::string request(std::string myurl, bool post, Hashmap<std::string,std::string> &map,std::vector<std::string> &headers);
std::string request(std::string myurl, bool post, Hashmap<std::string, std::string> &map, std::vector<std::string> &headers);
private: private:
static size_t write_data(void *buffer, size_t size, size_t buffersize, FILE *stream); static size_t write_data(void *buffer, size_t size, size_t buffersize, FILE *stream);

View File

@ -9,10 +9,13 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
template <class keytype,class valuetype> class Hashmap { template<class keytype, class valuetype>
class Hashmap {
public: public:
void add(keytype key,keytype value); void add(keytype key, keytype value);
keytype getKey(int position); keytype getKey(int position);
valuetype getValue(int position); valuetype getValue(int position);
int size(); int size();

View File

@ -5,6 +5,6 @@
#include "TelegramAPI.h" #include "TelegramAPI.h"
void TelegramAPI::sendMessage(std::string text) { void TelegramAPI::sendMessage(std::string text) {
std::string reply = request("https://api.telegram.org/bot"+ apikey + "/sendmessage?chat_id="+chatid+"&text="+text); std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage?chat_id=" + chatid + "&text=" + text);
std::cout << "[DEBUG] " <<reply << std::endl; std::cout << "[DEBUG] " << reply << std::endl;
} }

View File

@ -9,7 +9,7 @@
#include <string> #include <string>
#include "API.h" #include "API.h"
class TelegramAPI : API{ class TelegramAPI : API {
public: public:
void sendMessage(std::string text); void sendMessage(std::string text);

View File

@ -58,7 +58,7 @@ int main(int argc, char *argv[]) {
if (dynurepl != "{\"statusCode\":200}") { if (dynurepl != "{\"statusCode\":200}") {
logger.logToLogfile(" [ERROR] failed to write ip to dynu api!"); logger.logToLogfile(" [ERROR] failed to write ip to dynu api!");
} else{ } else {
TelegramAPI tele; TelegramAPI tele;
tele.sendMessage(oldip + " moved to " + ip); tele.sendMessage(oldip + " moved to " + ip);
} }