12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #include "core/utils/Utility.hpp"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "ErrorSimulator.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) \
- check_return Core::Error Core::toString(type t, char* buffer, int size) { \
- if(size < 0) { \
- return Error::NEGATIVE_ARGUMENT; \
- } \
- return snprintf(buffer, static_cast<unsigned int>(size), format, \
- static_cast<cast>(t)) >= size \
- ? Error::CAPACITY_REACHED \
- : Error::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")
- Core::Error Core::putChar(int c) {
- return putchar(c) == EOF ? Error::BLOCKED_STDOUT : Error::NONE;
- }
- void Core::memorySet(void* p, int c, i64 n) {
- if(n <= 0) {
- return;
- }
- memset(p, c, static_cast<u64>(n));
- }
- void Core::memoryCopy(void* dest, const void* src, i64 n) {
- if(n <= 0) {
- return;
- }
- memcpy(dest, src, static_cast<u64>(n));
- }
- bool Core::memoryCompare(const void* a, const void* b, i64 n) {
- return n <= 0 ? true : memcmp(a, b, static_cast<u64>(n)) == 0;
- }
- Core::Error Core::reallocate(char*& p, i64 n) {
- if(n <= 0) {
- free(p);
- p = nullptr;
- return Error::NONE;
- }
- char* newP = static_cast<char*>(realloc(p, static_cast<u64>(n)));
- if(newP == nullptr || CORE_REALLOC_FAIL(newP)) {
- return Error::OUT_OF_MEMORY;
- }
- p = newP;
- return Error::NONE;
- }
- void Core::free(void* p) {
- ::free(p);
- }
|