diff --git a/CMakeLists.txt b/CMakeLists.txt index b5f7f55b..a54a9493 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -131,8 +131,13 @@ check_symbol_exists(funopen stdio.h HAVE_OPEN_FUNOPEN) check_symbol_exists(fopencookie stdio.h HAVE_OPEN_FOPENCOOKIE) if(NOT HAVE_OPEN_MEMSTREAM) - if (HAVE_OPEN_FUNOPEN AND HAVE_OPEN_FOPENCOOKIE) - message(STATUS "using open_memstream implementation") + if (HAVE_OPEN_FUNOPEN) + message(STATUS "implementing open_memstream using funopen()") + target_compile_definitions(tinycbor PRIVATE HAVE_OPEN_FUNOPEN) + target_sources(tinycbor PRIVATE src/open_memstream.c) + elseif (HAVE_OPEN_FOPENCOOKIE) + message(STATUS "implementing open_memstream using fopencookie()") + target_compile_definitions(tinycbor PRIVATE HAVE_OPEN_FOPENCOOKIE) target_sources(tinycbor PRIVATE src/open_memstream.c) else() target_compile_definitions(tinycbor PRIVATE WITHOUT_OPEN_MEMSTREAM) diff --git a/src/open_memstream.c b/src/open_memstream.c index 33653784..ed94e898 100644 --- a/src/open_memstream.c +++ b/src/open_memstream.c @@ -35,10 +35,10 @@ #if defined(__unix__) || defined(__APPLE__) # include #endif -#ifdef __APPLE__ +#if defined(HAVE_OPEN_FUNOPEN) typedef int RetType; typedef int LenType; -#elif __linux__ +#elif defined(HAVE_OPEN_FOPENCOOKIE) typedef ssize_t RetType; typedef size_t LenType; #else @@ -99,9 +99,9 @@ FILE *open_memstream(char **bufptr, size_t *lenptr) *bufptr = NULL; *lenptr = 0; -#ifdef __APPLE__ +#if defined(HAVE_OPEN_FUNOPEN) return funopen(b, NULL, write_to_buffer, NULL, close_buffer); -#elif __linux__ +#elif defined(HAVE_OPEN_FOPENCOOKIE) static const cookie_io_functions_t vtable = { NULL, write_to_buffer,