Classic Computer Magazine Archive COMPUTE! ISSUE 61 / JUNE 1985 / PAGE 82

BASIC File Editor For Commodore

Henry A. Doenlen

Edit ASCII files in the form of numbered BASIC lines with this short utility for the Commodore 64 and VIC-20.

One of the best features of any Commodore computer is its BASIC line editor. By using the insert, delete, and cursor control keys, you can easily move the cursor anywhere on the screen and edit a BASIC program without having to retype entire lines.

Unfortunately, ASCII data files—files of characters such as those produced by many word processor or database management programs—are not so easy to edit. To change one data item in a file, you must either rerun the program that produced the file, or write another program that reads the file, makes the change you want, and writes the file back to disk or tape. Both options are time-consuming.

Disguising ASCII As BASIC

Although the Commodore BASIC editor is not designed to edit such files, you can make it do the job with a simple trick: Disguise the ASCII data as a collection of BASIC lines by adding line numbers and quotation marks. For example, enter this line:

10 "I'M REALLY ASCII DATA"

You can't execute this line in a program, of course, without getting an error. But the BASIC editor can handle it with ease, letting you add or delete characters, change the line number, or relist the line. "BASIC File Editor" uses this trick to let you edit ASCII files, adding artificial line numbers and quotation marks when it loads a file into memory, and deleting them when the file is resaved.

Type in BASIC File Editor and save it. If you are using a Commodore 64 or VIC-20 with a disk drive, enter the program exactly as it is listed. Tape users should omit lines 5, 220, and 360–387, change the 42 in line 350 to 35, and change the following lines as shown:

110 PRINT "{DOWN}LOAD FILE"   :INP
    UTF$                     :rem 232
120 CLOSE1 : OPEN1, 1, 0, F$  :rem 38
230 CLOSE1 : OPEN1, 1, 1, F$  :rem 41

Before editing an important file, you may want to practice on a test file. This five-line program makes a test file for you (tape users should change the 8 in line 10 to a 1):

10 OPEN1, 8, 1, "@0:ASCTEST, P, W"
20 PRINT#1, "THIS PROGRAM MAKES"
30 PRINT#1, "ASCII DATA LOOK"
40 PRINT#1, "LIKE BASIC LINES."
50 CLOSE1

After making the test file, load and run BASIC File Editor. The menu offers four options: You can press L to load a file, S to save a file, C to clear data from memory, or E to list your data and exit the program. To load the test file, press L, then type ASCTEST when prompted for the filename. (Disk users should then enter P to indicate that ASCTEST is stored as a program file, as indicated by the ,P in line 10.) After the main menu reappears, press E to list the data and return to immediate mode.

Editing Your Data

As you can see, the ASCTEST data is spliced onto the end of BASIC File Editor in the form of numbered program lines. The data lines can be edited like any other BASIC lines. Try making some changes. To add new data at the end of the file, enter new lines with higher line numbers. Use intermediate line numbers to insert new data between existing lines. Don't forget that the data in each line must be enclosed in quotation marks.

When you're done editing, enter RUN to reenter the File Editor program and press S to resave the file. Again you'll be prompted to enter a filename. If you are using a disk drive, do not use the same filename (ASCTEST) unless you want the revised file to replace the original file. If you use a different filename, a new file is created. Of course, if you are using tape you must also be careful to rewind the cassette if you want the old file erased, or be careful not to overwrite the old copy if you want it preserved.

Now press C to clear the data area, and L to load your new file. After it loads, press E to verify that the changes were successful. If you had not pressed C, the new file would have been appended to the data already in memory. This makes it easy to append one file to another.

You should avoid using RUN/ STOP or RUN/STOP-RESTORE to break out of the program. Always exit by pressing E at the menu, or important memory pointers will be left scrambled. If you do break out, rerun the program, then do a load followed by a clear before attempting any further editing.

When A Program Isn't A Program

For tape users, there's only one simple way to store ASCII data: as a tape data file. Hence, the tape version of the File Editor program works only with such files. However, disk users have greater flexibility in choosing a file type.

The most common format for character data storage on disk is the sequential file. Such files are easy to create: Simply OPEN 1,8,1, "filename, S,W" (the final ,S,W indicates that the file called filename is to be a Sequential file open for Writing). Then use PRINT#1 to write the desired data to the file and CLOSE 1 when finished. Such files will show up in the disk directory as SEQ. However, it's also possible to store the data in a program file. The procedure is the same as for sequential files, except that the ,S,W in the OPEN statement is changed to ,P,W (where the P indicates a program file). Otherwise, you still use PRINT# to write data to the file, as illustrated in the ASCTEST file created in the example above. There are several advantages to storing data in program file format. For example, with careful planning the program file of ASCII data can be retrieved with LOAD, which is significantly faster than using GET# or INPUT#. You may only rarely encounter ASCII data stored in program files, but the File Editor program can handle it in case you do.

Although BASIC File Editor allows you to edit ASCII data stored in program files, it does not allow you to edit BASIC programs stored in program files. While the file type is the same, all the BASIC keywords in a program are represented in the disk program file as single characters called tokens, which appear as reverse-video characters. Moreover, changes which affect the length of any program lines will cause the edited program to crash when loaded and run. Also, the File Editor can only be used to edit ASCII data files, which means it is not directly compatible with some database and word processing programs—including computed SpeedScript word processor—which store characters as their Commodore screen code values rather than as ASCII values. (SpeedScript does allow you to print an ASCII file to disk, which could then be edited.)

It is possible to use the File Editor to load ASCII data from program files and store it into sequential files, and vice versa. However, in this case the replace feature will not function properly. That is, you cannot use the same filename for the edited file if you are storing it as a different type from the original.

