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 
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  AString Read(size_t a_NumBytes);
79 
81  int Write(const void * a_Buffer, size_t a_NumBytes);
82 
84  long Seek (int iPosition);
85 
87  long Tell (void) const;
88 
90  long GetSize(void) const;
91 
93  int ReadRestOfFile(AString & a_Contents);
94 
96  static bool Exists(const AString & a_FileName); // Exported in ManualBindings.cpp
97 
100  static bool Delete(const AString & a_Path); // Exported in ManualBindings.cpp
101 
104  static bool DeleteFile(const AString & a_FileName); // Exported in ManualBindings.cpp
105 
108  static bool DeleteFolder(const AString & a_FolderName); // Exported in ManualBindings.cpp
109 
113  static bool DeleteFolderContents(const AString & a_FolderName); // Exported in ManualBindings.cpp
114 
116  static bool Rename(const AString & a_OrigPath, const AString & a_NewPath); // Exported in ManualBindings.cpp
117 
120  static bool Copy(const AString & a_SrcFileName, const AString & a_DstFileName); // Exported in ManualBindings.cpp
121 
123  static bool IsFolder(const AString & a_Path); // Exported in ManualBindings.cpp
124 
126  static bool IsFile(const AString & a_Path); // Exported in ManualBindings.cpp
127 
129  static long GetSize(const AString & a_FileName); // Exported in ManualBindings.cpp
130 
132  static bool CreateFolder(const AString & a_FolderPath); // Exported in ManualBindings.cpp
133 
137  static bool CreateFolderRecursive(const AString & a_FolderPath); // Exported in ManualBindings.cpp
138 
140  static AString ReadWholeFile(const AString & a_FileName); // Exported in ManualBindings.cpp
141 
144  static AString ChangeFileExt(const AString & a_FileName, const AString & a_NewExt); // Exported in ManualBindings.cpp
145 
150  static unsigned GetLastModificationTime(const AString & a_FileName); // Exported in ManualBindings.cpp
151 
152  // tolua_begin
153 
156  static AString GetPathSeparator(void);
157 
159  static AString GetExecutableExt(void);
160 
161  // tolua_end
162 
164  static AStringVector GetFolderContents(const AString & a_Folder); // Exported in ManualBindings.cpp
165 
166  int Printf(const char * a_Fmt, fmt::ArgList);
167  FMT_VARIADIC(int, Printf, const char *)
168 
169 
170  void Flush(void);
171 
172 private:
173  FILE * m_File;
174 } ; // tolua_export
175 
176 
177 
178 
static bool Exists(const AString &a_FileName)
Returns true if the file specified exists.
Definition: File.cpp:294
static bool DeleteFolderContents(const AString &a_FolderName)
Deletes all content from the specified folder.
Definition: File.cpp:333
static AString GetPathSeparator(void)
Returns the path separator used by the current platform.
Definition: File.cpp:669
static bool Delete(const AString &a_Path)
Deletes a file or a folder, returns true if successful.
Definition: File.cpp:304
static bool Rename(const AString &a_OrigPath, const AString &a_NewPath)
Renames a file or folder, returns true if successful.
Definition: File.cpp:377
long Tell(void) const
Returns the current position (bytes from file start) or -1 for failure; asserts if not open...
Definition: File.cpp:216
static bool Copy(const AString &a_SrcFileName, const AString &a_DstFileName)
Copies a file, returns true if successful.
Definition: File.cpp:386
static AString GetExecutableExt(void)
Returns the customary executable extension used by the current platform.
Definition: File.cpp:682
long GetSize(void) const
Returns the size of file, in bytes, or -1 for failure; asserts if not open.
Definition: File.cpp:232
static bool CreateFolder(const AString &a_FolderPath)
Creates a new folder with the specified name.
Definition: File.cpp:454
static AString ReadWholeFile(const AString &a_FileName)
Returns the entire contents of the specified file as a string.
Definition: File.cpp:573
int Printf(const char *a_Fmt, fmt::ArgList)
Definition: File.cpp:695
static char PathSeparator()
Definition: File.h:42
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:262
bool IsOpen(void) const
Definition: File.cpp:116
std::vector< AString > AStringVector
Definition: StringUtils.h:14
static bool IsFile(const AString &a_Path)
Returns true if the specified path is a regular file.
Definition: File.cpp:425
Definition: File.h:37
bool Open(const AString &iFileName, eMode iMode)
Definition: File.cpp:50
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:196
static bool DeleteFolder(const AString &a_FolderName)
Deletes a folder, returns true if successful.
Definition: File.cpp:320
static bool IsFolder(const AString &a_Path)
Returns true if the specified path is a folder.
Definition: File.cpp:410
static bool CreateFolderRecursive(const AString &a_FolderPath)
Creates a new folder with the specified name, creating its parents if needed.
Definition: File.cpp:467
std::string AString
Definition: StringUtils.h:13
FILE * m_File
Definition: File.h:173
static AStringVector GetFolderContents(const AString &a_Folder)
Returns the list of all items in the specified folder (files, folders, nix pipes, whatever&#39;s there)...
Definition: File.cpp:498
void Flush(void)
Flushes all the bufferef output into the file (only when writing)
Definition: File.cpp:705
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:179
eMode
The mode in which to open the file.
Definition: File.h:52
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, or -1 on failure; asserts if not open.
Definition: File.cpp:142
cFile(void)
Simple constructor - creates an unopened file object, use Open() to open / create a real file...
Definition: File.cpp:18
void Close(void)
Definition: File.cpp:100
static bool DeleteFile(const AString &a_FileName)
Deletes a file, returns true if successful.
Definition: File.cpp:368
bool IsEOF(void) const
Definition: File.cpp:125
static unsigned GetLastModificationTime(const AString &a_FileName)
Returns the last modification time (in current timezone) of the specified file.
Definition: File.cpp:645
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()
Auto-closes the file, if open.
Definition: File.cpp:38