-
Notifications
You must be signed in to change notification settings - Fork 5
/
fs-pc.cpp
67 lines (53 loc) · 2.06 KB
/
fs-pc.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
// Support for files, either PC or None
#include "c3.h"
// BEGIN: These are shared for all file system versions
CELL_T inputStk[ISTK_SZ+1], input_sp, input_fp;
void ipush(CELL_T x) { if (input_sp < ISTK_SZ) inputStk[++input_sp] = x; }
CELL_T ipop() { return (0 < input_sp) ? inputStk[input_sp--] : 0; }
static char block_fn[16];
char* getBlockFN(int num) { sprintf(block_fn, "block-%03d.fth", num); return block_fn; }
// END: These are shared for all file system versions
#ifdef isPC
void fileInit() { input_fp = input_sp = 0; }
CELL_T fOpen(const char *nm, const char *md) { return (CELL_T)fopen(nm, md); }
void fClose(CELL_T fp) { fclose((FILE*)fp); }
int fRead(char *buf, int sz, int num, CELL_T fp) {
return (int)fread(buf, sz, num, (FILE*)fp);
}
int fWrite(char *buf, int sz, int count, CELL_T fp) {
return (int)fwrite(buf, sz, count, (FILE*)fp);
}
int writeBlock(int blk, char *data, int sz) {
int nw = 0;
CELL_T fh = fOpen(getBlockFN(blk), "wb");
if (fh) {
nw = fWrite(data, 1, sz, fh);
fClose(fh);
}
return nw;
}
int readBlock(int blk, char *data, int sz) {
int nr = 0;
CELL_T fh = fOpen(getBlockFN(blk), "rb");
if (fh) {
nr = fRead(data, 1, sz, fh);
fClose(fh);
}
return nr;
}
int fGets(CELL_T fh, char *buf, int sz) {
buf[0] = 0;
fgets(buf, sz, (FILE*)fh);
return strLen(buf);
}
#elif defined (_NoFS_)
void fileInit() { }
static void nf() { printString("-noFile-"); }
CELL_T fOpen(const char *nm, const char *md) { nf(); return 0; }
void fClose(CELL_T fp) { nf(); }
int fRead(char *buf, int sz, int num, CELL_T fp) { nf(); return 0; }
int fWrite(char *buf, int sz, int num, CELL_T fp) { nf(); return 0; }
void blockLoad(int num) { nf(); }
int readBlock(int blk, char *buf, int sz) { nf(); return 0; }
int writeBlock(int blk, char *buf, int sz) { nf(); return 0; }
#endif