Berserk
FileSystem.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_FILESYSTEM_HPP
29 #define BERSERK_FILESYSTEM_HPP
30 
31 #include <core/Config.hpp>
32 #include <core/Data.hpp>
33 #include <core/Typedefs.hpp>
34 #include <core/string/String.hpp>
35 #include <core/templates/Ref.hpp>
36 
37 #include <cstdio>
38 #include <mutex>
39 #include <unordered_map>
40 #include <vector>
41 
43 
53 class FileSystem final {
54 public:
56  enum class EntryType {
58  File,
60  Directory,
62  Link,
64  Unknown
65  };
66 
68  struct Entry {
71  };
72 
75  BRK_API ~FileSystem() = default;
76 
88  BRK_API std::FILE *OpenFile(const String &filepath, const String &mode);
89 
94  BRK_API void CloseFile(std::FILE *file);
95 
107  BRK_API Ref<Data> ReadFile(const String &filepath);
108 
118  BRK_API void AddSearchPath(String path);
119 
128  BRK_API void SetSearchPaths(std::vector<String> searchPaths);
129 
141  BRK_API String GetFullFilePath(const String &filename);
142 
154  BRK_API String GetFullDirPath(const String &dirname);
155 
162  BRK_API String GetFileExtension(const String &filename);
163 
174  BRK_API String GetFileName(const String &filename, bool withoutExtension = false);
175 
182  BRK_API bool IsAbsolutePath(const String &filename);
183 
191  BRK_API bool IsFileExists(const String &filename);
192 
200  BRK_API bool IsDirExists(const String &dirname);
201 
209  BRK_API std::vector<Entry> ListDir(const String &dir);
210 
216 
222 
223 private:
224  void Init();
225  void ClearCache();
226  bool IsFileExistsAbs(const String &filename);
227  bool IsDirExistsAbs(const String &dirname);
228  String ResolveFilePath(const String &prefix, const String &file);
229  String ResolveDirPath(const String &prefix, const String &dir);
230  String GetPathForFile(const String &path, const String &filename);
231 
233  std::vector<String> mSearchPaths;
234 
236  std::unordered_map<String, String> mCachedFullFilePath;
237 
239  std::unordered_map<String, String> mCachedFullDirPath;
240 
242  String mExecutablePath;
243 
244  mutable std::recursive_mutex mMutex;
245 };
246 
252 
253 #endif//BERSERK_FILESYSTEM_HPP
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_API
Definition: Config.hpp:32
Target platform file system access and file utils.
Definition: FileSystem.hpp:53
BRK_API Ref< Data > ReadFile(const String &filepath)
Read file by file path.
Definition: FileSystem.cpp:44
BRK_API String GetFileExtension(const String &filename)
Retrieve file extension.
Definition: FileSystem.cpp:158
BRK_API String GetExecutableDir() const
Path to the executable directory where file of the application is located.
Definition: FileSystem.cpp:199
EntryType
Type of directory entry.
Definition: FileSystem.hpp:56
BRK_API void CloseFile(std::FILE *file)
Close opened file.
Definition: FileSystem.cpp:39
BRK_API bool IsFileExists(const String &filename)
Check if specified file exists.
Definition: FileSystem.cpp:166
BRK_API void AddSearchPath(String path)
Add search path.
Definition: FileSystem.cpp:69
BRK_API std::FILE * OpenFile(const String &filepath, const String &mode)
Open file by file path and mode.
Definition: UnixFileSystem.cpp:44
BRK_API String GetFullDirPath(const String &dirname)
Get full path for specified directory.
Definition: FileSystem.cpp:126
BRK_API bool IsAbsolutePath(const String &filename)
Check is passed path is absolute.
Definition: UnixFileSystem.cpp:68
BRK_API String GetFileName(const String &filename, bool withoutExtension=false)
Get file name from file path.
Definition: UnixFileSystem.cpp:48
BRK_API ~FileSystem()=default
BRK_API const String & GetExecutablePath()
Path to the executable file of the application.
Definition: FileSystem.cpp:195
BRK_API FileSystem()
Definition: FileSystem.cpp:35
BRK_API bool IsDirExists(const String &dirname)
Check if specified directory exists.
Definition: FileSystem.cpp:171
BRK_API String GetFullFilePath(const String &filename)
Get full path for specified file.
Definition: FileSystem.cpp:99
BRK_API void SetSearchPaths(std::vector< String > searchPaths)
Set search paths for paths resolution.
Definition: FileSystem.cpp:83
BRK_API std::vector< Entry > ListDir(const String &dir)
List entries of the specified directory.
Definition: UnixFileSystem.cpp:72
Utf-8 encoded std based default string class.
Definition: GLDevice.cpp:46
Directory entry.
Definition: FileSystem.hpp:68
String name
Definition: FileSystem.hpp:69
EntryType type
Definition: FileSystem.hpp:70