blob: af5eb6c4471f013efdc67ac98968e124c2bfad4d [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>
Geoff Langda5777c2014-07-11 09:52:58 -040011#include <vector>
12
Austin Kinross922a9fb2014-10-21 14:26:33 -070013size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& outBuffer)
14{
15 // Attempt to just print to the current buffer
16 int len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, vararg);
17 if (len < 0 || static_cast<size_t>(len) >= outBuffer.size())
18 {
19 // Buffer was not large enough, calculate the required size and resize the buffer
20 len = vsnprintf(NULL, 0, fmt, vararg);
21 outBuffer.resize(len + 1);
22
23 // Print again
24 len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, vararg);
25 }
26 ASSERT(len >= 0);
27 return static_cast<size_t>(len);
28}
29
Shannon Woods8e7d7a32014-09-02 17:09:08 -040030std::string FormatString(const char *fmt, va_list vararg)
Geoff Langda5777c2014-07-11 09:52:58 -040031{
Jamie Madille4ea2022015-03-26 20:35:05 +000032 static std::vector<char> buffer(512);
Geoff Langda5777c2014-07-11 09:52:58 -040033
Austin Kinross922a9fb2014-10-21 14:26:33 -070034 size_t len = FormatStringIntoVector(fmt, vararg, buffer);
Jamie Madillb4fd0c92014-10-01 17:40:24 -040035 return std::string(&buffer[0], len);
Geoff Langda5777c2014-07-11 09:52:58 -040036}
37
Shannon Woods8e7d7a32014-09-02 17:09:08 -040038std::string FormatString(const char *fmt, ...)
Geoff Langda5777c2014-07-11 09:52:58 -040039{
40 va_list vararg;
41 va_start(vararg, fmt);
42 std::string result = FormatString(fmt, vararg);
43 va_end(vararg);
44 return result;
45}