From 99932b9b9471bdd27a48876c386001190c6e75e6 Mon Sep 17 00:00:00 2001 From: V-KMilev Date: Fri, 10 Mar 2023 03:28:34 +0200 Subject: [PATCH] Common error handling & file read --- common/error/error_handle.cpp | 31 ++++++++++++++++++++++++++++++ common/error/error_handle.h | 36 +++++++++++++++++++++++++++++++++++ common/files/file_read.cpp | 22 +++++++++++++++++++++ common/files/file_read.h | 15 +++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 common/error/error_handle.cpp create mode 100644 common/error/error_handle.h create mode 100644 common/files/file_read.cpp create mode 100644 common/files/file_read.h diff --git a/common/error/error_handle.cpp b/common/error/error_handle.cpp new file mode 100644 index 0000000..a99aed4 --- /dev/null +++ b/common/error/error_handle.cpp @@ -0,0 +1,31 @@ +#include "error_handle.h" + +#include + +void __m_assert( + const char* expected_str, + bool expected, + const char* file, + int line, + const char* function, + const char* message +) { + if (expected) { + return; + } + + if(message) { + std::cerr << "M_ASSERT FAILED: " + << function << " [ERROR::" << expected_str << "] at [" << file << ":" << line << "]\n" + << "Message: " << message << "\n"; + } else { + std::cerr << "M_ASSERT FAILED: " + << function << " [ERROR::" << expected_str << "] at [" << file << ":" << line << "]\n"; + } + +#ifdef _WIN32 + __debugbreak(); +#else + abort(); +#endif +} diff --git a/common/error/error_handle.h b/common/error/error_handle.h new file mode 100644 index 0000000..94555e4 --- /dev/null +++ b/common/error/error_handle.h @@ -0,0 +1,36 @@ +#pragma once + +/** + * @brief Private function called only by the macro + * m_assert used for printing the assert + * + * @note Assert print is been called only if the given + * expectation is wrong. There are two print formats. + * The first is without a comment message and the + * second is with comment message. __FILE__, __LINE__ + * and __FUNCTION__ are equal of the file, line and + * funtion the M_ASSERT is being executed. __VA_ARGS__ + * is the representation of the comment message + * + * @param[in] expected_str + * @param[in] expected + * @param[in] file + * @param[in] line + * @param[in] function + * @param[in] message + */ +void __m_assert( + const char* expected_str, + bool expected, + const char* file, + int line, + const char* function, + const char* message = nullptr +); + +#ifdef NDEBUG + #define M_ASSERT(Expected, ...) +#else + #define M_ASSERT(Expected, ...) \ + __m_assert(#Expected, Expected, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__) +#endif diff --git a/common/files/file_read.cpp b/common/files/file_read.cpp new file mode 100644 index 0000000..e278c7b --- /dev/null +++ b/common/files/file_read.cpp @@ -0,0 +1,22 @@ +#include "file_read.h" + +#include + +#include "error/error_handle.h" + +std::string fileToString(const std::string &file_name) { + std::ifstream file(file_name); + + if(file_name.empty()) { return ""; } + + M_ASSERT(file.is_open()); + + std::string buffer = std::string( + std::istreambuf_iterator(file), + std::istreambuf_iterator() + ); + + M_ASSERT(!buffer.empty()); + + return buffer; +} diff --git a/common/files/file_read.h b/common/files/file_read.h new file mode 100644 index 0000000..fd1953e --- /dev/null +++ b/common/files/file_read.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +/** + * @brief Reads the given file by seeking through it. + * + * @note There is a check if the file_name is valid or not. + * We get the size of the file by seeing to the end of it + * and ftell(). The buffer we return is been filled by fread() + * + * @param[in] file_name + * @return std::string + */ +std::string fileToString(const std::string &file_name);