-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.h
38 lines (34 loc) · 1.17 KB
/
fs.h
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
#include <quickjs/quickjs-libc.h>
#include <quickjs/quickjs.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSValue js_fs_read(JSContext *ctx,JSValueConst self,int argc,JSValueConst *args) {
const char *filename = JS_ToCString(ctx,args[0]);
FILE *file = fopen(filename,"rb");
char *data = (char*) malloc(200 * sizeof(char));
if (!file ) {
JSValue ex = JS_ThrowReferenceError(ctx,"'%s': %s\n",filename,strerror(errno));
}
else {
int c;
while ( ( c = fgetc(file) ) != EOF ) {
sprintf(data,"%s%c",data,c);
}
return JS_NewString(ctx,data);
}
return JS_NULL;
}
static const JSCFunctionListEntry js_fs_functions[] = {
JS_CFUNC_DEF("readFile",1,js_fs_read),
};
static int js_init_fs(JSContext *ctx,JSModuleDef *module) {
return JS_SetModuleExportList(ctx, module,js_fs_functions,countof(js_fs_functions));
}
static void js_fs_init(JSContext *ctx,const char *name) {
JSModuleDef *module = JS_NewCModule(ctx,name, js_init_fs);
JS_AddModuleExportList(ctx, module,js_fs_functions,countof(js_fs_functions));
}