Commit | Line | Data |
---|---|---|
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 | |
14 | private{ | |
15 | ||
938d9dba HE |
16 | 10 constant \N |
17 | 13 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. | |
50 | create (LINE-TERMINATOR) \n c, | |
51 | : LINE-TERMINATOR ( -- c-addr u ) (line-terminator) 1 ; | |
52 | ||
662a735a HE |
53 | \ Standard throw code |
54 | \ See: http://lars.nocrew.org/forth2012/exception.html#table:throw | |
6f3de396 HE |
55 | -72 constant THROW_RENAME_FILE |
56 | ||
662a735a HE |
57 | \ Copy the string C-ADDR/U1 to C-ADDR2 and append a NUL. |
58 | : PLACE-CSTR ( c-addr1 u1 c-addr2 -- ) | |
59 | 2dup 2>r ( c-addr1 u1 c-addr2 ) ( r: u1 c-addr2 ) | |
60 | swap cmove ( ) ( r: u1 c-addr2 ) | |
61 | 0 2r> + c! ( ) | |
62 | ; | |
63 | ||
fe6f537b HE |
64 | : MULTI-LINE-COMMENT ( "comment<rparen>" -- ) |
65 | BEGIN | |
66 | >in @ ')' parse ( >in c-addr len ) | |
67 | nip + >in @ = ( delimiter-not-found? ) | |
68 | WHILE ( ) | |
69 | refill 0= IF EXIT THEN ( ) | |
70 | REPEAT | |
71 | ||
72 | ; | |
73 | ||
593eb738 HE |
74 | }private |
75 | ||
593eb738 HE |
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 ) | |
aad4537d HE |
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 ) | |
938d9dba | 84 | 0= IF i i 0<> 0 UNLOOP EXIT THEN \ End of file? ( ) |
aad4537d HE |
85 | a i chars + c@ |
86 | CASE | |
87 | \n OF i true 0 UNLOOP EXIT ENDOF | |
88 | \r OF | |
89 | \ Detect \r\n | |
938d9dba | 90 | a i chars + f skip-\n ( ior ) |
aad4537d HE |
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 | |
593eb738 HE |
98 | ; |
99 | ||
100 | : WRITE-LINE ( c-addr u fileid -- ior ) | |
aad4537d HE |
101 | { f } |
102 | f write-file ( ior ) | |
103 | ?dup | |
104 | IF \ IO error | |
105 | ELSE line-terminator f write-file | |
106 | THEN | |
593eb738 HE |
107 | ; |
108 | ||
6f3de396 HE |
109 | : RENAME-FILE ( c-addr1 u1 c-addr2 u2 -- ior ) |
110 | { a1 u1 a2 u2 | new } | |
662a735a HE |
111 | \ Convert the file-names to C-strings by copying them after HERE. |
112 | a1 u1 here place-cstr | |
6f3de396 | 113 | here u1 1+ chars + to new |
662a735a | 114 | a2 u2 new place-cstr |
6f3de396 HE |
115 | here new (rename-file) 0= |
116 | IF 0 | |
117 | ELSE throw_rename_file | |
118 | THEN | |
119 | ; | |
120 | ||
fe6f537b HE |
121 | : ( ( "comment<rparen>" -- ) |
122 | source-id | |
123 | CASE | |
124 | -1 OF postpone ( ENDOF | |
125 | 0 OF postpone ( ENDOF | |
126 | multi-line-comment | |
127 | ENDCASE | |
128 | ; immediate | |
129 | ||
593eb738 | 130 | privatize |