blob: 7099c2173003882c2eceb086678fe9b649ae963a [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{
22 // Attempt to just print to the current buffer
23 int len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, vararg);
24 if (len < 0 || static_cast<size_t>(len) >= outBuffer.size())
25 {
26 // Buffer was not large enough, calculate the required size and resize the buffer
27 len = vsnprintf(NULL, 0, fmt, vararg);
28 outBuffer.resize(len + 1);
29
30 // Print again
31 len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, vararg);
32 }
33 ASSERT(len >= 0);
34 return static_cast<size_t>(len);
35}
36
Shannon Woods8e7d7a32014-09-02 17:09:08 -040037std::string FormatString(const char *fmt, va_list vararg)
Geoff Langda5777c2014-07-11 09:52:58 -040038{
Jamie Madille4ea2022015-03-26 20:35:05 +000039 static std::vector<char> buffer(512);
Geoff Langda5777c2014-07-11 09:52:58 -040040
Austin Kinross922a9fb2014-10-21 14:26:33 -070041 size_t len = FormatStringIntoVector(fmt, vararg, buffer);
Jamie Madillb4fd0c92014-10-01 17:40:24 -040042 return std::string(&buffer[0], len);
Geoff Langda5777c2014-07-11 09:52:58 -040043}
44
Shannon Woods8e7d7a32014-09-02 17:09:08 -040045std::string FormatString(const char *fmt, ...)
Geoff Langda5777c2014-07-11 09:52:58 -040046{
47 va_list vararg;
48 va_start(vararg, fmt);
49 std::string result = FormatString(fmt, vararg);
50 va_end(vararg);
51 return result;
52}