Skip to content

Commit

Permalink
CDRIVER-995 config check for _set_output_format, timespec
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdavis committed Nov 16, 2015
1 parent 4fb312c commit 4df8a8d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
27 changes: 18 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project (libbson)

include(CheckFunctionExists)
include(CheckIncludeFile)
include(CheckStructHasMember)
include(CheckSymbolExists)
include(TestBigEndian)
include(InstallRequiredSystemLibraries)

Expand Down Expand Up @@ -34,14 +36,25 @@ if (RT_LIBRARY)
set(CMAKE_REQUIRED_LIBRARIES ${RT_LIBRARY})
endif()

# See https://public.kitware.com/Bug/view.php?id=15659
CHECK_SYMBOL_EXISTS(snprintf stdio.h BSON_HAVE_SNPRINTF)
if (NOT BSON_HAVE_SNPRINTF)
set(BSON_HAVE_SNPRINTF 0)
endif ()

CHECK_FUNCTION_EXISTS(_set_output_format BSON_NEEDS_SET_OUTPUT_FORMAT)
if (NOT BSON_NEEDS_SET_OUTPUT_FORMAT)
set(BSON_NEEDS_SET_OUTPUT_FORMAT 0)
endif ()

CHECK_STRUCT_HAS_MEMBER("struct timespec" tv_sec time.h BSON_HAVE_TIMESPEC)
if (NOT BSON_HAVE_TIMESPEC)
set(BSON_HAVE_TIMESPEC 0)
endif ()

if (MSVC)
set (BSON_OS 2)
set (BSON_HAVE_CLOCK_GETTIME 0)
if (MSVC14)
set (BSON_HAVE_SNPRINTF 1)
else()
set (BSON_HAVE_SNPRINTF 0)
endif()
set (BSON_HAVE_STDBOOL_H 0)
set (BSON_HAVE_STRNLEN 0)
set (BSON_EXTRA_ALIGN 1)
Expand All @@ -57,10 +70,6 @@ else ()
if (NOT BSON_HAVE_STRNLEN)
set(BSON_HAVE_STRNLEN 0)
endif ()
CHECK_FUNCTION_EXISTS(snprintf BSON_HAVE_SNPRINTF)
if (NOT BSON_HAVE_SNPRINTF)
set(BSON_HAVE_SNPRINTF 0)
endif ()
CHECK_INCLUDE_FILE(stdbool.h BSON_HAVE_STDBOOL_H)
if (NOT BSON_HAVE_STDBOOL_H)
set(BSON_HAVE_STDBOOL_H 0)
Expand Down
8 changes: 8 additions & 0 deletions build/autotools/FindDependencies.m4
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ AC_CHECK_FUNC(strnlen, [AC_SUBST(BSON_HAVE_STRNLEN, 1)])
AC_SUBST(BSON_HAVE_SNPRINTF, 0)
AC_CHECK_FUNC(snprintf, [AC_SUBST(BSON_HAVE_SNPRINTF, 1)])

# Check for _set_output_format (unlikely, only Visual Studio 2013 and older)
AC_SUBST(BSON_NEEDS_SET_OUTPUT_FORMAT, 0)
AC_CHECK_FUNCS(_set_output_format, [AC_SUBST(BSON_NEEDS_SET_OUTPUT_FORMAT, 1)])

# Check for struct timespec
AC_SUBST(BSON_HAVE_TIMESPEC, 0)
AC_CHECK_TYPES([struct timespec], [AC_SUBST(BSON_HAVE_TIMESPEC, 1)])

# Check for clock_gettime and if it needs -lrt
AC_SUBST(BSON_HAVE_CLOCK_GETTIME, 0)
AC_SEARCH_LIBS([clock_gettime], [rt], [AC_SUBST(BSON_HAVE_CLOCK_GETTIME, 1)])
Expand Down
17 changes: 17 additions & 0 deletions src/bson/bson-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@
#endif


/*
* Define to 1 if you have _set_output_format (VS2013 and older).
*/
#define BSON_NEEDS_SET_OUTPUT_FORMAT @BSON_NEEDS_SET_OUTPUT_FORMAT@
#if BSON_NEEDS_SET_OUTPUT_FORMAT != 1
# undef BSON_NEEDS_SET_OUTPUT_FORMAT
#endif

/*
* Define to 1 if you have struct timespec available on your platform.
*/
#define BSON_HAVE_TIMESPEC @BSON_HAVE_TIMESPEC@
#if BSON_HAVE_TIMESPEC != 1
# undef BSON_HAVE_TIMESPEC
#endif


/*
* Define to 1 if you want extra aligned types in libbson
*/
Expand Down
9 changes: 2 additions & 7 deletions src/bson/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
*/


#if defined(_MSC_VER) && (_MSC_VER >= 1400 && _MSC_VER < 1900)
#define NEEDS_SET_TWO_DIGIT_EXPONENT
#include <stdio.h> // _set_output_format and _TWO_DIGIT_EXPONENT Added in vs2005 and removed in vs2015.
#endif

#include "bson.h"
#include "b64_ntop.h"
#include "bson-private.h"
Expand Down Expand Up @@ -2336,13 +2331,13 @@ _bson_as_json_visit_double (const bson_iter_t *iter,
{
bson_json_state_t *state = data;

#ifdef NEEDS_SET_TWO_DIGIT_EXPONENT
#if BSON_NEEDS_SET_OUTPUT_FORMAT
unsigned int current_format = _set_output_format(_TWO_DIGIT_EXPONENT);
#endif

bson_string_append_printf (state->str, "%.15g", v_double);

#ifdef NEEDS_SET_TWO_DIGIT_EXPONENT
#if BSON_NEEDS_SET_OUTPUT_FORMAT
_set_output_format(current_format);
#endif

Expand Down
2 changes: 1 addition & 1 deletion tests/TestSuite.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
# define AtomicInt_DecrementAndTest(p) (bson_atomic_int_add(p, -1) == 0)


#if defined(_MSC_VER) && _MSC_VER < 1900
#if !defined(BSON_HAVE_TIMESPEC)
struct timespec
{
time_t tv_sec;
Expand Down

0 comments on commit 4df8a8d

Please sign in to comment.