1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /* Polyfills snprintf() on platforms that don't provide it, and provides
8  * related utilities. */
9 
10 #ifndef mozilla_Snprintf_h_
11 #define mozilla_Snprintf_h_
12 
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <stdarg.h>
16 
17 // Older MSVC versions do not provide snprintf(), but they do provide
18 // vsnprintf(), which has the same semantics except that if the number of
19 // characters written equals the buffer size, it does not write a null
20 // terminator, so we wrap it to do so.
21 #if defined(_MSC_VER) && _MSC_VER < 1900
22 #include "mozilla/Attributes.h"
snprintf(char * buffer,size_t n,const char * format,...)23 MOZ_ALWAYS_INLINE int snprintf(char* buffer, size_t n, const char* format, ...)
24 {
25   va_list args;
26   va_start(args, format);
27   int result = vsnprintf(buffer, n, format, args);
28   va_end(args);
29   buffer[n - 1] = '\0';
30   return result;
31 }
32 #endif
33 
34 // In addition, in C++ code, on all platforms, provide an snprintf_literal()
35 // function which uses template argument deduction to deduce the size of the
36 // buffer, avoiding the need for the user to pass it in explicitly.
37 #ifdef __cplusplus
38 template <size_t N>
snprintf_literal(char (& buffer)[N],const char * format,...)39 int snprintf_literal(char (&buffer)[N], const char* format, ...)
40 {
41   va_list args;
42   va_start(args, format);
43   int result = vsnprintf(buffer, N, format, args);
44   va_end(args);
45   buffer[N - 1] = '\0';
46   return result;
47 }
48 #endif
49 
50 #endif  /* mozilla_Snprintf_h_ */
51