Fix some of the checkAPIs.pl warnings for g_warning.
[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  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifndef __WS_PRINTF_H__
24 #define __WS_PRINTF_H__
25
26 /*
27  * GLib's string utility routines are slow on windows, likely due to calling
28  * g_printf_string_upper_bound. Using ws_snprintf and ws_vsnprintf in hot
29  * code paths can speed up program execution. Otherwise you're probably safe
30  * sticking with g_snprintf and g_vsnprintf.
31  */
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif /* __cplusplus */
36
37 #ifdef _WIN32
38 #include <strsafe.h>
39
40 #if _MSC_VER < 1900
41 #include <stdarg.h>
42
43 /*
44  * vsnprintf_s's return value isn't compatible with C99 vsnprintf. We don't
45  * return anything in order to avoid confusion.
46  */
47
48 static __inline void
49 ws_vsnprintf(char *buffer, size_t size_of_buffer, const char *format, va_list argptr) {
50     /* We could alternatively use StringCchVPrintfA */
51     vsnprintf_s(buffer, size_of_buffer, _TRUNCATE, format, argptr);
52 }
53
54 #else /* _MSC_VER uses UCRT */
55
56 /* The UCRT versions of snprintf and vsnprintf conform to C99 */
57
58 static __inline void
59 ws_vsnprintf(char *buffer, size_t size_of_buffer, const char *format, va_list argptr)
60 {
61     vsnprintf(buffer, size_of_buffer, format, argptr);
62 }
63
64 #endif /* _MSC_VER */
65
66 #else /* _WIN32 */
67
68 #include <glib.h>
69
70 /*
71  * Use g_vsnprintf. On Linux and macOS these should be a thin wrapper around
72  * vsprintf.
73  */
74
75 static inline void
76 ws_vsnprintf(char *buffer, size_t size_of_buffer, const char *format, va_list argptr)
77 {
78     g_vsnprintf(buffer, (gulong) size_of_buffer, format, argptr);
79 }
80
81 #endif /* _WIN32 */
82
83 #ifdef _WIN32
84 static __inline void
85 #else
86 static inline void
87 #endif
88 ws_snprintf(char *buffer, size_t size_of_buffer, const char * format, ...) {
89     va_list argptr;
90
91     va_start(argptr, format);
92     ws_vsnprintf(buffer, size_of_buffer, format, argptr);
93     va_end(argptr);
94 }
95
96 /* This is intended to fool checkAPIs.pl for places that have "debugging"
97 (using printf) usually wrapped in an #ifdef, but checkAPIs.pl isn't smart
98 enough to figure that out.
99 Dissectors should still try to use proto_tree_add_debug_text when the
100 debugging context has a protocol tree.
101 */
102 #define ws_debug_printf     printf
103
104 /* This is intended to fool checkAPIs.pl for few places that have legitimate
105 use for g_warning. This should be used sparingly.
106 */
107 #define ws_g_warning  g_warning
108
109 #ifdef __cplusplus
110 }
111 #endif /* __cplusplus */
112
113 #endif /* __WS_PRINTF_H__ */