From 0590f6d2f664c43c216782a5b4d83c17606b8ed5 Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Sat, 13 Jan 2024 01:17:03 +0200 Subject: [PATCH] more helpers --- helpers/common_helpers.cpp | 80 ++++++++++++++++++++++- helpers/common_helpers/common_helpers.hpp | 17 +++++ helpers/dbg_log.cpp | 19 ++++++ helpers/dbg_log/dbg_log.hpp | 6 ++ 4 files changed, 121 insertions(+), 1 deletion(-) diff --git a/helpers/common_helpers.cpp b/helpers/common_helpers.cpp index 651f9c20..d7017896 100644 --- a/helpers/common_helpers.cpp +++ b/helpers/common_helpers.cpp @@ -1,6 +1,5 @@ #include "common_helpers/common_helpers.hpp" #include -#include #include #include @@ -41,6 +40,7 @@ void common_helpers::write(std::ofstream &file, const std::string &data) std::wstring common_helpers::str_to_w(const std::string &str) { + if (str.empty()) return std::wstring(); auto cvt_state = std::mbstate_t(); const char* src = &str[0]; size_t conversion_bytes = std::mbsrtowcs(nullptr, &src, 0, &cvt_state); @@ -51,6 +51,7 @@ std::wstring common_helpers::str_to_w(const std::string &str) std::string common_helpers::wstr_to_a(const std::wstring &wstr) { + if (wstr.empty()) return std::string(); auto cvt_state = std::mbstate_t(); const wchar_t* src = &wstr[0]; size_t conversion_bytes = std::wcsrtombs(nullptr, &src, 0, &cvt_state); @@ -104,3 +105,80 @@ bool common_helpers::ends_with_i(const std::wstring &target, const std::wstring return _target.compare(_target.length() - _query.length(), _query.length(), _query) == 0; } + +std::filesystem::path to_absolute_impl(std::filesystem::path &path, std::filesystem::path &base) +{ + if (path.is_absolute()) { + return path; + } + + return std::filesystem::absolute(base / path); +} + +std::string common_helpers::to_absolute(const std::string &path, const std::string &base) +{ + if (path.empty()) return path; + auto path_abs = to_absolute_impl( + std::filesystem::path(path), + base.empty() ? std::filesystem::current_path() : std::filesystem::path(base) + ); + return path_abs.string(); +} + +std::wstring common_helpers::to_absolute(const std::wstring &path, const std::wstring &base) +{ + if (path.empty()) return path; + auto path_abs = to_absolute_impl( + std::filesystem::path(path), + base.empty() ? std::filesystem::current_path() : std::filesystem::path(base) + ); + return path_abs.wstring(); +} + +bool common_helpers::file_exist(std::filesystem::path &filepath) +{ + if (std::filesystem::is_directory(filepath)) { + return false; + } else if (std::filesystem::exists(filepath)) { + return true; + } + + return false; +} + +bool common_helpers::file_exist(const std::string &filepath) +{ + if (filepath.empty()) return false; + std::filesystem::path path(filepath); + return file_exist(path); +} + +bool common_helpers::file_exist(const std::wstring &filepath) +{ + if (filepath.empty()) return false; + std::filesystem::path path(filepath); + return file_exist(path); +} + +bool common_helpers::dir_exist(std::filesystem::path &dirpath) +{ + if (std::filesystem::is_directory(dirpath)) { + return true; + } + + return false; +} + +bool common_helpers::dir_exist(const std::string &dirpath) +{ + if (dirpath.empty()) return false; + std::filesystem::path path(dirpath); + return dir_exist(path); +} + +bool common_helpers::dir_exist(const std::wstring &dirpath) +{ + if (dirpath.empty()) return false; + std::filesystem::path path(dirpath); + return dir_exist(path); +} diff --git a/helpers/common_helpers/common_helpers.hpp b/helpers/common_helpers/common_helpers.hpp index a6ca5860..95ec6af6 100644 --- a/helpers/common_helpers/common_helpers.hpp +++ b/helpers/common_helpers/common_helpers.hpp @@ -2,6 +2,7 @@ #include #include +#include namespace common_helpers { @@ -23,4 +24,20 @@ bool ends_with_i(const std::string &target, const std::string &query); bool ends_with_i(const std::wstring &target, const std::wstring &query); +std::string to_absolute(const std::string &path, const std::string &base = std::string()); + +std::wstring to_absolute(const std::wstring &path, const std::wstring &base = std::wstring()); + +bool file_exist(std::filesystem::path &filepath); + +bool file_exist(const std::string &filepath); + +bool file_exist(const std::wstring &filepath); + +bool dir_exist(std::filesystem::path &dirpath); + +bool dir_exist(const std::string &dirpath); + +bool dir_exist(const std::wstring &dirpath); + } diff --git a/helpers/dbg_log.cpp b/helpers/dbg_log.cpp index 4d888161..fe4a0e81 100644 --- a/helpers/dbg_log.cpp +++ b/helpers/dbg_log.cpp @@ -1,4 +1,5 @@ #include "dbg_log/dbg_log.hpp" +#include "common_helpers/common_helpers.hpp" #include #include @@ -34,6 +35,24 @@ bool dbg_log::init(const char *path) return true; } +void dbg_log::write(const std::wstring &str) +{ + +#ifndef EMU_RELEASE_BUILD + write(common_helpers::wstr_to_a(str)); +#endif + +} + +void dbg_log::write(const std::string &str) +{ + +#ifndef EMU_RELEASE_BUILD + write(str.c_str()); +#endif + +} + void dbg_log::write(const char *fmt, ...) { diff --git a/helpers/dbg_log/dbg_log.hpp b/helpers/dbg_log/dbg_log.hpp index 95869624..aeb73d55 100644 --- a/helpers/dbg_log/dbg_log.hpp +++ b/helpers/dbg_log/dbg_log.hpp @@ -1,5 +1,7 @@ #pragma once +#include + namespace dbg_log { @@ -7,6 +9,10 @@ bool init(const wchar_t *path); bool init(const char *path); +void write(const std::wstring &str); + +void write(const std::string &str); + void write(const char* fmt, ...); void close();