Customizing The Editor

BASIC File Editor works best with ordinary alphanumeric data (letters and numbers), such as you might find in a word processing file. Carriage return characters (CHR$(13)) are interpreted as separators. When BASIC File Editor finds a carriage return while loading the file, it terminates the current data line and begins a new one.

It should not be difficult to customize this program for your own particular needs. Before doing so, however, look at line 350. The FOR-NEXT statement sets up a loop that counts through the lines of the program (42 for disk or 35 for tape). If you add or delete any lines in the File Editor program, you'll need to change this value from 35 or 42 to whatever is appropriate.

BASIC File Editor

Please refer to "COMPUTEI's Guide to Typing In Programs" before entering this listing.

5 OPEN15,8,15         :rem 195
10 PRINT "{CLR}{3 SPACES}BASIC
    FILE EDITOR {DOWN}":rem 31
20 PRINT "{HOME}{2 DOWN}SELECT
    OPTION:"           :rem 59
30 PRINT " L=LOAD FILE":PRINT"
    S=SAVE FILE":PRINT" C=CLEA
   R":PRINT" E=EXIT/LIST"
                        :rem 11
40 GETA$:IFA$=""THEN40 :rem 235
50 IFA$="E"THENCLOSE15 :LIST390
   -:END               :rem 122
60 FORC=1TO3:IFA$=MID$("LSC" ,C
   ,1)THENONCGOSUB80,210, 300:G
   OTO10               :rem 201
70 NEXTC:GOTO20        :rem 190
80 POKE45, PEEK(55):POKE46,PEEK
   ( 56)-l:GOSUB340:L=PEEK(44)*
   256+PEEK(43)         :rem 11
90 IFPEEK(L)+PEEK( L+1)=0THEN11
   0                    :rem 49
100 N=PEEK(L+3)*256+PEEK( L+2):
    L=PEEK(L+1)*256+PEEK(L) :GO
    TO90               :rem 190
110 PRINT "{DOWN}LOAD FILE":INP
    UTF$:PRINT" {DOWN}PROGRAM O
    R SEQUENTIAL{2 SPACES}( P/S
    )":INPUTT$          :rem 29
120 CLOSE1:OPEN1, 8,2,F$+","+T$
    +",R" :GOSUB360:IFFL=1THEN1
    0                    :rem 6
130 A=L:T=PEEK( 46)*256+PEEK(45
    )-10               :rem 104
140 L=A:A=A+3:N=N+10:POKEA, N/2
    56:POKEA-1, N-PEEK(A)*256:A
    =A+1:POKE A,34       :rem 4
150 A=A+1 :GET#1,CS:D=ASC(C$+CH
    R$(0)) :IF D<>13ANDA<TTHENP
    OKEA,D:GOTO150      :rem 80
160 POKEA, 34:POKEA+1,0:A=A+2:P
    OKEL+1,A/256:POKEL,A-PEEK (
    L+1)*256           :rem 208
170 IFST=0ANDA<TTHEN140
                       :rem 163
180 IFA>=TTHENPRINT "OUT OF MEM
    ORY"               :rem 158
190 POKEA,0:POKEA+1 ,0:A=A+2:PO
    KE4,A/256:POKE 3,A-PEEK( 4)
    *256              :rem 155
200 GOSUB330:CLOSE 1:RETURN
                      :rem 163
210 GOSUB350:PRINT"{DOWN}SAVE
    {SPACE}FILE":INPUTF$
                       :rem 74
220 PRINT"{DOWN}PROGRAM OR SEQ
    UENTIAL{2 SPACES}(P/S)":IN
    PUTT$             :rem 143
230 CLOSE1:OPEN1,8,2,"@0:"+F$+
    ","+T$+",W":GOSUB360:IFFL=
    1THEN10            :rem 38
240 IFPEEK(A)+PEEK(A+1)=0THENC
    LOSE1:RETURN      :rem 119
250 A=A+3             :rem 180
260 A=A+1:IFPEEK(A)=0THENPRINT
    #1,"":A=A+1:GOTO240
                      :rem 126
270 IFPEEK(A)<>34THEN260
                      :rem 135
280 A=A+1:IFPEEK(A)=34THEN260
                      :rem 160
290 PRINT#1,CHR$(PEEK(A));:GOT
    O280              :rem 249
300 PRINT"CLEAR":GOSUB350:POKE
    A,0:POKEA+l,0:A=A+2
                      :rem 219
310 POKE4,A/256:POKE3,A-PEEK(4
    )*256             :rem 215
320 GOSUB3 30:RETURN  :rem 197
330 POKE46,PEEK(4):POKE45,PEEK
    (3)               :rem 172
340 POKE47,PEEK(45):POKE48,PEE
    K(46):POKE49,PEEK(45):POKE
    50,PEEK(46):RETURN:rem 242
350 A=PEEK(44)*256+PEEK(43):FO
    RN=1TO42:A=PEEK(A+l)*256+P
    EEK(A):NEXTN:RETURN
                      :rem 254
360 PRINT:POKE198,0:INPUT#15,A
    $,B$,C$,D$         :rem 42
370 IFB$="OK"THENFL=0:RETURN
                      :rem 242
380 PRINTA$" "B$" "C$" "D$" ":
    FL=1:PRINT"{DOWN}HIT ANY K
    EY" :A$=""        :rem 254
385 GETA$:IFA$=""THEN385
                       :rem 99
387 RETURN            :rem 130
390 REM====== FILE FOLLOWS ===
    ======         :rem 150