/*! \fn int QTest::qt_asprintf(QTestCharBuffer *buf, const char *format, ...); \internal */ int qt_asprintf(QTestCharBuffer *str, const char *format, ...) { static const int MAXSIZE = 1024*1024*2; Q_ASSERT(str); int size = str->size(); va_list ap; int res = 0; for (;;) { va_start(ap, format); res = qvsnprintf(str->data(), size, format, ap); va_end(ap); str->data()[size - 1] = '\0'; if (res >= 0 && res < size) { // We succeeded break; } // buffer wasn't big enough, try again. // Note, we're assuming that a result of -1 is always due to running out of space. size *= 2; if (size > MAXSIZE) { break; } if (!str->reset(size)) break; // out of memory - take what we have } filter_unprintable(str->data()); return res; }
void dataLogF(const char* format, ...) { char buffer[1024]; va_list args; va_start(args, format); qvsnprintf(buffer, sizeof(buffer), format, args); va_end(args); qDebug("%s", buffer); }
//----------------------------------------------------------------------------- // output an error and exit loader. static void mas_error(const char *format, ...) { char b[MAXSTR]; va_list va; va_start(va, format); qvsnprintf(b, sizeof b, format, va); va_end(va); loader_failure("mas loader critical error : %s", b); }
local void nomem(const char *format,...) { char buf[512]; va_list va; va_start(va,format); qvsnprintf(buf, sizeof(buf), format, va); va_end(va); fatal("No memory: %s",buf); }
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; }
SIM_MOD_IMPORT void sim_printf(void *ctx, const char *fmt, ...) { QByteArray buffer(16*1024, 0); va_list args; va_start(args, fmt); qvsnprintf(buffer.data(), buffer.size()-1, fmt, args); va_end(args); static_cast<SimModuleRunner*>(ctx)->print(Text, QString::fromUtf8(buffer)); }
static bool backtrace_command(FILE *outb, const char *format, ...) { bool ret = false; char buffer[50]; /* * Please note that vsnprintf() is not ASync safe (ie. cannot safely * be used from a signal handler.) If this proves to be a problem * then the cmd string can be built by more basic functions such as * strcpy, strcat, and a home-made integer-to-ascii function. */ va_list args; char cmd[512]; va_start(args, format); qvsnprintf(cmd, 512, format, args); va_end(args); char *foo = cmd; #if 0 foo = "echo hi"; #endif if(FILE *inb = popen(foo, "r")) { while(!feof(inb)) { int len = fread(buffer, 1, sizeof(buffer), inb); if(!len) break; if(!ret) { fwrite("Output from ", 1, strlen("Output from "), outb); strtok(cmd, " "); fwrite(cmd, 1, strlen(cmd), outb); fwrite("\n", 1, 1, outb); ret = true; } fwrite(buffer, 1, len, outb); } fclose(inb); } return ret; }
void dataLogFV(const char* format, va_list args) { char buffer[1024]; qvsnprintf(buffer, sizeof(buffer), format, args); qDebug("%s", buffer); }
QT_BEGIN_NAMESPACE /***************************************************************************** Q3CString member functions *****************************************************************************/ /*! \class Q3CString \reentrant \brief The Q3CString class provides an abstraction of the classic C zero-terminated char array (char *). \compat Q3CString tries to behave like a more convenient \c{const char *}. The price of doing this is that some algorithms will perform badly. For example, append() is O(length()) since it scans for a null terminator. Although you might use Q3CString for text that is never exposed to the user, for most purposes, and especially for user-visible text, you should use QString. QString provides implicit sharing, Unicode and other internationalization support, and is well optimized. Note that for the Q3CString methods that take a \c{const char *} parameter the \c{const char *} must either be 0 (null) or not-null and '\0' (NUL byte) terminated; otherwise the results are undefined. A Q3CString that has not been assigned to anything is \e null, i.e. both the length and the data pointer is 0. A Q3CString that references the empty string ("", a single '\0' char) is \e empty. Both null and empty Q3CStrings are legal parameters to the methods. Assigning \c{const char *} 0 to Q3CString produces a null Q3CString. The length() function returns the length of the string; resize() resizes the string and truncate() truncates the string. A string can be filled with a character using fill(). Strings can be left or right padded with characters using leftJustify() and rightJustify(). Characters, strings and regular expressions can be searched for using find() and findRev(), and counted using contains(). Strings and characters can be inserted with insert() and appended with append(). A string can be prepended with prepend(). Characters can be removed from the string with remove() and replaced with replace(). Portions of a string can be extracted using left(), right() and mid(). Whitespace can be removed using stripWhiteSpace() and simplifyWhiteSpace(). Strings can be converted to uppercase or lowercase with upper() and lower() respectively. Strings that contain numbers can be converted to numbers with toShort(), toInt(), toLong(), toULong(), toFloat() and toDouble(). Numbers can be converted to strings with setNum(). Many operators are overloaded to work with Q3CStrings. Q3CString also supports some more obscure functions, e.g. sprintf(), setStr() and setExpand(). \sidebar Note on Character Comparisons In Q3CString the notion of uppercase and lowercase and of which character is greater than or less than another character is locale dependent. This affects functions which support a case insensitive option or which compare or lowercase or uppercase their arguments. Case insensitive operations and comparisons will be accurate if both strings contain only ASCII characters. (If \c $LC_CTYPE is set, most Unix systems do "the right thing".) Functions that this affects include contains(), find(), findRev(), \l operator<(), \l operator<=(), \l operator>(), \l operator>=(), lower() and upper(). This issue does not apply to \l{QString}s since they represent characters using Unicode. \endsidebar Performance note: The Q3CString methods for QRegExp searching are implemented by converting the Q3CString to a QString and performing the search on that. This implies a deep copy of the Q3CString data. If you are going to perform many QRegExp searches on a large Q3CString, you will get better performance by converting the Q3CString to a QString yourself, and then searching in the QString. */ /*! \fn Q3CString Q3CString::left(uint len) const \internal */ /*! \fn Q3CString Q3CString::right(uint len) const \internal */ /*! \fn Q3CString Q3CString::mid(uint index, uint len) const \internal */ /*! \fn Q3CString Q3CString::lower() const Use QByteArray::toLower() instead. */ /*! \fn Q3CString Q3CString::upper() const Use QByteArray::toUpper() instead. */ /*! \fn Q3CString Q3CString::stripWhiteSpace() const Use QByteArray::trimmed() instead. */ /*! \fn Q3CString Q3CString::simplifyWhiteSpace() const Use QByteArray::simplified() instead. */ /*! \fn Q3CString& Q3CString::insert(uint index, const char *c) \internal */ /*! \fn Q3CString& Q3CString::insert(uint index, char c) \internal */ /*! \fn Q3CString& Q3CString::prepend(const char *c) \internal */ /*! \fn Q3CString& Q3CString::remove(uint index, uint len) \internal */ /*! \fn Q3CString& Q3CString::replace(uint index, uint len, const char *c) \internal */ /*! \fn Q3CString& Q3CString::replace(char c, const Q3CString &after) \internal */ /*! \fn Q3CString& Q3CString::replace(char c, const char *after) \internal */ /*! \fn Q3CString& Q3CString::replace(const Q3CString &b, const Q3CString &a) \internal */ /*! \fn Q3CString& Q3CString::replace(const char *b, const char *a) \internal */ /*! \fn Q3CString& Q3CString::replace(char b, char a) \internal */ /*! \fn Q3CString::Q3CString() Constructs a null string. \sa isNull() */ /*! \fn Q3CString::Q3CString(const QByteArray &ba) Constructs a copy of \a ba. */ /*! \fn Q3CString::Q3CString(const Q3CString &s) Constructs a shallow copy \a s. */ /*! \fn Q3CString::Q3CString(int size) Constructs a string with room for \a size characters, including the '\0'-terminator. Makes a null string if \a size == 0. If \a size \> 0, then the first and last characters in the string are initialized to '\0'. All other characters are uninitialized. \sa resize(), isNull() */ /*! \fn Q3CString::Q3CString(const char *str) Constructs a string that is a deep copy of \a str. If \a str is 0 a null string is created. \sa isNull() */ /*! \fn Q3CString::Q3CString(const char *str, uint maxsize) Constructs a string that is a deep copy of \a str. The copy will be at most \a maxsize bytes long including the '\0'-terminator. Example: \snippet doc/src/snippets/code/src_qt3support_tools_q3cstring.cpp 0 If \a str contains a 0 byte within the first \a maxsize bytes, the resulting Q3CString will be terminated by this 0. If \a str is 0 a null string is created. \sa isNull() */ /*! \fn Q3CString &Q3CString::operator=(const QByteArray &ba) Assigns byte array \a ba to this Q3CString. */ /*! \fn Q3CString &Q3CString::operator=(const Q3CString &s) Assigns a shallow copy of \a s to this string and returns a reference to this string. */ /*! \fn Q3CString &Q3CString::operator=(const char *str) \overload Assigns a deep copy of \a str to this string and returns a reference to this string. If \a str is 0 a null string is created. \sa isNull() */ /* \fn bool Q3CString::isNull() const Returns true if the string is null, i.e. if data() == 0; otherwise returns false. A null string is also an empty string. Example: \snippet doc/src/snippets/code/src.qt3support.tools.q3cstring.cpp 1 \sa isEmpty(), length(), size() */ /* \fn bool Q3CString::isEmpty() const Returns true if the string is empty, i.e. if length() == 0; otherwise returns false. An empty string is not always a null string. See example in isNull(). \sa isNull(), length(), size() */ /* \fn uint Q3CString::length() const Returns the length of the string, excluding the '\0'-terminator. Equivalent to calling \c strlen(data()). Null strings and empty strings have zero length. \sa size(), isNull(), isEmpty() */ /* \fn bool Q3CString::truncate(uint pos) Truncates the string at position \a pos. Equivalent to calling \c resize(pos+1). Example: \snippet doc/src/snippets/code/src.qt3support.tools.q3cstring.cpp 2 \sa resize() */ /*! Implemented as a call to the native vsprintf() (see the manual for your C library). If the string is shorter than 256 characters, this sprintf() calls resize(256) to decrease the chance of memory corruption. The string is resized back to its actual length before sprintf() returns. Example: \snippet doc/src/snippets/code/src_qt3support_tools_q3cstring.cpp 3 \warning All vsprintf() implementations will write past the end of the target string (*this) if the \a format specification and arguments happen to be longer than the target string, and some will also fail if the target string is longer than some arbitrary implementation limit. Giving user-supplied arguments to sprintf() is risky: Sooner or later someone will paste a huge line into your application. */ Q3CString &Q3CString::sprintf(const char *format, ...) { detach(); va_list ap; va_start(ap, format); if (size() < 256) resize(256); // make string big enough qvsnprintf(data(), size(), format, ap); resize(qstrlen(constData())); va_end(ap); return *this; }