1 //
2 // Copyright (c) 2017-2020 Advanced Micro Devices, Inc. All rights reserved.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22
23 #include "Common.h"
24
25 #ifdef _WIN32
26
ReadFile(std::vector<char> & out,const char * fileName)27 void ReadFile(std::vector<char>& out, const char* fileName)
28 {
29 std::ifstream file(fileName, std::ios::ate | std::ios::binary);
30 assert(file.is_open());
31 size_t fileSize = (size_t)file.tellg();
32 if(fileSize > 0)
33 {
34 out.resize(fileSize);
35 file.seekg(0);
36 file.read(out.data(), fileSize);
37 }
38 else
39 out.clear();
40 }
41
SetConsoleColor(CONSOLE_COLOR color)42 void SetConsoleColor(CONSOLE_COLOR color)
43 {
44 WORD attr = 0;
45 switch(color)
46 {
47 case CONSOLE_COLOR::INFO:
48 attr = FOREGROUND_INTENSITY;;
49 break;
50 case CONSOLE_COLOR::NORMAL:
51 attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
52 break;
53 case CONSOLE_COLOR::WARNING:
54 attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
55 break;
56 case CONSOLE_COLOR::ERROR_:
57 attr = FOREGROUND_RED | FOREGROUND_INTENSITY;
58 break;
59 default:
60 assert(0);
61 }
62
63 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
64 SetConsoleTextAttribute(out, attr);
65 }
66
PrintMessage(CONSOLE_COLOR color,const char * msg)67 void PrintMessage(CONSOLE_COLOR color, const char* msg)
68 {
69 if(color != CONSOLE_COLOR::NORMAL)
70 SetConsoleColor(color);
71
72 printf("%s\n", msg);
73
74 if (color != CONSOLE_COLOR::NORMAL)
75 SetConsoleColor(CONSOLE_COLOR::NORMAL);
76 }
77
PrintMessage(CONSOLE_COLOR color,const wchar_t * msg)78 void PrintMessage(CONSOLE_COLOR color, const wchar_t* msg)
79 {
80 if(color != CONSOLE_COLOR::NORMAL)
81 SetConsoleColor(color);
82
83 wprintf(L"%s\n", msg);
84
85 if (color != CONSOLE_COLOR::NORMAL)
86 SetConsoleColor(CONSOLE_COLOR::NORMAL);
87 }
88
89 static const size_t CONSOLE_SMALL_BUF_SIZE = 256;
90
PrintMessageV(CONSOLE_COLOR color,const char * format,va_list argList)91 void PrintMessageV(CONSOLE_COLOR color, const char* format, va_list argList)
92 {
93 size_t dstLen = (size_t)::_vscprintf(format, argList);
94 if(dstLen)
95 {
96 bool useSmallBuf = dstLen < CONSOLE_SMALL_BUF_SIZE;
97 char smallBuf[CONSOLE_SMALL_BUF_SIZE];
98 std::vector<char> bigBuf(useSmallBuf ? 0 : dstLen + 1);
99 char* bufPtr = useSmallBuf ? smallBuf : bigBuf.data();
100 ::vsprintf_s(bufPtr, dstLen + 1, format, argList);
101 PrintMessage(color, bufPtr);
102 }
103 }
104
PrintMessageV(CONSOLE_COLOR color,const wchar_t * format,va_list argList)105 void PrintMessageV(CONSOLE_COLOR color, const wchar_t* format, va_list argList)
106 {
107 size_t dstLen = (size_t)::_vcwprintf(format, argList);
108 if(dstLen)
109 {
110 bool useSmallBuf = dstLen < CONSOLE_SMALL_BUF_SIZE;
111 wchar_t smallBuf[CONSOLE_SMALL_BUF_SIZE];
112 std::vector<wchar_t> bigBuf(useSmallBuf ? 0 : dstLen + 1);
113 wchar_t* bufPtr = useSmallBuf ? smallBuf : bigBuf.data();
114 ::vswprintf_s(bufPtr, dstLen + 1, format, argList);
115 PrintMessage(color, bufPtr);
116 }
117 }
118
PrintMessageF(CONSOLE_COLOR color,const char * format,...)119 void PrintMessageF(CONSOLE_COLOR color, const char* format, ...)
120 {
121 va_list argList;
122 va_start(argList, format);
123 PrintMessageV(color, format, argList);
124 va_end(argList);
125 }
126
PrintMessageF(CONSOLE_COLOR color,const wchar_t * format,...)127 void PrintMessageF(CONSOLE_COLOR color, const wchar_t* format, ...)
128 {
129 va_list argList;
130 va_start(argList, format);
131 PrintMessageV(color, format, argList);
132 va_end(argList);
133 }
134
PrintWarningF(const char * format,...)135 void PrintWarningF(const char* format, ...)
136 {
137 va_list argList;
138 va_start(argList, format);
139 PrintMessageV(CONSOLE_COLOR::WARNING, format, argList);
140 va_end(argList);
141 }
142
PrintWarningF(const wchar_t * format,...)143 void PrintWarningF(const wchar_t* format, ...)
144 {
145 va_list argList;
146 va_start(argList, format);
147 PrintMessageV(CONSOLE_COLOR::WARNING, format, argList);
148 va_end(argList);
149 }
150
PrintErrorF(const char * format,...)151 void PrintErrorF(const char* format, ...)
152 {
153 va_list argList;
154 va_start(argList, format);
155 PrintMessageV(CONSOLE_COLOR::WARNING, format, argList);
156 va_end(argList);
157 }
158
PrintErrorF(const wchar_t * format,...)159 void PrintErrorF(const wchar_t* format, ...)
160 {
161 va_list argList;
162 va_start(argList, format);
163 PrintMessageV(CONSOLE_COLOR::WARNING, format, argList);
164 va_end(argList);
165 }
166
SaveFile(const wchar_t * filePath,const void * data,size_t dataSize)167 void SaveFile(const wchar_t* filePath, const void* data, size_t dataSize)
168 {
169 FILE* f = nullptr;
170 _wfopen_s(&f, filePath, L"wb");
171 if(f)
172 {
173 fwrite(data, 1, dataSize, f);
174 fclose(f);
175 }
176 else
177 assert(0);
178 }
179
180 #endif // #ifdef _WIN32
181