rename Version.h to StaticData.h

new temp file path for windows and linux
temp file now writeable on windows
compare string find method with SIZE_MAX instead of ULONG_MAX
This commit is contained in:
lukas 2020-05-26 16:51:29 +02:00
parent 3f75484f77
commit 81de98bf6c
8 changed files with 31 additions and 29 deletions

View File

@ -66,15 +66,19 @@ if (${WinBuild})
# or set(CMAKE_CXX_STANDARD_LIBRARIES -lcurl -lpthread -static-libgcc -static-libstdc++ -lcrypto -lssl -lws2_32 -static -DCURL_STATICLIB) # or set(CMAKE_CXX_STANDARD_LIBRARIES -lcurl -lpthread -static-libgcc -static-libstdc++ -lcrypto -lssl -lws2_32 -static -DCURL_STATICLIB)
# or add_definitions(-DCURL_STATICLIB) # or add_definitions(-DCURL_STATICLIB)
# windows config path is same as executable # windows config path is in %appdata% folder of user
set(CONFIG_PATH "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"") set(CONFIG_PATH "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"")
# temp file is also stored in appdata folder
set(TempFilePath "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"")
else () else ()
message(STATUS "using nativ gcc toolchain.") message(STATUS "using nativ gcc toolchain.")
set(LIBSUFFIX .so) set(LIBSUFFIX .so)
set(SUFFIX "") set(SUFFIX "")
# set /etc/ config path # set linux config path
set(CONFIG_PATH "\"/etc/\"") set(CONFIG_PATH "\"/etc/\"")
# set path of temp file
set(TempFilePath "\"/var/tmp/\"")
endif () endif ()
# test compiler settings and enable languages here # test compiler settings and enable languages here
@ -146,10 +150,10 @@ message("")
#read sample config #read sample config
FILE(READ ${CMAKE_SOURCE_DIR}/config/dynuiprefresher.cfg SAMPLECONFIG) FILE(READ ${CMAKE_SOURCE_DIR}/config/dynuiprefresher.cfg SAMPLECONFIG)
#add version header #add StaticData header
FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/StaticData.h
"/** "/**
* Version header to store Version, Config dir and a Sample config * StaticData header to store Version, Config dir and a Sample config
* Do not edit this file manually, it is generated by the cmake script! * Do not edit this file manually, it is generated by the cmake script!
* *
* @author Lukas Heiligenbrunner * @author Lukas Heiligenbrunner
@ -160,8 +164,9 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h
#include <string> #include <string>
namespace Version { namespace StaticData {
const std::string VERSION = \"${PROJECT_VERSION}\"; const std::string VERSION = \"${PROJECT_VERSION}\";
const std::string TempFilePath = ${TempFilePath};
const std::string ConfigDir = ${CONFIG_PATH}; const std::string ConfigDir = ${CONFIG_PATH};
const std::string ConfName = \"${Application_Name}.cfg\"; const std::string ConfName = \"${Application_Name}.cfg\";
const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\"; const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\";

View File

@ -1,6 +1,6 @@
#include "Config.h" #include "Config.h"
#include "Logger.h" #include "Logger.h"
#include "Version.h" #include "StaticData.h"
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
@ -20,7 +20,7 @@ bool Config::telegramSupport;
bool Config::readConfig() { bool Config::readConfig() {
libconfig::Config cfg; libconfig::Config cfg;
try { try {
cfg.readFile(std::string(Version::ConfigDir + Version::ConfName).c_str()); cfg.readFile(std::string(StaticData::ConfigDir + StaticData::ConfName).c_str());
} }
catch (const libconfig::FileIOException &fioex) { catch (const libconfig::FileIOException &fioex) {
std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl; std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl;
@ -28,13 +28,14 @@ bool Config::readConfig() {
// check if config folder exists // check if config folder exists
struct stat info{}; struct stat info{};
if (stat(Version::ConfigDir.c_str(), &info) != 0) { if (stat(StaticData::ConfigDir.c_str(), &info) != 0) {
Logger::warning("The config folder doesn't exist. Trying to create it."); Logger::warning("The config folder doesn't exist. Trying to create it.");
// mkdir command is different defined for windows
#ifdef __unix #ifdef __unix
int check = mkdir(Version::ConfigDir.c_str(), 777); int check = mkdir(StaticData::ConfigDir.c_str(), 777);
#else #else
int check = mkdir(Version::ConfigDir.c_str()); int check = mkdir(StaticData::ConfigDir.c_str());
#endif #endif
// check if directory is created or not // check if directory is created or not
@ -51,9 +52,9 @@ bool Config::readConfig() {
std::ofstream myfile; std::ofstream myfile;
myfile.open(Version::ConfigDir + Version::ConfName); myfile.open(StaticData::ConfigDir + StaticData::ConfName);
if (myfile.is_open()) { if (myfile.is_open()) {
myfile << Version::SAMPLECONFIG; myfile << StaticData::SAMPLECONFIG;
myfile.close(); myfile.close();
} else { } else {
Logger::error("error creating file"); Logger::error("error creating file");
@ -94,7 +95,7 @@ bool Config::validateConfig() {
libconfig::Config cfg; libconfig::Config cfg;
try { try {
Logger::message("reading config file"); Logger::message("reading config file");
cfg.readFile(Version::ConfigDir.c_str()); cfg.readFile(StaticData::ConfigDir.c_str());
} }
catch (const libconfig::FileIOException &fioex) { catch (const libconfig::FileIOException &fioex) {
Logger::warning("config file doesn't exist or permission denied!"); Logger::warning("config file doesn't exist or permission denied!");

View File

@ -1,12 +1,13 @@
#include "FileLogger.h" #include "FileLogger.h"
#include "IpHelper.h" #include "IpHelper.h"
#include "StaticData.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
void FileLogger::safeip(std::string ip) { void FileLogger::safeip(std::string ip) {
std::ofstream out; std::ofstream out;
out.open("ip.txt", std::ios::out); out.open(StaticData::TempFilePath + "temp-dynuiprefresher.txt", std::ios::out);
out << ip; out << ip;
@ -15,7 +16,7 @@ void FileLogger::safeip(std::string ip) {
std::string FileLogger::readip() { std::string FileLogger::readip() {
std::ifstream in; std::ifstream in;
in.open("ip.txt", std::ios::in); in.open(StaticData::TempFilePath + "temp-dynuiprefresher.txt", std::ios::in);
std::string ip; std::string ip;

View File

@ -4,7 +4,7 @@
#include "api/DynuAPI.h" #include "api/DynuAPI.h"
#include "api/TelegramAPI.h" #include "api/TelegramAPI.h"
#include "Config.h" #include "Config.h"
#include "Version.h" #include "StaticData.h"
#include "IpHelper.h" #include "IpHelper.h"
#include <chrono> #include <chrono>
@ -53,7 +53,7 @@ void IPRefresher::checkIPAdress(bool force) {
IPRefresher::IPRefresher(bool loop) { IPRefresher::IPRefresher(bool loop) {
if (loop) { if (loop) {
Logger::message("startup of service"); Logger::message("startup of service");
Logger::message("Version: " + Version::VERSION); Logger::message("Version: " + StaticData::VERSION);
while (true) { while (true) {
Logger::message("starting check"); Logger::message("starting check");

View File

@ -1,7 +1,5 @@
#include "IpHelper.h" #include "IpHelper.h"
#include <climits>
bool IpHelper::isIpValid(std::string ip) { bool IpHelper::isIpValid(std::string ip) {
return (ip.find('.') != ULONG_MAX); return (ip.find('.') != SIZE_MAX);
} }

View File

@ -1,8 +1,6 @@
#include "api/TelegramAPI.h" #include "api/TelegramAPI.h"
#include "Logger.h" #include "Logger.h"
#include <climits>
int TelegramAPI::sendMessage(const std::string &text) { int TelegramAPI::sendMessage(const std::string &text) {
Hashmap<std::string, std::string> args; Hashmap<std::string, std::string> args;
args.add("chat_id", chatid); args.add("chat_id", chatid);
@ -12,8 +10,8 @@ int TelegramAPI::sendMessage(const std::string &text) {
std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers); std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers);
if (reply.find("\"error_code\"") != ULONG_MAX) { if (reply.find("\"error_code\"") != SIZE_MAX) {
Logger::error("failed to refresh the ip (Dynu API)"); Logger::error("failed to send the Telegram Message");
return -1; return -1;
} }
return 1; return 1;

View File

@ -1,4 +1,4 @@
#include "Version.h" #include "StaticData.h"
#include "IPRefresher.h" #include "IPRefresher.h"
#include "Logger.h" #include "Logger.h"
#include "Config.h" #include "Config.h"
@ -19,7 +19,7 @@ int main(int argc, char *argv[]) {
<< "[-ip] [--currentip] get current global ip" << std::endl << "[-ip] [--currentip] get current global ip" << std::endl
<< "[no argument] normal ip check and refresh" << std::endl; << "[no argument] normal ip check and refresh" << std::endl;
} else if (firstarg == "-v" || firstarg == "--version") { } else if (firstarg == "-v" || firstarg == "--version") {
std::cout << "Version " << Version::VERSION << std::endl; std::cout << "Version " << StaticData::VERSION << std::endl;
} else if (firstarg == "-f" || firstarg == "--force") { } else if (firstarg == "-f" || firstarg == "--force") {
IPRefresher ipr; IPRefresher ipr;
if (Config::readConfig()) { if (Config::readConfig()) {

View File

@ -4,7 +4,6 @@
#include <FileLogger.h> #include <FileLogger.h>
#include <api/IPAPI.h> #include <api/IPAPI.h>
#include <climits>
#include "gtest/gtest.h" #include "gtest/gtest.h"
/** /**
@ -21,7 +20,7 @@ TEST(ReadIp, testzeroIpIfNotExists) {
TEST(IPAPI, testIpAPIcheckIPSyntax) { TEST(IPAPI, testIpAPIcheckIPSyntax) {
IPAPI ipapi; IPAPI ipapi;
std::string ip = ipapi.getGlobalIp(); std::string ip = ipapi.getGlobalIp();
if (ip.find('.') == ULONG_MAX) { if (ip.find('.') == SIZE_MAX) {
// error when ip doesn't contain a . // error when ip doesn't contain a .
ASSERT_TRUE(false); ASSERT_TRUE(false);
} else { } else {