49 lines
1.5 KiB
C++
49 lines
1.5 KiB
C++
#ifndef POKEMONSCRIPTTESTER_REQUIREMENTFAILED_HPP
|
|
#define POKEMONSCRIPTTESTER_REQUIREMENTFAILED_HPP
|
|
#include <exception>
|
|
#include <sstream>
|
|
|
|
class RequirementFailed : public std::exception {
|
|
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 = "") {
|
|
auto l = GetLine(path, line);
|
|
std::stringstream ss;
|
|
ss << "Requirement failed at: " << path << ":" << line << std::endl;
|
|
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; }
|
|
|
|
const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return _msg; }
|
|
};
|
|
|
|
#endif // POKEMONSCRIPTTESTER_REQUIREMENTFAILED_HPP
|