PokemonScriptTester/src/Tester/RequirementFailed.hpp

49 lines
1.5 KiB
C++
Raw Permalink Normal View History

#ifndef POKEMONSCRIPTTESTER_REQUIREMENTFAILED_HPP
#define POKEMONSCRIPTTESTER_REQUIREMENTFAILED_HPP
2021-08-22 17:03:03 +00:00
#include <exception>
#include <sstream>
class RequirementFailed : public std::exception {
2021-08-22 17:03:03 +00:00
static inline std::string& ltrim(std::string& s, const char* t = " \t\n\r\f\v") {
s.erase(0, s.find_first_not_of(t));
return s;
}
static std::string GetLine(const std::string& path, size_t num) {
std::ifstream file;
file.open(path);
file.seekg(std::ios::beg);
for (size_t i = 0; i < num - 1; ++i) {
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::string line;
std::getline(file, line);
file.close();
line = ltrim(line);
return line;
}
char* _msg;
public:
RequirementFailed(const std::string& path, size_t line, const std::string& message = "") {
2021-08-22 17:03:03 +00:00
auto l = GetLine(path, line);
std::stringstream ss;
ss << "Requirement failed at: " << path << ":" << line << std::endl;
2021-08-22 17:03:03 +00:00
ss << l;
if (message.length() > 0) {
ss << std::endl << message;
}
auto str = ss.str();
_msg = new char[str.length() + 1];
str.copy(_msg, str.length(), 0);
_msg[str.length()] = 0;
}
~RequirementFailed() { delete[] _msg; }
2021-08-22 17:03:03 +00:00
const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return _msg; }
};
#endif // POKEMONSCRIPTTESTER_REQUIREMENTFAILED_HPP