config: Add missing define for (v)snprintf functions.
[platform/upstream/cmocka.git] / include / cmocka_private.h
1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef CMOCKA_PRIVATE_H_
18 #define CMOCKA_PRIVATE_H_
19
20 #include "config.h"
21
22 #ifdef _WIN32
23 #include <windows.h>
24
25 # ifdef _MSC_VER
26
27 #  undef inline
28 #  define inline __inline
29
30 #  define strcasecmp _stricmp
31 #  define strncasecmp _strnicmp
32
33 #  if defined(HAVE__SNPRINTF_S)
34 #   undef snprintf
35 #   define snprintf(d, n, ...) _snprintf_s((d), (n), _TRUNCATE, __VA_ARGS__)
36 #  else /* HAVE__SNPRINTF_S */
37 #   if defined(HAVE__SNPRINTF)
38 #     undef snprintf
39 #     define snprintf _snprintf
40 #   else /* HAVE__SNPRINTF */
41 #    if !defined(HAVE_SNPRINTF)
42 #     error "no snprintf compatible function found"
43 #    endif /* HAVE_SNPRINTF */
44 #   endif /* HAVE__SNPRINTF */
45 #  endif /* HAVE__SNPRINTF_S */
46
47 #  if defined(HAVE__VSNPRINTF_S)
48 #   undef vsnprintf
49 #   define vsnprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v))
50 #  else /* HAVE__VSNPRINTF_S */
51 #   if defined(HAVE__VSNPRINTF)
52 #    undef vsnprintf
53 #    define vsnprintf _vsnprintf
54 #   else
55 #    if !defined(HAVE_VSNPRINTF)
56 #     error "No vsnprintf compatible function found"
57 #    endif /* HAVE_VSNPRINTF */
58 #   endif /* HAVE__VSNPRINTF */
59 #  endif /* HAVE__VSNPRINTF_S */
60 # endif /* _MSC_VER */
61
62 /*
63  * Backwards compatibility with headers shipped with Visual Studio 2005 and
64  * earlier.
65  */
66 WINBASEAPI BOOL WINAPI IsDebuggerPresent(VOID);
67
68 #ifndef PRIdS
69 # define PRIdS "Id"
70 #endif
71
72 #ifndef PRIu64
73 # define PRIu64 "I64u"
74 #endif
75
76 #ifndef PRIuMAX
77 # define PRIuMAX PRIu64
78 #endif
79
80 #ifndef PRIxMAX
81 #define PRIxMAX "I64x"
82 #endif
83
84 #ifndef PRIXMAX
85 #define PRIXMAX "I64X"
86 #endif
87
88 #else /* _WIN32 */
89
90 #ifndef __PRI64_PREFIX
91 # if __WORDSIZE == 64
92 #  define __PRI64_PREFIX "l"
93 # else
94 #  define __PRI64_PREFIX "ll"
95 # endif
96 #endif
97
98 #ifndef PRIdS
99 # define PRIdS "zd"
100 #endif
101
102 #ifndef PRIu64
103 # define PRIu64 __PRI64_PREFIX "u"
104 #endif
105
106 #ifndef PRIuMAX
107 # define PRIuMAX __PRI64_PREFIX "u"
108 #endif
109
110 #ifndef PRIxMAX
111 #define PRIxMAX __PRI64_PREFIX "x"
112 #endif
113
114 #ifndef PRIXMAX
115 #define PRIXMAX __PRI64_PREFIX "X"
116 #endif
117
118 #include <signal.h>
119 #endif /* _WIN32 */
120
121 /** Free memory space */
122 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
123
124 /** Zero a structure */
125 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
126
127 /** Zero a structure given a pointer to the structure */
128 #define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
129
130 /** Get the size of an array */
131 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
132
133 /** Overwrite the complete string with 'X' */
134 #define BURN_STRING(x) do { if ((x) != NULL) memset((x), 'X', strlen((x))); } while(0)
135
136 /**
137  * This is a hack to fix warnings. The idea is to use this everywhere that we
138  * get the "discarding const" warning by the compiler. That doesn't actually
139  * fix the real issue, but marks the place and you can search the code for
140  * discard_const.
141  *
142  * Please use this macro only when there is no other way to fix the warning.
143  * We should use this function in only in a very few places.
144  *
145  * Also, please call this via the discard_const_p() macro interface, as that
146  * makes the return type safe.
147  */
148 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
149
150 /**
151  * Type-safe version of discard_const
152  */
153 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
154
155 #endif /* CMOCKA_PRIVATE_H_ */