1 //
2 // Copyright 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
SaveFileHelper(const std::string & filePathIn)21 SaveFileHelper::SaveFileHelper(const std::string &filePathIn)
22 : mOfs(filePathIn, std::ios::binary | std::ios::out), mFilePath(filePathIn)
23 {
24 if (!mOfs.is_open())
25 {
26 FATAL() << "Could not open " << filePathIn;
27 }
28 }
29
~SaveFileHelper()30 SaveFileHelper::~SaveFileHelper()
31 {
32 printf("Saved '%s'.\n", mFilePath.c_str());
33 }
34
checkError()35 void SaveFileHelper::checkError()
36 {
37 if (mOfs.bad())
38 {
39 FATAL() << "Error writing to " << mFilePath;
40 }
41 }
42
write(const uint8_t * data,size_t size)43 void SaveFileHelper::write(const uint8_t *data, size_t size)
44 {
45 mOfs.write(reinterpret_cast<const char *>(data), size);
46 }
47
48 } // namespace angle
49
ArrayString(unsigned int i)50 std::string ArrayString(unsigned int i)
51 {
52 // We assume that UINT_MAX and GL_INVALID_INDEX are equal.
53 ASSERT(i != UINT_MAX);
54
55 std::stringstream strstr;
56 strstr << "[";
57 strstr << i;
58 strstr << "]";
59 return strstr.str();
60 }
61
ArrayIndexString(const std::vector<unsigned int> & indices)62 std::string ArrayIndexString(const std::vector<unsigned int> &indices)
63 {
64 std::stringstream strstr;
65
66 for (auto indicesIt = indices.rbegin(); indicesIt != indices.rend(); ++indicesIt)
67 {
68 // We assume that UINT_MAX and GL_INVALID_INDEX are equal.
69 ASSERT(*indicesIt != UINT_MAX);
70 strstr << "[";
71 strstr << (*indicesIt);
72 strstr << "]";
73 }
74
75 return strstr.str();
76 }
77
FormatStringIntoVector(const char * fmt,va_list vararg,std::vector<char> & outBuffer)78 size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char> &outBuffer)
79 {
80 va_list varargCopy;
81 va_copy(varargCopy, vararg);
82
83 int len = vsnprintf(nullptr, 0, fmt, vararg);
84 ASSERT(len >= 0);
85
86 outBuffer.resize(len + 1, 0);
87
88 len = vsnprintf(outBuffer.data(), outBuffer.size(), fmt, varargCopy);
89 va_end(varargCopy);
90 ASSERT(len >= 0);
91 return static_cast<size_t>(len);
92 }
93