Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Filesystem.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 /**
34  * callback function type for file change notifications
35  */
36 typedef Status (*FileReloadFunc)(void* param, const VfsPath& path);
37 
38 /**
39  * register a callback function to be called by ReloadChangedFiles
40  */
41 void RegisterFileReloadFunc(FileReloadFunc func, void* obj);
42 
43 /**
44  * delete a callback function registered with RegisterFileReloadFunc
45  * (removes any with the same func and obj)
46  */
47 void UnregisterFileReloadFunc(FileReloadFunc func, void* obj);
48 
49 /**
50  * poll for directory change notifications and reload all affected files.
51  * must be called regularly (e.g. once a frame), else notifications
52  * may be lost.
53  * note: polling is much simpler than asynchronous notifications.
54  **/
55 extern Status ReloadChangedFiles();
56 
57 /**
58  * Helper function to handle API differences between Boost Filesystem v2 and v3
59  */
60 std::wstring GetWstringFromWpath(const fs::wpath& path);
61 
63 ERROR_TYPE(CVFSFile, LoadFailed);
64 ERROR_TYPE(CVFSFile, AlreadyLoaded);
65 
66 /**
67  * Reads a file, then gives read-only access to the contents
68  */
69 class CVFSFile
70 {
71 public:
72  CVFSFile();
73  ~CVFSFile();
74 
75  /**
76  * Returns either PSRETURN_OK or PSRETURN_CVFSFile_LoadFailed
77  * @note Dies if the file has already been successfully loaded
78  */
79  PSRETURN Load(const PIVFS& vfs, const VfsPath& filename);
80 
81  /**
82  * Returns buffer of this file as a stream of bytes
83  * @note file must have been successfully loaded
84  */
85  const u8* GetBuffer() const;
86  size_t GetBufferSize() const;
87 
88  /**
89  * Returns contents of file as a string
90  * @note file must have been successfully loaded
91  */
92  CStr GetAsString() const;
93 
94  /**
95  * Returns contents of a UTF-8 encoded file as a string with optional BOM removed
96  * @note file must have been successfully loaded
97  */
98  CStr DecodeUTF8() const;
99 
100 private:
101  shared_ptr<u8> m_Buffer;
102  size_t m_BufferSize;
103 };
104 
105 #endif // #ifndef INCLUDED_PS_FILESYSTEM
#define u8
Definition: types.h:39
CStr DecodeUTF8() const
Returns contents of a UTF-8 encoded file as a string with optional BOM removed.
Definition: Filesystem.cpp:153
size_t m_BufferSize
Definition: Filesystem.h:102
Reads a file, then gives read-only access to the contents.
Definition: Filesystem.h:69
shared_ptr< IVFS > PIVFS
Definition: vfs.h:226
std::wstring GetWstringFromWpath(const fs::wpath &path)
Helper function to handle API differences between Boost Filesystem v2 and v3.
Definition: Filesystem.cpp:98
#define ERROR_TYPE(a, b)
Definition: Errors.h:96
#define ERROR_GROUP(a)
Definition: Errors.h:87
CStr GetAsString() const
Returns contents of file as a string.
Definition: Filesystem.cpp:148
Status(* FileReloadFunc)(void *param, const VfsPath &path)
callback function type for file change notifications
Definition: Filesystem.h:36
Status ReloadChangedFiles()
poll for directory change notifications and reload all affected files.
Definition: Filesystem.cpp:70
void UnregisterFileReloadFunc(FileReloadFunc func, void *obj)
delete a callback function registered with RegisterFileReloadFunc (removes any with the same func and...
Definition: Filesystem.cpp:44
u32 PSRETURN
Definition: Errors.h:75
void RegisterFileReloadFunc(FileReloadFunc func, void *obj)
register a callback function to be called by ReloadChangedFiles
Definition: Filesystem.cpp:39
Definition: path.h:75
const u8 * GetBuffer() const
Returns buffer of this file as a stream of bytes.
Definition: Filesystem.cpp:138
i64 Status
Error handling system.
Definition: status.h:171
shared_ptr< u8 > m_Buffer
Definition: Filesystem.h:101
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename)
Returns either PSRETURN_OK or PSRETURN_CVFSFile_LoadFailed.
Definition: Filesystem.cpp:117
bool VfsFileExists(const VfsPath &pathname)
Definition: Filesystem.cpp:34
size_t GetBufferSize() const
Definition: Filesystem.cpp:143
PIVFS g_VFS
Definition: Filesystem.cpp:30