blob: 2ced98d8b4a6702dbe5a1edc2e8b38a78a2936cf [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"
8
Olli Etuaho86ffde52014-10-03 14:51:54 +03009#include <stdio.h>
Geoff Langda5777c2014-07-11 09:52:58 -040010#include <vector>
11
Shannon Woods8e7d7a32014-09-02 17:09:08 -040012std::string FormatString(const char *fmt, va_list vararg)
Geoff Langda5777c2014-07-11 09:52:58 -040013{
14 static std::vector<char> buffer(512);
15
16 // Attempt to just print to the current buffer
Shannon Woods8e7d7a32014-09-02 17:09:08 -040017 int len = vsnprintf(&buffer[0], buffer.size(), fmt, vararg);
Geoff Langda5777c2014-07-11 09:52:58 -040018 if (len < 0 || static_cast<size_t>(len) >= buffer.size())
19 {
20 // Buffer was not large enough, calculate the required size and resize the buffer
Shannon Woods8e7d7a32014-09-02 17:09:08 -040021 len = vsnprintf(NULL, 0, fmt, vararg);
Geoff Langda5777c2014-07-11 09:52:58 -040022 buffer.resize(len + 1);
23
24 // Print again
Shannon Woods8e7d7a32014-09-02 17:09:08 -040025 vsnprintf(&buffer[0], buffer.size(), fmt, vararg);
Geoff Langda5777c2014-07-11 09:52:58 -040026 }
27
Jamie Madillb4fd0c92014-10-01 17:40:24 -040028 return std::string(&buffer[0], len);
Geoff Langda5777c2014-07-11 09:52:58 -040029}
30
Shannon Woods8e7d7a32014-09-02 17:09:08 -040031std::string FormatString(const char *fmt, ...)
Geoff Langda5777c2014-07-11 09:52:58 -040032{
33 va_list vararg;
34 va_start(vararg, fmt);
35 std::string result = FormatString(fmt, vararg);
36 va_end(vararg);
37 return result;
38}