| 1 |
\ https://github.com/philburk/pforth/fth/file.fth |
| 2 |
\ READ-LINE and WRITE-LINE |
| 3 |
\ |
| 4 |
\ This code is part of pForth. |
| 5 |
\ |
| 6 |
\ The pForth software code is dedicated to the public domain, |
| 7 |
\ and any third party may reproduce, distribute and modify |
| 8 |
\ the pForth software code or any derivative works thereof |
| 9 |
\ without any compensation or license. The pForth software |
| 10 |
\ code is provided on an "as is" basis without any warranty |
| 11 |
\ of any kind, including, without limitation, the implied |
| 12 |
\ warranties of merchantability and fitness for a particular |
| 13 |
\ purpose and their equivalents under the laws of any jurisdiction. |
| 14 |
|
| 15 |
private{ |
| 16 |
|
| 17 |
10 constant \N |
| 18 |
13 constant \R |
| 19 |
|
| 20 |
\ Unread one char from file FILEID. |
| 21 |
: UNREAD { fileid -- ior } |
| 22 |
fileid file-position ( ud ior ) |
| 23 |
?dup |
| 24 |
IF nip nip \ IO error |
| 25 |
ELSE 1 s>d d- fileid reposition-file |
| 26 |
THEN |
| 27 |
; |
| 28 |
|
| 29 |
\ Read the next available char from file FILEID and if it is a \n then |
| 30 |
\ skip it; otherwise unread it. IOR is non-zero if an error occured. |
| 31 |
\ C-ADDR is a buffer that can hold at least one char. |
| 32 |
: SKIP-\N { c-addr fileid -- ior } |
| 33 |
c-addr 1 fileid read-file ( u ior ) |
| 34 |
?dup |
| 35 |
IF \ Read error? |
| 36 |
nip |
| 37 |
ELSE ( u ) |
| 38 |
0= |
| 39 |
IF \ End of file? |
| 40 |
0 |
| 41 |
ELSE |
| 42 |
c-addr c@ \n = ( is-it-a-\n? ) |
| 43 |
IF 0 |
| 44 |
ELSE fileid unread |
| 45 |
THEN |
| 46 |
THEN |
| 47 |
THEN |
| 48 |
; |
| 49 |
|
| 50 |
\ This is just s\" \n" but s\" isn't yet available. |
| 51 |
create (LINE-TERMINATOR) \n c, |
| 52 |
: LINE-TERMINATOR ( -- c-addr u ) (line-terminator) 1 ; |
| 53 |
|
| 54 |
\ Standard throw code |
| 55 |
\ See: http://lars.nocrew.org/forth2012/exception.html#table:throw |
| 56 |
-72 constant THROW_RENAME_FILE |
| 57 |
|
| 58 |
\ Copy the string C-ADDR/U1 to C-ADDR2 and append a NUL. |
| 59 |
: PLACE-CSTR ( c-addr1 u1 c-addr2 -- ) |
| 60 |
2dup 2>r ( c-addr1 u1 c-addr2 ) ( r: u1 c-addr2 ) |
| 61 |
swap cmove ( ) ( r: u1 c-addr2 ) |
| 62 |
0 2r> + c! ( ) |
| 63 |
; |
| 64 |
|
| 65 |
: MULTI-LINE-COMMENT ( "comment<rparen>" -- ) |
| 66 |
BEGIN |
| 67 |
>in @ ')' parse ( >in c-addr len ) |
| 68 |
nip + >in @ = ( delimiter-not-found? ) |
| 69 |
WHILE ( ) |
| 70 |
refill 0= IF EXIT THEN ( ) |
| 71 |
REPEAT |
| 72 |
; |
| 73 |
|
| 74 |
}private |
| 75 |
|
| 76 |
\ This treats \n, \r\n, and \r as line terminator. Reading is done |
| 77 |
\ one char at a time with READ-FILE hence READ-FILE should probably do |
| 78 |
\ some form of buffering for good efficiency. |
| 79 |
: READ-LINE ( c-addr u1 fileid -- u2 flag ior ) |
| 80 |
{ a u f } |
| 81 |
u 0 ?DO |
| 82 |
a i chars + 1 f read-file ( u ior' ) |
| 83 |
?dup IF nip i false rot UNLOOP EXIT THEN \ Read error? ( u ) |
| 84 |
0= IF i i 0<> 0 UNLOOP EXIT THEN \ End of file? ( ) |
| 85 |
a i chars + c@ |
| 86 |
CASE |
| 87 |
\n OF i true 0 UNLOOP EXIT ENDOF |
| 88 |
\r OF |
| 89 |
\ Detect \r\n |
| 90 |
a i chars + f skip-\n ( ior ) |
| 91 |
?dup IF i false rot UNLOOP EXIT THEN \ IO Error? ( ) |
| 92 |
i true 0 UNLOOP EXIT |
| 93 |
ENDOF |
| 94 |
ENDCASE |
| 95 |
LOOP |
| 96 |
\ Line doesn't fit in buffer |
| 97 |
u true 0 |
| 98 |
; |
| 99 |
|
| 100 |
: WRITE-LINE ( c-addr u fileid -- ior ) |
| 101 |
{ f } |
| 102 |
f write-file ( ior ) |
| 103 |
?dup |
| 104 |
IF \ IO error |
| 105 |
ELSE line-terminator f write-file |
| 106 |
THEN |
| 107 |
; |
| 108 |
|
| 109 |
: RENAME-FILE ( c-addr1 u1 c-addr2 u2 -- ior ) |
| 110 |
{ a1 u1 a2 u2 | new } |
| 111 |
\ Convert the file-names to C-strings by copying them after HERE. |
| 112 |
a1 u1 here place-cstr |
| 113 |
here u1 1+ chars + to new |
| 114 |
a2 u2 new place-cstr |
| 115 |
here new (rename-file) 0= |
| 116 |
IF 0 |
| 117 |
ELSE throw_rename_file |
| 118 |
THEN |
| 119 |
; |
| 120 |
|
| 121 |
\ A limit used to perform a sanity check on the size argument for |
| 122 |
\ RESIZE-FILE. |
| 123 |
2variable RESIZE-FILE-LIMIT |
| 124 |
10000000 0 resize-file-limit 2! \ 10MB is somewhat arbitrarily chosen |
| 125 |
|
| 126 |
: RESIZE-FILE ( ud fileid -- ior ) |
| 127 |
-rot 2dup resize-file-limit 2@ d> ( fileid ud big? ) |
| 128 |
IF |
| 129 |
." Argument (" 0 d.r ." ) is larger then RESIZE-FILE-LIMIT." cr |
| 130 |
." (You can increase RESIZE-FILE-LIMIT with 2!)" cr |
| 131 |
abort |
| 132 |
ELSE |
| 133 |
rot (resize-file) |
| 134 |
THEN |
| 135 |
; |
| 136 |
|
| 137 |
: ( ( "comment<rparen>" -- ) |
| 138 |
source-id |
| 139 |
CASE |
| 140 |
-1 OF postpone ( ENDOF |
| 141 |
0 OF postpone ( ENDOF |
| 142 |
\ for input from files |
| 143 |
multi-line-comment |
| 144 |
ENDCASE |
| 145 |
; immediate |
| 146 |
|
| 147 |
\ We basically try to open the file in read-only mode. That seems to |
| 148 |
\ be the best that we can do with ANSI C. If we ever want to do |
| 149 |
\ something more sophisticated, like calling access(2), we must create |
| 150 |
\ a proper primitive. (OTOH, portable programs can't assume much |
| 151 |
\ about FILE-STATUS and non-portable programs could create a custom |
| 152 |
\ function for access(2).) |
| 153 |
: FILE-STATUS ( c-addr u -- 0 ior ) |
| 154 |
r/o bin open-file ( fileid ior1 ) |
| 155 |
?dup |
| 156 |
IF nip 0 swap ( 0 ior1 ) |
| 157 |
ELSE close-file 0 swap ( 0 ior2 ) |
| 158 |
THEN |
| 159 |
; |
| 160 |
|
| 161 |
privatize |