Pyrogenesis trunk
Filesystem.h
Go to the documentation of this file.
1/* Copyright (C) 2021 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"
24#include "ps/CStrForward.h"
25#include "ps/Errors.h"
26
27extern PIVFS g_VFS;
28
29extern bool VfsFileExists(const VfsPath& pathname);
30
31extern bool VfsDirectoryExists(const VfsPath& pathname);
32
33/**
34 * callback function type for file change notifications
35 */
36typedef Status (*FileReloadFunc)(void* param, const VfsPath& path);
37
38/**
39 * register a callback function to be called by ReloadChangedFiles
40 */
41void 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 */
47void 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 **/
56
57/**
58 * Helper function to handle API differences between Boost Filesystem v2 and v3
59 */
60std::wstring GetWstringFromWpath(const fs::wpath& path);
61
63ERROR_TYPE(CVFSFile, LoadFailed);
64ERROR_TYPE(CVFSFile, AlreadyLoaded);
65
66/**
67 * Reads a file, then gives read-only access to the contents
68 */
70{
71public:
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 * @param log Whether to log a failure to load a file
79 */
80 PSRETURN Load(const PIVFS& vfs, const VfsPath& filename, bool log = true);
81
82 /**
83 * Returns buffer of this file as a stream of bytes
84 * @note file must have been successfully loaded
85 */
86 const u8* GetBuffer() const;
87 size_t GetBufferSize() const;
88
89 /**
90 * Returns contents of file as a string
91 * @note file must have been successfully loaded
92 */
93 CStr8 GetAsString() const;
94
95 /**
96 * Returns contents of a UTF-8 encoded file as a string with optional BOM removed
97 * @note file must have been successfully loaded
98 */
99 CStr8 DecodeUTF8() const;
100
101private:
102 std::shared_ptr<u8> m_Buffer;
104};
105
106#endif // #ifndef INCLUDED_PS_FILESYSTEM
u32 PSRETURN
Definition: Errors.h:75
void RegisterFileReloadFunc(FileReloadFunc func, void *obj)
register a callback function to be called by ReloadChangedFiles
Definition: Filesystem.cpp:45
ERROR_GROUP(CVFSFile)
ERROR_TYPE(CVFSFile, LoadFailed)
bool VfsDirectoryExists(const VfsPath &pathname)
Definition: Filesystem.cpp:39
void UnregisterFileReloadFunc(FileReloadFunc func, void *obj)
delete a callback function registered with RegisterFileReloadFunc (removes any with the same func and...
Definition: Filesystem.cpp:50
PIVFS g_VFS
Definition: Filesystem.cpp:30
bool VfsFileExists(const VfsPath &pathname)
Definition: Filesystem.cpp:34
Status ReloadChangedFiles()
poll for directory change notifications and reload all affected files.
Definition: Filesystem.cpp:76
std::wstring GetWstringFromWpath(const fs::wpath &path)
Helper function to handle API differences between Boost Filesystem v2 and v3.
Definition: Filesystem.cpp:100
Status(* FileReloadFunc)(void *param, const VfsPath &path)
callback function type for file change notifications
Definition: Filesystem.h:36
Reads a file, then gives read-only access to the contents.
Definition: Filesystem.h:70
~CVFSFile()
Definition: Filesystem.cpp:115
CStr8 GetAsString() const
Returns contents of file as a string.
Definition: Filesystem.cpp:151
const u8 * GetBuffer() const
Returns buffer of this file as a stream of bytes.
Definition: Filesystem.cpp:141
std::shared_ptr< u8 > m_Buffer
Definition: Filesystem.h:102
PSRETURN Load(const PIVFS &vfs, const VfsPath &filename, bool log=true)
Returns either PSRETURN_OK or PSRETURN_CVFSFile_LoadFailed.
Definition: Filesystem.cpp:119
CVFSFile()
Definition: Filesystem.cpp:110
CStr8 DecodeUTF8() const
Returns contents of a UTF-8 encoded file as a string with optional BOM removed.
Definition: Filesystem.cpp:156
size_t m_BufferSize
Definition: Filesystem.h:103
size_t GetBufferSize() const
Definition: Filesystem.cpp:146
Definition: path.h:80
path wpath
Definition: pch_boost.h:53
def log(severity, message)
Definition: tests.py:27
Definition: vfs_util.cpp:39
i64 Status
Error handling system.
Definition: status.h:173
uint8_t u8
Definition: types.h:37
std::shared_ptr< IVFS > PIVFS
Definition: vfs.h:220