Implement RENAME-FILE
[pforth] / fth / file.fth
CommitLineData
593eb738
HE
1\ READ-LINE and WRITE-LINE
2\
938d9dba 3\ This code is part of pForth.
593eb738 4\
938d9dba
HE
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.
593eb738
HE
13
14private{
15
938d9dba
HE
1610 constant \N
1713 constant \R
593eb738
HE
18
19\ Unread one char from file FILEID.
938d9dba
HE
20: UNREAD { fileid -- ior }
21 fileid file-position ( ud ior )
593eb738
HE
22 ?dup
23 IF nip nip \ IO error
938d9dba 24 ELSE 1 s>d d- fileid reposition-file
593eb738
HE
25 THEN
26;
27
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.
938d9dba
HE
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 )
aad4537d
HE
33 ?dup
34 IF \ Read error?
35 nip
36 ELSE ( u )
37 0=
38 IF \ End of file?
39 0
40 ELSE
938d9dba 41 c-addr c@ \n = ( is-it-a-\n? )
aad4537d 42 IF 0
938d9dba 43 ELSE fileid unread
aad4537d
HE
44 THEN
45 THEN
46 THEN
593eb738
HE
47;
48
49\ This is just s\" \n" but s\" isn't yet available.
50create (LINE-TERMINATOR) \n c,
51: LINE-TERMINATOR ( -- c-addr u ) (line-terminator) 1 ;
52
6f3de396
HE
53-72 constant THROW_RENAME_FILE
54
593eb738
HE
55}private
56
593eb738
HE
57\ This treats \n, \r\n, and \r as line terminator. Reading is done
58\ one char at a time with READ-FILE hence READ-FILE should probably do
59\ some form of buffering for good efficiency.
60: READ-LINE ( c-addr u1 fileid -- u2 flag ior )
aad4537d
HE
61 { a u f }
62 u 0 ?DO
63 a i chars + 1 f read-file ( u ior' )
64 ?dup IF nip i false rot UNLOOP EXIT THEN \ Read error? ( u )
938d9dba 65 0= IF i i 0<> 0 UNLOOP EXIT THEN \ End of file? ( )
aad4537d
HE
66 a i chars + c@
67 CASE
68 \n OF i true 0 UNLOOP EXIT ENDOF
69 \r OF
70 \ Detect \r\n
938d9dba 71 a i chars + f skip-\n ( ior )
aad4537d
HE
72 ?dup IF i false rot UNLOOP EXIT THEN \ IO Error? ( )
73 i true 0 UNLOOP EXIT
74 ENDOF
75 ENDCASE
76 LOOP
77 \ Line doesn't fit in buffer
78 u true 0
593eb738
HE
79;
80
81: WRITE-LINE ( c-addr u fileid -- ior )
aad4537d
HE
82 { f }
83 f write-file ( ior )
84 ?dup
85 IF \ IO error
86 ELSE line-terminator f write-file
87 THEN
593eb738
HE
88;
89
6f3de396
HE
90: RENAME-FILE ( c-addr1 u1 c-addr2 u2 -- ior )
91 { a1 u1 a2 u2 | new }
92 \ Convert the file-names to C-strings by copying them after HERE
93 \ with trailing zeros added.
94 a1 here u1 move
95 0 here u1 chars + c!
96 here u1 1+ chars + to new
97 a2 new u2 move
98 0 new u2 chars + c!
99 here new (rename-file) 0=
100 IF 0
101 ELSE throw_rename_file
102 THEN
103;
104
593eb738 105privatize