1 /* Copyright  (C) 2010-2018 The RetroArch team
2  *
3  * ---------------------------------------------------------------------------------------
4  * The following license statement only applies to this file (compat_snprintf.c).
5  * ---------------------------------------------------------------------------------------
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /* THIS FILE HAS NOT BEEN VALIDATED ON PLATFORMS BESIDES MSVC */
24 #ifdef _MSC_VER
25 
26 #include <retro_common.h>
27 #if _MSC_VER >= 1900
28 #include <stdio.h> /* added for _vsnprintf_s and _vscprintf on VS2015 and VS2017 */
29 #endif
30 #include <stdarg.h>
31 
32 #if _MSC_VER < 1800
33 #define va_copy(dst, src) ((dst) = (src))
34 #endif
35 
36 #if _MSC_VER < 1300
37 #define _vscprintf c89_vscprintf_retro__
38 
c89_vscprintf_retro__(const char * format,va_list pargs)39 static int c89_vscprintf_retro__(const char *format, va_list pargs)
40 {
41    int retval;
42    va_list argcopy;
43    va_copy(argcopy, pargs);
44    retval = vsnprintf(NULL, 0, format, argcopy);
45    va_end(argcopy);
46    return retval;
47 }
48 #endif
49 
50 /* http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010 */
51 
c99_vsnprintf_retro__(char * outBuf,size_t size,const char * format,va_list ap)52 int c99_vsnprintf_retro__(char *outBuf, size_t size, const char *format, va_list ap)
53 {
54    int count = -1;
55 
56    if (size != 0)
57 #if (_MSC_VER <= 1310)
58        count = _vsnprintf(outBuf, size, format, ap);
59 #else
60        count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
61 #endif
62    if (count == -1)
63        count = _vscprintf(format, ap);
64 
65    return count;
66 }
67 
c99_snprintf_retro__(char * outBuf,size_t size,const char * format,...)68 int c99_snprintf_retro__(char *outBuf, size_t size, const char *format, ...)
69 {
70    int count;
71    va_list ap;
72 
73    va_start(ap, format);
74    count = c99_vsnprintf_retro__(outBuf, size, format, ap);
75    va_end(ap);
76 
77    return count;
78 }
79 #endif
80