Skip to content

Commit

Permalink
Common error handling & file read
Browse files Browse the repository at this point in the history
  • Loading branch information
V-KMilev committed Mar 10, 2023
1 parent 8ecf21b commit 99932b9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
31 changes: 31 additions & 0 deletions common/error/error_handle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "error_handle.h"

#include <iostream>

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
}
36 changes: 36 additions & 0 deletions common/error/error_handle.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions common/files/file_read.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "file_read.h"

#include <fstream>

#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<char>(file),
std::istreambuf_iterator<char>()
);

M_ASSERT(!buffer.empty());

return buffer;
}
15 changes: 15 additions & 0 deletions common/files/file_read.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <string>

/**
* @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);

0 comments on commit 99932b9

Please sign in to comment.