1 \ READ-LINE and WRITE-LINE
3 \ This code is part of pForth.
5 \ Permission to use, copy, modify, and/or distribute this
6 \ software for any purpose with or without fee is hereby granted.
8 \ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9 \ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10 \ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 \ THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
12 \ CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 \ FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
14 \ CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 \ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 \ Unread one char from file FILEID.
23 : UNREAD { fileid -- ior }
24 fileid file-position ( ud ior )
27 ELSE 1 s>d d- fileid reposition-file
31 \ Read the next available char from file FILEID and if it is a \n then
32 \ skip it; otherwise unread it. IOR is non-zero if an error occured.
33 \ C-ADDR is a buffer that can hold at least one char.
34 : SKIP-\N { c-addr fileid -- ior }
35 c-addr 1 fileid read-file ( u ior )
44 c-addr c@ \n = ( is-it-a-\n? )
52 \ This is just s\" \n" but s\" isn't yet available.
53 create (LINE-TERMINATOR) \n c,
54 : LINE-TERMINATOR ( -- c-addr u ) (line-terminator) 1 ;
57 \ See: http://lars.nocrew.org/forth2012/exception.html#table:throw
58 -72 constant THROW_RENAME_FILE
60 \ Copy the string C-ADDR/U1 to C-ADDR2 and append a NUL.
61 : PLACE-CSTR ( c-addr1 u1 c-addr2 -- )
62 2dup 2>r ( c-addr1 u1 c-addr2 ) ( r: u1 c-addr2 )
63 swap cmove ( ) ( r: u1 c-addr2 )
67 : MULTI-LINE-COMMENT ( "comment<rparen>" -- )
69 >in @ ')' parse ( >in c-addr len )
70 nip + >in @ = ( delimiter-not-found? )
72 refill 0= IF EXIT THEN ( )
78 \ This treats \n, \r\n, and \r as line terminator. Reading is done
79 \ one char at a time with READ-FILE hence READ-FILE should probably do
80 \ some form of buffering for good efficiency.
81 : READ-LINE ( c-addr u1 fileid -- u2 flag ior )
84 a i chars + 1 f read-file ( u ior' )
85 ?dup IF nip i false rot UNLOOP EXIT THEN \ Read error? ( u )
86 0= IF i i 0<> 0 UNLOOP EXIT THEN \ End of file? ( )
89 \n OF i true 0 UNLOOP EXIT ENDOF
92 a i chars + f skip-\n ( ior )
93 ?dup IF i false rot UNLOOP EXIT THEN \ IO Error? ( )
98 \ Line doesn't fit in buffer
102 : WRITE-LINE ( c-addr u fileid -- ior )
107 ELSE line-terminator f write-file
111 : RENAME-FILE ( c-addr1 u1 c-addr2 u2 -- ior )
112 { a1 u1 a2 u2 | new }
113 \ Convert the file-names to C-strings by copying them after HERE.
114 a1 u1 here place-cstr
115 here u1 1+ chars + to new
117 here new (rename-file) 0=
119 ELSE throw_rename_file
123 \ A limit used to perform a sanity check on the size argument for
125 2variable RESIZE-FILE-LIMIT
126 10000000 0 resize-file-limit 2! \ 10MB is somewhat arbitrarily chosen
128 : RESIZE-FILE ( ud fileid -- ior )
129 -rot 2dup resize-file-limit 2@ d> ( fileid ud big? )
131 ." Argument (" 0 d.r ." ) is larger then RESIZE-FILE-LIMIT." cr
132 ." (You can increase RESIZE-FILE-LIMIT with 2!)" cr
139 : ( ( "comment<rparen>" -- )
142 -1 OF postpone ( ENDOF
143 0 OF postpone ( ENDOF
144 \ for input from files
149 \ We basically try to open the file in read-only mode. That seems to
150 \ be the best that we can do with ANSI C. If we ever want to do
151 \ something more sophisticated, like calling access(2), we must create
152 \ a proper primitive. (OTOH, portable programs can't assume much
153 \ about FILE-STATUS and non-portable programs could create a custom
154 \ function for access(2).)
155 : FILE-STATUS ( c-addr u -- 0 ior )
156 r/o bin open-file ( fileid ior1 )
158 IF nip 0 swap ( 0 ior1 )
159 ELSE close-file 0 swap ( 0 ior2 )