10 '	*****************************************************

	*****	   UTILITY  REPLACEMENT  PROGRAM	*****
11 '	*****						*****

	*****		by Richard Altman		*****

	*****		    2/15/90			*****
12 '	*****						*****

	*****	Copyright (c) 1990 by Richard Altman	*****

	*****************************************************
13 '
14 '
15 '	 USER SUPPORTED
16 '	 ==== =========
17 '
18 '     This program is user-supported software.  It is copyrighted and cannot
19 ' be sold for profit (without the author's express written permission),  but
20 ' it may be copied and distributed for free.
21 '
22 '     The  SHAREWARE  concept is a  distribution method  that dispenses with
23 ' heavy  marketing/advertising costs and gives the user the  opportunity  to
24 ' try a software program before buying.   Its continued existence depends on
25 ' each user paying for what he does, in fact, use.
26 '
27 '     If you find this program [REPLACE.BAS] useful, please send the $20 (or
28 ' more) registration fee directly to the author:
29 '
30 '		Richard S. Altman -- P.O. Box 4388 -- Clearlake, CA 95422
31 '
32 '      Upon registration,  you will receive a floppy diskette containing the
33 ' latest version of this program, as well as a printed manual and a COMPILED
34 ' version.  You will also receive a FREE calendar printing program and other
35 ' programs by the same author.
36 '
37 '      WHEN ORDERING,  please be sure to indicate  single or double  density
38 ' disk drive,  and the name of the program  [REPLACE.BAS].  Comments on pro-
39 ' grams are also most welcome!
40 '
41 '     Due to  possible unforeseen circumstances,  the above offer is subject
42 ' to change without notice.
43 '
44 ' **************************************************************************
45 '	SET UP VARIABLES:

	String:    A$-F$	  Integer:    G-Z


