There will come a time when you want to display text. There is always that doc file you forgot to print, or couldn't. You know the story; a newly acquired extension is fired into place and carefully pored over. Then it dawns, you can't remember what the syntax for a command is. The makers of STOS kindly gave you a program to 'type' out a document but it was, to say the least, naff. Simple doc loaders and displayers are ten-a-penny, but also tend to be naff and slow. I have a hard drive, and waiting for a big text file to be loaded in using LINE INPUT is a right pain.
Then what about that on-line manual you want to add to your latest masterpiece. Or lots of descriptive text to your adventure. In both these cases using some methods will leave the user in a comatosed state; long before they get to read your textual creation.
Lets look at the ways you can load and display a text file:
1 - The most obvious way to load a text file is to use (as mentioned)
the LINE INPUT command. This is fine and dandy but suffers from a few problems
Mainly speed. It's biggest advantage is that all the text is put into the
string automatically, and needs no work, so 10 out of 10 for ease of use.
See program FASTLD1.BAS for an example of this in operation.
2 -Load the entire file into a string and then split it up into
individual lines. This is a much faster method, using the INSTR command
you can look for carriage returns (CHR$(13)). The biggest drawback to this
method is string length. 64k is the maximum string allowed in STOS and
that can be quite limiting if you're trying to load really big text file.
It's much, MUCH faster than LINE INPUTting though. For most 'medium' sized
texts this is the thing to use...
See program FASTLD2.BAS for an example of this in operation.
3 -Load the entire file into a memory bank. THE BEST. No problems
with the size of the file, the bank can be as big as memory, and there's
no problem with speed, BLOADing takes a LOT less time than LINE INPUTing.
There is a problem of course, you may have guessed - how to print this
file to screen as the text is not in strings.
See program FASTLD3.BAS for an example of this in operation.
All three methods have their advantages and disadvantages but it's number 3 that seems to have most going for it. It doesn't take long to load, you can control memory allocation precisely and - possibly the best part - you can use packed text files. IF only we can get passed the printing problem. Scanning through you STOS manual there is a section wittily entitled 'Appendix E' subtitled 'The STOS Basic Traps.' Having a look at the list of Trap calls will not make interesting reading, however there is one of interest:
PRINT STRING Prints a string of A0=Pointer to string characters in a window String is terminated by 0When this is linked to load and display option 3 then we have a winner. The program FASTLD3.BAS has lots of luverly rems to explain how to use this command. So I won't go through it in detail, but here is the basic structure:
areg(0)=address - Holds the address of the string you want to print, the string must end in chr$(0) [or zero if it's a memory location]You can also use the command in form:
areg(0)=vaptr(A$)But I don't know why you would!
dreg(7)=1This sets up what command will be used in the following trap call. ALWAYS set this to one (1) or strange things might happen, or worse your program will bomb.
And lo and behold your string will have been printed to the screen at the current cursor position.
Of course it depends entirely on what you're actually doing. If only 2 or 3 line of text are to be read in, then LINE INPUTing will be the best. However if there's more than that, then PLEASE use one of the other methods. I really, REALLY, hate waiting for text files to load
There you have it, a fairly simple and efficient way to load and display text. This method also has a hidden advantage. If you own the excellent Missing Link extentsion from Top Notch then you can use the TEXT command without any modification. So increasing the display speed by a factor of something big. A 50k text file take about 40 seconds to 'page' through using the above method, but using the TEXT command it drops to a jaw dropping 10 seconds (probably less!). In order to use this command try the following have a look at FASTLD3A.BAS (only if you have Missing Link)
Don't you just love articles with tables. I tried all 4 routines in
a compiled and interpreted mode, just to have a speed comparison. There
are two times given for each program, this is because on the first run
of the program STOS has to do all sorts of internal jiggery, but on the
second it has no need. Therefore the second time is (should be) a little
faster (all times are in seconds - approximately):
| Command | Interpreted 1st run | Interpreted nth run | Compiled 1st run | Compiled nth run |
| Line Input | 4.33 | 2.98 | 3.46 | 2.96 |
| Input$(#1,x) | 1.70 | 1.00 | 1.70 | 0.78 |
| Trap 3 | 1.78 | 0.85 | 1.70 | 1.70 |
| TEXT | 1.30 | 0.26 | 1.23 | 1.21 |
I should note that some of the times were a little strange. i.e. 0.26 interpreted and then 1.21 compiled (!!?) Whatever the case there is a clear speed advantage over Line Input. SO DON'T USE IT....
The example program is a document displayer which uses this method of loading and displaying the text. I have written it so that it can be easily incorporated into other programs, but you'll have to work out how to do that yourself. If you do use it in your proggies then I would appreciate a mention.
It should be pointed out that the program is not easy to follow, but all the THEORY stated above is included, along with a few bells and whistles. Pressing HELP will give the low down on the keyboard commands. Variable names are the usual mix of T,A and XQWD3 and the like.
As a final note: the biggest advantage of this method is that text files can be packed. You obviously can't pack a text file that is going to be LINE INPUTted, you get daft characters all over the place. Load the file into memory, depack it and then do the search for carriage returns and line feeds. What a nice techno-jiggery fing!.
Bob Goodfellow (C) November 1993