installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / src / shared / winpty_snprintf.h
1 // Copyright (c) 2016 Ryan Prichard
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20
21 #ifndef WINPTY_SNPRINTF_H
22 #define WINPTY_SNPRINTF_H
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "WinptyAssert.h"
29
30 #if defined(__CYGWIN__) || defined(__MSYS__)
31 #define WINPTY_SNPRINTF_FORMAT(fmtarg, vararg) \
32     __attribute__((format(printf, (fmtarg), ((vararg)))))
33 #elif defined(__GNUC__)
34 #define WINPTY_SNPRINTF_FORMAT(fmtarg, vararg) \
35     __attribute__((format(ms_printf, (fmtarg), ((vararg)))))
36 #else
37 #define WINPTY_SNPRINTF_FORMAT(fmtarg, vararg)
38 #endif
39
40 // Returns a value between 0 and size - 1 (inclusive) on success.  Returns -1
41 // on failure (including truncation).  The output buffer is always
42 // NUL-terminated.
43 inline int
44 winpty_vsnprintf(char *out, size_t size, const char *fmt, va_list ap) {
45     ASSERT(size > 0);
46     out[0] = '\0';
47 #if defined(_MSC_VER) && _MSC_VER < 1900
48     // MSVC 2015 added a C99-conforming vsnprintf.
49     int count = _vsnprintf_s(out, size, _TRUNCATE, fmt, ap);
50 #else
51     // MinGW configurations frequently provide a vsnprintf function that simply
52     // calls one of the MS _vsnprintf* functions, which are not C99 conformant.
53     int count = vsnprintf(out, size, fmt, ap);
54 #endif
55     if (count < 0 || static_cast<size_t>(count) >= size) {
56         // On truncation, some *printf* implementations return the
57         // non-truncated size, but other implementations returns -1.  Return
58         // -1 for consistency.
59         count = -1;
60         // Guarantee NUL termination.
61         out[size - 1] = '\0';
62     } else {
63         // Guarantee NUL termination.
64         out[count] = '\0';
65     }
66     return count;
67 }
68
69 // Wraps winpty_vsnprintf.
70 inline int winpty_snprintf(char *out, size_t size, const char *fmt, ...)
71     WINPTY_SNPRINTF_FORMAT(3, 4);
72 inline int winpty_snprintf(char *out, size_t size, const char *fmt, ...) {
73     va_list ap;
74     va_start(ap, fmt);
75     const int count = winpty_vsnprintf(out, size, fmt, ap);
76     va_end(ap);
77     return count;
78 }
79
80 // Wraps winpty_vsnprintf with automatic size determination.
81 template <size_t size>
82 int winpty_vsnprintf(char (&out)[size], const char *fmt, va_list ap) {
83     return winpty_vsnprintf(out, size, fmt, ap);
84 }
85
86 // Wraps winpty_vsnprintf with automatic size determination.
87 template <size_t size>
88 int winpty_snprintf(char (&out)[size], const char *fmt, ...)
89     WINPTY_SNPRINTF_FORMAT(2, 3);
90 template <size_t size>
91 int winpty_snprintf(char (&out)[size], const char *fmt, ...) {
92     va_list ap;
93     va_start(ap, fmt);
94     const int count = winpty_vsnprintf(out, size, fmt, ap);
95     va_end(ap);
96     return count;
97 }
98
99 #endif // WINPTY_SNPRINTF_H