Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
FileManagement.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_PLATFORM_FILE_FILE_MANAGEMENT_H_
34#define DVD_PLATFORM_FILE_FILE_MANAGEMENT_H_
35
36namespace Divide {
37
38enum class FileError : U8 {
39 NONE = 0,
40 FILE_NOT_FOUND,
41 FILE_EMPTY,
42 FILE_CREATE_ERROR,
43 FILE_READ_ERROR,
44 FILE_OPEN_ERROR,
45 FILE_WRITE_ERROR,
46 FILE_DELETE_ERROR,
47 FILE_OVERWRITE_ERROR,
48 FILE_COPY_ERROR,
49 FILE_TARGET_BUFFER_ERROR,
50 COUNT
51};
52
53namespace Names
54{
55 static const char* fileError[] = {
56 "NONE", "FILE_NOT_FOUND", "FILE_EMPTY", "FILE_CREATE_ERROR","FILE_READ_ERROR", "FILE_OPEN_ERROR", "FILE_WRITE_ERROR", "FILE_DELETE_ERROR", "FILE_OVERWRITE_ERROR", "FILE_COPY_ERROR", "FILE_TARGET_BUFFER_ERROR", "UNKNOWN"
57 };
58}
59
60struct SysInfo;
61class PlatformContext;
62struct Paths {
63 constexpr static const char g_pathSeparator =
64#if defined(IS_WINDOWS_BUILD)
65 '\\';
66#else //IS_WINDOWS_BUILD
67 '/';
68#endif //IS_WINDOWS_BUILD
69
70 static ResourcePath g_logPath;
71 static ResourcePath g_screenshotPath;
72 static ResourcePath g_assetsLocation;
73 static ResourcePath g_modelsLocation;
74 static ResourcePath g_shadersLocation;
75 static ResourcePath g_texturesLocation;
76 static ResourcePath g_proceduralTexturesLocation;
77 static ResourcePath g_heightmapLocation;
78 static ResourcePath g_climatesLowResLocation;
79 static ResourcePath g_climatesMedResLocation;
80 static ResourcePath g_climatesHighResLocation;
81 static ResourcePath g_imagesLocation;
82 static ResourcePath g_materialsLocation;
83 static ResourcePath g_soundsLocation;
84 static ResourcePath g_xmlDataLocation;
85 static ResourcePath g_navMeshesLocation;
86 static ResourcePath g_scenesLocation;
87 static ResourcePath g_projectsLocation;
88 static ResourcePath g_saveLocation;
89 static ResourcePath g_nodesSaveLocation;
90 static ResourcePath g_GUILocation;
91 static ResourcePath g_fontsPath;
92 static ResourcePath g_iconsPath;
93 static ResourcePath g_localisationPath;
94 static ResourcePath g_cacheLocation;
95 static ResourcePath g_buildTypeLocation;
96 static ResourcePath g_terrainCacheLocation;
97 static ResourcePath g_geometryCacheLocation;
98 static ResourcePath g_collisionMeshCacheLocation;
99
100 struct Editor {
101 static ResourcePath g_saveLocation;
102 };
103
104 struct Scripts {
105 static ResourcePath g_scriptsLocation;
106 static ResourcePath g_scriptsAtomsLocation;
107 };
108
109 struct Textures {
110 static ResourcePath g_metadataLocation;
111 static Str<8> g_ddsExtension;
112 };
113
114 struct Shaders {
115 static ResourcePath g_cacheLocation;
116 static ResourcePath g_cacheLocationGL;
117 static ResourcePath g_cacheLocationVK;
118 static ResourcePath g_cacheLocationText;
119 static ResourcePath g_cacheLocationSpv;
120 static ResourcePath g_cacheLocationRefl;
121
122 static Str<8> g_ReflectionExt;
123 static Str<8> g_SPIRVExt;
124 // Shader subfolder name that contains SPIRV shader files
125 static ResourcePath g_SPIRVShaderLoc;
126
127 struct GLSL {
128 // these must match the last 4 characters of the atom file
129 static Str<8> g_fragAtomExt;
130 static Str<8> g_vertAtomExt;
131 static Str<8> g_geomAtomExt;
132 static Str<8> g_tescAtomExt;
133 static Str<8> g_teseAtomExt;
134 static Str<8> g_compAtomExt;
135 static Str<8> g_comnAtomExt;
136
137 // Shader subfolder name that contains GLSL shader files
138 static ResourcePath g_GLSLShaderLoc;
139 // Atom folder names in parent shader folder
140 static ResourcePath g_fragAtomLoc;
141 static ResourcePath g_vertAtomLoc;
142 static ResourcePath g_geomAtomLoc;
143 static ResourcePath g_tescAtomLoc;
144 static ResourcePath g_teseAtomLoc;
145 static ResourcePath g_compAtomLoc;
146 static ResourcePath g_comnAtomLoc;
147 }; //class GLSL
148 }; //class Shaders
149
150 // include command regex pattern
151 static constexpr auto g_includePattern = ctll::fixed_string{ R"(\s*#\s*include\s+["<]([^">]+)*[">])" };
152 // define regex pattern
153 static constexpr auto g_definePattern = ctll::fixed_string{ R"(([#!][A-z]{2,}[\s]{1,}?([A-z]{2,}[\s]{1,}?)?)([\\‍(]?[^\s\\)]{1,}[\\)]?)?)" };
154 // use command regex pattern
155 static constexpr auto g_usePattern = ctll::fixed_string{ R"(\s*use\s*\‍(\s*\"(.+)\"\s*\).*)" };
156 // shader uniform patter
157 static constexpr auto g_uniformPattern = ctll::fixed_string{ R"(\s*uniform\s+\s*([^),^;^\s]*)\s+([^),^;^\s]*\[*\s*\]*)\s*(?:=*)\s*(?:\d*.*)\s*(?:;+).*)" };
158 // project specifying command line argument
159 static constexpr auto g_useProjectPattern = ctll::fixed_string{ R"((--project\s*=\s*)([0-9a-zA-Z]*))" };
160
161 static void initPaths();
162}; //class Paths
163
164struct FileEntry
165{
166 ResourcePath _name{};
167 U64 _lastWriteTime{0u};
168};
169using FileList = vector<FileEntry>;
170
171[[nodiscard]] ResourcePath getWorkingDirectory();
172
173[[nodiscard]] bool pathExists(const ResourcePath& filePath);
174
175[[nodiscard]] bool fileExists(const ResourcePath& filePathAndName);
176[[nodiscard]] bool fileExists(const ResourcePath& filePath, std::string_view fileName);
177
178[[nodiscard]] bool fileIsEmpty(const ResourcePath& filePathAndName);
179[[nodiscard]] bool fileIsEmpty(const ResourcePath& filePath, std::string_view fileName);
180
181[[nodiscard]] FileError createDirectory(const ResourcePath& path);
182[[nodiscard]] FileError removeDirectory( const ResourcePath& path );
183
184[[nodiscard]] bool createFile(const ResourcePath& filePathAndName, bool overwriteExisting);
185
186[[nodiscard]] bool deleteAllFiles(const ResourcePath& filePath, const char* extensionNoDot = nullptr, const char* extensionToSkip = nullptr);
187
188[[nodiscard]] bool getAllFilesInDirectory(const ResourcePath& filePath, FileList& listInOut, const char* extensionNoDot = nullptr);
189
190[[nodiscard]] FileError fileLastWriteTime(const ResourcePath& filePathAndName, U64& timeOutSec);
191[[nodiscard]] FileError fileLastWriteTime(const ResourcePath& filePath, std::string_view fileName, U64& timeOutSec);
192
193[[nodiscard]] size_t numberOfFilesInDirectory(const ResourcePath& path);
194
196[[nodiscard]] FileError readFile(const ResourcePath& filePath, std::string_view fileName, FileType fileType, std::ifstream& sreamOut);
197[[nodiscard]] FileError readFile(const ResourcePath& filePath, std::string_view fileName, FileType fileType, string& contentOut);
198[[nodiscard]] FileError readFile(const ResourcePath& filePath, std::string_view fileName, FileType fileType, std::string& contentOut);
199[[nodiscard]] FileError readFile(const ResourcePath& filePath, std::string_view fileName, FileType fileType, Byte* contentOut, size_t& sizeInOut);
200
201[[nodiscard]] FileError openFile(const ResourcePath& filePath, std::string_view fileName);
202[[nodiscard]] FileError openFile(std::string_view cmd, const ResourcePath& filePath, std::string_view fileName);
203
204[[nodiscard]] FileError writeFile(const ResourcePath& filePath, std::string_view fileName, bufferPtr content, size_t length, FileType fileType);
205[[nodiscard]] FileError writeFile(const ResourcePath& filePath, std::string_view fileName, const char* content, size_t length, FileType fileType);
206
207[[nodiscard]] FileError deleteFile(const ResourcePath& filePath, std::string_view fileName);
208
209[[nodiscard]] FileError copyFile(const ResourcePath& sourcePath, std::string_view sourceName, const ResourcePath& targetPath, std::string_view targetName, bool overwrite);
210
211[[nodiscard]] FileError copyDirectory(const ResourcePath& sourcePath, const ResourcePath& targetPath, bool recursively, bool overwrite);
212
213[[nodiscard]] FileError findFile(const ResourcePath& filePath, std::string_view fileName, string& foundPath);
214
215[[nodiscard]] bool hasExtension(std::string_view fileName, std::string_view extensionNoDot);
216
217[[nodiscard]] bool hasExtension(const ResourcePath& filePath, std::string_view extensionNoDot);
218
219[[nodiscard]] string getExtension(std::string_view fileName );
220
221[[nodiscard]] string getExtension(const ResourcePath& filePath );
222
223[[nodiscard]] ResourcePath getTopLevelFolderName(const ResourcePath& filePath);
224
225[[nodiscard]] string stripExtension(std::string_view fileName ) noexcept;
226
227[[nodiscard]] ResourcePath stripExtension(const ResourcePath& filePath) noexcept;
228
229[[nodiscard]] string stripQuotes( std::string_view input);
230
231[[nodiscard]] FileNameAndPath splitPathToNameAndLocation(const ResourcePath& input);
232
233[[nodiscard]] string extractFilePathAndName(char* argv0);
234
235}; //namespace Divide
236
237#endif //DVD_PLATFORM_FILE_FILE_MANAGEMENT_H_
238
239#include "FileManagement.inl"
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
FileError removeDirectory(const ResourcePath &path)
FileError openFile(const std::string_view cmd, const ResourcePath &filePath, const std::string_view fileName)
std::byte Byte
ResourcePath getTopLevelFolderName(const ResourcePath &filePath)
bool hasExtension(const ResourcePath &filePath, const std::string_view extensionNoDot)
FileError writeFile(const ResourcePath &filePath, const std::string_view fileName, const char *content, const size_t length, const FileType fileType)
FileError readFile(const ResourcePath &filePath, std::string_view fileName, FileType fileType, std::ifstream &sreamOut)
size_t numberOfFilesInDirectory(const ResourcePath &path)
uint8_t U8
ResourcePath getWorkingDirectory()
bool deleteAllFiles(const ResourcePath &filePath, const char *extension, const char *extensionToSkip)
string getExtension(const std::string_view fileName)
FileError createDirectory(const ResourcePath &path)
FileError copyFile(const ResourcePath &sourcePath, const std::string_view sourceName, const ResourcePath &targetPath, const std::string_view targetName, const bool overwrite)
bool fileIsEmpty(const ResourcePath &filePathAndName)
bool getAllFilesInDirectory(const ResourcePath &filePath, FileList &listInOut, const char *extensionNoDot)
FileError findFile(const ResourcePath &filePath, const std::string_view fileName, string &foundPath)
string stripExtension(const std::string_view fileName) noexcept
FileError copyDirectory(const ResourcePath &sourcePath, const ResourcePath &targetPath, bool recursively, bool overwrite)
FileError deleteFile(const ResourcePath &filePath, const std::string_view fileName)
bool fileExists(const ResourcePath &filePathAndName)
bool pathExists(const ResourcePath &filePath)
string extractFilePathAndName(char *argv0)
void * bufferPtr
uint64_t U64
FileError fileLastWriteTime(const ResourcePath &filePathAndName, U64 &timeOutSec)
FileNameAndPath splitPathToNameAndLocation(const ResourcePath &input)
bool createFile(const ResourcePath &filePathAndName, const bool overwriteExisting)
string stripQuotes(const std::string_view input)