33//
44
55#include < Utils/FileSystem/FileSystem.h>
6- #include < Utils/Debug .h>
6+ #include < Utils/FileSystem/VirtualFS .h>
77#include < Utils/Platform/Platform.h>
8- #include < Utils/FileSystem/Path.h>
98#include < Utils/Common/Hashes.h>
109#include < Utils/Common/StringUtils.h>
1110#include < Utils/Profile/TracyContext.h>
@@ -21,33 +20,11 @@ namespace SR_UTILS_NS {
2120 return data;
2221 }
2322
24- SR_COMMON_DLL_API char * FileSystem::Load (std::string path) {
25- // / open file
26- std::ifstream infile (path);
27- if (!infile.is_open ()) {
28- SR_ERROR (" FileSystem::Load() : failed open file!\n\t Path: " +path);
29- return nullptr ;
30- }
31-
32-
33- // / get length of file
34- infile.seekg (0 , std::ios::end);
35- size_t length = infile.tellg ();
36- infile.seekg (0 , std::ios::beg);
37-
38- char * buffer = new char [length];
39-
40- // / read file
41- infile.read (buffer, length);
42-
43- return buffer;
44- }
45-
4623 SR_COMMON_DLL_API std::vector<std::string_view> FileSystem::ReadAllTextAsStringViewVector (const Path& path, std::string& buffer) {
4724 SR_TRACY_ZONE;
4825 std::vector<std::string_view> result;
4926
50- if (!SR_PLATFORM_NS ::ReadFile (path, buffer)) {
27+ if (!SR_UTILS_NS::FileSystem ::ReadFile (path, buffer)) {
5128 return result;
5229 }
5330
@@ -78,88 +55,6 @@ namespace SR_UTILS_NS {
7855 return result;
7956 }
8057
81- SR_COMMON_DLL_API std::string FileSystem::ReadAllText (const std::string& path) {
82- SR_TRACY_ZONE;
83-
84- std::string data = std::string ();
85- std::ifstream stream (path, std::ios::in);
86- if (!stream) {
87- SR_ERROR (" FileSystem::ReadAllText() : failed to open \" " + path + " \" file!" );
88- return data;
89- }
90-
91- stream.seekg (0 , std::ios::end);
92- std::streampos bytes = stream.tellg ();
93- stream.seekg (0 , std::ios::beg);
94- data.reserve (bytes);
95-
96- if (stream.is_open ()) {
97- std::string line;
98- bool first = false ;
99- while (getline (stream, line)) {
100- if (!first) {
101- first = true ;
102- data += line;
103- }
104- else {
105- data += " \n " ;
106- data += line;
107- }
108- }
109- stream.close ();
110- }
111- return data;
112- }
113-
114- SR_COMMON_DLL_API std::vector<uint8_t > FileSystem::ReadFileAsVector (const std::string &path) {
115- std::ifstream file (path, std::ifstream::binary | std::ios::in);
116-
117- if (file.fail ()) {
118- return std::vector<uint8_t >();
119- }
120-
121- file.seekg (0 , std::ios::end);
122- std::streampos end = file.tellg ();
123- file.seekg (0 , std::ios::beg);
124- std::streampos start = file.tellg ();
125- size_t size = static_cast <size_t >(end - start);
126-
127- std::vector<uint8_t > result (size);
128-
129- file.read (reinterpret_cast <char *>(result.data ()), size);
130-
131- return result;
132- }
133-
134- SR_COMMON_DLL_API std::vector<char > FileSystem::ReadBinary (const std::string_view path) {
135- /* std::ifstream ifd(path, std::ios::binary | std::ios::ate);
136- int size = ifd.tellg();
137- ifd.seekg(0, std::ios::beg);
138- std::vector<char> buffer;
139- buffer.resize(size); // << resize not reserve
140- ifd.read(buffer.data(), size);*/
141-
142- // std::ifstream input(path, std::ios::binary);
143- // std::vector<uint32_t> buffer(std::istreambuf_iterator<char>(input), {});
144-
145- std::ifstream file (path.data (), std::ios::ate | std::ios::binary);
146-
147- if (!file.is_open ()) {
148- SR_ERROR (" FileSystem::ReadBinary() : failed to open \" {}\" file!" , path);
149- return std::vector<char >();
150- }
151-
152- size_t fileSize = (size_t ) file.tellg ();
153- std::vector<char > buffer (fileSize);
154-
155- file.seekg (0 );
156- file.read (buffer.data (), fileSize);
157-
158- file.close ();
159-
160- return buffer;
161- }
162-
16358 SR_COMMON_DLL_API bool FileSystem::CreatePath (std::string path, uint32_t offset) {
16459 if (path.empty ()) {
16560 return false ;
@@ -187,15 +82,11 @@ namespace SR_UTILS_NS {
18782 #endif
18883 }
18984
190- // std::string FileSystem::GetFullPath(const std::string& path) {
191- // #ifdef SR_WIN32
192- // char fullFilename[MAX_PATH];
193- // GetFullPathName(path.c_str(), MAX_PATH, fullFilename, nullptr);
194- // return std::string(fullFilename);
195- // #else
196- // return std::string();
197- // #endif
198- // }
85+ SR_COMMON_DLL_API bool FileSystem::ReadFile (const Path& path, std::string& buffer) {
86+ SR_TRACY_ZONE;
87+
88+ return SR_PLATFORM_NS::ReadFile (path, buffer);
89+ }
19990
20091 SR_COMMON_DLL_API std::string FileSystem::NormalizePath (const std::string &path) {
20192 SR_TRACY_ZONE;
@@ -228,29 +119,6 @@ namespace SR_UTILS_NS {
228119 return true ;
229120 }
230121
231- SR_COMMON_DLL_API std::string FileSystem::ReadBinaryAsString (const Path& path, bool checkError) {
232- SR_TRACY_ZONE;
233-
234- std::ifstream file (path.ToStringRef (), std::ios::ate | std::ios::binary);
235-
236- if (!file.is_open ()) {
237- SR_UNUSED_VARIABLE (checkError);
238- SRAssert2 (!checkError, " FileSystem::ReadBinaryAsString() : failed to open \" " + path.ToStringRef () + " \" file!" );
239- return std::string ();
240- }
241-
242- size_t fileSize = (size_t ) file.tellg ();
243- std::string buffer;
244- buffer.resize (fileSize);
245-
246- file.seekg (0 );
247- file.read (buffer.data (), fileSize);
248-
249- file.close ();
250-
251- return buffer;
252- }
253-
254122 SR_COMMON_DLL_API uint64_t FileSystem::GetFileHash (const std::string& path) {
255123 SR_TRACY_ZONE;
256124 SR_TRACY_ZONE_TEXT (path);
@@ -259,7 +127,7 @@ namespace SR_UTILS_NS {
259127
260128 {
261129 SR_TRACY_ZONE_N (" Read file" );
262- if (!SR_PLATFORM_NS ::ReadFile (path, buffer)) {
130+ if (!SR_UTILS_NS::FileSystem ::ReadFile (path, buffer)) {
263131 SR_WARN (" FileSystem::GetFileHash() : failed to read file!\n\t Path: " + path);
264132 return SR_UINT64_MAX;
265133 }
@@ -297,8 +165,16 @@ namespace SR_UTILS_NS {
297165 return hash;
298166 }
299167
300- SR_COMMON_DLL_API std::shared_ptr<std::vector<uint8_t >> FileSystem::ReadFileAsBlob (const std::string &path) {
301- return std::make_shared<std::vector<uint8_t >>(std::move (ReadFileAsVector (path)));
168+ SR_COMMON_DLL_API std::shared_ptr<std::string> FileSystem::ReadFileAsBlob (const std::string& path) {
169+ SR_TRACY_ZONE;
170+
171+ std::shared_ptr<std::string> pBuffer = std::make_shared<std::string>();
172+ if (!SR_UTILS_NS::FileSystem::ReadFile (path, *pBuffer)) {
173+ SR_ERROR (" FileSystem::ReadFileAsBlob() : failed to read file!\n\t Path: " + path);
174+ return nullptr ;
175+ }
176+
177+ return pBuffer;
302178 }
303179
304180 SR_COMMON_DLL_API uint64_t FileSystem::ReadHashFromFile (const Path& path) {
0 commit comments