blob: d3d29a32cf648ce785bda376c1570cef654cb416 [file] [log] [blame]
Geoff Langda5777c2014-07-11 09:52:58 -04001//
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 Lang22072132014-11-20 15:15:01 -05008#include "common/debug.h"
9
Olli Etuaho86ffde52014-10-03 14:51:54 +030010#include <stdio.h>
Jamie Madillc9bdeff2016-02-08 12:36:55 -050011
12#include <limits>
Geoff Langda5777c2014-07-11 09:52:58 -040013#include <vector>
14
Jamie Madillc9bdeff2016-02-08 12:36:55 -050015namespace angle
16{
17const uintptr_t DirtyPointer = std::numeric_limits<uintptr_t>::max();
18}
19
Austin Kinross922a9fb2014-10-21 14:26:33 -070020size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& outBuffer)
21{
Corentin Walleza8ccb952016-12-16 15:12:57 -050022 // 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 Kinross922a9fb2014-10-21 14:26:33 -070027 // Attempt to just print to the current buffer
Corentin Walleza8ccb952016-12-16 15:12:57 -050028 int len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy);
29 va_end(varargCopy);
30
Austin Kinross922a9fb2014-10-21 14:26:33 -070031 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 Hef81ce4a2017-04-24 10:49:17 +080034 len = vsnprintf(nullptr, 0, fmt, vararg);
Austin Kinross922a9fb2014-10-21 14:26:33 -070035 outBuffer.resize(len + 1);
36
37 // Print again
Corentin Walleza8ccb952016-12-16 15:12:57 -050038 va_copy(varargCopy, vararg);
39 len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy);
40 va_end(varargCopy);
Austin Kinross922a9fb2014-10-21 14:26:33 -070041 }
42 ASSERT(len >= 0);
43 return static_cast<size_t>(len);
44}