Realloc bufsz only if no error
[clinfo] / src / ms_support.h
1 /* Missing functions and other misc stuff to support
2  * the horrible MS C compiler
3  *
4  * TODO could be improved by version-checking for C99 support
5  */
6
7 // also disable strncpy vs strncpy_s warning
8 #pragma warning(disable : 4996)
9
10 // No inline in MS C
11 #define inline __inline
12
13 // No snprintf in MS C, copy over implementation taken from
14 // stackoverflow
15
16 #include <stdarg.h>
17 #include <stdio.h>
18
19 inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)
20 {
21     int count = -1;
22
23     if (size != 0)
24         count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
25     if (count == -1)
26         count = _vscprintf(format, ap);
27
28     return count;
29 }
30
31 inline int c99_snprintf(char* str, size_t size, const char* format, ...)
32 {
33     int count;
34     va_list ap;
35
36     va_start(ap, format);
37     count = c99_vsnprintf(str, size, format, ap);
38     va_end(ap);
39
40     return count;
41 }
42
43 #define snprintf c99_snprintf
44
45 // And no __func__ either
46
47 #define __func__ __FUNCTION__