From 95c2b4624423309993bd27d8abc97a641510d302 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 11 Aug 2022 10:26:11 -0700 Subject: [PATCH] linux compatibility fixes --- CMakeLists.txt | 14 ++++++++++++-- include/cram.h | 8 ++++++++ src/cram.c | 28 ---------------------------- tools/cli/json_writer.h | 4 +++- tools/cli/lib/{ => windows}/dirent.h | 0 tools/cli/main.c | 10 +++++----- 6 files changed, 28 insertions(+), 36 deletions(-) rename tools/cli/lib/{ => windows}/dirent.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9088036..8462248 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,18 +57,27 @@ if(BUILD_SHARED_LIBS) endif() if(BUILD_CLI) - add_executable(cramcli - tools/cli/lib/dirent.h + file(GLOB CLI_SOURCES tools/cli/lib/stb_image_write.h tools/cli/json_writer.h tools/cli/main.c ) + if(WIN32) + list(APPEND CLI_SOURCES tools/cli/lib/dirent.h) + endif() + + add_executable(cramcli ${CLI_SOURCES}) + if(BUILD_SHARED_LIBS) target_link_libraries(cramcli PUBLIC Cram) else() target_link_libraries(cramcli PRIVATE Cram) endif() + + if(WIN32) + target_include_directories(cramcli PUBLIC $) + endif() endif() if(BUILD_SHARED_LIBS) @@ -80,6 +89,7 @@ endif() # Build flags if(NOT MSVC) set_property(TARGET Cram PROPERTY COMPILE_FLAGS "-std=gnu99 -Wall -Wno-strict-aliasing -pedantic") + target_link_libraries(Cram PUBLIC m) endif() # includes diff --git a/include/cram.h b/include/cram.h index df58882..cb60119 100644 --- a/include/cram.h +++ b/include/cram.h @@ -54,6 +54,14 @@ #define SEPARATOR '/' #endif +#ifndef max +#define max(x, y) (((x) > (y)) ? (x) : (y)) +#endif + +#ifndef min +#define min(x, y) (((x) < (y)) ? (x) : (y)) +#endif + #ifdef __cplusplus extern "C" { diff --git a/src/cram.c b/src/cram.c index 5d546a6..2eaba3d 100644 --- a/src/cram.c +++ b/src/cram.c @@ -104,19 +104,6 @@ typedef struct PackScoreInfo /* Pixel data functions */ -/* https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */ -static uint32_t Cram_Internal_NextPowerOfTwo(uint32_t v) -{ - v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v++; - return v; -} - static uint8_t Cram_Internal_IsImageEqual(Cram_Image *a, Cram_Image *b) { int32_t i; @@ -499,21 +486,6 @@ Cram_Context* Cram_Init(Cram_ContextCreateInfo *createInfo) return (Cram_Context*) context; } -static char* Cram_Internal_GetImageName(const char *path) -{ - char *lastSeparator = strrchr(path, SEPARATOR) + 1; - size_t returnBytes = strlen(lastSeparator) + 1; - char *name = malloc(returnBytes); - int32_t i; - - for (i = 0; i < returnBytes; i += 1) - { - name[i] = lastSeparator[i]; - } - - return name; -} - void Cram_AddFile(Cram_Context *context, const char *path) { Cram_Internal_Context *internalContext = (Cram_Internal_Context*) context; diff --git a/tools/cli/json_writer.h b/tools/cli/json_writer.h index 069ade9..62816ab 100644 --- a/tools/cli/json_writer.h +++ b/tools/cli/json_writer.h @@ -29,6 +29,8 @@ #include #include +#include +#include #define INITIAL_JSON_OUTPUT_CAPACITY 2048 @@ -114,7 +116,7 @@ void JsonBuilder_AppendStringProperty(JsonBuilder *builder, char *propertyName, void JsonBuilder_AppendIntProperty(JsonBuilder *builder, char *propertyName, int32_t value) { char buffer[65]; - itoa(value, buffer, 10); + sprintf(buffer, "%d", value); JsonBuilder_AppendProperty(builder, propertyName, buffer, 0); } diff --git a/tools/cli/lib/dirent.h b/tools/cli/lib/windows/dirent.h similarity index 100% rename from tools/cli/lib/dirent.h rename to tools/cli/lib/windows/dirent.h diff --git a/tools/cli/main.c b/tools/cli/main.c index b1a5232..54fdab0 100644 --- a/tools/cli/main.c +++ b/tools/cli/main.c @@ -24,8 +24,8 @@ * */ +#include #include "cram.h" -#include "dirent.h" #include "json_writer.h" #define STB_IMAGE_WRITE_IMPLEMENTATION @@ -48,7 +48,7 @@ static const char* GetFilenameExtension(const char *filename) /* Mostly taken from K&R C 2nd edition page 182 */ static void dirwalk(char *dir) { - dirent *dp; + struct dirent *dp; DIR *dfd; char subname[2048]; @@ -135,8 +135,8 @@ int main(int argc, char *argv[]) { Cram_ContextCreateInfo createInfo; uint8_t *pixelData; - uint32_t width; - uint32_t height; + int32_t width; + int32_t height; char *arg; char *inputDirPath = NULL; char *outputDirPath = NULL; @@ -299,7 +299,7 @@ int main(int argc, char *argv[]) return 1; } - fprintf(jsonOutput, jsonBuilder->string); + fprintf(jsonOutput, "%s", jsonBuilder->string); JsonBuilder_Destroy(jsonBuilder); fclose(jsonOutput);