Utility.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "core/utils/Utility.hpp"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "ErrorSimulator.hpp"
  6. #include "core/utils/Logger.hpp"
  7. static Core::ExitHandler exitHandler = nullptr;
  8. static void* exitData = nullptr;
  9. void Core::exitWithHandler(const char* file, int line, int value) {
  10. if(value != 0) {
  11. printf("%sExit from %s:%d with value %d%s\n", Core::Logger::COLOR_RED,
  12. Core::Logger::getFileName(file), line, value,
  13. Core::Logger::COLOR_RESET);
  14. }
  15. if(exitHandler != nullptr) {
  16. exitHandler(value, exitData);
  17. }
  18. exit(value);
  19. }
  20. void Core::setExitHandler(ExitHandler eh, void* data) {
  21. exitHandler = eh;
  22. exitData = data;
  23. }
  24. #define CORE_TO_STRING(type, cast, format) \
  25. check_return Core::Error Core::toString(type t, char* buffer, int size) { \
  26. if(size < 0) { \
  27. return ErrorCode::NEGATIVE_ARGUMENT; \
  28. } \
  29. return snprintf(buffer, static_cast<unsigned int>(size), format, \
  30. static_cast<cast>(t)) >= size \
  31. ? ErrorCode::CAPACITY_REACHED \
  32. : ErrorCode::NONE; \
  33. }
  34. CORE_TO_STRING(signed short, signed short, "%hd")
  35. CORE_TO_STRING(unsigned short, unsigned short, "%hu")
  36. CORE_TO_STRING(signed int, signed int, "%d")
  37. CORE_TO_STRING(unsigned int, unsigned int, "%u")
  38. CORE_TO_STRING(signed long, signed long, "%ld")
  39. CORE_TO_STRING(unsigned long, unsigned long, "%lu")
  40. CORE_TO_STRING(signed long long, signed long long, "%lld")
  41. CORE_TO_STRING(unsigned long long, unsigned long long, "%llu")
  42. CORE_TO_STRING(float, double, "%.2f")
  43. CORE_TO_STRING(double, double, "%.2lf")
  44. CORE_TO_STRING(long double, long double, "%.2Lf")
  45. Core::Error Core::putChar(int c) {
  46. return putchar(c) == EOF ? ErrorCode::BLOCKED_STDOUT : ErrorCode::NONE;
  47. }
  48. Core::Error Core::putChars(const char* s) {
  49. return fputs(s, stdout) == EOF ? ErrorCode::BLOCKED_STDOUT
  50. : ErrorCode::NONE;
  51. }
  52. void Core::memorySet(void* p, int c, i64 n) {
  53. if(n <= 0) {
  54. return;
  55. }
  56. memset(p, c, static_cast<u64>(n));
  57. }
  58. void Core::memoryCopy(void* dest, const void* src, i64 n) {
  59. if(n <= 0) {
  60. return;
  61. }
  62. memcpy(dest, src, static_cast<u64>(n));
  63. }
  64. bool Core::memoryCompare(const void* a, const void* b, i64 n) {
  65. return n <= 0 ? true : memcmp(a, b, static_cast<u64>(n)) == 0;
  66. }
  67. Core::Error Core::reallocate(char*& p, i64 n) {
  68. if(n <= 0) {
  69. free(p);
  70. p = nullptr;
  71. return ErrorCode::NONE;
  72. }
  73. char* newP = static_cast<char*>(realloc(p, static_cast<u64>(n)));
  74. if(newP == nullptr || CORE_REALLOC_FAIL(newP)) {
  75. return ErrorCode::OUT_OF_MEMORY;
  76. }
  77. p = newP;
  78. return ErrorCode::NONE;
  79. }
  80. void Core::free(void* p) {
  81. ::free(p);
  82. }