Merge pull request #19 from Lukas-Heiligenbrunner/configfilewriteable

Configfilewriteable
This commit is contained in:
Lukas-Heiligenbrunner 2020-05-21 11:28:49 +02:00 committed by GitHub
commit 8c0c888472
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 5 deletions

View File

@ -66,14 +66,14 @@ if (${WinBuild})
# or add_definitions(-DCURL_STATICLIB)
# windows config path is same as executable
set(CONFIG_PATH "./iprefresher.cfg")
set(CONFIG_PATH "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"")
else ()
message(STATUS "using nativ gcc toolchain.")
set(LIBSUFFIX .so)
set(SUFFIX "")
# set /etc/ config path
set(CONFIG_PATH "/etc/iprefresher.cfg")
set(CONFIG_PATH "\"/etc/\"")
endif ()
# test compiler settings and enable languages here
@ -159,7 +159,8 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h
namespace Version {
const std::string VERSION = \"${PROJECT_VERSION}\";
const std::string ConfigDir = \"${CONFIG_PATH}\";
const std::string ConfigDir = ${CONFIG_PATH};
const std::string ConfName = \"iprefresher.cfg\";
const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\";
}"
)
@ -198,6 +199,9 @@ ELSE ()
set_target_properties(iprefresher PROPERTIES SUFFIX ".exe")
install(TARGETS iprefresher DESTINATION .)
# create config directory
install(DIRECTORY DESTINATION "../Common\ Files/iprefresher")
# install .dll dependencies
# todo check if files exist...
install(FILES /usr/${TOOLCHAIN_PREFIX}/sys-root/mingw/bin/libcurl-4.dll

View File

@ -6,6 +6,7 @@
#include <cstring>
#include <fstream>
#include <libconfig.h++>
#include <sys/stat.h>
std::string Config::dynuapikey;
std::string Config::domainid; //id of the dynu domain
@ -19,13 +20,38 @@ bool Config::telegramSupport;
bool Config::readConfig() {
libconfig::Config cfg;
try {
cfg.readFile(Version::ConfigDir.c_str());
cfg.readFile(std::string(Version::ConfigDir + Version::ConfName).c_str());
}
catch (const libconfig::FileIOException &fioex) {
std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl;
// check if config folder exists
struct stat info{};
if (stat(Version::ConfigDir.c_str(), &info) != 0) {
Logger::warning("The config folder doesn't exist. Trying to create it.");
#ifdef __unix
int check = mkdir(Version::ConfigDir.c_str(), 777);
#else
int check = mkdir(Version::ConfigDir.c_str());
#endif
// check if directory is created or not
if (!check)
Logger::message("config directory successfully created. ");
else
Logger::error("unable to create config directory.");
} else if (info.st_mode & S_IFDIR) {
Logger::debug("config directory exists already");
} else {
Logger::error("A file exists with the same name as the config dir should be");
}
std::ofstream myfile;
myfile.open(Version::ConfigDir);
myfile.open(Version::ConfigDir + Version::ConfName);
if (myfile.is_open()) {
myfile << Version::SAMPLECONFIG;
myfile.close();