Line |
Branch |
Exec |
Source |
1 |
|
|
#pragma once |
2 |
|
|
#include "autoarray.h" |
3 |
|
|
#include "assocarr.h" |
4 |
|
|
#include "libstr.h" |
5 |
|
|
|
6 |
|
|
// RPG Hacker: A virtual file system which can work with physical files |
7 |
|
|
// as well as in-memory files. |
8 |
|
|
|
9 |
|
|
typedef void* virtual_file_handle; |
10 |
|
|
static const virtual_file_handle INVALID_VIRTUAL_FILE_HANDLE = nullptr; |
11 |
|
|
|
12 |
|
|
enum virtual_file_error |
13 |
|
|
{ |
14 |
|
|
vfe_none, |
15 |
|
|
|
16 |
|
|
vfe_doesnt_exist, |
17 |
|
|
vfe_access_denied, |
18 |
|
|
vfe_not_regular_file, |
19 |
|
|
vfe_unknown, |
20 |
|
|
|
21 |
|
|
vfe_num_errors |
22 |
|
|
}; |
23 |
|
|
|
24 |
|
|
struct memory_buffer |
25 |
|
|
{ |
26 |
|
|
const void* data; |
27 |
|
|
size_t length; |
28 |
|
|
}; |
29 |
|
|
|
30 |
|
|
class virtual_filesystem |
31 |
|
|
{ |
32 |
|
|
public: |
33 |
|
|
void initialize(const char** include_paths, size_t num_include_paths); |
34 |
|
|
void destroy(); |
35 |
|
|
|
36 |
|
|
virtual_file_handle open_file(const char* path, const char* base_path); |
37 |
|
|
void close_file(virtual_file_handle file_handle); |
38 |
|
|
|
39 |
|
|
size_t read_file(virtual_file_handle file_handle, void* out_buffer, size_t pos, size_t num_bytes); |
40 |
|
|
|
41 |
|
|
size_t get_file_size(virtual_file_handle file_handle); |
42 |
|
|
|
43 |
|
|
bool is_path_absolute(const char* path); |
44 |
|
|
|
45 |
|
|
string create_absolute_path(const char* base, const char* target); |
46 |
|
|
|
47 |
|
|
void add_memory_file(const char* name, const void* buffer, size_t length); |
48 |
|
|
|
49 |
|
24 |
inline virtual_file_error get_last_error() |
50 |
|
|
{ |
51 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
24 |
return m_last_error; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
private: |
55 |
|
|
enum virtual_file_type |
56 |
|
|
{ |
57 |
|
|
vft_physical_file, |
58 |
|
|
vft_memory_file |
59 |
|
|
}; |
60 |
|
|
|
61 |
|
|
virtual_file_type get_file_type_from_path(const char* path); |
62 |
|
|
|
63 |
|
|
assocarr<memory_buffer> m_memory_files; |
64 |
|
|
autoarray<string> m_include_paths; |
65 |
|
|
virtual_file_error m_last_error; |
66 |
|
|
}; |
67 |
|
|
|
68 |
|
|
|