-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvfsfileio.hpp
64 lines (58 loc) · 1.76 KB
/
vfsfileio.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include <list>
#include <mutex>
#include "engine/exception.hpp"
#include "engine/file.hpp"
#include "filepath.hpp"
extern "C" {
#include "vfs/vfsreader.h"
}
class VFSFileIO : public FileIO {
protected:
vfs_handle_p mFile;
FilePath mWriteDir;
public:
explicit VFSFileIO(const std::string& path) : mFile(NULL), mWriteDir("") {
mFile = vfs_open(path.c_str());
if (mFile == NULL) {
throw Interpreter::RuntimeException("vfs_open failed:" + path);
}
mWriteDir = FilePath(path).dir();
}
~VFSFileIO() {
if (mFile) {
vfs_close(mFile);
mFile = NULL;
}
}
void EnumFile(std::list<std::string>& files) {
int count = 0;
const vfs_dir_entry_p* entry_list = vfs_get_all_files(mFile, &count);
for (int i = 0; i < count; i++) {
char path[512] = {0};
int path_size = 512;
vfs_get_file_full_path(mFile, entry_list[i], path, &path_size);
path[path_size] = 0;
files.push_back(path);
}
}
size_t Write(const std::string& name, const void* content, size_t contentSize) {
StdFileIO w(mWriteDir);
return w.Write(name, content, contentSize);
}
void* Read(const std::string& name, size_t& contentSize) {
int error = 0;
vfs_dir_entry_p pEntry = vfs_lookup(mFile, name.c_str());
if (pEntry == NULL) {
NVT_LOG_ERROR("file not found:", name);
return NULL;
}
void* pContext = vfs_get_file_all_content(mFile, pEntry, &error);
if (error != 0) {
NVT_LOG_ERROR("vfs error ", error);
return NULL;
}
contentSize = vfs_file_size(pEntry);
return pContext;
}
};