-
Notifications
You must be signed in to change notification settings - Fork 0
/
IDBFS_test.c
69 lines (58 loc) · 1.65 KB
/
IDBFS_test.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <emscripten.h>
//-----------------------------------------------------------------------
EMSCRIPTEN_KEEPALIVE
char *LoadData() {
int fd;
int size;
char *buff;
fd = open("/data/textfile.txt", O_RDONLY);
if (fd == -1) return strerror(errno);
size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
buff = (char *) malloc(size+1);
read(fd, buff, size);
buff[size] = '\0';
close(fd);
return buff;
}
//-----------------------------------------------------------------------
EMSCRIPTEN_KEEPALIVE
void SaveData(char *data) {
int fd;
int size;
if (data == NULL) return;
fd = open("/data/textfile.txt", O_CREAT | O_WRONLY, 0666);
if (fd == -1) {
printf("ERROR: could not open the file for writing!\n, %s\n", strerror(errno));
return;
}
size = strlen(data);
printf("saving %i bytes... %s\n", size, data);
write(fd, data, size);
ftruncate(fd, size);
close(fd);
EM_ASM ( FS.syncfs(false, function (err) {} ); );
}
//-----------------------------------------------------------------------
int main() {
EM_ASM(
FS.mkdir('/data');
FS.mount(IDBFS, {}, '/data');
FS.syncfs(true, function (err) {
// provide the DOM side a way to execute code after directory is mounted
if (typeof Module.OnDataMounted !== 'undefined') {
Module.OnDataMounted();
}
} );
);
emscripten_exit_with_live_runtime();
}
//-----------------------------------------------------------------------