Forth Blocks
Volume Number: 1
Issue Number: 10
Column Tag: Forth Forum
"Converting Forth Blocks to Ascii Text
"Accuracy in Modula-2 Reviewed
By Jörg Langowski, Chemical Engineer, Fed. Rep. of Germany, MacTutor
Editorial Board
With the advent of other Forth systems than MacForth, and Forth-related
packages such as NEON, a utility is needed that lets us quickly convert the MacForth
'Blocks' format into a standard ASCII text file and back. NEON, for instance, expects its
input from a normal text file. Also this Journal expects its input from a MacWrite
file, and after I have written an example program, cutting it out of the Forth screens
and pasting it into my column is quite cumbersome.
So here is a routine that does this job automatically (it contains some ideas out of
a program on one Call-A.P.P.L.E. public domain disk). At the same time we are going to
learn something about the Standard File Interface package, which is undocumented (but
fully usable) in MacForth 1.1.
What do we need to know to convert files from Blocks to Text format and back?
The type of a Blocks file is BLKS and the creator M4TH, a Text file such as saved by
'text only' MacWrite or MDS Edit is of type TEXT and creator MACA (this probably
stands for Mac Ascii). In principle, both files contain ASCII text, which, however, is
organized differently.
The format of a Blocks file
MacForth blocks files consist of screens , each of which contain 16 lines of 64
characters. One line is always 64 characters long and padded with blanks after the last
character. This format is a relic from early FORTH times and - in my opinion - rather
obsolete. But since the MacForth editor uses it, we have to live with it. The length of a
file is of course very simply related to the number of screens; each screen occupies
exactly 1024 bytes.
Text files, on the other hand, contain lines of ASCII characters which are
separated by carriage returns (CR).
One very simple thing that one can try out to communicate between the two types
of files is to change type and creator of a Blocks file to TEXT and MACA, hoping that it is
recognized by Edit, for example. When I tried this, it would show a file that had only
one line (of course, since there are no CRs). So we do have to read the Blocks file, line
by line, and create a new text file to which we write those lines.
Reading Forth blocks
A Forth block in a file is accessed through the word BLOCK, which accepts the
block number on top of stack and returns the address of a buffer that contains the
block. The block is automatically read in if it is not already there.
The position of a line in the block is at (64 * line number). The word
>LINE.START in the program example returns the address of a line in a given block. We
then remove trailing blanks from this line, add a CR at the end and write it to the Text
file (which we have opened before). This is what the word COPY.BLOCK does, which is
called from the main menu: It traverses the Blocks file line by line and writes the Text
file.
ASCII to Blocks conversion
The reverse direction is a little bit more complicated. The total number of lines
in a blocks file is known exactly from its size, so we know ahead of time how many
screens we have to convert; also a Text file can be extended arbitrarily by just adding
lines. Not so the other way around: a Blocks file must be extended by appending blocks
before writing to them, and the end of a Text file is known only when an end-of-file
occurs.
In the example, each text line is read into the string variable TEXT.BUF with the
character count in the first byte and the actual line in the following bytes. This string
is then copied into a new line in the buffer; when 11 lines have been processed in this
way, the buffer is written out to disk (UPDATE FLUSH) and the Blocks file extended by