-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathefxc2CompilerIncludes.cpp
More file actions
168 lines (158 loc) · 5.2 KB
/
efxc2CompilerIncludes.cpp
File metadata and controls
168 lines (158 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//--------------------------------------------------------------------------------------
// File: efxc2CompilerIncludes.cpp
//
// Copyright (c) J. Peter Mugaas
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//--------------------------------------------------------------------------------------
/*
This unit does cause some warnings with SonarLint.I'm not sure what to do about these warnings
since the unit is part of an inheritance callback scheme from a .DLL. The API is defined in
d3dcommon.h.
*/
#include "efxc2CompilerIncludes.h"
namespace efxc2CompilerIncludes {
static void TrimTrailingWhiteSpace(const char* buf, std::uintmax_t* fileSize) {
size_t tmp = *fileSize;
for (size_t i = *fileSize; i > 0; --i) {
if (buf[i - 1] >= 32) { //-V3539 //-V2563
break;
}
else {
tmp--;
}
}
*fileSize = tmp;
return;
}
static bool LoadFile(const std::filesystem::path& currentFile, int verbose, char** buf, std::uintmax_t* fileSize) {
std::ifstream f;
std::error_code ec;
bool result = true;
*fileSize = std::filesystem::file_size(currentFile, ec);
if (ec.value() == 0) {
if (verbose) {
#ifdef _WIN32
std::wcout << M_FORMAT(L"Found {}\n", currentFile.native());
#else
std::cout << M_FORMAT("Found {}\n", currentFile.native());
#endif
}
*buf = new char[*fileSize]; //-V2511
(void)memset(*buf, 0, *fileSize);
f = std::ifstream(currentFile, std::ios::in | std::ios::binary);
if (f.is_open()) {
(void)f.read(*buf, *fileSize);
f.close();
TrimTrailingWhiteSpace(*buf, fileSize);
}
else {
result = false;
}
}
else {
result = false;
}
return result;
}
__declspec(nothrow) HRESULT __stdcall efxc2CompilerIncludes::CompilerIncludes::Open(D3D_INCLUDE_TYPE IncludeType,
LPCSTR pFileName,
LPCVOID pParentData,
LPCVOID* ppData, UINT* pBytes) {
auto result = E_FAIL; //-V3515 //-V2523
*ppData = nullptr;
*pBytes = 0;
std::filesystem::path Filename = std::string(pFileName);
char* buf = nullptr;
std::uintmax_t fileSize = 0;
try {
if (verbose && debug) {
std::cout << "Called CompilerIncludes::Open(\n";
switch (IncludeType) {
case D3D_INCLUDE_LOCAL: // #include "FILE"
std::cout << "\tIncludeType: D3D_INCLUDE_LOCAL\n";
break;
case D3D_INCLUDE_SYSTEM: // #include <FILE>
std::cout << "\tIncludeType: D3D_INCLUDE_SYSTEM\n";
break;
/* D3D10_INCLUDE_LOCAL is an alias for D3D_INCLUDE_LOCAL.
D3D10_INCLUDE_SYSTEM is an alias for D3D_INCLUDE_SYSTEM */
default:;
break;
}
#ifdef _WIN32
std::wcout << M_FORMAT(L"\tpFileName: {}\n", Filename.native());
#else
std::cout << M_FORMAT("\tpFileName {}\n", Filename.native());
#endif
if (pParentData != nullptr) {
std::cout << "\tpParentData: *****)\n";
}
else {
std::cout << "\tpParentData: nullptr)\n";
}
}
std::filesystem::path TryInputFile = "";
/* In this function, we have to disable V3515 because the Win32 API headers did not
define the E_FAIL constant with a U suffix. */
if (!input_parent_path.empty()) {
TryInputFile = input_parent_path;
TryInputFile += std::filesystem::path::preferred_separator;
TryInputFile += Filename;
if (LoadFile(TryInputFile, verbose, &buf, &fileSize)) {
*ppData = buf;
/* These is deliberately a UINT because of an API limitation in this .DLL inheritance callback. */
*pBytes = static_cast<UINT>(fileSize);
result = S_OK;
}
}
if ((result == E_FAIL) && (LoadFile(Filename, verbose, &buf, &fileSize))) { //-V3515 //-V2523
*ppData = buf;
/* These is deliberately a UINT because of an API limitation in this .DLL inheritance callback. */
*pBytes = static_cast<UINT>(fileSize);
result = S_OK;
}
if (result == E_FAIL) { //-V3515 //-V2523
for (std::filesystem::path const& currentDir : dirs) {
TryInputFile = currentDir;
TryInputFile += std::filesystem::path::preferred_separator;
TryInputFile += Filename;
if (LoadFile(TryInputFile, verbose, &buf, &fileSize)) {
*ppData = buf;
/* These is deliberately a UINT because of an API limitation in this .DLL inheritance callback. */
*pBytes = static_cast<UINT>(fileSize);
result = S_OK;
}
}
}
}
catch (...) {
result = E_FAIL; //-V3515 //-V2523
}
return result;
}
/* do not change this signature, it's part of an "inheritance" API. */
__declspec(nothrow) HRESULT __stdcall efxc2CompilerIncludes::CompilerIncludes::Close(LPCVOID pData) {
if (verbose && debug) {
std::cout << "Called CompilerIncludes::Close(\n";
if (pData != nullptr) {
std::cout << "\tpData: *****\n";
}
else {
std::cout << "\tpData: nullptr\n";
}
}
auto buf = M_BIT_CAST<char*>(pData);
delete[] buf; //-V2511
return S_OK;
}
/* do not change this signature, it's part of an "inheritance" API. */
void efxc2CompilerIncludes::CompilerIncludes::AddIncludeDir(const efxc2Utils::M_STRING_VIEW _dir)
{
efxc2Utils::M_STRING dir = { _dir.data(), _dir.size() };
(void)dirs.emplace(dirs.end(), dir);
(void)dirs[dirs.size() - 1].make_preferred(); //-V2563 //-V3539
}
}