Skip to content

Commit d52f842

Browse files
committed
ENH: MSVS 2013 snprintf compatible substitute
Simplify the backwards compatible snprintf configuration for pre 1900 version of MSVC. Otherwise prefer C++11 syntax using std::snprintf.
1 parent 6219eae commit d52f842

File tree

5 files changed

+42
-27
lines changed

5 files changed

+42
-27
lines changed

include/json/config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
#define JSON_API
5555
#endif
5656

57+
#if defined(_MSC_VER) && _MSC_VER < 1900
58+
// As recommended at https://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
59+
# define jsoncpp_snprintf msvc_pre1900_c99_snprintf
60+
extern int msvc_pre1900_c99_snprintf(char *outBuf, size_t size, const char *format, ...);
61+
#else
62+
# define jsoncpp_snprintf std::snprintf
63+
#endif
64+
5765
// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for
5866
// integer
5967
// Storages, and 64 bits integer support is disabled.

src/jsontestrunner/main.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ struct Options {
2828

2929
static JSONCPP_STRING normalizeFloatingPointStr(double value) {
3030
char buffer[32];
31-
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
32-
sprintf_s(buffer, sizeof(buffer), "%.16g", value);
33-
#else
34-
snprintf(buffer, sizeof(buffer), "%.16g", value);
35-
#endif
31+
jsoncpp_snprintf(buffer, sizeof(buffer), "%.16g", value);
3632
buffer[sizeof(buffer) - 1] = 0;
3733
JSONCPP_STRING s(buffer);
3834
JSONCPP_STRING::size_type index = s.find_last_of("eE");
@@ -105,11 +101,7 @@ static void printValueTree(FILE* fout,
105101
Json::ArrayIndex size = value.size();
106102
for (Json::ArrayIndex index = 0; index < size; ++index) {
107103
static char buffer[16];
108-
#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
109-
sprintf_s(buffer, sizeof(buffer), "[%u]", index);
110-
#else
111-
snprintf(buffer, sizeof(buffer), "[%u]", index);
112-
#endif
104+
jsoncpp_snprintf(buffer, sizeof(buffer), "[%u]", index);
113105
printValueTree(fout, value[index], path + buffer);
114106
}
115107
} break;

src/lib_json/json_reader.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,14 @@
2222
#if __cplusplus >= 201103L
2323
#include <cstdio>
2424

25-
#if !defined(snprintf)
26-
#define snprintf std::snprintf
27-
#endif
28-
2925
#if !defined(sscanf)
3026
#define sscanf std::sscanf
3127
#endif
3228
#else
33-
#include <stdio.h>
29+
#include <cstdio>
3430

3531
#if defined(_MSC_VER)
3632
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
37-
#if !defined(snprintf)
38-
#define snprintf _snprintf
39-
#endif
4033
#endif
4134
#endif
4235

@@ -813,7 +806,7 @@ JSONCPP_STRING Reader::getLocationLineAndColumn(Location location) const {
813806
int line, column;
814807
getLocationLineAndColumn(location, line, column);
815808
char buffer[18 + 16 + 16 + 1];
816-
snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column);
809+
jsoncpp_snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column);
817810
return buffer;
818811
}
819812

@@ -1833,7 +1826,7 @@ JSONCPP_STRING OurReader::getLocationLineAndColumn(Location location) const {
18331826
int line, column;
18341827
getLocationLineAndColumn(location, line, column);
18351828
char buffer[18 + 16 + 16 + 1];
1836-
snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column);
1829+
jsoncpp_snprintf(buffer, sizeof(buffer), "Line %d, Column %d", line, column);
18371830
return buffer;
18381831
}
18391832

src/lib_json/json_value.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,34 @@
1919
#include <algorithm> // min()
2020
#include <cstddef> // size_t
2121

22+
23+
#if defined(_MSC_VER) && _MSC_VER < 1900
24+
static int msvc_pre1900_c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap)
25+
{
26+
int count = -1;
27+
28+
if (size != 0)
29+
count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
30+
if (count == -1)
31+
count = _vscprintf(format, ap);
32+
33+
return count;
34+
}
35+
36+
int msvc_pre1900_c99_snprintf(char *outBuf, size_t size, const char *format, ...)
37+
{
38+
int count;
39+
va_list ap;
40+
41+
va_start(ap, format);
42+
count = msvc_pre1900_c99_vsnprintf(outBuf, size, format, ap);
43+
va_end(ap);
44+
45+
return count;
46+
}
47+
48+
#endif
49+
2250
// Disable warning C4702 : unreachable code
2351
#if defined(_MSC_VER) && _MSC_VER >= 1800 // VC++ 12.0 and above
2452
#pragma warning(disable : 4702)

src/lib_json/json_writer.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
#define isfinite std::isfinite
2828
#endif
2929

30-
#if !defined(snprintf)
31-
#define snprintf std::snprintf
32-
#endif
3330
#else
3431
#include <math.h>
3532
#include <stdio.h>
@@ -46,9 +43,6 @@
4643
#endif
4744

4845
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
49-
#if !defined(snprintf)
50-
#define snprintf _snprintf
51-
#endif
5246
#endif
5347

5448
#if defined(__sun) && defined(__SVR4) // Solaris
@@ -145,7 +139,7 @@ JSONCPP_STRING valueToString(double value,
145139

146140
JSONCPP_STRING buffer(size_t(36), '\0');
147141
while (true) {
148-
int len = snprintf(
142+
int len = jsoncpp_snprintf(
149143
&*buffer.begin(), buffer.size(),
150144
(precisionType == PrecisionType::significantDigits) ? "%.*g" : "%.*f",
151145
precision, value);

0 commit comments

Comments
 (0)