blob: c1367c460a305d17ce95a5be8ea0f538119f2670 [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"
Austin Kinross922a9fb2014-10-21 14:26:33 -07008#include "debug.h"
Olli Etuaho86ffde52014-10-03 14:51:54 +03009#include <stdio.h>
Geoff Langda5777c2014-07-11 09:52:58 -040010#include <vector>
11
Austin Kinross922a9fb2014-10-21 14:26:33 -070012size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& outBuffer)
13{
14 // Attempt to just print to the current buffer
15 int len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, vararg);
16 if (len < 0 || static_cast<size_t>(len) >= outBuffer.size())
17 {
18 // Buffer was not large enough, calculate the required size and resize the buffer
19 len = vsnprintf(NULL, 0, fmt, vararg);
20 outBuffer.resize(len + 1);
21
22 // Print again
23 len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, vararg);
24 }
25 ASSERT(len >= 0);
26 return static_cast<size_t>(len);
27}
28
Shannon Woods8e7d7a32014-09-02 17:09:08 -040029std::string FormatString(const char *fmt, va_list vararg)
Geoff Langda5777c2014-07-11 09:52:58 -040030{
31 static std::vector<char> buffer(512);
32
Austin Kinross922a9fb2014-10-21 14:26:33 -070033 size_t len = FormatStringIntoVector(fmt, vararg, buffer);
Jamie Madillb4fd0c92014-10-01 17:40:24 -040034 return std::string(&buffer[0], len);
Geoff Langda5777c2014-07-11 09:52:58 -040035}
36
Shannon Woods8e7d7a32014-09-02 17:09:08 -040037std::string FormatString(const char *fmt, ...)
Geoff Langda5777c2014-07-11 09:52:58 -040038{
39 va_list vararg;
40 va_start(vararg, fmt);
41 std::string result = FormatString(fmt, vararg);
42 va_end(vararg);
43 return result;
44}