Pyrogenesis  trunk
Filesystem.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_PS_FILESYSTEM
19 #define INCLUDED_PS_FILESYSTEM
20 
21 #include "lib/file/file.h"
22 #include "lib/file/io/io.h"
24 #include "lib/file/vfs/vfs_util.h"
25 
26 #include "ps/CStr.h"
27 #include "ps/Errors.h"
28 
29 extern PIVFS g_VFS;
30 
31 extern bool VfsFileExists(const VfsPath& pathname);
32 
33 extern bool VfsDirectoryExists(const VfsPath& pathname);
34 
35 /**
36  * callback function type for file change notifications
37  */
38 typedef Status (*FileReloadFunc)(void* param, const VfsPath& path);
39 
40 /**
41  * register a callback function to be called by ReloadChangedFiles
42  */
43 void RegisterFileReloadFunc(FileReloadFunc func, void* obj);
44 
45 /**
46  * delete a callback function registered with RegisterFileReloadFunc
47  * (removes any with the same func and obj)
48  */
49 void UnregisterFileReloadFunc(FileReloadFunc func, void* obj);
50 
51 /**
52  * poll for directory change notifications and reload all affected files.
53  * must be called regularly (e.g. once a frame), else notifications
54  * may be lost.
55  * note: polling is much simpler than asynchronous notifications.
56  **/
57 extern Status ReloadChangedFiles();
58 
59 /**
60  * Helper function to handle API differences between Boost Filesystem v2 and v3
61  */
62 std::wstring GetWstringFromWpath(const fs::wpath& path);
63 
65 ERROR_TYPE(CVFSFile, LoadFailed);
66 ERROR_TYPE(CVFSFile, AlreadyLoaded);
67 
68 /**
69  * Reads a file, then gives read-only access to the contents
70  */
71 class CVFSFile
72 {
73 public:
74  CVFSFile();
75  ~CVFSFile();
76 
77  /**
78  * Returns either PSRETURN_OK or PSRETURN_CVFSFile_LoadFailed
79  * @note Dies if the file has already been successfully loaded
80  * @param log Whether to log a failure to load a file
81  */
82  PSRETURN Load(const PIVFS& vfs, const VfsPath& filename, bool log = true);
83 
84  /**
85  * Returns buffer of this file as a stream of bytes
86  * @note file must have been successfully loaded
87  */
88  const u8* GetBuffer() const;
89  size_t GetBufferSize() const;
90 
91  /**
92  * Returns contents of file as a string
93  * @note file must have been successfully loaded
94  */
95  CStr GetAsString() const;
96 
97  /**
98  * Returns contents of a UTF-8 encoded file as a string with optional BOM removed
99  * @note file must have been successfully loaded
100  */
101  CStr DecodeUTF8() const;
102 
103 private:
104  shared_ptr<u8> m_Buffer;
105  size_t m_BufferSize;
106 };
107 
108 #endif // #ifndef INCLUDED_PS_FILESYSTEM
CStr DecodeUTF8() const
Returns contents of a UTF-8 encoded file as a string with optional BOM removed.
Definition: Filesystem.cpp:157
size_t m_BufferSize
Definition: Filesystem.h:105
void RegisterFileReloadFunc(FileReloadFunc func, void *obj)
register a callback function to be called by ReloadChangedFiles
Definition: Filesystem.cpp:44
Reads a file, then gives read-only access to the contents.
Definition: Filesystem.h:71
void UnregisterFileReloadFunc(FileReloadFunc func, void *obj)
delete a callback function registered with RegisterFileReloadFunc (removes any with the same func and...
Definition: Filesystem.cpp:49
shared_ptr< IVFS > PIVFS
Definition: vfs.h:226
Status ReloadChangedFiles()
poll for directory change notifications and reload all affected files.
Definition: Filesystem.cpp:75
def log(severity, message)
Definition: tests.py:21
uint8_t u8
Definition: types.h:37
CStr GetAsString() const
Returns contents of file as a string.
Definition: Filesystem.cpp:152
PIVFS g_VFS
Definition: Filesystem.cpp:29
Status(* FileReloadFunc)(void *param, const VfsPath &path)
callback function type for file change notifications
Definition: Filesystem.h:38
bool VfsFileExists(const VfsPath &pathname)
Definition: Filesystem.cpp:33
u32 PSRETURN
Definition: Errors.h:75
Definition: path.h:77
CVFSFile()
Definition: Filesystem.cpp:111
const u8 * GetBuffer() const
Returns buffer of this file as a stream of bytes.
Definition: Filesystem.cpp:142
i64 Status
Error handling system.
Definition: status.h:171
ERROR_GROUP(CVFSFile)
~CVFSFile()
Definition: Filesystem.cpp:116
bool VfsDirectoryExists(const VfsPath &pathname)
Definition: Filesystem.cpp:38
shared_ptr< u8 > m_Buffer
Definition: Filesystem.h:104
size_t GetBufferSize() const
Definition: Filesystem.cpp:147
Definition: vfs_util.cpp:39
std::wstring GetWstringFromWpath(const fs::wpath &path)
Helper function to handle API differences between Boost Filesystem v2 and v3.
Definition: Filesystem.cpp:101
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename, bool log=true)
Returns either PSRETURN_OK or PSRETURN_CVFSFile_LoadFailed.
Definition: Filesystem.cpp:120
ERROR_TYPE(CVFSFile, LoadFailed)