forked from project-gemmi/gemmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprintf.cpp
68 lines (61 loc) · 1.85 KB
/
sprintf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2017 Global Phasing Ltd.
#include <gemmi/sprintf.hpp>
#include <stdarg.h> // for va_list
#ifdef USE_STD_SNPRINTF // useful for benchmarking and testing only
# include <cstdio>
# include <algorithm> // for min
#else
# define STB_SPRINTF_IMPLEMENTATION
# define STB_SPRINTF_STATIC
# define STB_SPRINTF_NOUNALIGNED 1
// Making functions from stb_sprintf static may trigger warnings.
# if defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wunused-function"
# endif
# if defined(__clang__)
# pragma clang diagnostic ignored "-Wunused-function"
# endif
// To use system stb_sprintf.h (not recommended, but some Linux distros
// don't like bundled libraries) define GEMMI_USE_SYSTEM_STB or remove
// third_party/stb_sprintf.h.
# if defined(__has_include)
# if !__has_include("../third_party/stb_sprintf.h")
# define GEMMI_USE_SYSTEM_STB 1
# endif
# endif
# ifdef GEMMI_USE_SYSTEM_STB
# pragma message("Using system stb_sprintf.h, not the bundled one. It may not work.")
# include <stb/stb_sprintf.h>
# else
# include "../third_party/stb_sprintf.h"
# endif
#endif // USE_STD_SNPRINTF
namespace gemmi {
// We copy functions from sprintf.h only to have them declared with GEMMI_DLL.
int sprintf_z(char *buf, char const *fmt, ...) {
int result;
va_list va;
va_start(va, fmt);
#ifdef USE_STD_SNPRINTF
result = std::vsprintf(buf, fmt, va);
#else
result = STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);
#endif
va_end(va);
return result;
}
int snprintf_z(char *buf, int count, char const *fmt, ...) {
int result;
va_list va;
va_start(va, fmt);
#ifdef USE_STD_SNPRINTF
result = std::vsnprintf(buf, count, fmt, va);
// stbsp_snprintf always returns a zero-terminated string
buf[std::min(result, count-1)] = '\0';
#else
result = STB_SPRINTF_DECORATE(vsnprintf)(buf, count, fmt, va);
#endif
va_end(va);
return result;
}
} // namespace gemmi