| Re: Parsing text files with standard words |
|
 |
|
 |
|
 |
|
 |
Group: comp.lang.forth · Group Profile
Author: The Beez'The Beez' Date: Sep 15, 2008 04:44
On Sep 14, 9:07Â pm, Gerry jackson9000.fsnet.co.uk> wrote:
> FILE-SOURCE ( fileid -- ) is like INCLUDE-FILE but does not
> invoke the Forth text interpreter i.e. it saves the current
> input source specification and makes the file specified by
> fileid the new input source. Hence REFILL can be used to read
> the file from the file position existing at the time FILE-SOURCE
> is called.
>
> CLOSE-SOURCE ( -- ) simply closes the current input source and
> restores the previous one.
>
> These solve the problem as far as I am concerned but can't be
> defined in a standard way (I think). A simple example of usage
> showing how standard words such as REFILL and SOURCE could be
> used to process a file:
>
> : xyz  ( caddr u -- )
> Â Â r/o open-file throw file-source cr
> Â Â begin refill while source type cr repeat
> Â Â close-source
> ;
In 4tH it functions that way! With USE you can revector all I/O words,
so not only refill, but also TYPE, CR, etc. Your program would look
like:
: xyz
input open dup dup 0= throw use cr ( fileid)
begin refill while 0 parse type cr repeat ( fileid)
close ( --)
;
4tH also supports ANS Forth FILE words, but I recommend you don't mix
the two. 4tH also knows SOURCE, but "0 PARSE" is cleaner IMHO.
Other words of interest:
OPEN ( a n mode -- fileid)
If fileid equals zero the file could not be opened.
USE ( fileid --)
Revectors the input or output device to fileid.
CLOSE ( fileid
|