From 10e2102b291af6f53c9d57b9c5f0b51f365ba6b4 Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Tue, 8 Mar 2016 17:28:43 +0100 Subject: [PATCH] gmio_core/internal: add portable snprintf() wrapper --- CMakeLists.txt | 5 +++ src/gmio_core/config.h.cmake | 2 + src/gmio_core/internal/c99_stdio_compat.h | 46 +++++++++++++++++++++++ src/gmio_stl/stla_read.c | 9 +++-- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/gmio_core/internal/c99_stdio_compat.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c0ad67..de60ce8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,6 +130,11 @@ if(NOT GMIO_BUILD_STRICT_C90) set(CMAKE_REQUIRED_LIBRARIES) # Pop changes check_include_files(stdbool.h GMIO_HAVE_STDBOOL_H) + + check_function_exists(snprintf GMIO_HAVE_SNPRINTF_FUNC) + if(WIN32 AND NOT GMIO_HAVE_SNPRINTF_FUNC) + check_function_exists(_snprintf GMIO_HAVE_WIN__SNPRINTF_FUNC) + endif() endif() # Check POSIX features diff --git a/src/gmio_core/config.h.cmake b/src/gmio_core/config.h.cmake index 2856ff9..0a34baa 100644 --- a/src/gmio_core/config.h.cmake +++ b/src/gmio_core/config.h.cmake @@ -42,6 +42,8 @@ #cmakedefine GMIO_HAVE_STRTOF_FUNC #cmakedefine GMIO_HAVE_POWF_FUNC +#cmakedefine GMIO_HAVE_SNPRINTF_FUNC +#cmakedefine GMIO_HAVE_WIN__SNPRINTF_FUNC /* POSIX */ #cmakedefine GMIO_HAVE_SYS_TYPES_H diff --git a/src/gmio_core/internal/c99_stdio_compat.h b/src/gmio_core/internal/c99_stdio_compat.h new file mode 100644 index 0000000..2bfd9ee --- /dev/null +++ b/src/gmio_core/internal/c99_stdio_compat.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** gmio +** Copyright Fougue (2 Mar. 2015) +** contact@fougue.pro +** +** This software is a reusable library whose purpose is to provide complete +** I/O support for various CAD file formats (eg. STL) +** +** This software is governed by the CeCILL-B license under French law and +** abiding by the rules of distribution of free software. You can use, +** modify and/ or redistribute the software under the terms of the CeCILL-B +** license as circulated by CEA, CNRS and INRIA at the following URL +** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html". +****************************************************************************/ + +#ifndef GMIO_INTERNAL_C99_STDIO_COMPAT_H +#define GMIO_INTERNAL_C99_STDIO_COMPAT_H + +#include "../global.h" + +#include +#include + +/* Note: + * Visual C++ provides snprintf() since version 2015, before that it was + * used to provide only equivalent _snprintf() + * + * snprintf() appears in C99 + */ + +#ifdef GMIO_HAVE_SNPRINTF_FUNC +# define gmio_snprintf snprintf +#elif defined(GMIO_HAVE_WIN__SNPRINTF_FUNC) +# define gmio_snprintf _snprintf +#else +/* No existing snprintf()-like function, call unsafe vsprintf() as fallback */ +GMIO_INLINE int gmio_snprintf(char* buf, size_t bufn, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); +} +#endif + +#endif /* GMIO_INTERNAL_C99_STDIO_COMPAT_H */ diff --git a/src/gmio_stl/stla_read.c b/src/gmio_stl/stla_read.c index c563203..a193b2b 100644 --- a/src/gmio_stl/stla_read.c +++ b/src/gmio_stl/stla_read.c @@ -22,6 +22,7 @@ #include "internal/stla_parsing.h" #include "../gmio_core/error.h" +#include "../gmio_core/internal/c99_stdio_compat.h" #include "../gmio_core/internal/helper_memblock.h" #include "../gmio_core/internal/helper_stream.h" #include "../gmio_core/internal/helper_task_iface.h" @@ -370,10 +371,10 @@ void stla_error_token_expected( struct gmio_stla_parse_data* data, enum gmio_stla_token token) { char msg[256] = {0}; - snprintf(msg, sizeof(msg), - "expected <%s>, got <%s>", - stla_token_to_string(token), - stla_token_to_string(data->token)); + gmio_snprintf(msg, sizeof(msg), + "expected <%s>, got <%s>", + stla_token_to_string(token), + stla_token_to_string(data->token)); stla_error_msg(data, msg); }