|Column Tag:||Basic School
Reading & Writing Disk Files
By Dave Kelly, MacTutor Editorial Board, Ontario, CA
READING & WRITING Z FAST WAY
Reading and writing data is often taken for granted. Yet we read and write data every time we turn on our computers. Fundamental to the Macintosh is the handling of graphics, sometimes in MacPaint format, and sometimes in DRAW or PICT format. Zedcor has some interesting commands that will speed up your data handling no matter which format you are using.
Fig. 1 Paint scroller program scrolls PICT images
The commands I am referring to are WRITE FILE (E-159) and READ FILE (E-126). I have used the examples in the manual as a base to set up my demo programs. Before I discuss this more, lets review the syntax:
READ FILE [#] filenumber%, desinationAddress&, NumberofBytes&
WRITE FILE [#] filenumber%, desinationAddress&, NumberofBytes&
The key is to set up variables that you want to read or write, find the pointer to the variable (destinationAddress& ) and read or write the desired NumberofBytes&. Easy, right? I can just imagine some of the possibilities. How about a communications program? Or a file transfer program? Or how about writing your own minifinder?
The WRITE FILE example in the ZBasic manual demonstrates a way that variables may be temporarily stored (quickly) to disk, run another application, then return to this program and read the variables back via READ FILE. This could always be done before (with other Basic commands), but not as fast. Ive taken the WRITE FILE example and modified slightly to create a Mini Finder type program. Those of you who have been around the Macintosh for a while will remember the old days when the minifinder option was not a part of the Finder. Several minifinder type programs popped up. By replacing the regular finder with a smaller finder, space on the disk can be preserved. That was really all you could do when disks were limited to 400K. Now with 800K disks and hard disks, the need is not as great. Still, with all the things being added to the system file, if you dont have a hard disk your system disk is probably pretty full. My system file on my hard disk is over 1M byte (with all the fonts loaded of course).
HOW IT WORKS
First you must realize that this program will not work properly unless you compile it into a full-blown application. Since the program will launch another application it will be impossible to return to ZBasic and load you basic source code to run it again. So just compile as an application first. Also, this application must be located in the blessed folder (i.e. the system folder). The reason for this is that this application will temporarily take the place of the Finder (which also must remain in the system folder). In a sense, our ZBasic application becomes a system application.
The program makes use of two global variables (available to all Macintosh applications) namely, CurApName ($910) and FinderName ($2F0). The variable FinderName contains the name of the Finder application (usually Finder). CurApName contains the name of your application program. An application has no way to know if a user has changed its name. If the application needs to no its own name, it may refer to CurApName. The trick that this application uses is to temporarily change the name of the Finder application to the current application name. The CurApName and FinderName variables are stored in Pascal format with length first with the string following. Be careful to observe the proper length of the strings. CurApName is type Str31 which means it is a string with length (max) of 32. FinderName can only be 16 characters long because it is of type Str15. (A partial list of global variables is found on page E-198 in the ZBasic manual).
Dummy first and last variables are set up to determine the block of variables that you want to read. The variables will be read or write from that block of memory which is marked by the first and last variables. It is important to realize that ZBasic determines which variable is last during the compile process not during run time. This is the reason for the subroutine Get Last Variable Pointer at the end of the program. Notice that the last variable is defined at the very end of the program. If you only have a few variables to store you may want to position the first and last to include only those variables. Just remember that space should be reserved for reading variables back. Just for fun the program records the timer count as one of the variables to be stored. When the program is re-run after launching another application, it will check the timer again and display the elapsed time.
An important additional comment is that you should remember to tell the user what you have in mind when every you display a GetFile dialog box. IF your program displays a dialog box to load/save a file or as in this case to launch an application, be sure the the user knows why the dialog is displayed. If the dialog was displayed as the result of a menu that was selected then the purpose is self explanatory. Occasionally, Ive seen programs that start up by displaying a GetFile dialog, without specifying that the application is looking for a paint type file (for example). Well, I dont keep my paint files in the same folder as the applications so I wouldnt know which type of file was being requested. My suggested solution is to display another dialog above or below the GetFile dialog which explains what kind of info is being requested. This is one way to make the program much more user usable.
The ZBasic Quick ZFinder demo may spark some more ideas to help your next programming project. The listing is included at the end of this column.
MORE FILE READING
The next program was derived from the READ FILE demo on page E-126 of the ZBasic manual. Actually the only part of the demo that is used here is the method of reading a paint file. For more information on reading paint files I refer you to my column in September 1985 (gee, that was 2 years ago). Using READ FILE we read 720 lines with 72 bytes per line. The BLOCKMOVE statement is used to move the paint picture into a reserved space in memory. Some of the variables used in the 1st example on page E-126 are:
X%(1),71 -> represents a single line (72 chars) of the paint picture.
X$(719) -> 720 lines of the paint picture.
FL& -> File length (does not include pattern header).
The toolbox routine UNPACKBITS is used to decompress the paint file a line at a time. The BLOCKMOVE statement moves the unpacked picture for safe keeping until the entire file is unpacked. Be sure that this routine is not used between Event ON statements and Event OFF as it will slow down a lot. That is because the Event statements cause the compiler to put GETNEXTEVENT calls at the beginning of each basic line. This routine works about 10 times faster without GETNEXTEVENT traps.
The are a few side effects of the Event loop which should be explained. For some reason the PUT statement didnt work properly outside of the event loop. Originally, I had put the initial PUT statement in the subroutine that reads the paint file. This caused the picture to be erase by some refreshing of the output window. But when placed in the loop, the refreshing recognized that there was a change. I cant explain why this happened. Perhaps, Andy or Michael Gariepy of Zedcor could explain this one sometime.
Credit for the cursors and scrolling go to Lee Bass of West Covina, CA. (Thanks Lee). Lees original program had no menu and no event loop. Really, the event loop should be short as possible (this one is kind of long), but since Lee implemented the mouse functions without an event loop thats how it stayed. I would prefer that ON MOUSE be used to detect any mouse movements and remove all the appropriate statements from the event loop. If you are just learning you may want to try modifying the program by implementing ON MOUSE GOSUB type structure to the program. Lees cursors were created with ResEdit and converted to RMaker files to make it easier for you to type in these programs if you are so inclined. The window was divided in to thirds horizontally and thirds vertically to give 9 sections for the nine different cursors. During the main event loop the mouse is read to determine its position and the cursor is modified according to which of the window sections the coordinates fall. Pretty simple but maybe you have a better way. If you have any suggestions feel free to send them in.
This program requires that you use RMaker to create the cursor resources. Since the current version of RMaker doesnt work well with HFS you should move the compiled application and RMaker out on to the desktop and run it from there. The RMaker source file can be in any folder. RMaker appends the cursor resources to the application file only if you have named the application the same as the name in the first line of the RMaker source file. If you cant find RMaker (distributed with ZBasic) or dont like it (or just plain dont understand it) you may want to create your cursors with ResEdit. The cursor resource ID numbers are :
257-> up arrow
258-> down arrow
259-> left arrow
260-> right arrow
261-> top/left arrow
262-> top/right arrow
263-> bottom/right arrow
264-> bottom/left arrow
To use ResEdit, create a new CURS resource for each ID number and either draw the cursor with its mask for each of the ID numbers above or use the RMaker listing to type data directly into the resource. Open the CURS resource as a general type and type the hex code directly into the window. You can check your work by opening the resource again as a cursor type and editing as needed. This is a good way to make your own custom cursors too. I recommend using resources whenever possible with your compiled applications because it makes it much easier to modify things later if you have to change anything. The cursor editor in ResEdit will do what you need.
Well, the column section is short this month, but youve got two programs to play with. Would you like to see a column about a particular subject? Just ask! Now that ZBasic is going strong there is such a large amount of material to discuss that it is difficult to know where to begin. Your preferences are welcome.
Other related MacTutor columns: Random Access Files Sept. 1986; Reading Paint Files, Sept. 1985; On Fonts & Cursors, Oct. 1986; Reading MacPaint Files, May 1987; Scrolling in ZBasic, April 1987. Some of these and others are also available in Best of MacTutor, Vol. 1 book and The Complete MacTutor, Vol. 2 book.
An unrelated note: Among the numerous bugs in the MS Compiler which have come upon us here is another interesting one:
INPUT;Enter a number to print : ,N
This program gives a system BOMB when the binary version is used. With the decimal version everything works fine. The output is:
Enter a number to print :1E-3 1.000099E-03
Enter a number to print :1E-4 .0001001
Enter a number to print :1E-5 .0000101
Enter a number to print :1E-6 .0000011
Enter a number to print :1E-7 .0000001
Enter a number to print :1E-8 BOMB!!!!
There has been no new response from Microsoft regarding the compiler. I still highly recommend ZBasic 4.0. Microsoft, are you listening??
REM ZBasic Quick Finder
REM ©MacTutor and Zedcor, 1987
REM Adapted from E-159 of ZBasic Manual
REM By Dave Kelly
REM This program allows you to Execute other applications
REM and return with all variables as they were
REM THIS APPLICATION MUST BE IN SYSTEM FOLDER
REM TO WORK WITH HFS
REM NOTE: ALL VARIABLES ARE RESTORED UPON RETURN
REM TO THIS PROGRAM
GOSUB Get Last Variable pointer
REM Check to see if returning from another application
REM If returning from an application
LONG IF LOF(1,1)>10
REM reload all the variables
READ FILE #1,VARPTR(FirstVariable%), LastVariable&-VARPTR(FirstVariable%)
PRINTLast Application: ;:PRINT Application$
PRINTElapsed Time: ;TIMER-StartTime&; Seconds
PRINT SPC(5);Welcome to Macintosh.
FOR I=1 TO SysLength
REM SystemFinder$ is the name of the original Startup appl.
PRINTPlease select an application to run.
REM Moves This Applications filename to
REM FinderName global variable
FOR I=0 TO 15: REM filename MUST be less than 16
REM *** Get name of application to execute
REM APPL=any executable file
REM Last variable used as dummy for READ and WRITE
LONG IF Application$=
REM Restore Finder filename as desktop start-up
FOR I=1 TO LEN(SystemFinder$)+1
POKE FinderName&+I, PEEK(VARPTR(SystemFinder$)+I)
RECORD#1,0,0:REM Reset file pointer to file begin
REM *** Save Variables before executing Application
WRITE FILE #1,VARPTR(FirstVariable%), LastVariable&-VARPTR(FirstVariable%)
REM Execute Application now...
Get Last Variable pointer
REM Paint Scroll Demo
REM ©Mactutor 1987
REM By Dave Kelly and Lee Bass
REM Thanks Lee!!
REM Adapted from E-126 of ZBasic manual
DIM X%(1),71 X$(719):X%(0)=576:X%(1)=720
ScrollInc=20:Increment for moving picture
MENU 1,1,1,Open /O
ON MENU GOSUB MenuEvent:MENU ON:MOUSE ON
Main Program Loop
IF New=1 THEN PUT (-XChange,-YChange),X%(0), PSET:New=0
LONG IF Horizpos>0 AND Horizpos<380 AND Vertpos>0 AND Vertpos<230' in
LONG IF Vertpos>153:REM bottom section
IF Horizpos<127 THEN CursorNumber=264
IF Horizpos>=127 AND Horizpos<=253 THEN CursorNumber=258
IF Horizpos>253 THEN CursorNumber=263
LONG IF Vertpos>=77 AND Vertpos<=153
REM middle section
IF Horizpos<127 THEN CursorNumber=259
IF Horizpos>=127 AND Horizpos<=253 THEN CursorNumber=265
IF Horizpos>253 THEN CursorNumber=260
LONG IF Vertpos<77:REM top section
IF Horizpos<127 THEN CursorNumber=261
IF Horizpos>=127 AND Horizpos<=253 THEN CursorNumber=257
IF Horizpos>253 THEN CursorNumber=262
LONG IF MouseEvent<0
REM cursor down change xx,yy to move picture
IF CursorNumber=259 THEN XChange=XChange-ScrollInc
IF CursorNumber=260 THEN XChange=XChange+ScrollInc
IF CursorNumber=257 THEN YChange=YChange-ScrollInc
IF CursorNumber=258 THEN YChange=YChange+ScrollInc
IF CursorNumber=261 THEN XChange=XChange- ScrollInc:YChange=YChange-ScrollInc
IF CursorNumber=262 THEN XChange=XChange+ScrollInc:YChange=YChange-ScrollInc
IF CursorNumber=263 THEN XChange=XChange+ScrollInc:YChange=YChange+ScrollInc
IF CursorNumber=264 THEN XChange=XChange- ScrollInc:YChange
IF YChange<0 THEN YChange=0
IF XChange<0 THEN XChange=0
IF YChange>720-230 THEN YChange=720-230
IF XChange>576-380 THEN XChange=576-380
CURSOR 0:CursorNumber=0:REM Not in our window
MENU OFF:MOUSE OFF
IF Menuitem=1 THEN GOSUB OpenFile
IF Menuitem=2 THEN END
PRINT SPC(5);Please Select a Paint File to View;
IF F$= THEN CALL SHOWWINDOW(Wptr&):RETURN
PRINT Now Loading ;F$;
REM Read in the Paint File
OPENI,1,F$,1,V% : S&=LOF(1):REM Set length of Paint File
FL&=S&-512:CURSOR 4:RECORD #1,512
FOR I = 1 TO 720:REM Lines in the Paint Picture
LONG IF N>180
IF N>FL& THEN NX=FL& ELSE NX=N
READ FILE #1,A&+256-N,NX:FL&=FL&-NX
WINDOW CLOSE 2
;RMaker File for Paint Scroll Program
0080 0140 0220 0410 0808 1004 2002 7E3F 0220 0220 0220 0220 0220 0220
0220 03E0 0080 01C0 03E0 07F0 0FF8 1FFC 3FFE 7FFF 03E0 03E0 03E0 03E0
03E0 03E0 03E0 03E0 0000 0008
03E0 0220 0220 0220 0220 0220 0220 0220 7E3F 2002 1004 0808 0410 0220
0140 0080 03E0 03E0 03E0 03E0 03E0 03E0 03E0 03E0 7FFF 3FFE 1FFC 0FF8
07F0 03E0 01C0 0080 000F 0008
0100 0300 0500 0900 1100 21FF 4001 8001 4001 21FF 1100 0900 0500 0300
0100 0000 0100 0300 0700 0F00 1F00 3FFF 7FFF FFFF 7FFF 3FFF 1F00 0F00
0700 0300 0100 0000 0007 0000
0080 00C0 00A0 0090 0088 FF84 8002 8001 8002 FF84 0088 0090 00A0 00C0
0080 0000 0080 00C0 00E0 00F0 00F8 FFFC FFFE FFFF FFFE FFFC 00F8 00F0
00E0 00C0 0080 0000 0007 000F
FFC0 8080 8100 8200 8100 8080 9040 A820 C410 8220 0140 0080 0000 0000
0000 0000 FFC0 FF80 FF00 FE00 FF00 FF80 FFC0 EFE0 C7F0 83E0 01C0 0080
0000 0000 0000 0000 0000 0000
07FE 0202 0102 0082 0102 0202 0412 082A 1046 0882 0500 0200 0000 0000
0000 0000 07FE 03FE 01FE 00FE 01FE 03FE 07FE 0FEE 1FC6 0F82 0700 0200
0000 0000 0000 0000 0000 000E
0000 0000 0000 0000 0200 0500 0882 1046 082A 0412 0202 0102 0082 0102
0202 07FE 0000 0000 0000 0000 0200 0700 0F82 1FC6 0FEE 07FE 03FE 01FE
00FE 01FE 03FE 07FE 000F 000F
0000 0000 0000 0000 0080 0140 8220 C410 A820 9040 8080 8100 8200 8100
8080 FFC0 0000 0000 0000 0000 0080 01C0 83E0 C7F0 EFE0 FFC0 FF80 FF00
FE00 FF00 FF80 FFC0 000F 0000
0000 0080 01C0 03E0 0080 0080 1004 3086 7DDF 3086 1004 0080 0080 03E0
01C0 0080 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0008 0008