2015-12-10 05:06:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
2019-06-03 14:21:30 +00:00
|
|
|
** Copyright (C) 2016-2019 Ivailo Monev
|
2015-12-10 05:06:13 +02:00
|
|
|
**
|
2019-07-02 18:13:44 +00:00
|
|
|
** This file is part of the QtCore module of the Katie Toolkit.
|
2015-12-10 05:06:13 +02:00
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qplatformdefs.h"
|
|
|
|
|
|
|
|
#include "qbytearray.h"
|
|
|
|
#include "qstring.h"
|
|
|
|
|
|
|
|
#include "string.h"
|
2019-05-13 00:07:12 +00:00
|
|
|
#ifdef QT_VSNPRINTF
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QByteArray
|
|
|
|
|
|
|
|
A portable \c vsnprintf() function. Will call \c ::vsnprintf(), \c
|
|
|
|
::_vsnprintf(), or \c ::vsnprintf_s depending on the system, or
|
|
|
|
fall back to an internal version.
|
|
|
|
|
|
|
|
\a fmt is the \c printf() format string. The result is put into
|
|
|
|
\a str, which is a buffer of at least \a n bytes.
|
|
|
|
|
|
|
|
The caller is responsible to call \c va_end() on \a ap.
|
|
|
|
|
|
|
|
\warning Since vsnprintf() shows different behavior on certain
|
|
|
|
platforms, you should not rely on the return value or on the fact
|
|
|
|
that you will always get a 0 terminated string back.
|
|
|
|
|
|
|
|
Ideally, you should never call this function but use QString::sprintf()
|
|
|
|
instead.
|
|
|
|
|
|
|
|
\sa qsnprintf(), QString::sprintf()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
|
|
|
|
{
|
2019-05-13 00:07:12 +00:00
|
|
|
#ifndef QT_VSNPRINTF
|
2015-12-10 05:06:13 +02:00
|
|
|
if (!str || !fmt)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
QString buf;
|
|
|
|
buf.vsprintf(fmt, ap);
|
|
|
|
|
|
|
|
QByteArray ba = buf.toLocal8Bit();
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
size_t blen = qMin(size_t(ba.length()), size_t(n - 1));
|
|
|
|
memcpy(str, ba.constData(), blen);
|
|
|
|
str[blen] = '\0'; // make sure str is always 0 terminated
|
|
|
|
}
|
|
|
|
|
|
|
|
return ba.length();
|
|
|
|
#else
|
|
|
|
return QT_VSNPRINTF(str, n, fmt, ap);
|
|
|
|
#endif
|
2019-05-13 00:07:12 +00:00
|
|
|
}
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QByteArray
|
|
|
|
|
|
|
|
A portable snprintf() function, calls qvsnprintf.
|
|
|
|
|
|
|
|
\a fmt is the \c printf() format string. The result is put into
|
|
|
|
\a str, which is a buffer of at least \a n bytes.
|
|
|
|
|
|
|
|
\warning Call this function only when you know what you are doing
|
|
|
|
since it shows different behavior on certain platforms.
|
|
|
|
Use QString::sprintf() to format a string instead.
|
|
|
|
|
|
|
|
\sa qvsnprintf(), QString::sprintf()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int qsnprintf(char *str, size_t n, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
int ret = qvsnprintf(str, n, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|