Classic Computer Magazine Archive COMPUTE! ISSUE 34 / MARCH 1983 / PAGE 171

Apple Subroutine Capture

R. W. W. Taylor and Max Hailperin

Do you include certain favorite BASIC subroutines in program after program? The easiest way to incorporate a standard subroutine into a new program is to EXEC the code from an existing text file, as explained on page 76 of the Apple II DOS Manual. A short program is given on that page for "capturing" specified lines from a program already in memory and writing the lines as text to a sequential file for later retrieval by an EXEC command.

The main inconvenience of this particular approach is that the capture subroutine must be typed in new each time it is to be used, with details specific to the situation at hand.

This nuisance can be avoided. In fact, it is possible to create and store a master file Capture so that a user who simply types EXEC CAPTURE will be interrogated about the desired file name and line-number range, and the desired capture will then be performed without any further action by the user.

The text of Capture appears in Program 1. This text can be entered into a file by a program such as File Builder (Program 2). Note the subroutine at line 8000. The purpose of this subroutine is to allow input of arbitrary text strings, including commas, colons, and hyphens. It is a good example of the sort of subroutine that is handy to capture and maintain for re-use in other programs.

Saving To Memory

Once Capture has been stored on disk, and a program containing lines to be captured has been loaded or created, the command EXEC CAPTURE is issued. The first effect is to overlay lines 1-18 of the program in memory — lines in this range cannot be captured. These lines are then run by the RUN at the end of Capture. The user is asked to specify a name for the file to be created and two line numbers indicating the range of code to be captured. The line numbers must be entered separated by a comma.

The program then proceeds to build a file called Tempcapture, incorporating the information supplied by the user. Before ending, the program issues a command to EXEC TEMPCAPTURE. Once again, lines in the range 1-18 are overlaid, and the new lines are run. This time, the desired capture is performed, Tempcapture is deleted, and the completion of the task is announced.

Note that if the user's disk already happens to contain a text file named Tempcapture, this file will be overwritten and then deleted. An already existing text file will also be overwritten if its name is specified as the file to be created. However, if the name specified represents an existing binary, Applesoft, or integer file, a "FILE TYPE MISMATCH" message will be generated, and the process will halt without any damage to the file.

Program 1: Text For Capture File
1 REM - CAPTURE SUBROUTINE
2 CD$ - CHR$ (4): REM CONTROL D
3 Q$ - CHR$ (162): REM QUOTE CHARACTER
4 HOME : INPUT "FILE NAME TO BE CREATED? ";F$
5 VTAB 4: INPUT "LINES TO BE CAPTURED? ";L0%, L1%
6 PRINT CD$; "OPEN TEMPCAPTURE"
7 PRINT CD$; "WRITE TEMPCAPTURE"
8 PRINT "4 PRINT CD$;";Q$;"OPEN";F$ + Q$
9 PRINT "5 PRINT CD$;";Q$;"WRITE";F$ + Q$
10 PRINT "6 LIST ";L0%; "-";L1%
11 PRINT "7 PRINT CD$;";Q$;"CLOSE ";F$ + Q$
12 PRINT "8 PRINT CD$;";Q$;"DELETE TEMPCAPTURE";Q$
13 PRINT "9 HOME : PRINT "; Q$;"FILE";F$;" HAS BEEN CREATED.";Q$
14 PRINT "10 END"
15 PRINT "RUN"
16 PRINT CD$;"CLOSE TEMPCAPTURE"
17 PRINT CD$;"EXEC TEMP CAPTURE"
18 END

Program 2: EXEC File Builder

10 REM ** FILE BUILDER **
20 CD* = CHR$ (4): REM CONTROL D
30 HOME : PRINT "ENTER NAME OF FILE TO BE BUILT:"
40 PRINT : HTAB 10: INPUT F$: HTAB 10: VTAB PEEK (37): PRINT " "
50 PRINT : PRINT "INPUT LINES ONE BY ONE."
60 PRINT "TO END, JUST PRESS RETURN."
70 VTAB 9: POKE 34, 8: REM SET TOP OF TEXT WINDOW
80 PRINT CD$; "OPEN ";F$: PRINT CD$; "DELETE";F$; PRINT CD$;"OPEN "; F$
90  FOR I = 0 TO 1
100 PRINT "*": GOSUB 8000
110 IF 0 <  LEN (IN$) THEN I = 0 : PRINT CD$; "WRITE";F$ : PRINT IN$ : PRINT CD$
120 NEXT I
130 PRINT CD$; "CLOSE";F$
140 HOME : POKE 34, 0 : REM RESET TEXT WINDOW
150 PRINT "*FILE "; F$;"HAS BEEN BUILT."
160 END
8000 CALL 54572 : REM INPUT SUBROUTINE
8010 FOR B = 512 TO 751
8020 IF PEEK (B) < > 0 THEN NEXT
8030 IN$ = " "
8040 POKE PEEK (131) + 256 * PEEK (132) + 1, 0
8050 POKE PEEK (131) + 256 * PEEK (132) + 2, 2
8060 POKE PEEK (131) + 256 * PEEK (132), B - 512
8070 IN$ = MID$ (IN$, 1)
8080 RETURN