1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qplatformdefs.h" |
5 | |
6 | #include "qbytearray.h" |
7 | #include "qstring.h" |
8 | |
9 | #include "string.h" |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | /*! |
14 | \macro QT_NO_QSNPRINTF |
15 | \since 6.8 |
16 | \relates QByteArray |
17 | |
18 | Defining this macro removes the availability of the qsnprintf() and |
19 | qvsnprintf() functions. See the functions' documentation for why you may |
20 | want to disable them. |
21 | |
22 | \sa qsnprintf(), qvsnprintf(). |
23 | */ |
24 | |
25 | #if !defined(QT_VSNPRINTF) || defined(Q_QDOC) |
26 | |
27 | /*! |
28 | \fn int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap) |
29 | \relates QByteArray |
30 | |
31 | \obsolete |
32 | |
33 | Use C++11's \c{std::vsnprintf()} from \c{<cstdio>} instead. |
34 | |
35 | A portable \c vsnprintf() function. Will call \c ::vsnprintf(), \c |
36 | ::_vsnprintf(), or \c ::vsnprintf_s depending on the system, or |
37 | fall back to an internal version. |
38 | |
39 | \a fmt is the \c printf() format string. The result is put into |
40 | \a str, which is a buffer of at least \a n bytes. |
41 | |
42 | The caller is responsible to call \c va_end() on \a ap. |
43 | |
44 | \warning Since vsnprintf() shows different behavior on certain |
45 | platforms, you should not rely on the return value or on the fact |
46 | that you will always get a 0 terminated string back. There are also |
47 | differences in how \c{%a} (hex floats) and \c{%ls} (wide strings) are |
48 | handled on WebAssembly and Android. |
49 | |
50 | Ideally, you should never call this function but use QString::asprintf() |
51 | instead. |
52 | |
53 | \sa qsnprintf(), QString::asprintf() |
54 | */ |
55 | |
56 | Q_CORE_EXPORT // QT_NO_QSNPRINTF is in effect |
57 | int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap) |
58 | { |
59 | if (!str || !fmt) |
60 | return -1; |
61 | |
62 | const QByteArray ba = QString::vasprintf(fmt, ap).toLocal8Bit(); |
63 | |
64 | if (n > 0) { |
65 | size_t blen = qMin(size_t(ba.length()), size_t(n - 1)); |
66 | memcpy(str, ba.constData(), blen); |
67 | str[blen] = '\0'; // make sure str is always 0 terminated |
68 | } |
69 | |
70 | return ba.length(); |
71 | } |
72 | |
73 | #else |
74 | |
75 | QT_BEGIN_INCLUDE_NAMESPACE |
76 | #include <stdio.h> |
77 | QT_END_INCLUDE_NAMESPACE |
78 | |
79 | Q_CORE_EXPORT // QT_NO_QSNPRINTF is in effect |
80 | int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap) |
81 | { |
82 | return QT_VSNPRINTF(s: str, maxlen: n, format: fmt, arg: ap); |
83 | } |
84 | |
85 | #endif |
86 | |
87 | /*! |
88 | \fn int qsnprintf(char *str, size_t n, const char *fmt, ...) |
89 | \target bytearray-qsnprintf |
90 | \relates QByteArray |
91 | |
92 | \obsolete |
93 | |
94 | Use C++11's \c{std::snprintf()} from \c{<cstdio>} instead. |
95 | |
96 | A portable snprintf() function, calls qvsnprintf. |
97 | |
98 | \a fmt is the \c printf() format string. The result is put into |
99 | \a str, which is a buffer of at least \a n bytes. |
100 | |
101 | \warning Call this function only when you know what you are doing |
102 | since it shows different behavior on certain platforms. |
103 | Use QString::asprintf() to format a string instead. |
104 | |
105 | \sa qvsnprintf(), QString::asprintf() |
106 | */ |
107 | |
108 | Q_CORE_EXPORT // QT_NO_QSNPRINTF is in effect |
109 | int qsnprintf(char *str, size_t n, const char *fmt, ...) |
110 | { |
111 | va_list ap; |
112 | va_start(ap, fmt); |
113 | |
114 | int ret = qvsnprintf(str, n, fmt, ap); |
115 | va_end(ap); |
116 | |
117 | return ret; |
118 | } |
119 | |
120 | QT_END_NAMESPACE |
121 | |