-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs_handle.c
88 lines (78 loc) · 2.32 KB
/
fs_handle.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
#include "fs_handle.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int handle_close(lua_State *L) {
fclose((FILE*)lua_touserdata(L, lua_upvalueindex(1)));
return 0;
}
char checkChar(char c) {
if (c == 127) return '?';
if ((c >= 32 && c < 127) || c == '\n' || c == '\t' || c == '\r') return c;
else return '?';
}
int handle_readAll(lua_State *L) {
FILE * fp = (FILE*)lua_touserdata(L, lua_upvalueindex(1));
if (feof(fp)) return 0;
long pos = ftell(fp);
fseek(fp, 0, SEEK_END);
long size = ftell(fp) - pos;
char * retval = (char*)malloc(size + 1);
memset(retval, 0, size + 1);
fseek(fp, pos, SEEK_SET);
int i;
for (i = 0; !feof(fp) && i < size; i++)
retval[i] = checkChar(fgetc(fp));
lua_pushstring(L, retval);
return 1;
}
int handle_readLine(lua_State *L) {
FILE * fp = (FILE*)lua_touserdata(L, lua_upvalueindex(1));
if (feof(fp)) return 0;
long size = 0;
while (fgetc(fp) != '\n' && !feof(fp)) size++;
fseek(fp, 0 - size - 1, SEEK_CUR);
char * retval = (char*)malloc(size + 1);
for (int i = 0; i < size; i++) retval[i] = checkChar(fgetc(fp));
fgetc(fp);
lua_pushstring(L, retval);
return 1;
}
int handle_readChar(lua_State *L) {
FILE * fp = (FILE*)lua_touserdata(L, lua_upvalueindex(1));
if (feof(fp)) return 0;
char retval[2];
retval[0] = checkChar(fgetc(fp));
lua_pushstring(L, retval);
return 1;
}
int handle_readByte(lua_State *L) {
FILE * fp = (FILE*)lua_touserdata(L, lua_upvalueindex(1));
if (feof(fp)) return 0;
char retval = fgetc(fp);
lua_pushinteger(L, retval);
return 1;
}
int handle_writeString(lua_State *L) {
const char * str = lua_tostring(L, 1);
FILE * fp = (FILE*)lua_touserdata(L, lua_upvalueindex(1));
fwrite(str, strlen(str), 1, fp);
return 0;
}
int handle_writeLine(lua_State *L) {
const char * str = lua_tostring(L, 1);
FILE * fp = (FILE*)lua_touserdata(L, lua_upvalueindex(1));
fwrite(str, strlen(str), 1, fp);
fputc('\n', fp);
return 0;
}
int handle_writeByte(lua_State *L) {
const char b = lua_tointeger(L, 1) & 0xFF;
FILE * fp = (FILE*)lua_touserdata(L, lua_upvalueindex(1));
fputc(b, fp);
return 0;
}
int handle_flush(lua_State *L) {
fflush((FILE*)lua_touserdata(L, lua_upvalueindex(1)));
return 0;
}