46 DEFINT G-Z:DEFSTR A-F:DIM F(24),E(500),D(8),C(8),A(12)
47 CLR=CHR$(24): CX=CHR$(23): E=CHR$(32): C=CHR$(34):CLS=CHR$(26)
48 ESC$=CHR$(27):FE=ESC$+"=":GOSUB 5020:F=STRING$(6,32):FR=STRING$(60,45)
49 FOR X=1 TO 24:F(X)=FE+CHR$(X+31)+E:NEXT:C7=CHR$(7):FS=STRING$(79,45)
50 DEF FNF(X,Y)=FE+CHR$(X+31)+CHR$(Y+31):LP=0:ON ERROR GOTO 6500
51 DEF FNTITLE$(X,M$)=FNF(X,1)+STRING$((80-LEN(M$))/2,12)+M$:WIDTH 255
52 U$=" UTILITY   REPLACEMENT  PROGRAM ":GOSUB 7000:GOSUB 80:GOTO 100
55 '
70 L$=A(X)+" to "+A(X+1):GOSUB 5050:PRINT",  ";:X=X+2:RETURN
75 PRINT CLS;FS:L$=FNF(2,23)+E+U$+E:GOSUB 5065:PRINT:PRINT FS:RETURN
80 PRINT F(5)CX:RETURN
85 '
100 '	Display Opening Screen
105 PRINT FNF(7,7)"This program will replace any portions of your program with"
110 PRINT F"anything you wish.   To use it, you must:"
115 PRINT FNF(10,10)"1.  Be sure each line of your program is less than ";
120 PRINT"245 bytes."FNF(11,10)"2.  Save your program in ";:L$="ASCII format"
125 GOSUB 5050:PRINT":  ("C"FILENAME"C",A)."
130 PRINT FNF(12,10)"3.  The Disk must have enough free space for a 2nd ";
135 PRINT"ASCII copy."FNF(13,10)"4.  Run this REPLACEMENT UTILITY."
140 L$=FNF(18,10)+"  Are you ready to run the REPLACEMENT Utility ?   (Y/N)  "
145 PRINT FNF(19,37)"~~~~~~~~~~~ ~~~~~~~"C7:GOSUB 5065:PRINT"   ";
150 GOSUB 6000:IF AK="Y" OR AK=CHR$(13) THEN 160
155 IF AK="N" THEN PRINT:PRINT:END:GOTO 150 ELSE 150
160 PRINT FNF(18,10)CX"What is the FILENAME of your program--- ";
165 INPUT FLNAME:IF FLNAME="" THEN 160
175 IF RIGHT$(FLNAME,4)<>".BAS" THEN FLNAME=FLNAME+".BAS"
180 '
200 '	Check for File's existance
210 L$=FNF(18,10)+"Checking to see if `"+FLNAME+"' exists...."
220 GOSUB 5050:PRINT"   "CX;:FOR T=1 TO 500:NEXT
230 NAME FLNAME AS FLNAME
240 L1=LEN(FLNAME):A1=FLNAME:FOR X=1 TO L1
250 IF ASC(MID$(A1,X,1))>90 THEN MID$(A1,X,1)=CHR$(ASC(MID$(A1,X,1))-32)
260 NEXT:FLNAME=A1:GOSUB 8500
270 '
300 '	Check for Long Lines
310 L$="Checking for over-long lines....":PRINT FNF(20,10)CX;:GOSUB 5050
320 PRINT"   ";:OPEN "I",1,FLNAME:L=0
330 IF EOF(1) THEN CLOSE:IF K=1 THEN 1500 ELSE PRINT C7:GOTO 400
340 L=L+1:LINE INPUT #1,A
350 IF K=1 THEN 360 ELSE IF LEN(A)>245 THEN PRINT FNF(6,7)L$;CX;F(8)
360 IF LEN(A)>245 THEN PRINT"Line "LEFT$(A,INSTR(A," "))"is"LEN(A)"bytes.":K=1
370 GOTO 330
380 '
400 '	Input NAME2
405 L$=FNF(20,10)+"The modified program will be saved under a different FILENAME.":GOSUB 5050
410 LL=L:PRINT FNF(21,10)"What will be the ";:L$="new":GOSUB 5050
415 PRINT" FILENAME--- "CLR;:INPUT FLNAME2
420 IF FLNAME2="" THEN 405
425 LX=1:LX=INSTR(FLNAME2,"."):IF LX>0 THEN 435
430 IF RIGHT$(FLNAME2,4)<>".BAS" THEN FLNAME2=FLNAME2+".BAS"
435 IF FLNAME<>FLNAME2 THEN 450
440 L$=FNF(21,10)+"  The FILENAMEs must be different.  "+C7+CX:GOSUB 5065
445 FOR T=1 TO 3000:NEXT:GOTO 405
450 L2=LEN(FLNAME2):A2=FLNAME2:FOR X=1 TO L2
455 IF ASC(MID$(A2,X,1))>90 THEN MID$(A2,X,1)=CHR$(ASC(MID$(A2,X,1))-32)
460 NEXT:FLNAME2=A2
465 '
500 '	Input Strings to Replace
505 PRINT FNF(7,7)CX"Filename:  "FLNAME"		Modified Filename:  ";
510 PRINT FLNAME2;FNF(2,66)D2$;
515 J=0:PRINT F(9)CX;C7
520 FOR M=1 TO 8:PRINT FNF(M+9,7)"Replace what?  ";
525 BX="":LINE INPUT BX:D(M)=BX:L(M)=LEN(BX)
530 IF BX="" THEN IF M=1 THEN 520 ELSE PRINT:GOTO 555
535 PRINT FNF(M+9,40)"With what?  ";:DX=""
540 LINE INPUT DX:C(M)=DX:M(M)=LEN(DX):IF M(M)>L(M) THEN J=1
545 NEXT M:PRINT FNF(20,7)"You have reached the maximum of eight changes."
550 PRINT FNF(22,7)"Press <ANY KEY> to continue.   ";:GOSUB 6000
555 PRINT FNF(19,7)CX;:L$=" Check the above CHANGES. ":GOSUB 5065
560 PRINT"    ";:L$=" Are they correct? ":GOSUB 5065
565 PRINT FNF(20,7)FR;FNF(19,57)"(Y/N)   "C7;
570 GOSUB 6000:IF AK="Y" OR AK=CHR$(13) THEN 600 ELSE IF AK<>"N" THEN 570
575 PRINT FNF(21,7)"Press <A> to ABORT `NO' answer and continue."CX
580 PRINT F"Press <RETURN> to retype above responses.   ";
585 GOSUB 6000:IF AK="A" THEN 600 ELSE IF AK=CHR$(13) THEN 515 ELSE 585
590 '
600 '	Check for NEW STRINGS longer than ORIGINAL
610 IF J=0 THEN 1000
620 L$=FNF(20,7)+"  <<< WARNING >>>  ":PRINT F(18)CX;FNF(19,7)FR"----"
630 PRINT F;:GOSUB 5065:PRINT"   Since you are replacing some parts of your"
640 PRINT F"program with ";:L$="larger parts":GOSUB 5050:L$="may"
650 PRINT",  the new lines ";:GOSUB 5050:PRINT" be longer than 245"
660 PRINT F"bytes.  If this happens, you will not be able to load the program"
670 PRINT F"back into the computer again.     (C)ontinue/ (A)bort   ";
680 GOSUB 6000:IF AK="C" THEN PRINT F(20)CX:GOTO 1000
690 IF AK="A" THEN 555 ELSE 680
700 '
1000 '	====  REPLACE  ====
1010 OPEN "I",1,FLNAME:OPEN"O",2,FLNAME2:GOSUB 80:R=0
1020 PRINT FNF(7,7)L"line";:IF L<>1 THEN PRINT"s";
1030 PRINT" left to modify.  ":L$=FNF(7,40)+"New":GOSUB 5050
1040 PRINT" Filename:  "FLNAME2:GOSUB 5025
1050 IF EOF(1) THEN Z=12:GOTO 2000
1060 LINE INPUT #1,A:FOR Q=1 TO M-1:N=1
1070 N=INSTR(N,A,D(Q)):IF N>0 THEN LQ=1:A=LEFT$(A,N-1)+C(Q)+RIGHT$(A,LEN(A)-N-L(Q)+1):N=N+M(Q):GOTO 1070
1080 NEXT Q:IF LQ=1 THEN LX=INSTR(A," "):R=R+1:E(R)=LEFT$(A,LX):LQ=0
1090 PRINT #2,A:PRINT F(10);:LX=1:LX=INSTR(A,CHR$(10))
1110 IF LX>0 AND LX<71 THEN PRINT LEFT$(A,LX)CX:GOTO 1130
1120 PRINT LEFT$(A,70)CX
1130 L=L-1:GOTO 1020
1140 '
1500 '	Some lines need to be shortened
1510 PRINT:PRINT:PRINT F;
1520 L$="  Your program has NOT been modified.   The above lines need to  "
1530 GOSUB 5065:PRINT:PRINT F;C7;
1540 L$="  be shortened.  You will have to edit them before continuing.   "
1550 GOSUB 5065:END:GOTO 555 
1560 '
1750 GOSUB 80:Z=12
1770 '
2000 '	Search is Finished/ Display Options
2010 PRINT F(Z):L$=STRING$(79,32):GOSUB 5050:PRINT C7:L$="  OPTIONS:  "
2020 GOSUB 5065:PRINT"  [D]  Display List of Changed Lines"
2025 PRINT FNF(Z+2,52)" [Q]  Quit to SYSTEM":GOSUB 5020
2030 PRINT FNF(Z+3,14)" [P]  =Print= List of Changed Lines"
2035 PRINT FNF(Z+3,52)" [L]  Load `"FLNAME2"'   ";:CLOSE
2040 GOSUB 6000:IF AK="D" OR AK=CHR$(13) THEN 3000
2050 IF AK="P" THEN 3500
2055 IF AK="L" THEN 4000
2060 IF AK="Q" THEN 4500 ELSE 2040
2070 '
3000 '	Display List
3010 GOSUB 80:GOSUB 5025:K=1:KQ=160:KK=0:NG=8:IF R<109 THEN NG=12
3015 NL=5:NF=1:KK=KK+KQ:IF R<KK THEN KK=R
3020 FOR Y=K TO KK
3025 PRINT FNF(NL,NF)E(Y);:NL=NL+1
3029 IF NL=21 THEN NL=5:NF=NF+NG
3030 NEXT Y:IF R>KK THEN K=K+KQ:GOSUB 3050:GOTO 3015
3040 Z=20:GOTO 2000
3045 '
3050 '	End of Page Subroutine
3060 L$=FNTITLE$(23,"   Press <ANY KEY> to continue.   ")
3065 GOSUB 5020:GOSUB 3070:GOSUB 80:GOTO 5025
3070 AQ=INKEY$:AK=""
3100 GOSUB 5065:TX=0
3110 AK=INKEY$:IF AK<>"" THEN RETURN ELSE TX=TX+1:IF TX<150 THEN 3110
3120 PRINT L$;:TX=0
3130 AK=INKEY$:IF AK<>"" THEN RETURN ELSE TX=TX+1:IF TX<100 THEN 3130
3140 GOTO 3100
3150 '
3500 '	Print List on Printer
3505 LP=9:GOSUB 80:GOSUB 5020
3510 PRINT FNF(9,7)"SEARCH List is being output to Printer.   ";
3515 LPRINT ESC$"N"ESC$"!":WIDTH LPRINT 80:JK=R/6
3520 LPRINT FS:LPRINT"     "FLNAME2;TAB(25)U$;TAB(62)"Date: "D2$
3525 LN=22:LPRINT"     Replacements:  ";:FOR Y=1 TO 8
3530 LN=LN+LEN(C(Y))+2:IF C(Y)="" THEN LN=LN-2:GOTO 3545
3532 IF LN<80 THEN 3540
3535 LPRINT:LN=22:LPRINT STRING$(20,32);
3540 LPRINT C(Y)"//";
3545 NEXT Y:LPRINT FS:LPRINT ESC$"E";ESC$;CHR$(34):WIDTH LPRINT 96
3550 FOR Y=1 TO JK:IF E(Y)="" THEN 3560
3555 LPRINT F;E(Y);TAB(20)E(Y+JK);TAB(34)E(Y+JK*2);TAB(48)E(Y+JK*3);
3560 LPRINT TAB(62)E(Y+JK*4);TAB(76)E(Y+JK*5)
3565 NEXT Y:LPRINT CHR$(12):GOTO 1750
3570 '
4000 '	End Program   ***   Load MODIFIED Program
4010 GOSUB 4100:L$=FNF(8,7)+"  FINISHED MODIFYING  "+C7:GOSUB 5065
4020 PRINT FNF(10,7)">>>>>  The original program: `"FLNAME"' remains on the ";
4030 PRINT"disk in its":PRINT F"ASCII format.  It has NOT been changed."
4035 PRINT FNF(14,7)">>>>>  The modified program: `"FLNAME2"' is now being ";
4040 PRINT"loaded back in.":PRINT F"You should SAVE it on the disk ";
4050 PRINT"again using MBASIC:   ";:A=FLNAME2
4055 LX=INSTR(A,"."):L$=" SAVE "+C+LEFT$(A,LX-1)+C+" ":GOSUB 5065:PRINT
4060 PRINT F"so it will load faster than the ASCII version."
4070 GOSUB 5020:CLOSE:PRINT F(18):LOAD FLNAME2:END:RUN
4080 '
4100 '	Check for Hard Copy
4110 GOSUB 80:IF LP=9 THEN RETURN
4120 L$=FNF(9,7)+"You haven't PRINTED the List of Changed Lines"+C7:GOSUB 5050
4130 PRINT"."FNF(11,7)"Are you sure you're ready to LOAD:  `"FLNAME2"'  ??   ";
4140 GOSUB 6000:IF AK="Y" THEN LP=9:GOTO 4100
4150 IF AK="N" OR AK=CHR$(13) THEN 1750 ELSE 4140
4160 ' 
4500 '	Quit to SYSTEM
4510 GOSUB 80
4520 PRINT FNF(9,7)"Do you really want to QUIT to SYSTEM ??  (Y/N)   "C7;
4530 GOSUB 6000:IF AK="N" OR AK=CHR$(13) THEN 1750
4540 IF AK="Y" THEN PRINT F(7)"End of Program":SYSTEM
4550 '
5000 '	*****	Misc. Subroutines


