HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vsnprintf.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_ARCH_VSNPRINTF_H
8 #define PXR_BASE_ARCH_VSNPRINTF_H
9 
10 /// \file arch/vsnprintf.h
11 /// \ingroup group_arch_Strings
12 /// Architecture dependent memory-safe sprintf capability
13 
14 #include "pxr/pxr.h"
15 #include "pxr/base/arch/api.h"
17 
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <string>
21 
23 
24 /// \addtogroup group_arch_Strings
25 ///@{
26 
27 /// Return the number of characters (not including the null character)
28 /// necessary for a particular sprintf into a string.
29 ///
30 /// \c ArchVsnprintf guarantees the C99 behavior of vsnprintf on all systems:
31 /// it returns the number of bytes (not including the terminating null
32 /// character) needed to actually print the requested string. If \c size
33 /// indicates that \c str has enough capacity to hold the result, then the
34 /// function actually prints into \c str.
35 ///
36 /// You probably want to use the functionality of this call via
37 /// \c TfStringPrintf().
38 ///
40 int ArchVsnprintf(char *str, size_t size, const char *format, va_list ap)
41 #ifndef doxygen
42  ARCH_PRINTF_FUNCTION(3, 0)
43 #endif /* doxygen */
44  ;
45 
46 /// Returns a string formed by a printf()-like specification.
47 ///
48 /// \c ArchStringPrintf() is a memory-safe architecture-independent way of
49 /// forming a string using printf()-like formatting. For example,
50 /// \code
51 /// string formatMsg(const string& caller, int i, double val[])
52 /// {
53 /// return ArchStringPrintf("%s: val[%d] = %g\n", caller.c_str(), i, val[i]);
54 /// }
55 /// \endcode
56 ///
57 /// The function is safe only to the extent that the arguments match the
58 /// formatting string. In particular, be careful to pass strings themselve
59 /// into \c ArchStringPrintf() as in the above example (i.e. \c caller.c_str()
60 /// as opposed to just passing \c caller).
61 ///
63 std::string ArchStringPrintf(const char *fmt, ...)
64 #ifndef doxygen
65  ARCH_PRINTF_FUNCTION(1, 2)
66 #endif /* doxygen */
67  ;
68 
69 /// Returns a string formed by a printf()-like specification.
70 ///
71 /// \c ArchVStringPrintf() is equivalent to \c ArchStringPrintf() except that
72 /// it is called with a \c va_list instead of a variable number of arguments.
73 /// \c ArchVStringPrintf() does not call the \c va_end macro. Consequently,
74 /// the value of \c ap is undefined after the call. A functions that calls \c
75 /// ArchVStringPrintf() should call \c va_end(ap) itself afterwards.
76 ///
78 std::string ArchVStringPrintf(const char *fmt, va_list ap)
79 #ifndef doxygen
80  ARCH_PRINTF_FUNCTION(1, 0)
81  ;
82 #endif
83 
84 /// @}
85 
87 
88 #endif // PXR_BASE_ARCH_VSNPRINTF_H
ARCH_API std::string ArchVStringPrintf(const char *fmt, va_list ap)
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
ARCH_API int ArchVsnprintf(char *str, size_t size, const char *format, va_list ap)
ARCH_API std::string ArchStringPrintf(const char *fmt,...)
GLsizeiptr size
Definition: glcorearb.h:664
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
#define ARCH_API
Definition: api.h:23