From ba63d6b1da884953969f01e91f6039f1b24d19c6 Mon Sep 17 00:00:00 2001 From: ambigipathyv Date: Mon, 18 Mar 2024 11:32:37 +0100 Subject: [PATCH] Created module bakatools --- README.md | 0 premake5.lua | 52 ++++++++++++++++++++++++ src/bakatools.h | 5 +++ src/bakatools/logging/assert.h | 48 ++++++++++++++++++++++ src/bakatools/logging/log.cpp | 28 +++++++++++++ src/bakatools/logging/log.h | 47 +++++++++++++++++++++ src/bakatools/string/format.h | 63 +++++++++++++++++++++++++++++ src/bakatools/string/slice.cpp | 17 ++++++++ src/bakatools/string/string_tools.h | 10 +++++ src/bakatools/string/to_lower.cpp | 12 ++++++ src/bakatools/string/to_upper.cpp | 12 ++++++ src/bakatools/string/trim.cpp | 13 ++++++ src/bakatoolspch.h | 12 ++++++ 13 files changed, 319 insertions(+) create mode 100644 README.md create mode 100644 premake5.lua create mode 100644 src/bakatools.h create mode 100644 src/bakatools/logging/assert.h create mode 100644 src/bakatools/logging/log.cpp create mode 100644 src/bakatools/logging/log.h create mode 100644 src/bakatools/string/format.h create mode 100644 src/bakatools/string/slice.cpp create mode 100644 src/bakatools/string/string_tools.h create mode 100644 src/bakatools/string/to_lower.cpp create mode 100644 src/bakatools/string/to_upper.cpp create mode 100644 src/bakatools/string/trim.cpp create mode 100644 src/bakatoolspch.h diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/premake5.lua b/premake5.lua new file mode 100644 index 0000000..6f1c5b5 --- /dev/null +++ b/premake5.lua @@ -0,0 +1,52 @@ +project "bakatools" + kind "StaticLib" + language "C++" + cppdialect "C++17" + systemversion "latest" + + targetdir("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}") + objdir("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}") + + includedirs + { + "%{prj.location}/vendor/spdlog/include", + "%{prj.location}/src/" + } + + files + { + "%{prj.location}/src/bakatools/**.h", + "%{prj.location}/src/bakatools/**.cpp", + "%{prj.location}/src/bakatoolspch.h", + } + + filter "system:windows" + defines + { + "BK_PLAFORM_WINDOWS" + } + + filter "system:linux" + defines + { + "BK_PLAFORM_LINUX" + } + + filter "configurations:Debug" + defines + { + "BK_DEBUG", + "DEBUG" + } + runtime "Debug" + symbols "on" + + + filter "configurations:Release" + defines + { + "BK_RELEASE", + "NDEBUG" + } + runtime "Release" + optimize "on" diff --git a/src/bakatools.h b/src/bakatools.h new file mode 100644 index 0000000..8c6c8ee --- /dev/null +++ b/src/bakatools.h @@ -0,0 +1,5 @@ +#pragma once + +#include +#include +#include \ No newline at end of file diff --git a/src/bakatools/logging/assert.h b/src/bakatools/logging/assert.h new file mode 100644 index 0000000..e76c44d --- /dev/null +++ b/src/bakatools/logging/assert.h @@ -0,0 +1,48 @@ +#pragma once + +/*! \file assert.h +This file contains the assert macros. CORE macros musn't be used by the application. +*/ + +#include "log.h" +#include + +#define BK_STRINGIFY(x) #x + +#ifdef BK_DEBUG + #if defined(BK_PLATFORM_WINDOWS) + #define BK_DEBUGBREAK() __debugbreak() + #elif defined(BK_PLATFORM_LINUX) + #include + #define BK_DEBUGBREAK() raise(SIGTRAP) + #else + #error "Plaform doesn't support debug yet" + #endif + #define BK_ENABLE_ASSERT +#else + #define BK_DEBUGBREAK() +#endif + +#ifdef BK_ENABLE_ASSERT + /*! \def BK_CORE_VAMSG_ASSERT(check, msg, ...) + Assertes a condition, and throw an error with the formatted message as description + @param check : Condionne to assert + @param msg : format string error message + @param ... : variable arguments to put in the string + */ + #define BK_CORE_VAMSG_ASSERT(check, msg, ...) if(!(check)) { BK_CORE_ERROR(Tools::format("Assertion [%s] failed at %s:%d\n\tError : %s", BK_STRINGIFY(check), __FILE__, __LINE__, msg), __VA_ARGS__); BK_DEBUGBREAK(); } + #define BK_CORE_MSG_ASSERT(check, msg) if(!(check)) { BK_CORE_ERROR("Assertion [{0}] failed at {1}:{2}\n\tError : {3}", BK_STRINGIFY(check), __FILE__, __LINE__, msg); BK_DEBUGBREAK(); } + #define BK_CORE_ASSERT(check) if(!(check)) { BK_CORE_ERROR("Assertion [{0}] failed at {1}:{2}", BK_STRINGIFY(check), __FILE__, __LINE__); BK_DEBUGBREAK(); } + + #define BK_VAMSG_ASSERT(check, msg, ...) if(!(check)) { BK_ERROR(Bk::Tools::format("Assertion [%s] failed at %s:%d\n\tError : %s", BK_STRINGIFY(check), __FILE__, __LINE__, msg), __VA_ARGS__); BK_DEBUGBREAK(); } + #define BK_MSG_ASSERT(check, msg) if(!(check)) { BK_ERROR("Assertion [{0}] failed at {1}:{2}\n\tError : {3}", BK_STRINGIFY(check), __FILE__, __LINE__, msg); BK_DEBUGBREAK(); } + #define BK_ASSERT(check) if(!(check)) { BK_ERROR("Assertion [{0}] failed at {1}:{2}", BK_STRINGIFY(check), __FILE__, __LINE__); BK_DEBUGBREAK(); } +#else + #define BK_CORE_VAMSG_ASSERT(check, msg, ...) + #define BK_CORE_MSG_ASSERT(check, msg) + #define BK_CORE_ASSERT(check) + + #define BK_VAMSG_ASSERT(check, msg, ...) + #define BK_MSG_ASSERT(check, msg) + #define BK_ASSERT(check) +#endif \ No newline at end of file diff --git a/src/bakatools/logging/log.cpp b/src/bakatools/logging/log.cpp new file mode 100644 index 0000000..4d6da7e --- /dev/null +++ b/src/bakatools/logging/log.cpp @@ -0,0 +1,28 @@ +#include "log.h" + +namespace Bk { + + std::shared_ptr Log::p_core_logger; + std::shared_ptr Log::p_app_logger; + + void Log::init() + { + std::vector log_sinks; + log_sinks.emplace_back(std::make_shared()); + log_sinks.emplace_back(std::make_shared("bakara.log", true)); + + log_sinks[0]->set_pattern("%^[%T] %n: %v%$"); + log_sinks[1]->set_pattern("[%T] [%l] %n: %v"); + + p_core_logger = std::make_shared("BAKARA", begin(log_sinks), end(log_sinks)); + spdlog::register_logger(p_core_logger); + p_core_logger->set_level(spdlog::level::trace); + p_core_logger->flush_on(spdlog::level::trace); + + p_app_logger = std::make_shared("APP", begin(log_sinks), end(log_sinks)); + spdlog::register_logger(p_app_logger); + p_app_logger->set_level(spdlog::level::trace); + p_app_logger->flush_on(spdlog::level::trace); + } + +} \ No newline at end of file diff --git a/src/bakatools/logging/log.h b/src/bakatools/logging/log.h new file mode 100644 index 0000000..e6e5220 --- /dev/null +++ b/src/bakatools/logging/log.h @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace Bk { + + class Log + { + public: + static void init(); + + static std::shared_ptr& get_core_logger() { return p_core_logger; } + static std::shared_ptr& get_app_logger() { return p_app_logger; } + private: + static std::shared_ptr p_core_logger; + static std::shared_ptr p_app_logger; + }; + #ifdef BK_DEBUG + #define BK_CORE_TRACE(...) ::Bk::Log::get_core_logger()->trace(__VA_ARGS__) + #define BK_CORE_INFO(...) ::Bk::Log::get_core_logger()->info(__VA_ARGS__) + #define BK_CORE_WARN(...) ::Bk::Log::get_core_logger()->warn(__VA_ARGS__) + #define BK_CORE_ERROR(...) ::Bk::Log::get_core_logger()->error(__VA_ARGS__) + #define BK_CORE_CRITICAL(...) ::Bk::Log::get_core_logger()->critical(__VA_ARGS__) + + #define BK_TRACE(...) ::Bk::Log::get_app_logger()->trace(__VA_ARGS__) + #define BK_INFO(...) ::Bk::Log::get_app_logger()->info(__VA_ARGS__) + #define BK_WARN(...) ::Bk::Log::get_app_logger()->warn(__VA_ARGS__) + #define BK_ERROR(...) ::Bk::Log::get_app_logger()->error(__VA_ARGS__) + #define BK_CRITICAL(...) ::Bk::Log::get_app_logger()->critical(__VA_ARGS__) + #else + #define BK_CORE_TRACE(...) + #define BK_CORE_INFO(...) + #define BK_CORE_WARN(...) + #define BK_CORE_ERROR(...) + #define BK_CORE_CRITICAL(...) + + #define BK_TRACE(...) + #define BK_INFO(...) + #define BK_WARN(...) + #define BK_ERROR(...) + #define BK_CRITICAL(...) + #endif +} \ No newline at end of file diff --git a/src/bakatools/string/format.h b/src/bakatools/string/format.h new file mode 100644 index 0000000..bae24bc --- /dev/null +++ b/src/bakatools/string/format.h @@ -0,0 +1,63 @@ +#pragma once + +/*! \file format.h +This file provides functions to do string formatting. +*/ + +#include + +namespace Bk::Tools { + inline void format_impl(std::stringstream& ss, const char* format) { + while (*format) { + if (*format == '%' && *++format != '%') // %% == % (not a format directive) + throw std::invalid_argument("not enough arguments !\n"); + ss << *format++; + } + } + + template + void format_impl(std::stringstream& ss, const char* format, Arg arg, Args... args) { + while (*format) { + if (*format == '%' && *++format != '%') { + auto current_format_qualifier = *format; + switch(current_format_qualifier) { + case 'd' : + if (!std::is_integral()) throw std::invalid_argument("%d introduces integral argument"); + break; + case 'f' : + if (!std::is_floating_point()) throw std::invalid_argument("%f introduces floating point argument"); + break; + case 'b' : + if(arg) ss << "true"; + else ss << "false"; + return format_impl(ss, ++format, args...); + case 's' : + break; + // etc. + default: + throw std::invalid_argument("Not a standard format"); + break; + } + // it's true you'd have to handle many more format qualifiers, but on a safer basis + ss << arg; // arg type is deduced + return format_impl(ss, ++format, args...); // one arg less + } + ss << *format++; + } // the format string is exhausted and we still have args : throw + throw std::invalid_argument("Too many arguments\n"); + } + + /*! \fn std::string Bk::Tools::format(const char* fmt, Args... args) + Formats a string, printf like. Accepts integers, floating point numbers, strings, booléen. + @param fmt : string to format + @param args : variable arguments to put in the string + @return String formatted + */ + template + inline std::string string_format(const char* fmt, Args... args) { + std::stringstream ss; + format_impl(ss, fmt, args...); + return ss.str(); + } +} + diff --git a/src/bakatools/string/slice.cpp b/src/bakatools/string/slice.cpp new file mode 100644 index 0000000..b55bef6 --- /dev/null +++ b/src/bakatools/string/slice.cpp @@ -0,0 +1,17 @@ +#include "string_tools.h" + +namespace Bk::Tools { + std::unique_ptr> string_split(std::string& str, std::string delimiter, int cpt) + { + std::string s(str); + std::unique_ptr> splits(new std::vector(0)); + size_t pos = 0; + while (((pos = s.find(delimiter)) != std::string::npos) && cpt-- != 0) + { + splits->push_back(s.substr(0, pos)); + s.erase(0, pos + delimiter.length()); + } + if (s.length()) splits->push_back(s); + return splits; + } +} \ No newline at end of file diff --git a/src/bakatools/string/string_tools.h b/src/bakatools/string/string_tools.h new file mode 100644 index 0000000..b3ed320 --- /dev/null +++ b/src/bakatools/string/string_tools.h @@ -0,0 +1,10 @@ +#pragma once + +#include "format.h" + +namespace Bk::Tools { + std::string string_to_lower(std::string& str); + std::string string_to_upper(std::string& str); + std::unique_ptr> string_split(std::string& str, std::string delimiter, int cpt = -1); + void string_trim(std::string& str, const std::string& whitespace = " \b\0"); +} \ No newline at end of file diff --git a/src/bakatools/string/to_lower.cpp b/src/bakatools/string/to_lower.cpp new file mode 100644 index 0000000..a753dd7 --- /dev/null +++ b/src/bakatools/string/to_lower.cpp @@ -0,0 +1,12 @@ +#include "string_tools.h" + +namespace Bk::Tools { + std::string string_to_lower(std::string& str) + { + for (int i = 0; i < str.length(); i++) + { + str[i] = std::tolower(str[i]); + } + return str; + } +} \ No newline at end of file diff --git a/src/bakatools/string/to_upper.cpp b/src/bakatools/string/to_upper.cpp new file mode 100644 index 0000000..a223fca --- /dev/null +++ b/src/bakatools/string/to_upper.cpp @@ -0,0 +1,12 @@ +#include "string_tools.h" + +namespace Bk::Tools { + std::string string_to_upper(std::string& str) + { + for (int i = 0; i < str.length(); i++) + { + str[i] = std::toupper(str[i]); + } + return str; + } +} \ No newline at end of file diff --git a/src/bakatools/string/trim.cpp b/src/bakatools/string/trim.cpp new file mode 100644 index 0000000..87e0b9e --- /dev/null +++ b/src/bakatools/string/trim.cpp @@ -0,0 +1,13 @@ +#include "string_tools.h" + +namespace Bk::Tools { + void string_trim(std::string& str, const std::string& whitespace) + { + const auto strBegin = str.find_first_not_of(whitespace); + const auto strEnd = str.find_last_not_of(whitespace); + if (strBegin != std::string::npos) + { + str.erase(0, strBegin); + } + } +} \ No newline at end of file diff --git a/src/bakatoolspch.h b/src/bakatoolspch.h new file mode 100644 index 0000000..29475e8 --- /dev/null +++ b/src/bakatoolspch.h @@ -0,0 +1,12 @@ +#pragme once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include \ No newline at end of file