123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #include "utils/Utility.hpp"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- static Core::ExitHandler exitHandler = nullptr;
- static void* exitData = nullptr;
- void Core::exitWithHandler(const char* file, int line, int value) {
- if(value != 0) {
- printf("\33[1;31mExit from %s:%d with value %d\33[39;49m\n", file, line,
- value);
- }
- 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, int n) {
- if(n <= 0) {
- return;
- }
- memset(p, c, static_cast<unsigned int>(n));
- }
- void Core::memoryCopy(void* dest, const void* src, int n) {
- if(n <= 0) {
- return;
- }
- memcpy(dest, src, static_cast<unsigned int>(n));
- }
- bool Core::memoryCompare(const void* a, const void* b, int n) {
- return n <= 0 ? true : memcmp(a, b, static_cast<unsigned int>(n)) == 0;
- }
- Core::Error Core::reallocate(char*& p, int n) {
- if(n <= 0) {
- free(p);
- p = nullptr;
- return Error::NONE;
- }
- char* newP = static_cast<char*>(realloc(p, static_cast<unsigned int>(n)));
- if(newP == nullptr) {
- return Error::OUT_OF_MEMORY;
- }
- p = newP;
- return Error::NONE;
- }
- void Core::free(void* p) {
- ::free(p);
- }
|