forked from ccxvii/mujs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsfile.c
143 lines (116 loc) · 2.71 KB
/
jsfile.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
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
141
142
143
#include "jsi.h"
#include <stdio.h>
#include <errno.h>
static void jsB_fexists(js_State *J)
{
const char *filename = js_tostring(J, 1);
FILE *f;
f = fopen(filename, "r");
if(errno != 0)
{
js_pushboolean(J, 0);
}
else
{
fclose(f);
js_pushboolean(J, 1);
}
}
static void jsB_fwrite(js_State *J)
{
if(!js_isdefined(J, 1) || !js_isdefined(J, 2))
{
js_error(J, "File name and file content are required");
}
const char *filename = js_tostring(J, 1);
const char *filecont = js_tostring(J, 2);
FILE *f;
f = fopen(filename, "w");
if(errno != 0)
{
js_error(J, "cannot write file '%s': %s", filename, strerror(errno));
}
if (f == NULL) {
fclose(f);
js_error(J, "cannot write file '%s': %s", filename, strerror(errno));
}
if (fprintf(f, "%s", filecont) < 0) {
fclose(f);
js_error(J, "cannot write data in file '%s': %s", filename, strerror(errno));
}
fclose(f);
}
static void jsB_fappend(js_State *J)
{
if(!js_isdefined(J, 1) || !js_isdefined(J, 2))
{
js_error(J, "File name and file content are required");
}
const char *filename = js_tostring(J, 1);
const char *filecont = js_tostring(J, 2);
FILE *f;
f = fopen(filename, "a");
if(errno != 0)
{
js_error(J, "cannot append to file '%s': %s", filename, strerror(errno));
}
if (f == NULL) {
fclose(f);
js_error(J, "cannot append to file '%s': %s", filename, strerror(errno));
}
if (fprintf(f, "%s", filecont) < 0) {
fclose(f);
js_error(J, "cannot append data in file '%s': %s", filename, strerror(errno));
}
fclose(f);
}
static void jsB_fread(js_State *J)
{
const char *filename = js_tostring(J, 1);
FILE *f;
char *s;
int n, t;
f = fopen(filename, "rb");
if (!f) {
js_error(J, "cannot open file '%s': %s", filename, strerror(errno));
}
if (fseek(f, 0, SEEK_END) < 0) {
fclose(f);
js_error(J, "cannot seek in file '%s': %s", filename, strerror(errno));
}
n = ftell(f);
if (n < 0) {
fclose(f);
js_error(J, "cannot tell in file '%s': %s", filename, strerror(errno));
}
if (fseek(f, 0, SEEK_SET) < 0) {
fclose(f);
js_error(J, "cannot seek in file '%s': %s", filename, strerror(errno));
}
s = malloc(n + 1);
if (!s) {
fclose(f);
js_error(J, "out of memory");
}
t = fread(s, 1, n, f);
if (t != n) {
free(s);
fclose(f);
js_error(J, "cannot read data from file '%s': %s", filename, strerror(errno));
}
s[n] = 0;
js_pushstring(J, s);
free(s);
fclose(f);
}
void jsB_initfile(js_State *J)
{
js_pushobject(J, jsV_newobject(J, JS_CFILE, J->Object_prototype));
{
jsB_propf(J, "File.exists", jsB_fexists, 1);
jsB_propf(J, "File.write", jsB_fwrite, 2);
jsB_propf(J, "File.append", jsB_fappend, 2);
jsB_propf(J, "File.read", jsB_fread, 1);
}
js_defglobal(J, "File", JS_DONTENUM);
}