1 \ READ-LINE and WRITE-LINE
3 \ This file is in the public domain.
11 \ Unread one char from file FILEID.
12 : UNREAD ( fileid -- ior )
14 f file-position ( ud ior )
17 ELSE 1 s>d d- f reposition-file
21 \ Read the next available char from file FILEID and if it is a \n then
22 \ skip it; otherwise unread it. IOR is non-zero if an error occured.
23 \ C-ADDR is a buffer that can hold at least on char.
24 : SKIP-\n ( c-addr fileid -- ior )
26 a 1 f read-file ( u ior )
35 a c@ \n = ( is-it-a-\n? )
43 \ This is just s\" \n" but s\" isn't yet available.
44 create (LINE-TERMINATOR) \n c,
45 : LINE-TERMINATOR ( -- c-addr u ) (line-terminator) 1 ;
50 \ This treats \n, \r\n, and \r as line terminator. Reading is done
51 \ one char at a time with READ-FILE hence READ-FILE should probably do
52 \ some form of buffering for good efficiency.
53 : READ-LINE ( c-addr u1 fileid -- u2 flag ior )
56 a i chars + 1 f read-file ( u ior' )
57 ?dup IF nip i false rot UNLOOP EXIT THEN \ Read error? ( u )
58 0= IF i i 0> 0 UNLOOP EXIT THEN \ End of file? ( )
61 \n OF i true 0 UNLOOP EXIT ENDOF
64 a i 1+ chars + f skip-\n ( ior )
65 ?dup IF i false rot UNLOOP EXIT THEN \ IO Error? ( )
70 \ Line doesn't fit in buffer
74 : WRITE-LINE ( c-addr u fileid -- ior )
79 ELSE line-terminator f write-file