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(void);
162 
164  static AString GetExecutableExt(void);
165 
166  // tolua_end
167 
169  static AStringVector GetFolderContents(const AString & a_Folder); // Exported in ManualBindings.cpp
170 
171  int vPrintf(const char * a_Format, fmt::printf_args a_ArgList);
172  template <typename... Args>
173  int Printf(const char * a_Format, const Args & ... a_Args)
174  {
175  return vPrintf(a_Format, fmt::make_printf_args(a_Args...));
176  }
177 
179  void Flush(void);
180 
181 private:
182  FILE * m_File;
183 } ; // tolua_export
184 
185 
186 
187 
188 
190 template <class StreamType>
191 class FileStream final : public StreamType
192 {
193 public:
194 
195  FileStream(const std::string & Path);
196  FileStream(const std::string & Path, const typename FileStream::openmode Mode);
197 };
198 
199 
200 
201 
202 
205 
206 #ifdef __clang__
207 #pragma clang diagnostic push
208 #pragma clang diagnostic ignored "-Wweak-template-vtables" // http://bugs.llvm.org/show_bug.cgi?id=18733
209 #endif
210 
211 extern template class FileStream<std::ifstream>;
212 extern template class FileStream<std::ofstream>;
213 
214 #ifdef __clang__
215 #pragma clang diagnostic pop
216 #endif
FileStream::FileStream
FileStream(const std::string &Path)
Definition: File.cpp:716
cFile::IsFile
static bool IsFile(const AString &a_Path)
Returns true if the specified path is a regular file.
Definition: File.cpp:425
cFile::fmWrite
@ fmWrite
Definition: File.h:55
cFile::IsFolder
static bool IsFolder(const AString &a_Path)
Returns true if the specified path is a folder.
Definition: File.cpp:410
cFile::Open
bool Open(const AString &iFileName, eMode iMode)
Definition: File.cpp:52
cFile::vPrintf
int vPrintf(const char *a_Format, fmt::printf_args a_ArgList)
Definition: File.cpp:695
cFile::fmRead
@ fmRead
Definition: File.h:54
cFile::GetFolderContents
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::cFile
cFile(void)
Simple constructor - creates an unopened file object, use Open() to open / create a real file.
Definition: File.cpp:20
cFile::GetExecutableExt
static AString GetExecutableExt(void)
Returns the customary executable extension used by the current platform.
Definition: File.cpp:682
cFile::Delete
static bool Delete(const AString &a_Path)
Deletes a file or a folder, returns true if successful.
Definition: File.cpp:304
cFile::Rename
static bool Rename(const AString &a_OrigPath, const AString &a_NewPath)
Renames a file or folder, returns true if successful.
Definition: File.cpp:377
cFile::Close
void Close(void)
Definition: File.cpp:102
cFile::IsEOF
bool IsEOF(void) const
Definition: File.cpp:127
cFile::CreateFolder
static bool CreateFolder(const AString &a_FolderPath)
Creates a new folder with the specified name.
Definition: File.cpp:454
cFile::DeleteFolder
static bool DeleteFolder(const AString &a_FolderName)
Deletes a folder, returns true if successful.
Definition: File.cpp:320
cFile::fmAppend
@ fmAppend
Definition: File.h:57
cFile::Printf
int Printf(const char *a_Format, const Args &... a_Args)
Definition: File.h:173
cFile::Read
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
cFile::Seek
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
cFile
Definition: File.h:37
cFile::GetSize
long GetSize(void) const
Returns the size of file, in bytes, or -1 for failure; asserts if not open.
Definition: File.cpp:233
Block::Comparator::Mode
Mode
Definition: BlockStates.h:4496
cFile::ReadRestOfFile
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
cFile::Write
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
StringUtils.h
cFile::eMode
eMode
The mode in which to open the file.
Definition: File.h:52
cFile::Tell
long Tell(void) const
Returns the current position (bytes from file start) or -1 for failure; asserts if not open.
Definition: File.cpp:217
cFile::PathSeparator
static char PathSeparator()
Definition: File.h:42
cFile::Flush
void Flush(void)
Flushes all the bufferef output into the file (only when writing)
Definition: File.cpp:706
cFile::~cFile
~cFile()
Auto-closes the file, if open.
Definition: File.cpp:40
cFile::fmReadWrite
@ fmReadWrite
Definition: File.h:56
FileStream
A wrapper for file streams that enables exceptions.
Definition: File.h:191
cFile::ChangeFileExt
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
cFile::GetLastModificationTime
static unsigned GetLastModificationTime(const AString &a_FileName)
Returns the last modification time (in current timezone) of the specified file.
Definition: File.cpp:645
cFile::ReadWholeFile
static AString ReadWholeFile(const AString &a_FileName)
Returns the entire contents of the specified file as a string.
Definition: File.cpp:573
cFile::CreateFolderRecursive
static bool CreateFolderRecursive(const AString &a_FolderPath)
Creates a new folder with the specified name, creating its parents if needed.
Definition: File.cpp:467
cFile::DeleteFolderContents
static bool DeleteFolderContents(const AString &a_FolderName)
Deletes all content from the specified folder.
Definition: File.cpp:333
cFile::Copy
static bool Copy(const AString &a_SrcFileName, const AString &a_DstFileName)
Copies a file, returns true if successful.
Definition: File.cpp:386
cFile::IsOpen
bool IsOpen(void) const
Definition: File.cpp:118
cFile::Write
int Write(std::string_view a_String)
Definition: File.h:83
cFile::Exists
static bool Exists(const AString &a_FileName)
Returns true if the file specified exists.
Definition: File.cpp:294
AString
std::string AString
Definition: StringUtils.h:11
cFile::GetPathSeparator
static AString GetPathSeparator(void)
Returns the path separator used by the current platform.
Definition: File.cpp:669
cFile::m_File
FILE * m_File
Definition: File.h:182
cFile::DeleteFile
static bool DeleteFile(const AString &a_FileName)
Deletes a file, returns true if successful.
Definition: File.cpp:368
AStringVector
std::vector< AString > AStringVector
Definition: StringUtils.h:12