diff --git a/include/json/assertions.h b/include/json/assertions.h index 482c4ca48..9bf076f9a 100644 --- a/include/json/assertions.h +++ b/include/json/assertions.h @@ -7,7 +7,7 @@ #define CPPTL_JSON_ASSERTIONS_H_INCLUDED #include -#include +#include #if !defined(JSON_IS_AMALGAMATION) #include "config.h" diff --git a/include/json/config.h b/include/json/config.h index 33a84f5af..a36ca1555 100644 --- a/include/json/config.h +++ b/include/json/config.h @@ -5,8 +5,8 @@ #ifndef JSON_CONFIG_H_INCLUDED #define JSON_CONFIG_H_INCLUDED -#include -#include //typedef int64_t, uint64_t +#include +#include //typedef int64_t, uint64_t #include //typedef String /// If defined, indicates that json library is embedded in CppTL library. @@ -54,6 +54,14 @@ #define JSON_API #endif +#if defined(_MSC_VER) && _MSC_VER < 1900 +// As recommended at https://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 + extern JSON_API int msvc_pre1900_c99_snprintf(char *outBuf, size_t size, const char *format, ...); +# define jsoncpp_snprintf msvc_pre1900_c99_snprintf +#else +# define jsoncpp_snprintf std::snprintf +#endif + // If JSON_NO_INT64 is defined, then Json only support C++ "int" type for // integer // Storages, and 64 bits integer support is disabled. diff --git a/src/jsontestrunner/main.cpp b/src/jsontestrunner/main.cpp index 54ca30296..28691a2de 100644 --- a/src/jsontestrunner/main.cpp +++ b/src/jsontestrunner/main.cpp @@ -16,7 +16,7 @@ #include // sort #include #include -#include +#include struct Options { JSONCPP_STRING path; @@ -28,11 +28,7 @@ struct Options { static JSONCPP_STRING normalizeFloatingPointStr(double value) { char buffer[32]; -#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) - sprintf_s(buffer, sizeof(buffer), "%.16g", value); -#else - snprintf(buffer, sizeof(buffer), "%.16g", value); -#endif + jsoncpp_snprintf(buffer, sizeof(buffer), "%.16g", value); buffer[sizeof(buffer) - 1] = 0; JSONCPP_STRING s(buffer); JSONCPP_STRING::size_type index = s.find_last_of("eE"); @@ -105,11 +101,7 @@ static void printValueTree(FILE* fout, Json::ArrayIndex size = value.size(); for (Json::ArrayIndex index = 0; index < size; ++index) { static char buffer[16]; -#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) - sprintf_s(buffer, sizeof(buffer), "[%u]", index); -#else - snprintf(buffer, sizeof(buffer), "[%u]", index); -#endif + jsoncpp_snprintf(buffer, sizeof(buffer), "[%u]", index); printValueTree(fout, value[index], path + buffer); } } break; diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 3e58f44d0..ad04ba32b 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -22,21 +22,14 @@ #if __cplusplus >= 201103L #include -#if !defined(snprintf) -#define snprintf std::snprintf -#endif - #if !defined(sscanf) #define sscanf std::sscanf #endif #else -#include +#include #if defined(_MSC_VER) #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 -#if !defined(snprintf) -#define snprintf _snprintf -#endif #endif #endif @@ -813,7 +806,7 @@ JSONCPP_STRING Reader::getLocationLineAndColumn(Location location) const { int line, column; getLocationLineAndColumn(location, line, column); char buffer[18 + 16 + 16 + 1]; - snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column); + jsoncpp_snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column); return buffer; } @@ -1833,7 +1826,7 @@ JSONCPP_STRING OurReader::getLocationLineAndColumn(Location location) const { int line, column; getLocationLineAndColumn(location, line, column); char buffer[18 + 16 + 16 + 1]; - snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column); + jsoncpp_snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column); return buffer; } diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index c77d1df4d..ceb082196 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -10,7 +10,7 @@ #endif // if !defined(JSON_IS_AMALGAMATION) #include #include -#include +#include #include #include #ifdef JSON_USE_CPPTL @@ -19,6 +19,29 @@ #include // min() #include // size_t +// Provide implementation equivalent of std::snprintf for older _MSC compilers +#if defined(_MSC_VER) && _MSC_VER < 1900 +#include +static int msvc_pre1900_c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) +{ + int count = -1; + if (size != 0) + count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap); + if (count == -1) + count = _vscprintf(format, ap); + return count; +} + +int JSON_API msvc_pre1900_c99_snprintf(char *outBuf, size_t size, const char *format, ...) +{ + va_list ap; + va_start(ap, format); + const int count = msvc_pre1900_c99_vsnprintf(outBuf, size, format, ap); + va_end(ap); + return count; +} +#endif + // Disable warning C4702 : unreachable code #if defined(_MSC_VER) && _MSC_VER >= 1800 // VC++ 12.0 and above #pragma warning(disable : 4702) diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index a8b56ff80..91d97888b 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -27,12 +27,9 @@ #define isfinite std::isfinite #endif -#if !defined(snprintf) -#define snprintf std::snprintf -#endif #else -#include -#include +#include +#include #if defined(_MSC_VER) #if !defined(isnan) @@ -46,9 +43,6 @@ #endif #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 -#if !defined(snprintf) -#define snprintf _snprintf -#endif #endif #if defined(__sun) && defined(__SVR4) // Solaris @@ -145,7 +139,7 @@ JSONCPP_STRING valueToString(double value, JSONCPP_STRING buffer(size_t(36), '\0'); while (true) { - int len = snprintf( + int len = jsoncpp_snprintf( &*buffer.begin(), buffer.size(), (precisionType == PrecisionType::significantDigits) ? "%.*g" : "%.*f", precision, value); diff --git a/src/test_lib_json/jsontest.cpp b/src/test_lib_json/jsontest.cpp index 86ed4c17f..a578dbbe4 100644 --- a/src/test_lib_json/jsontest.cpp +++ b/src/test_lib_json/jsontest.cpp @@ -5,7 +5,7 @@ #define _CRT_SECURE_NO_WARNINGS 1 // Prevents deprecation warning with MSVC #include "jsontest.h" -#include +#include #include #if defined(_MSC_VER) diff --git a/src/test_lib_json/jsontest.h b/src/test_lib_json/jsontest.h index 9aab53880..33ebeb0b0 100644 --- a/src/test_lib_json/jsontest.h +++ b/src/test_lib_json/jsontest.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include // //////////////////////////////////////////////////////////////////