| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qvsnprintf.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count |
|---|---|---|
| 1 | /**************************************************************************** | - |
| 2 | ** | - |
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - |
| 4 | ** Contact: http://www.qt.io/licensing/ | - |
| 5 | ** | - |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
| 7 | ** | - |
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - |
| 9 | ** Commercial License Usage | - |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
| 11 | ** accordance with the commercial license agreement provided with the | - |
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - |
| 16 | ** | - |
| 17 | ** GNU Lesser General Public License Usage | - |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - |
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - |
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - |
| 22 | ** following information to ensure the GNU Lesser General Public License | - |
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - |
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
| 25 | ** | - |
| 26 | ** As a special exception, The Qt Company gives you certain additional | - |
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - |
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
| 29 | ** | - |
| 30 | ** $QT_END_LICENSE$ | - |
| 31 | ** | - |
| 32 | ****************************************************************************/ | - |
| 33 | - | |
| 34 | #include "qplatformdefs.h" | - |
| 35 | - | |
| 36 | #include "qbytearray.h" | - |
| 37 | #include "qstring.h" | - |
| 38 | - | |
| 39 | #include "string.h" | - |
| 40 | - | |
| 41 | QT_BEGIN_NAMESPACE | - |
| 42 | - | |
| 43 | #ifndef QT_VSNPRINTF | - |
| 44 | - | |
| 45 | /*! | - |
| 46 | \relates QByteArray | - |
| 47 | - | |
| 48 | A portable \c vsnprintf() function. Will call \c ::vsnprintf(), \c | - |
| 49 | ::_vsnprintf(), or \c ::vsnprintf_s depending on the system, or | - |
| 50 | fall back to an internal version. | - |
| 51 | - | |
| 52 | \a fmt is the \c printf() format string. The result is put into | - |
| 53 | \a str, which is a buffer of at least \a n bytes. | - |
| 54 | - | |
| 55 | The caller is responsible to call \c va_end() on \a ap. | - |
| 56 | - | |
| 57 | \warning Since vsnprintf() shows different behavior on certain | - |
| 58 | platforms, you should not rely on the return value or on the fact | - |
| 59 | that you will always get a 0 terminated string back. | - |
| 60 | - | |
| 61 | Ideally, you should never call this function but use QString::asprintf() | - |
| 62 | instead. | - |
| 63 | - | |
| 64 | \sa qsnprintf(), QString::asprintf() | - |
| 65 | */ | - |
| 66 | - | |
| 67 | int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap) | - |
| 68 | { | - |
| 69 | if (!str || !fmt) | - |
| 70 | return -1; | - |
| 71 | - | |
| 72 | const QByteArray ba = QString::vasprintf(fmt, ap).toLocal8Bit(); | - |
| 73 | - | |
| 74 | if (n > 0) { | - |
| 75 | size_t blen = qMin(size_t(ba.length()), size_t(n - 1)); | - |
| 76 | memcpy(str, ba.constData(), blen); | - |
| 77 | str[blen] = '\0'; // make sure str is always 0 terminated | - |
| 78 | } | - |
| 79 | - | |
| 80 | return ba.length(); | - |
| 81 | } | - |
| 82 | - | |
| 83 | #else | - |
| 84 | - | |
| 85 | QT_BEGIN_INCLUDE_NAMESPACE | - |
| 86 | #include <stdio.h> | - |
| 87 | QT_END_INCLUDE_NAMESPACE | - |
| 88 | - | |
| 89 | int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap) | - |
| 90 | { | - |
| 91 | return QT_VSNPRINTF(str, n, fmt, ap); executed 50713901 times by 545 tests: return ::vsnprintf(str, n, fmt, ap);Executed by:
| 50713901 |
| 92 | } | - |
| 93 | - | |
| 94 | #endif | - |
| 95 | - | |
| 96 | /*! | - |
| 97 | \target bytearray-qsnprintf | - |
| 98 | \relates QByteArray | - |
| 99 | - | |
| 100 | A portable snprintf() function, calls qvsnprintf. | - |
| 101 | - | |
| 102 | \a fmt is the \c printf() format string. The result is put into | - |
| 103 | \a str, which is a buffer of at least \a n bytes. | - |
| 104 | - | |
| 105 | \warning Call this function only when you know what you are doing | - |
| 106 | since it shows different behavior on certain platforms. | - |
| 107 | Use QString::asprintf() to format a string instead. | - |
| 108 | - | |
| 109 | \sa qvsnprintf(), QString::asprintf() | - |
| 110 | */ | - |
| 111 | - | |
| 112 | int qsnprintf(char *str, size_t n, const char *fmt, ...) | - |
| 113 | { | - |
| 114 | va_list ap; | - |
| 115 | va_start(ap, fmt); | - |
| 116 | - | |
| 117 | int ret = qvsnprintf(str, n, fmt, ap); | - |
| 118 | va_end(ap); | - |
| 119 | - | |
| 120 | return ret; executed 50414973 times by 545 tests: return ret;Executed by:
| 50414973 |
| 121 | } | - |
| 122 | - | |
| 123 | QT_END_NAMESPACE | - |
| Source code | Switch to Preprocessed file |