-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileSystem-wasm.cpp
140 lines (115 loc) · 6.07 KB
/
FileSystem-wasm.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "FileSystem.h"
// Helper functions
int IndexDBFileSystem_strlen(const char* str) {
if (str == 0) {
return 0;
}
const char *s = str;
while (*s) {
++s;
}
return (s - str);
}
// Wasm imports
extern "C" void IndexDBFileSystem_wasmLog(const char* msg, int msgLen);
extern "C" void IndexDBFileSystem_wasmWrite(const char* str_ptr, int str_len, const void* blob_ptr, int blob_size, FileSystem::fpWriteCallback success_callback, FileSystem::fpErrorCallback error_callback);
extern "C" void IndexDBFileSystem_wasmRead(const char* str_ptr, int str_len, const void* target_ptr, int target_size, FileSystem::fpReadCallback success_callback, FileSystem::fpErrorCallback error_callback);
extern "C" void IndexDBFileSystem_wasmCreateFile(const char* name_ptr, int name_len, FileSystem::fpPathCallback success_callback, FileSystem::fpErrorCallback error_callback);
extern "C" void IndexDBFileSystem_wasmCreateFolder(const char* name_ptr, int name_len, FileSystem::fpPathCallback success_callback, FileSystem::fpErrorCallback error_callback);
extern "C" void IndexDBFileSystem_wasmDelete(const char* name_ptr, int name_len, FileSystem::fpPathCallback success_callback, FileSystem::fpErrorCallback error_callback);
extern "C" void IndexDBFileSystem_wasmExists(const char* name_ptr, int name_len, FileSystem::fpExistsCallback success_callback);
extern "C" void IndexDBFileSystem_wasmPreDepthFirstTraversal(const char* name_ptr, int name_len, FileSystem::fpDepthFirstIterateCallback iterate_callback, FileSystem::fpDepthFirstFinishedCallback done_callback);
extern "C" void IndexDBFileSystem_wasmPostDepthFirstTraversal(const char* name_ptr, int name_len, FileSystem::fpDepthFirstIterateCallback iterate_callback, FileSystem::fpDepthFirstFinishedCallback done_callback);
extern unsigned char __heap_base;
// Wasm exports
#define export __attribute__ (( visibility( "default" ) )) extern "C"
export void IndexDBFileSystem_wasmTriggerErrorCallback(FileSystem::fpErrorCallback error_callback, const char* err_str) {
if (error_callback != 0) {
error_callback(err_str);
}
}
export void IndexDBFileSystem_wasmTriggerWriteCallback(FileSystem::fpWriteCallback write_callback, const char* file_name, int bytes_written) {
if (write_callback != 0) {
write_callback(file_name, bytes_written);
}
}
export void IndexDBFileSystem_wasmTriggerReadCallback(FileSystem::fpReadCallback read_callback, const char* file_name, void* fileData, int sizeInBytes) {
if (read_callback != 0) {
read_callback(file_name, fileData, (u32)sizeInBytes);
}
}
export void IndexDBFileSystem_wasmTriggerPathCallback(FileSystem::fpPathCallback callback, const char* file_name) {
if (callback != 0) {
callback(file_name);
}
}
export void IndexDBFileSystem_wasmTriggerExistsCallback(FileSystem::fpExistsCallback callback, const char* file_name, bool isDir, bool isFile) {
if (callback) {
callback(file_name, isDir, isFile);
}
}
export void IndexDBFileSystem_wasmTriggerDepthFirstIterateCallback(FileSystem::fpDepthFirstIterateCallback callback, const char* path, u32 depth, bool isDirectory, bool isFile) {
if (callback) {
callback(path, depth, isDirectory, isFile);
}
}
export void IndexDBFileSystem_wasmTriggerDepthFirstFinishedCallback(FileSystem::fpDepthFirstFinishedCallback callback) {
if (callback) {
callback();
}
}
export void* IndexDBFileSystem_wasmGetHeapPtr() {
void* memory = &__heap_base;
return memory;
}
// File System API
void FileSystem::Log(const char* msg) {
int len = IndexDBFileSystem_strlen(msg);
IndexDBFileSystem_wasmLog(msg, len);
}
void FileSystem::Write(const char* fileName, const void* fileContent, u32 contentSize, FileSystem::fpWriteCallback onSuccess, FileSystem::fpErrorCallback onError) {
int nameLen = IndexDBFileSystem_strlen(fileName);
IndexDBFileSystem_wasmWrite(fileName, nameLen, fileContent, (int)contentSize, onSuccess, onError);
}
void FileSystem::Read(const char* fileName, void* targetBuffer, u32 targetSize, FileSystem::fpReadCallback onSuccess, FileSystem::fpErrorCallback onError) {
int nameLen = IndexDBFileSystem_strlen(fileName);
IndexDBFileSystem_wasmRead(fileName,nameLen, targetBuffer, (int)targetSize, onSuccess, onError);
}
void FileSystem::CreateFile(const char* filePath, FileSystem::fpPathCallback onSuccess, FileSystem::fpErrorCallback onError) {
int nameLen = IndexDBFileSystem_strlen(filePath);
IndexDBFileSystem_wasmCreateFile(filePath, nameLen, onSuccess, onError);
}
void FileSystem::CreateFolder(const char* folderPath, FileSystem::fpPathCallback onSuccess, FileSystem::fpErrorCallback onError) {
int nameLen = IndexDBFileSystem_strlen(folderPath);
IndexDBFileSystem_wasmCreateFolder(folderPath, nameLen, onSuccess, onError);
}
void FileSystem::Delete(const char* path, FileSystem::fpPathCallback onSuccess, FileSystem::fpErrorCallback onError) {
int nameLen = IndexDBFileSystem_strlen(path);
IndexDBFileSystem_wasmDelete(path, nameLen, onSuccess, onError);
}
void FileSystem::Exists(const char* path, FileSystem::fpExistsCallback onSuccess) {
int nameLen = IndexDBFileSystem_strlen(path);
IndexDBFileSystem_wasmExists(path, nameLen, onSuccess);
}
void FileSystem::PreOrderDepthFirstTraversal(const char* path, FileSystem::fpDepthFirstIterateCallback onIterate, FileSystem::fpDepthFirstFinishedCallback onFinished) {
int nameLen = IndexDBFileSystem_strlen(path);
IndexDBFileSystem_wasmPreDepthFirstTraversal(path, nameLen, onIterate, onFinished);
}
void FileSystem::PostOrderDepthFirstTraversal(const char* path, FileSystem::fpDepthFirstIterateCallback onIterate, FileSystem::fpDepthFirstFinishedCallback onFinished) {
int nameLen = IndexDBFileSystem_strlen(path);
IndexDBFileSystem_wasmPostDepthFirstTraversal(path, nameLen, onIterate, onFinished);
}
void FileSystem::GetWorkingDir(char* outBuffer, u32 buffLen) {
if (buffLen >= 1) {
outBuffer[0] = '/';
}
if (buffLen >= 2) {
outBuffer[1] = '\0';
}
}
void FileSystem::Initialize() {
// Nothing to do....
}
void FileSystem::Shutdown() {
// Nothing to do....
}