From 26e0bbe445f8b96a33549d1a8d725c7e0ab7f4ea Mon Sep 17 00:00:00 2001 From: Martin Fiedler Date: Thu, 5 Sep 2024 22:10:11 +0200 Subject: [PATCH] bare minimum changes to compile (partly) on MSVC Changes: - made it possible to override ANSILOVE_EXTERN with something that's not GNU specific - re-implemented strndup in compat/ This makes it possible to compile the core functionality of libansilove (specifically, ansilove_ansi() and friends) with MSVC, if some implementation of GD is available. The _init(), _loadfile() and _clean() functions don't work yet, as they are centered around POSIX-exclusive APIs. This functionality is easily replaced by a few lines of glue code though. --- CMakeLists.txt | 5 +++++ compat/strndup.c | 22 ++++++++++++++++++++++ compat/strndup.h | 1 + include/ansilove.h | 2 ++ src/loaders/ansi.c | 4 ++++ 5 files changed, 34 insertions(+) create mode 100644 compat/strndup.c create mode 100644 compat/strndup.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 095db79..b47e7e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ include(CheckFunctionExists) include(GNUInstallDirs) # Check if system has strtonum +check_function_exists(strndup HAVE_STRNDUP) check_function_exists(strtonum HAVE_STRTONUM) check_function_exists(reallocarray HAVE_REALLOCARRAY) @@ -29,6 +30,10 @@ include_directories(${GD_INCLUDE_DIRS}) set(SRC src/clean.c src/drawchar.c src/fonts.c src/error.c src/loadfile.c src/init.c src/output.c src/savefile.c) set(LOADERS src/loaders/ansi.c src/loaders/artworx.c src/loaders/binary.c src/loaders/icedraw.c src/loaders/pcboard.c src/loaders/tundra.c src/loaders/xbin.c) +if(NOT HAVE_STRNDUP) + set(SRC ${SRC} compat/strndup.c) +endif() + if(NOT HAVE_STRTONUM) set(SRC ${SRC} compat/strtonum.c) endif() diff --git a/compat/strndup.c b/compat/strndup.c new file mode 100644 index 0000000..2ead4fe --- /dev/null +++ b/compat/strndup.c @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: 2024 Martin J. Fiedler +// SPDX-License-Identifier: MIT + +#include + +#include +#include +#include + +#include "strndup.h" + +char *strndup(const char *str, size_t size) { + char *buffer; + size_t srclen = strlen(str); + if (srclen < size) { size = srclen; } + buffer = (char*) malloc(size + 1); + if (buffer) { + strncpy(buffer, str, size); + } + buffer[size] = '\0'; + return buffer; +} diff --git a/compat/strndup.h b/compat/strndup.h new file mode 100644 index 0000000..c65e9f2 --- /dev/null +++ b/compat/strndup.h @@ -0,0 +1 @@ +char *strndup(const char *str, size_t size); diff --git a/include/ansilove.h b/include/ansilove.h index e8d6970..e54e741 100644 --- a/include/ansilove.h +++ b/include/ansilove.h @@ -23,7 +23,9 @@ extern "C" { #endif +#ifndef ANSILOVE_EXTERN #define ANSILOVE_EXTERN __attribute__((visibility("default"))) +#endif /* Version number */ #define ANSILOVE_VERSION "1.4.1" diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c index eea5e23..04c1804 100644 --- a/src/loaders/ansi.c +++ b/src/loaders/ansi.c @@ -25,6 +25,10 @@ #include "fonts.h" #include "output.h" +#ifndef HAVE_STRNDUP +#include "strndup.h" +#endif + #ifndef HAVE_STRTONUM #include "strtonum.h" #endif