gmio_core/internal: add portable snprintf() wrapper

This commit is contained in:
Hugues Delorme 2016-03-08 17:28:43 +01:00
parent ae24778347
commit 10e2102b29
4 changed files with 58 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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 <stddef.h>
#include <stdio.h>
/* 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 */

View File

@ -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);
}