-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource.cpp
223 lines (166 loc) · 5.45 KB
/
resource.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
Copyright (c) 2019 Robert Crossfield
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "stdafx.hpp"
#include <sstream>
#include <fstream>
#include <fcntl.h>
#include <sys/stat.h>
tSharedBuffer cResource::FileRead(const std::string& pFile) {
std::ifstream* fileStream;
auto fileBuffer = std::make_shared<std::vector<uint8_t>>();
// Attempt to open the file
fileStream = new std::ifstream(pFile.c_str(), std::ios::binary);
if (fileStream->is_open() != false) {
// Get file size
fileStream->seekg(0, std::ios::end);
fileBuffer->resize(static_cast<const unsigned int>(fileStream->tellg()));
fileStream->seekg(std::ios::beg);
// Allocate buffer, and read the file into it
fileStream->read((char*)fileBuffer->data(), fileBuffer->size());
if (!(*fileStream))
fileBuffer->clear();
}
// Close the stream
fileStream->close();
delete fileStream;
// All done ;)
return fileBuffer;
}
bool cResource::FileSave(const std::string& pFile, const std::string& pData) {
std::ofstream outfile(pFile, std::ofstream::binary);
if (!outfile.is_open())
return false;
outfile << pData;
outfile.close();
return true;
}
bool cResource::FileExists(const std::string& pPath) const {
struct stat info;
if (stat(pPath.c_str(), &info) != 0)
return false;
else if (info.st_mode & S_IFDIR)
return true;
else if (info.st_mode & S_IFMT)
return true;
return false;
}
bool cResource::isFile(const std::string& pPath) const {
struct stat info;
if (stat(pPath.c_str(), &info) != 0)
return false;
else if (info.st_mode & S_IFDIR)
return false;
else if (info.st_mode & S_IFMT)
return true;
return false;
}
#ifdef _MSC_VER
#include "Windows.h"
#include <direct.h>
std::string cResource::getcwd() {
char buff[1024];
(void)_getcwd(buff, 1024);
std::string cwd(buff);
return cwd;
}
std::vector<std::string> cResource::directoryList(const std::string& pPath, const std::string& pExtension) {
WIN32_FIND_DATA fdata;
HANDLE dhandle;
std::vector<std::string> results;
// Build the file path
std::stringstream finalPath;
if (pPath.size())
finalPath << pPath;
finalPath << "/*" << pExtension;
size_t size = MultiByteToWideChar(0, 0, finalPath.str().c_str(), (int)finalPath.str().length(), 0, 0);
WCHAR* pathFin = new WCHAR[size + 1];
memset(pathFin, 0, size + 1);
size = MultiByteToWideChar(0, 0, finalPath.str().c_str(), (int)size, pathFin, (int)size);
pathFin[size] = 0;
if ((dhandle = FindFirstFile(pathFin, &fdata)) == INVALID_HANDLE_VALUE) {
delete pathFin;
return results;
}
delete[] pathFin;
size_t tmp = 0;
{
char* file = new char[wcslen(fdata.cFileName) + 1];
memset(file, 0, wcslen(fdata.cFileName) + 1);
wcstombs_s(&tmp, file, wcslen(fdata.cFileName) + 1, fdata.cFileName, wcslen(fdata.cFileName));
results.push_back(std::string(file));
delete file;
}
while (1) {
if (FindNextFile(dhandle, &fdata)) {
char* file = new char[wcslen(fdata.cFileName) + 1];
memset(file, 0, wcslen(fdata.cFileName) + 1);
wcstombs_s(&tmp, file, wcslen(fdata.cFileName) + 1, fdata.cFileName, wcslen(fdata.cFileName));
results.push_back(std::string(file));
delete file;
}
else {
if (GetLastError() == ERROR_NO_MORE_FILES) {
break;
}
else {
FindClose(dhandle);
return results;
}
}
}
FindClose(dhandle);
return results;
}
#else
#include <dirent.h>
#include <unistd.h>
std::string findType;
std::string cResource::getcwd() {
char buff[1024];
::getcwd(buff, 1024);
std::string cwd(buff);
return cwd;
}
int file_select(const struct dirent* entry) {
std::string name = entry->d_name;
transform(name.begin(), name.end(), name.begin(), ::toupper);
if (name.find(findType) == std::string::npos)
return false;
return true;
}
std::vector<std::string> cResource::directoryList(const std::string& pPath, const std::string& pExtension) {
struct dirent** directFiles;
std::vector<std::string> results;
// Build the file path
std::stringstream finalPath;
finalPath << pPath << "/";
findType = pExtension;
transform(findType.begin(), findType.end(), findType.begin(), ::toupper);
int count = scandir(finalPath.str().c_str(), (dirent * **)& directFiles, file_select, 0);
for (int i = 0; i < count; ++i) {
results.push_back(std::string(directFiles[i]->d_name));
}
transform(findType.begin(), findType.end(), findType.begin(), ::tolower);
count = scandir(finalPath.str().c_str(), (dirent * **)& directFiles, file_select, 0);
for (int i = 0; i < count; ++i) {
results.push_back(std::string(directFiles[i]->d_name));
}
return results;
}
#endif