1 \ READ-LINE and WRITE-LINE
3 \ This code is part of pForth.
5 \ The pForth software code is dedicated to the public domain,
6 \ and any third party may reproduce, distribute and modify
7 \ the pForth software code or any derivative works thereof
8 \ without any compensation or license. The pForth software
9 \ code is provided on an "as is" basis without any warranty
10 \ of any kind, including, without limitation, the implied
11 \ warranties of merchantability and fitness for a particular
12 \ purpose and their equivalents under the laws of any jurisdiction.
19 \ Unread one char from file FILEID.
20 : UNREAD { fileid -- ior }
21 fileid file-position ( ud ior )
24 ELSE 1 s>d d- fileid reposition-file
28 \ Read the next available char from file FILEID and if it is a \n then
29 \ skip it; otherwise unread it. IOR is non-zero if an error occured.
30 \ C-ADDR is a buffer that can hold at least one char.
31 : SKIP-\N { c-addr fileid -- ior }
32 c-addr 1 fileid read-file ( u ior )
41 c-addr c@ \n = ( is-it-a-\n? )
49 \ This is just s\" \n" but s\" isn't yet available.
50 create (LINE-TERMINATOR) \n c,
51 : LINE-TERMINATOR ( -- c-addr u ) (line-terminator) 1 ;
55 \ This treats \n, \r\n, and \r as line terminator. Reading is done
56 \ one char at a time with READ-FILE hence READ-FILE should probably do
57 \ some form of buffering for good efficiency.
58 : READ-LINE ( c-addr u1 fileid -- u2 flag ior )
61 a i chars + 1 f read-file ( u ior' )
62 ?dup IF nip i false rot UNLOOP EXIT THEN \ Read error? ( u )
63 0= IF i i 0<> 0 UNLOOP EXIT THEN \ End of file? ( )
66 \n OF i true 0 UNLOOP EXIT ENDOF
69 a i chars + f skip-\n ( ior )
70 ?dup IF i false rot UNLOOP EXIT THEN \ IO Error? ( )
75 \ Line doesn't fit in buffer
79 : WRITE-LINE ( c-addr u fileid -- ior )
84 ELSE line-terminator f write-file