MxEngine
File.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include <filesystem>
32 #include <fstream>
33 
34 #include "Utilities/STL/MxString.h"
35 
36 namespace MxEngine
37 {
38  using FilePath = std::filesystem::path;
39  using FileStream = std::fstream;
40  using FileSystemTime = std::filesystem::file_time_type;
41 
47  class File
48  {
52  FileStream fileStream;
56  FilePath filePath;
57  public:
61  enum FileMode
62  {
63  READ = 0x1, // open for reading
64  WRITE = 0x2, // open for writing
65  BINARY = 0x4, // open as binary
66  };
67  using FileData = MxString;
68 
72  File() = default;
78  File(const FilePath& path, int mode = FileMode::READ);
84  File(const MxString& path, int mode = FileMode::READ);
90  File(const char* path, int mode = FileMode::READ);
91  File(const File&) = default;
92  File(File&&) = default;
93  File& operator=(const File&) = default;
94  File& operator=(File&&) = default;
95  ~File() = default;
96 
101  FileStream& GetStream();
105  bool IsOpen() const;
111  void Open(FilePath path, int mode = FileMode::READ);
117  void Open(const MxString& path, int mode = FileMode::READ);
123  void Open(const char* path, int mode = FileMode::READ);
124  /*
125  closes currently opened std::fstream object (file)
126  */
127  void Close();
132  FileData ReadAllText();
137  const FilePath& GetPath() const;
138 
143  template<typename T>
144  File& operator>>(T&& value);
149  template<typename T>
150  File& operator<<(T&& value);
151 
157  static FileData ReadAllText(const FilePath& path);
163  static FileData ReadAllText(const MxString& path);
169  static FileData ReadAllText(const char* path);
175  static bool Exists(const FilePath& path);
181  static bool Exists(const MxString& path);
187  static bool Exists(const char* path);
193  static bool IsFile(const MxString& path);
199  static bool IsFile(const FilePath& path);
205  static bool IsFile(const char* path);
211  static bool IsDirectory(const MxString& path);
217  static bool IsDirectory(const FilePath& path);
223  static bool IsDirectory(const char* path);
229  static FileSystemTime LastModifiedTime(const FilePath& path);
235  static FileSystemTime LastModifiedTime(const MxString& path);
241  static FileSystemTime LastModifiedTime(const char* path);
242  /*
243  creates directory is it is not exist
244  \param path path to directory
245  */
246  static void CreateDirectory(const FilePath& path);
247  /*
248  creates directory is it is not exist
249  \param path path to directory
250  */
251  static void CreateDirectory(const MxString& path);
252  /*
253  creates directory is it is not exist
254  \param path path to directory
255  */
256  static void CreateDirectory(const char* path);
257  /*
258  writes bytes to a file. File must be opened with binary mode
259  \param bytes pointer to the first byte to write
260  \param size how many bytes to write
261  */
262  void WriteBytes(const uint8_t* bytes, size_t size);
263  };
264 
265  template<typename T>
266  inline File& File::operator>>(T&& value)
267  {
268  this->fileStream >> std::forward<T>(value);
269  return *this;
270  }
271 
272  template<typename T>
273  inline File& File::operator<<(T&& value)
274  {
275  this->fileStream << std::forward<T>(value);
276  return *this;
277  }
278 
279  inline auto ToMxString(const FilePath& path)
280  {
281  return ToMxString(path.string());
282  }
283 
284  inline auto ToFilePath(const MxString& str)
285  {
286  return FilePath(str.c_str());
287  }
288 }
FileStream & GetStream()
Definition: File.cpp:62
FileData ReadAllText()
Definition: File.cpp:102
const FilePath & GetPath() const
Definition: File.cpp:113
bool IsOpen() const
Definition: File.cpp:67
File & operator<<(T &&value)
Definition: File.h:273
File()=default
static bool IsFile(const MxString &path)
Definition: File.cpp:148
void Open(FilePath path, int mode=FileMode::READ)
Definition: File.cpp:72
static bool Exists(const FilePath &path)
Definition: File.cpp:133
static bool IsDirectory(const MxString &path)
Definition: File.cpp:163
FileMode
Definition: File.h:61
File & operator>>(T &&value)
Definition: File.h:266
Definition: File.h:47
Definition: Application.cpp:49
static FileSystemTime LastModifiedTime(const FilePath &path)
Definition: File.cpp:178