Skip to content

Commit

Permalink
Update to version Beta 2.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
In-line committed Apr 18, 2018
1 parent 56cccf8 commit 6dd8231
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 130 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ add_subdirectory(test)

# main
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
set(FLAGS "-m32 -O2 -fvisibility=hidden -flto -fPIC")
set(FLAGS "-m32 -O3 -pipe -fvisibility-inlines-hidden -fvisibility=hidden -flto -fPIC")
set_target_properties(
${PROJECT_NAME} PROPERTIES COMPILE_FLAGS
"${FLAGS} \
-fdata-sections \
-ffunction-sections \
-Wall \
-Wextra \
-Wzero-as-null-pointer-constant \
-Werror \
-Wstrict-aliasing \
-Weffc++ \
-Wunknown-pragmas"
)
Expand Down
85 changes: 39 additions & 46 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <fstream>
#include <algorithm>

Config::Config(const std::shared_ptr<Logger> &logger)
Config::Config(const up::shared_ptr<Logger> &logger)
:
logger_(logger),
options_(),
Expand All @@ -59,19 +59,10 @@ bool Config::addOption(const std::string &title, Config::OptionType type, const

bool Config::deleteOption(const std::string &title)
{
bool hasSomethingToDelete = defaultOptions_.find(title) != defaultOptions_.end();
options_.erase(title);
auto end = defaultOptions_.end();
auto newEnd = std::remove_if(defaultOptions_.begin(), end, [&title](const decltype(defaultOptions_)::value_type &e) -> bool
{
if(e.first == title)
return true;
return false;
});
if(end == newEnd)
return false;

defaultOptions_.erase(newEnd, end);
return true;
defaultOptions_.erase(title);
return hasSomethingToDelete;
}

bool Config::addOption(const std::string &title, Config::OptionType type, const bool &defaultValue)
Expand All @@ -94,15 +85,13 @@ bool Config::addOptionVariant(const std::string &title, Config::OptionType type,
if(!checkVariant(type, defaultValue))
return false;

try {
options_.at(title);
}
catch (std::exception&) {
if(options_.find(title) != options_.end()) {
return false;
} else {
options_[title] = {type, defaultValue};
defaultOptions_.push_back({title , defaultValue});
defaultOptions_[title] = defaultValue;
return true;
}
return false;
}

std::pair<Config::OptionType, Config::VariantType> Config::getOption(const std::string &title) const
Expand All @@ -112,16 +101,16 @@ std::pair<Config::OptionType, Config::VariantType> Config::getOption(const std::

bool Config::setOption(const std::string &title, const Config::VariantType &newValue)
{
decltype(options_)::iterator position = options_.find(title);
auto position = options_.find(title);
if(position == options_.end())
return false;

const OptionType type = (*position).second.first;
const OptionType type = position->second.first;

if(!checkVariant(type, newValue))
return false;

(*position).second.second = newValue;
position->second.second = newValue;

return true;
}
Expand All @@ -148,8 +137,7 @@ bool Config::setOption(const std::string &title, const std::string &newValue)

void Config::loadConfig(const std::string &path)
{
std::ifstream file;
file.open(path);
std::ifstream file(path);

if(!file)
file.open(path, std::ios::out | std::ios::in | std::ios::trunc);
Expand All @@ -172,17 +160,17 @@ void Config::loadConfig(const std::string &path)
file.close();
}

std::shared_ptr<Logger> Config::getLogger() const
up::shared_ptr<Logger> Config::getLogger() const
{
return logger_;
}

std::shared_ptr<Logger> &Config::getLoggerRef()
up::shared_ptr<Logger> &Config::getLoggerRef()
{
return logger_;
}

void Config::setLogger(const std::shared_ptr<Logger> &logger)
void Config::setLogger(const up::shared_ptr<Logger> &logger)
{
logger_ = logger;
}
Expand Down Expand Up @@ -214,12 +202,10 @@ void Config::resetOptionsToDefaults()
{
for(auto &curPair : defaultOptions_)
{
decltype (options_)::iterator position = options_.find(curPair.first);
(*position).second.second = curPair.second;
options_.find(curPair.first)->second.second = curPair.second;
}
}


bool Config::readLine(const std::string &lineRef)
{
std::string line = trim(removeComments(lineRef));
Expand All @@ -240,31 +226,38 @@ bool Config::readLine(const std::string &lineRef)
logger_->debug(FNAME + " Left right: " + left + " = " + right);
try
{
decltype(options_)::mapped_type &value = options_.at(left);
auto &value = options_.at(left);

const auto BooleanFunction = [&value, this](const std::string &checkString) -> bool
{
const auto assignBool = [&value, this](bool assignValue){ value.second = assignValue; };
const std::unordered_map<std::string, std::function<void()>> switchMap =
{{"true",std::bind(assignBool, true)},
{"1",std::bind(assignBool, true)},
{"yes",std::bind(assignBool, true)},
{"yeah",std::bind(assignBool, true)},
const auto assignBool = [&value](bool assignValue) {
value.second = assignValue;
};

{"false",std::bind(assignBool, false)},
{"0",std::bind(assignBool, false)},
{"no",std::bind(assignBool, false)},
{"mazdan",std::bind(assignBool, false)}};
const std::unordered_map<std::string, std::function<void()>> switchMap =
{
{"true", std::bind(assignBool, true) },
{"1", std::bind(assignBool, true) },
{"yes", std::bind(assignBool, true) },
{"yeah", std::bind(assignBool, true) },
{"false", std::bind(assignBool, false) },
{"0", std::bind(assignBool, false) },
{"no", std::bind(assignBool, false) },
{"mazdan", std::bind(assignBool, false) }
};

std::string localCopy;
localCopy.reserve(checkString.size());

std::transform(checkString.begin(), checkString.end(), std::back_inserter(localCopy), ::tolower);
try {
switchMap.at(localCopy)();
} catch (std::exception&) {
logger_->error(FNAME + " Exception occured. Return from BF");

auto it = switchMap.find(localCopy);
if(it != switchMap.end()) {
it->second();
return true;
} else {
return false;
}
return true;
};

switch (value.first)
Expand Down
18 changes: 10 additions & 8 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@
*
*/

#pragma once
#ifndef CONFIG_H
#define CONFIG_H

#include <memory>
#include <vector>
#include <string>
#include <unordered_map>
#include <boost/variant.hpp>


#include "logger.h"
#include "helper/shared.h"

class Config
{
public:
Config(const std::shared_ptr<Logger> &logger);
explicit Config(const up::shared_ptr<Logger> &logger);

enum class OptionType
{
Expand All @@ -55,7 +57,7 @@ class Config
typedef boost::variant<bool, int, std::string> VariantType;

bool addOption(const std::string &title, Config::OptionType type, const VariantType &defaultValue);
bool addOption(const std::string &title, OptionType type, const char* defaultValue);
bool addOption(const std::string &title, Config::OptionType type, const char* defaultValue);
bool addOption(const std::string &title, Config::OptionType type, const bool &defaultValue);
bool addOption(const std::string &title, Config::OptionType type, const int &defaultValue);
bool addOption(const std::string &title, Config::OptionType type, const std::string &defaultValue);
Expand All @@ -71,16 +73,16 @@ class Config
void loadConfig(const std::string &path);
void resetOptionsToDefaults();
bool readLine(const std::string &lineRef);
std::shared_ptr<Logger> getLogger() const;
std::shared_ptr<Logger> &getLoggerRef();
void setLogger(const std::shared_ptr<Logger> &logger);
up::shared_ptr<Logger> getLogger() const;
up::shared_ptr<Logger> &getLoggerRef();
void setLogger(const up::shared_ptr<Logger> &logger);

private:
bool checkVariant(const OptionType type, const VariantType &variant) const;
bool addOptionVariant(const std::string &title, OptionType type, const VariantType &defaultValue);
std::shared_ptr<Logger> logger_;
up::shared_ptr<Logger> logger_;
std::unordered_map<std::string, std::pair<Config::OptionType, VariantType>> options_;
std::vector<std::pair<std::string, VariantType>> defaultOptions_;
std::unordered_map<std::string, VariantType> defaultOptions_;

bool configWasLoadedOnce_;
};
Expand Down
1 change: 1 addition & 0 deletions src/helper/external_api.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#ifndef EXTERNAL_API_H
#define EXTERNAL_API_H

Expand Down
16 changes: 16 additions & 0 deletions src/helper/shared.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#ifndef SHARED_H
#define SHARED_H

Expand All @@ -15,6 +16,21 @@
#define COLD
#endif

#include <boost/smart_ptr/local_shared_ptr.hpp>
#include <boost/smart_ptr/make_local_shared.hpp>

namespace up
{
template<typename T>
using shared_ptr = boost::local_shared_ptr<T>;

template<class T, class... Args>
inline auto make_shared(Args&&... args)
{
return boost::make_local_shared<T>(std::forward<Args>(args)...);
}

}
// Here we can put typedefs that shared by many files

#endif // SHARED_H
16 changes: 7 additions & 9 deletions src/helper/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
#include <algorithm>
#include <iomanip>
#include <sstream>

std::vector<std::string> parseString(const std::string &inputString, bool removeQuotes/*=true*/, bool anySpaceCharacters/*=true*/) noexcept
{
std::vector<std::string> returnVector;


auto start = inputString.begin();
auto end = inputString.end();

Expand All @@ -59,7 +59,7 @@ std::vector<std::string> parseString(const std::string &inputString, bool remove
case '"': case '\'':
{
if(!removeQuotes)
insertString+=*index;
insertString += *index;

if(status == PARSING_STATUS::PARSING_QUOTE)
{
Expand All @@ -78,7 +78,7 @@ std::vector<std::string> parseString(const std::string &inputString, bool remove
(!anySpaceCharacters && *index == ' '))
{
if(status == PARSING_STATUS::PARSING_QUOTE)
insertString+=*index;
insertString += *index;
else if(status == PARSING_STATUS::PARSING_STARTED)
{
returnVector.push_back(insertString);
Expand All @@ -88,7 +88,7 @@ std::vector<std::string> parseString(const std::string &inputString, bool remove
}
else
{
insertString+=*index;
insertString += *index;
if(status != PARSING_STATUS::PARSING_QUOTE)
status = PARSING_STATUS::PARSING_STARTED;
}
Expand Down Expand Up @@ -165,12 +165,10 @@ std::string trim(const std::string &str) noexcept

std::string::size_type lastNonWhiteSpace = str.size();

auto crend = str.crend();
for(auto it = str.crbegin(); it != crend && std::isspace(*it); ++it)
{
auto crend = str.crend();
for(auto it = str.crbegin(); it != crend && std::isspace(*it); ++it)
{
--lastNonWhiteSpace;
}
--lastNonWhiteSpace;
}

return str.substr(firstNonWhiteSpace, lastNonWhiteSpace - firstNonWhiteSpace);
Expand Down
2 changes: 2 additions & 0 deletions src/helper/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
*
*/

#pragma once
#ifndef STRING_UTILS_H
#define STRING_UTILS_H

#include <vector>
#include <string>
#include <ctime>

std::vector<std::string> parseString(const std::string &inputString, bool removeQuotes = true, bool anySpaceCharacters = true) noexcept;
bool starts_with(const std::string &input,const std::string &prefix) noexcept;
bool is_string_whitespaces(const std::string &input) noexcept;
Expand Down
12 changes: 6 additions & 6 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,22 @@ bool Logger::log(const Logger::CategoryType &category, const std::string &str) c
switch (category)
{
case Logger::CategoryType::Debug:
endMessage="Debug: ";
endMessage = "Debug: ";
break;
case Logger::CategoryType::Info:
endMessage="Info: ";
endMessage = "Info: ";
break;
case Logger::CategoryType::Warning:
endMessage="Warning: ";
endMessage = "Warning: ";
break;
case Logger::CategoryType::Error:
endMessage="Error: ";
endMessage = "Error: ";
break;
case Logger::CategoryType::CriticalError:
endMessage="Critical error: ";
endMessage = "Critical error: ";
break;
}
endMessage+=str;
endMessage += str;

switch(outputType_)
{
Expand Down
Loading

0 comments on commit 6dd8231

Please sign in to comment.