1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include "core/utils/Utility.hpp"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #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<cast>(t)); \
- if(w < 0) { \
- return ErrorCode::ERROR; \
- } \
- return static_cast<size_t>(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<int>(ErrorCode::BLOCKED_STDOUT));
- }
- }
- void Core::print(const char* s) {
- if(fputs(s, stdout) < 0) {
- CORE_EXIT(static_cast<int>(ErrorCode::BLOCKED_STDOUT));
- }
- }
- void Core::printLine(const char* s) {
- if(puts(s) < 0) {
- CORE_EXIT(static_cast<int>(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<char*>(realloc(p, n));
- if(newP == nullptr || CORE_REALLOC_FAIL(newP)) {
- return ErrorCode::OUT_OF_MEMORY;
- }
- p = newP;
- return ErrorCode::NONE;
- }
|