mirror of
https://github.com/nineties/planckforth
synced 2024-12-25 21:58:22 +01:00
WIP Buffered File I/O
This commit is contained in:
parent
4d9365ad31
commit
ef3e15a3ca
3 changed files with 43 additions and 1 deletions
27
bootstrap.fs
27
bootstrap.fs
|
@ -2141,6 +2141,21 @@ need-defined allocate
|
|||
then
|
||||
;
|
||||
|
||||
( === Buffered File I/O === )
|
||||
|
||||
1024 constant BUFSIZE
|
||||
|
||||
struct
|
||||
file% field file>head
|
||||
cell% field file>rbuf
|
||||
cell% field file>rbeg
|
||||
cell% field file>rend
|
||||
cell% field file>wbuf
|
||||
cell% field file>wbeg
|
||||
cell% field file>wend
|
||||
end-struct bufferedfile%
|
||||
|
||||
|
||||
( === open/close === )
|
||||
|
||||
need-defined (open-file)
|
||||
|
@ -2150,12 +2165,22 @@ need-defined (read-file)
|
|||
|
||||
: open-file ( c-addr fam -- file e )
|
||||
2dup (open-file) throw
|
||||
file% %allot
|
||||
file% %allocate throw
|
||||
tuck file>obj !
|
||||
tuck file>fam !
|
||||
tuck file>name !
|
||||
['] (read-file) over file>read-file !
|
||||
['] (write-file) over file>write-file !
|
||||
dup file>fam @ W/O <> if
|
||||
BUFSIZE allocate throw over file>wbuf !
|
||||
0 over file>rbeg !
|
||||
BUFSIZE over file>rend !
|
||||
then
|
||||
dup file>fam @ R/O <> if
|
||||
BUFSIZE allocate throw over file>wbuf !
|
||||
0 over file>wbeg !
|
||||
BUFSIZE over file>wend !
|
||||
then
|
||||
success
|
||||
;
|
||||
|
||||
|
|
|
@ -160,6 +160,7 @@ defbinary("=", eq, ==, intptr_t)
|
|||
|
||||
/* File IO */
|
||||
#define SUCCESS 0
|
||||
#define ALLOCATE_ERROR -59
|
||||
#define CLOSE_FILE_ERROR -62
|
||||
#define OPEN_FILE_ERROR -69
|
||||
#define READ_FILE_ERROR -70
|
||||
|
@ -195,5 +196,12 @@ defcode("(write-file)", writefile) {
|
|||
push((r == size) ? SUCCESS : WRITE_FILE_ERROR);
|
||||
next();
|
||||
}
|
||||
defcode("allocate", allocate) {
|
||||
int size = pop();
|
||||
void *p = malloc(size);
|
||||
push((cell) p);
|
||||
push(p ? SUCCESS : ALLOCATE_ERROR);
|
||||
next();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -219,6 +219,7 @@ def argv():
|
|||
add_simple_operator('v', argv)
|
||||
|
||||
SUCCESS = 0
|
||||
ALLOCATE_ERROR = -59
|
||||
CLOSE_FILE_ERROR = -62
|
||||
OPEN_FILE_ERROR = -69
|
||||
READ_FILE_ERROR = -70
|
||||
|
@ -251,6 +252,14 @@ add_simple_operator('(open-file)', openfile)
|
|||
add_simple_operator('(close-file)', closefile)
|
||||
add_simple_operator('(write-file)', writefile)
|
||||
add_simple_operator('(read-file)', readfile)
|
||||
def allocate():
|
||||
size = pop()
|
||||
n = (size + 4 - 1) // 4
|
||||
addr = len(memory)*4
|
||||
mem.extend([0]*n)
|
||||
push(addr)
|
||||
push(SUCCESS)
|
||||
add_simple_operator('allocate', allocate)
|
||||
|
||||
start = read(HERE_CELL)
|
||||
comma(find('k'))
|
||||
|
|
Loading…
Reference in a new issue