#include "core/utils/Utility.hpp" #include #include #include #include "ErrorSimulator.hpp" #include "core/utils/Error.hpp" #include "core/utils/Logger.hpp" static Core::ExitHandler exitHandler = nullptr; static void* exitData = nullptr; void Core::exitWithHandler(const char* file, int line, int value) { if(value != 0) { printf("%sExit from %s:%d with value %d%s\n", Core::Logger::COLOR_RED, Core::Logger::getFileName(file), line, value, Core::Logger::COLOR_RESET); } if(exitHandler != nullptr) { exitHandler(value, exitData); } exit(value); } void Core::setExitHandler(ExitHandler eh, void* data) { exitHandler = eh; exitData = data; } #define CORE_TO_STRING(type, cast, format) \ CError Core::toString(type t, char* buffer, size_t size) { \ int w = snprintf(buffer, size, format, static_cast(t)); \ if(w < 0) { \ return ErrorCode::ERROR; \ } \ return static_cast(w) >= size ? ErrorCode::CAPACITY_REACHED \ : ErrorCode::NONE; \ } CORE_TO_STRING(signed short, signed short, "%hd") CORE_TO_STRING(unsigned short, unsigned short, "%hu") CORE_TO_STRING(signed int, signed int, "%d") CORE_TO_STRING(unsigned int, unsigned int, "%u") CORE_TO_STRING(signed long, signed long, "%ld") CORE_TO_STRING(unsigned long, unsigned long, "%lu") CORE_TO_STRING(signed long long, signed long long, "%lld") CORE_TO_STRING(unsigned long long, unsigned long long, "%llu") CORE_TO_STRING(float, double, "%.2f") CORE_TO_STRING(double, double, "%.2lf") CORE_TO_STRING(long double, long double, "%.2Lf") void Core::print(int c) { if(putchar(c) < 0) { CORE_EXIT(static_cast(ErrorCode::BLOCKED_STDOUT)); } } void Core::print(const char* s) { if(fputs(s, stdout) < 0) { CORE_EXIT(static_cast(ErrorCode::BLOCKED_STDOUT)); } } void Core::printLine(const char* s) { if(puts(s) < 0) { CORE_EXIT(static_cast(ErrorCode::BLOCKED_STDOUT)); } } Core::Error Core::reallocate(char*& p, size_t n) { if(n <= 0) { free(p); p = nullptr; return ErrorCode::NONE; } char* newP = static_cast(realloc(p, n)); if(newP == nullptr || CORE_REALLOC_FAIL(newP)) { return ErrorCode::OUT_OF_MEMORY; } p = newP; return ErrorCode::NONE; }