1 //
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 #include "common/debug.h"
9
10 #include <stdio.h>
11
12 #include <limits>
13 #include <vector>
14
15 namespace angle
16 {
17 // dirtyPointer is a special value that will make the comparison with any valid pointer fail and
18 // force the renderer to re-apply the state.
19 const uintptr_t DirtyPointer = std::numeric_limits<uintptr_t>::max();
20 }
21
ArrayString(unsigned int i)22 std::string ArrayString(unsigned int i)
23 {
24 // We assume that UINT_MAX and GL_INVALID_INDEX are equal.
25 ASSERT(i != UINT_MAX);
26
27 std::stringstream strstr;
28 strstr << "[";
29 strstr << i;
30 strstr << "]";
31 return strstr.str();
32 }
33
ArrayIndexString(const std::vector<unsigned int> & indices)34 std::string ArrayIndexString(const std::vector<unsigned int> &indices)
35 {
36 std::stringstream strstr;
37
38 for (auto indicesIt = indices.rbegin(); indicesIt != indices.rend(); ++indicesIt)
39 {
40 // We assume that UINT_MAX and GL_INVALID_INDEX are equal.
41 ASSERT(*indicesIt != UINT_MAX);
42 strstr << "[";
43 strstr << (*indicesIt);
44 strstr << "]";
45 }
46
47 return strstr.str();
48 }
49
FormatStringIntoVector(const char * fmt,va_list vararg,std::vector<char> & outBuffer)50 size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char>& outBuffer)
51 {
52 // The state of the va_list passed to vsnprintf is undefined after the call, do a copy in case
53 // we need to grow the buffer.
54 va_list varargCopy;
55 va_copy(varargCopy, vararg);
56
57 // Attempt to just print to the current buffer
58 int len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy);
59 va_end(varargCopy);
60
61 if (len < 0 || static_cast<size_t>(len) >= outBuffer.size())
62 {
63 // Buffer was not large enough, calculate the required size and resize the buffer
64 len = vsnprintf(nullptr, 0, fmt, vararg);
65 outBuffer.resize(len + 1);
66
67 // Print again
68 va_copy(varargCopy, vararg);
69 len = vsnprintf(&(outBuffer.front()), outBuffer.size(), fmt, varargCopy);
70 va_end(varargCopy);
71 }
72 ASSERT(len >= 0);
73 return static_cast<size_t>(len);
74 }
75