5005 '
5010 PRINT ESC$"B0";:RETURN:'		Inverse Video ON/OFF
5015 PRINT ESC$"C0";:RETURN:
5020 PRINT ESC$"B4";:RETURN:'		Turn Cursor ON/OFF
5025 PRINT ESC$"C4";:RETURN:
5030 PRINT ESC$"B1";:RETURN:'		Reduced Intensity ON/OFF
5035 PRINT ESC$"C1";:RETURN:
5040 PRINT ESC$"B3";:RETURN:'		Underline ON/OFF
5045 PRINT ESC$"C3";:RETURN:
5050 GOSUB 5040:PRINT L$;:GOSUB 5045:RETURN:'	Underline L$
5055 '
5060 '
5065 GOSUB 5010:PRINT L$;:GOTO 5015:'				BRIGHT Video
5070 '
6000 '	INKEY$ Subroutines
6010 K$=INKEY$:AK=""
6020 AK=INKEY$:IF AK="" THEN 6020
6030 IF ASC(AK)>96 THEN AK=CHR$(ASC(AK)-32)
6040 RETURN
6050 GOSUB 6010:IF AK<>CHR$(13) THEN 6050 ELSE RETURN
6060 '
6500 '	ERROR LINE Subroutine
6510 IF ERR=53 THEN L$=FNF(19,10)+"  ERROR:  File not found.  "+C7:GOSUB 5065:FOR T=1 TO 4000:NEXT:RESUME 160
6520 IF ERR=58 THEN RESUME 240
6530 PRINT:PRINT C7"Error"ERR"in Line"ERL:END:RUN
6540 '
7000 '	Instructions ??
7010 GOSUB 75:PRINT FNF(14,20)"Do you want instructions  (Y or N) ?  "C7;
7020 GOSUB 6000:IF AK="Y" OR AK="N" OR AK=CHR$(13) THEN 7030 ELSE 7020
7030 IF AK="Y" THEN 8000 ELSE RETURN
7040 '
8000 '	Display Instructions
8010 GOSUB 80:B="    "
8020 PRINT FNF(5,5)"It will allow you to replace any part of your program ";
8030 PRINT"with anything":PRINT B"you wish, up to eight replacements per ";
8040 PRINT"session.   For example:"FNF(8,8)"1.  You have finished writing a ";
8050 PRINT"program only to find you have":PRINT B"misspelled the word ";
8060 L$=" CHOICE ":GOSUB 5065:PRINT" as ";:L$=" CHOISE ":GOSUB 5065
8070 PRINT".   Rather than find all":PRINT B"the errors by hand, let this ";
8080 PRINT"Utility work for you."FNF(12,8)"2.  Perhaps you wish to change a ";
8090 PRINT"Variable.  You can change all":PRINT B"references to ";
8100 L$=E+C+"N1"+C+E:GOSUB 5065:PRINT" into ";:L$=E+C+"X5"+C+E:GOSUB 5065
8110 PRINT" by running this Utility."F(15)FS:AA=F+B+" Replace what?  "
8120 AB=F+F+"With what?  ":PRINT AA"CHOISE"AB"CHOICE":PRINT AA"N1    "AB"X5"
8130 PRINT AA"2056  "AB"10":PRINT AA"<ENTER>"FNF(21,5)"If the above sample ";
8140 PRINT"was real, you would be asked if corrections were"FNF(22,5);
8150 PRINT"necessary.  Then the Utility would make the requested replacements."
8160 PRINT FNF(24,5)"Press <ENTER> to continue.   ";:GOSUB 6050:GOSUB 80
8170 L$=FNF(5,5)+" << CAUTION!! >> ":GOSUB 5065:PRINT"  This program changes ";
8180 PRINT"ALL references to the":PRINT B"string you choose.  Be aware of the";
8190 PRINT" possible problems."FNF(9,5)"For example--  If you want to change ";
8200 PRINT"a Variable "C"N"C" to "C"X"C", then":PRINT B"the line:  ";
8210 L$=ESC$+"B3"+" 10 IF R=0 AND N=0 THEN 20 "+ESC$+"C3":GOSUB 5065
8220 PRINT" will be changed ....":PRINT FNF(11,11)"to:  ";:X=1
8230 L$=" 10 IF J=0 AXD X=0 THEX 20 ":GOSUB 5065:PRINT FNF(12,28)"^  ^      ^"
8240 PRINT FNF(14,5)"SUGGESTION:  If you are careful, this can be avoided.  ";
8250 PRINT"Make several":PRINT B;F"smaller changes:    Change  ";
8260 A(1)="N=":A(2)="X=":A(3)="=N":A(4)="=X":A(5)="(N)":A(6)="(X)":A(7)="N("
8270 A(8)="X(":A(9)="N1(":A(10)="X5(":A(11)="<=N":A(12)="<=X":GOSUB 70:GOSUB 70
8280 GOSUB 70:PRINT:PRINT B;F;:GOSUB 70:GOSUB 70:GOSUB 70:PRINT CHR$(8)"etc."
8290 PRINT FNF(19,5)"NOTE:  The ORIGINAL program will ";:L$="not":GOSUB 5050
8300 PRINT" be changed!!"C7;FNF(22,5)"Press <ENTER> to begin.    ";:GOTO 6050
8310 '
8500 RESTORE 8600:'	Today's Date CALCULATION   (= D2$)
8510 FOR X=1 TO 6:READ ID(X):NEXT
8520 FOR Y=3 TO 6:ID=ID(Y):GOSUB 8550:CV%(Y)=CVAL%:NEXT
8530 M$=STR$(CV%(5)):D$=STR$(CV%(6)):Y$=STR$(CV%(3))
8540 D2$=RIGHT$(M$,2)+"/"+RIGHT$(D$,2)+"/"+RIGHT$(Y$,2):RETURN
8550 OUT 32,ID:BCD%=INP(36):SX=INT(BCD%/16):S1=BCD%-16*SX:CVAL%=10*SX+S1:RETURN
8600 DATA 0,0,9,0,7,6
"+RIGHT$(Y$,2):RETURN
8550 OUT 32,ID:BCD%=INP(36):SX=INT(BCD%/16):S1=BCD%-16*SX:CVAL%=10*S