From 7a452ff97f5dd81d6e9f886d2a7ec728570720ea Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Thu, 16 Dec 2021 12:18:30 -0500 Subject: [PATCH] add util.h single file header --- src/main.cpp | 2 ++ src/shader.cpp | 1 + src/util.h | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 src/util.h diff --git a/src/main.cpp b/src/main.cpp index 8e513e1..f382546 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,8 @@ #include "GLDebug.h" #include "shader.h" #include "types.h" +#define UTIL_IMPLEMENTATION +#include "util.h" struct SDLHandles diff --git a/src/shader.cpp b/src/shader.cpp index 0b0da01..ac41a26 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -13,6 +13,7 @@ #define GL_DEBUG_IMPLEMENTATION #include "GLDebug.h" #include "shader.h" +#include "util.h" diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..3fd505c --- /dev/null +++ b/src/util.h @@ -0,0 +1,36 @@ + +#include +#include + +#include "types.h" + + +char* utilAllocateCStr(const char* str, u32 max_len = 256); +void utilSafeFree(void* p); + + +#ifdef UTIL_IMPLEMENTATION + +char* +utilAllocateCStr(const char* str, u32 max_len) +{ + u32 len = strlen(str) + 1; + + if (len > max_len) { + printf("%s(), %s , longer than %i\n", __FUNCTION__, str, max_len); + return nullptr; + } + + char* out = (char*) std::calloc(len, sizeof(u8)); + strncpy(out, str, len - 1); + return out; +} + +void +utilSafeFree(void* p) +{ + assert(p != nullptr); + free(p); +} + +#endif