WSUG: Fixup some quotes and apostrophes.
[metze/wireshark/wip.git] / wsutil / ws_printf.h
1 /*
2  * Wrappers for printf like functions.
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 2007 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #ifndef __WS_PRINTF_H__
12 #define __WS_PRINTF_H__
13
14 /*
15  * GLib's string utility routines are slow on windows, likely due to calling
16  * g_printf_string_upper_bound. Using ws_snprintf and ws_vsnprintf in hot
17  * code paths can speed up program execution. Otherwise you're probably safe
18  * sticking with g_snprintf and g_vsnprintf.
19  */
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
24
25 #ifdef _WIN32
26 #include <strsafe.h>
27
28 #if _MSC_VER < 1900
29 #include <stdarg.h>
30
31 /*
32  * vsnprintf_s's return value isn't compatible with C99 vsnprintf. We don't
33  * return anything in order to avoid confusion.
34  */
35
36 static __inline void
37 ws_vsnprintf(char *buffer, size_t size_of_buffer, const char *format, va_list argptr) {
38     /* We could alternatively use StringCchVPrintfA */
39     vsnprintf_s(buffer, size_of_buffer, _TRUNCATE, format, argptr);
40 }
41
42 #else /* _MSC_VER uses UCRT */
43
44 /* The UCRT versions of snprintf and vsnprintf conform to C99 */
45
46 static __inline void
47 ws_vsnprintf(char *buffer, size_t size_of_buffer, const char *format, va_list argptr)
48 {
49     vsnprintf(buffer, size_of_buffer, format, argptr);
50 }
51
52 #endif /* _MSC_VER */
53
54 #else /* _WIN32 */
55
56 #include <glib.h>
57
58 /*
59  * Use g_vsnprintf. On Linux and macOS these should be a thin wrapper around
60  * vsprintf.
61  */
62
63 static inline void
64 ws_vsnprintf(char *buffer, size_t size_of_buffer, const char *format, va_list argptr)
65 {
66     g_vsnprintf(buffer, (gulong) size_of_buffer, format, argptr);
67 }
68
69 #endif /* _WIN32 */
70
71 #ifdef _WIN32
72 static __inline void
73 #else
74 static inline void
75 #endif
76 ws_snprintf(char *buffer, size_t size_of_buffer, const char * format, ...) {
77     va_list argptr;
78
79     va_start(argptr, format);
80     ws_vsnprintf(buffer, size_of_buffer, format, argptr);
81     va_end(argptr);
82 }
83
84 /* This is intended to fool checkAPIs.pl for places that have "debugging"
85 (using printf) usually wrapped in an #ifdef, but checkAPIs.pl isn't smart
86 enough to figure that out.
87 Dissectors should still try to use proto_tree_add_debug_text when the
88 debugging context has a protocol tree.
89 */
90 #define ws_debug_printf     printf
91
92 /* This is intended to fool checkAPIs.pl for few places that have legitimate
93 use for g_warning. This should be used sparingly.
94 */
95 #define ws_g_warning  g_warning
96
97 #ifdef __cplusplus
98 }
99 #endif /* __cplusplus */
100
101 #endif /* __WS_PRINTF_H__ */