Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2014 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | #include "common/angleutils.h" |
Geoff Lang | 2207213 | 2014-11-20 15:15:01 -0500 | [diff] [blame] | 8 | #include "common/debug.h" |
| 9 | |
Olli Etuaho | 86ffde5 | 2014-10-03 14:51:54 +0300 | [diff] [blame] | 10 | #include <stdio.h> |
Jamie Madill | c9bdeff | 2016-02-08 12:36:55 -0500 | [diff] [blame] | 11 | |
| 12 | #include <limits> |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Jamie Madill | c9bdeff | 2016-02-08 12:36:55 -0500 | [diff] [blame] | 15 | namespace angle |
| 16 | { |
| 17 | const uintptr_t DirtyPointer = std::numeric_limits<uintptr_t>::max(); |
| 18 | } |
| 19 | |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 20 | size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& outBuffer) |
| 21 | { |
Corentin Wallez | a8ccb95 | 2016-12-16 15:12:57 -0500 | [diff] [blame] | 22 | // The state of the va_list passed to vsnprintf is undefined after the call, do a copy in case |
| 23 | // we need to grow the buffer. |
| 24 | va_list varargCopy; |
| 25 | va_copy(varargCopy, vararg); |
| 26 | |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 27 | // Attempt to just print to the current buffer |
Corentin Wallez | a8ccb95 | 2016-12-16 15:12:57 -0500 | [diff] [blame] | 28 | int len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy); |
| 29 | va_end(varargCopy); |
| 30 | |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 31 | if (len < 0 || static_cast<size_t>(len) >= outBuffer.size()) |
| 32 | { |
| 33 | // Buffer was not large enough, calculate the required size and resize the buffer |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 34 | len = vsnprintf(nullptr, 0, fmt, vararg); |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 35 | outBuffer.resize(len + 1); |
| 36 | |
| 37 | // Print again |
Corentin Wallez | a8ccb95 | 2016-12-16 15:12:57 -0500 | [diff] [blame] | 38 | va_copy(varargCopy, vararg); |
| 39 | len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy); |
| 40 | va_end(varargCopy); |
Austin Kinross | 922a9fb | 2014-10-21 14:26:33 -0700 | [diff] [blame] | 41 | } |
| 42 | ASSERT(len >= 0); |
| 43 | return static_cast<size_t>(len); |
| 44 | } |