Cuberite
A lightweight, fast and extensible game server for Minecraft
File.h
Go to the documentation of this file.
1 
2 // cFile.h
3 
4 // Interfaces to the cFile class providing an OS-independent abstraction of a file.
5 
6 /*
7 The object is optimized towards binary reads.
8 The object has no multithreading locks, don't use from multiple threads!
9 Usage:
10 1, Construct a cFile instance (no-param constructor)
11 2, Open a file using Open(), check return value for success
12 3, Read / write
13 4, Destroy the instance
14 
15 -- OR --
16 
17 1, Construct a cFile instance opening the file (filename-param constructor)
18 2, Check if the file was opened using IsOpen()
19 3, Read / write
20 4, Destroy the instance
21 
22 For reading entire files into memory, just use the static cFile::ReadWholeFile()
23 */
24 
25 
26 
27 
28 
29 #pragma once
30 
31 #include "StringUtils.h"
32 
33 
34 
35 // tolua_begin
36 
37 class cFile
38 {
39 public:
40 
41  // tolua_end
42  inline static char PathSeparator()
43  {
44  #ifdef _WIN32
45  return '\\';
46  #else
47  return '/';
48  #endif
49  }
50 
52  enum eMode
53  {
54  fmRead, // Read-only. If the file doesn't exist, object will not be valid
55  fmWrite, // Write-only. If the file already exists, it will be overwritten
56  fmReadWrite, // Read / write. If the file already exists, it will be left intact; writing will overwrite the data from the beginning
57  fmAppend // Write-only. If the file already exists cursor will be moved to the end of the file
58  } ;
59 
61  cFile(void);
62 
64  cFile(const AString & iFileName, eMode iMode);
65 
67  ~cFile();
68 
69  bool Open(const AString & iFileName, eMode iMode);
70  void Close(void);
71  bool IsOpen(void) const;
72  bool IsEOF(void) const;
73 
75  int Read(void * a_Buffer, size_t a_NumBytes);
76 
78  std::basic_string<std::byte> Read(size_t a_NumBytes);
79 
81  int Write(const void * a_Buffer, size_t a_NumBytes);
82 
83  int Write(std::string_view a_String)
84  {
85  return Write(a_String.data(), a_String.size());
86  }
87 
89  long Seek (int iPosition);
90 
92  long Tell (void) const;
93 
95  long GetSize(void) const;
96 
98  int ReadRestOfFile(AString & a_Contents);
99 
101  static bool Exists(const AString & a_FileName); // Exported in ManualBindings.cpp
102 
105  static bool Delete(const AString & a_Path); // Exported in ManualBindings.cpp
106 
109  static bool DeleteFile(const AString & a_FileName); // Exported in ManualBindings.cpp
110 
113  static bool DeleteFolder(const AString & a_FolderName); // Exported in ManualBindings.cpp
114 
118  static bool DeleteFolderContents(const AString & a_FolderName); // Exported in ManualBindings.cpp
119 
121  static bool Rename(const AString & a_OrigPath, const AString & a_NewPath); // Exported in ManualBindings.cpp
122 
125  static bool Copy(const AString & a_SrcFileName, const AString & a_DstFileName); // Exported in ManualBindings.cpp
126 
128  static bool IsFolder(const AString & a_Path); // Exported in ManualBindings.cpp
129 
131  static bool IsFile(const AString & a_Path); // Exported in ManualBindings.cpp
132 
134  static long GetSize(const AString & a_FileName); // Exported in ManualBindings.cpp
135 
137  static bool CreateFolder(const AString & a_FolderPath); // Exported in ManualBindings.cpp
138 
142  static bool CreateFolderRecursive(const AString & a_FolderPath); // Exported in ManualBindings.cpp
143 
145  static AString ReadWholeFile(const AString & a_FileName); // Exported in ManualBindings.cpp
146 
149  static AString ChangeFileExt(const AString & a_FileName, const AString & a_NewExt); // Exported in ManualBindings.cpp
150 
155  static unsigned GetLastModificationTime(const AString & a_FileName); // Exported in ManualBindings.cpp
156 
157  // tolua_begin
158 
161  static AString GetPathSeparator();
162 
164  static AString GetExecutableExt();
165 
166  // tolua_end
167 
169  static AStringVector GetFolderContents(const AString & a_Folder); // Exported in ManualBindings.cpp
170 
172  void Flush();
173 
174 private:
175  FILE * m_File;
176 } ; // tolua_export
177 
178 
179 
180 
181 
183 template <class StreamType>
184 class FileStream final : public StreamType
185 {
186 public:
187 
188  FileStream(const std::string & Path);
189  FileStream(const std::string & Path, const typename FileStream::openmode Mode);
190 };
191 
192 
193 
194 
195 
198 
199 #ifdef __clang__
200 #pragma clang diagnostic push
201 #pragma clang diagnostic ignored "-Wweak-template-vtables" // http://bugs.llvm.org/show_bug.cgi?id=18733
202 #endif
203 
204 extern template class FileStream<std::ifstream>;
205 extern template class FileStream<std::ofstream>;
206 
207 #ifdef __clang__
208 #pragma clang diagnostic pop
209 #endif
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
Definition: File.h:38
static bool CreateFolderRecursive(const AString &a_FolderPath)
Creates a new folder with the specified name, creating its parents if needed.
Definition: File.cpp:467
static AString GetExecutableExt()
Returns the customary executable extension used by the current platform.
Definition: File.cpp:682
static bool IsFolder(const AString &a_Path)
Returns true if the specified path is a folder.
Definition: File.cpp:410
int Write(std::string_view a_String)
Definition: File.h:83
int Read(void *a_Buffer, size_t a_NumBytes)
Reads up to a_NumBytes bytes into a_Buffer, returns the number of bytes actually read,...
Definition: File.cpp:144
static unsigned GetLastModificationTime(const AString &a_FileName)
Returns the last modification time (in current timezone) of the specified file.
Definition: File.cpp:645
long Seek(int iPosition)
Seeks to iPosition bytes from file start, returns old position or -1 for failure; asserts if not open...
Definition: File.cpp:197
static bool DeleteFile(const AString &a_FileName)
Deletes a file, returns true if successful.
Definition: File.cpp:368
static AString ChangeFileExt(const AString &a_FileName, const AString &a_NewExt)
Returns a_FileName with its extension changed to a_NewExt.
Definition: File.cpp:589
static bool Delete(const AString &a_Path)
Deletes a file or a folder, returns true if successful.
Definition: File.cpp:304
void Flush()
Flushes all the bufferef output into the file (only when writing)
Definition: File.cpp:695
static bool CreateFolder(const AString &a_FolderPath)
Creates a new folder with the specified name.
Definition: File.cpp:454
static bool Copy(const AString &a_SrcFileName, const AString &a_DstFileName)
Copies a file, returns true if successful.
Definition: File.cpp:386
static AString GetPathSeparator()
Returns the path separator used by the current platform.
Definition: File.cpp:669
static char PathSeparator()
Definition: File.h:42
static bool Rename(const AString &a_OrigPath, const AString &a_NewPath)
Renames a file or folder, returns true if successful.
Definition: File.cpp:377
eMode
The mode in which to open the file.
Definition: File.h:53
@ fmRead
Definition: File.h:54
@ fmWrite
Definition: File.h:55
@ fmAppend
Definition: File.h:57
@ fmReadWrite
Definition: File.h:56
cFile(void)
Simple constructor - creates an unopened file object, use Open() to open / create a real file.
Definition: File.cpp:20
static bool Exists(const AString &a_FileName)
Returns true if the file specified exists.
Definition: File.cpp:294
bool Open(const AString &iFileName, eMode iMode)
Definition: File.cpp:52
static AString ReadWholeFile(const AString &a_FileName)
Returns the entire contents of the specified file as a string.
Definition: File.cpp:573
static AStringVector GetFolderContents(const AString &a_Folder)
Returns the list of all items in the specified folder (files, folders, nix pipes, whatever's there).
Definition: File.cpp:498
~cFile()
Auto-closes the file, if open.
Definition: File.cpp:40
FILE * m_File
Definition: File.h:175
long GetSize(void) const
Returns the size of file, in bytes, or -1 for failure; asserts if not open.
Definition: File.cpp:233
static bool DeleteFolderContents(const AString &a_FolderName)
Deletes all content from the specified folder.
Definition: File.cpp:333
int ReadRestOfFile(AString &a_Contents)
Reads the file from current position till EOF into an AString; returns the number of bytes read or -1...
Definition: File.cpp:263
long Tell(void) const
Returns the current position (bytes from file start) or -1 for failure; asserts if not open.
Definition: File.cpp:217
bool IsOpen(void) const
Definition: File.cpp:118
int Write(const void *a_Buffer, size_t a_NumBytes)
Writes up to a_NumBytes bytes from a_Buffer, returns the number of bytes actually written,...
Definition: File.cpp:180
bool IsEOF(void) const
Definition: File.cpp:127
static bool IsFile(const AString &a_Path)
Returns true if the specified path is a regular file.
Definition: File.cpp:425
void Close(void)
Definition: File.cpp:102
static bool DeleteFolder(const AString &a_FolderName)
Deletes a folder, returns true if successful.
Definition: File.cpp:320
A wrapper for file streams that enables exceptions.
Definition: File.h:185
FileStream(const std::string &Path)
Definition: File.cpp:705