9.21 Parsing binary files. 4tH 4tH Forth compiler
விளம்பரம்
விளம்பரம்
CHAPTER 9. CHARACTER SEGMENT 110
Now that flag left by ’REFILL’ makes sense! If it is zero, we have reached the end of the line. Note that you don’t have to open a file in text-mode and both Microsoft ASCII and
Unix ASCII files are supported.
9.21
Parsing binary files
And what about binary files, like classic Forth blockfiles? Well, you could use ’REFILL’ in that context too, but it would probably break up words since it can’t find an end-of-line marker and its buffer is smaller than 1024 characters. Does that mean it can’t be done? No!
But ’REFILL’ makes it easier for you, because it handles a few tasks automatically.
First, it has its own buffer (TIB). When you’re not using ’REFILL’ you have to define one yourself. Second, it terminates the string for you. You don’t want ’PARSE-WORD’ to wander into new territory, do you? Third, it sets ’>IN’ for you every time its receives new input. You have to take care of that one too.
Never heard of ’>IN’? Well, the only way for ’PARSE-WORD’ to know on what position the previous scan ended is to store that information into a variable. This variable is called
’>IN’.
Not all internal 4tH variables are accessible, mostly because we can’t imagine what use they could have to you. Some variables are just better left alone. But ’>IN’ is available for some very obvious reason: you can reset it and make ’PARSE-WORD’ work for you. Note that for ’>IN’ to work, you have to make the buffer the parsing area by using ’SOURCE!’
The following program will read the first screen of a block-file for you and print out all the words. You will see that all spaces are eliminated and every word is printed on a new line, just the behavior you would expect from ’PARSE-WORD’.
1025 constant /buffer
/buffer 1- constant c/scr file InFile
/buffer string buffer \ 1: our own buffer
: openfile \ open the block file s" romans.blk" input open error?
abort" Cannot open file" to InFile
InFile use
\ save handle
\ read from file
;
: readfile buffer c/scr 2dup bl fill accept drop
InFile close
;
\ fill the buffer
\ address and count
\ clear the buffer
\ fill the buffer
\ close the file
: initparse
;
0 buffer c/scr chars + c!
buffer /buffer source!
0 >in !
: parseblock begin bl parse-word dup 0<> while type cr
\ screensize + terminator
\ size of the block
\ value for file
\ configures parsing
\ 2: terminate screen
\ 3: make buffer the parse area
\ 4: reset >IN
\ get word
\ length zero?
\ if so, print it
பதிவிறக்கு
விளம்பரம்