	REM ------FILE BIBBLD.BAS

	REM  BIBLIO BUILD/MODIFY ROUTINE
	COMMON KEYWD$(1),LIBNAME$,RLEN%,TRUE%,AUTH.LEN%,TITL.LEN%
	COMMON MAXBIB%,MAXDESC%,MAXDEF%,MAXKEYS%,MAXCON%,CONCEPT$(1)
	COMMON ISS.LEN%,CLEAR$,ERR%,JOUR.LEN%,KWD.LEN%,CMD$(1)
	COMMON LFT%(1),RGHT%(1),CON.KEY%(2),CON.RATE(2),QUERY$,L%
	
	REM  CONVERT INTEGER TO TWO ASCII HEX
	DEF FN.INT.TWO$(DUM%)
	HI%=DUM%/16 : LO%=DUM%-16*HI%
	IF HI%<10 THEN HI%=HI%+48 ELSE HI%=HI%+55
	IF LO%<10 THEN LO%=LO%+48 ELSE LO%=LO%+55
	FN.INT.TWO$=CHR$(HI%) + CHR$(LO%)
	RETURN
	FEND

	REM CONVERT ONE DIGIT REAL TO ASCII HEX
	DEF FN.REALONE$(DUM)
	ONE%=INT%(10.0*DUM)
	IF ONE%<10 THEN ONE%=ONE%+48 ELSE ONE%=ONE%+55
	FN.REALONE$=CHR$(ONE%)
	RETURN
	FEND

	GOSUB 6E1
	PRINT CLEAR$:CHAIN "BIBLIO"
	STOP

6E1     REM BUILD/MODIFY BIBLIOGRAPHY FILE
	PRINT CLEAR$
	PRINT TAB(12); "----------BIBLIO BUILD/MODIFY----------":PRINT
	REM IF NO VOCABULARY FILE, EXIT TO MENU
	IF END #1 THEN 6.1E1
	OPEN LIBNAME$+".VOC" AS 1
	IF SIZE(LIBNAME$+".VOC")=0 THEN DELETE 1 : GOTO 6.1E1
	VOC.LEN%=1

	REM OTHERWISE READ VOCABULARY
	IF END #1 THEN 6.2E1
	WHILE TRUE%
  	READ #1;KEYWD$(VOC.LEN%)
  	VOC.LEN%=VOC.LEN%+1
  	WEND

6.2E1   CLOSE 1
	VOC.LEN%=VOC.LEN%-1

	REM IF NO BIBLIO FILE, GO BUILD ONE
	IF END #2 THEN 6.3E1
	OPEN LIBNAME$+".BIB" RECL RLEN% AS 2
	IF SIZE(LIBNAME$+".BIB")=0 THEN DELETE 2 : GOTO 6.3E1

	REM OTHERWISE WE WANT TO ADD OR DELETE
	INPUT "DO YOU WISH TO ADD OR DELETE ARTICLES (A/D)? ";LINE ANS$
	ANS$=UCASE$(LEFT$(ANS$,1))
	IF ANS$="D" THEN 4E1

	REM FIND LAST SECTOR AND READ BIBLIO TO EOF
	IF END #2 THEN 6.37E1
	READ #2,1;LINE BUFF$
	BIB.LEN%=VAL(BUFF$)
	READ #2,BIB.LEN%-2;
	WHILE TRUE%
  	READ #2;BUFF$
  	WEND
	GOTO 6.37E1

6.3E1   REM BUILD IT
	BIB.LEN%=0
	CREATE LIBNAME$+".BIB" RECL RLEN% AS 2
	PRINT USING "######";#2,1;BIB.LEN%

	REM GET ENTRIES FOR BIBLIO AND WRITE TO FILE

6.37E1  BLANK$="                                             "
	PRINT "PRESSING RETURN IN RESPONSE TO THE PROMPT 'AUTHOR'"
	PRINT "TERMINATES THIS ROUTINE. ":PRINT
	WHILE TRUE%
  	IF BIB.LEN%>MAXBIB% THEN 6.34E1
  	BUFF$=""
  	INPUT "AUTHOR :  "; LINE AUTHOR$
  	IF LEN(AUTHOR$)=0 THEN PRINT USING "&"; #2;CHR$(26):GOTO 6.32E1
  	INPUT "TITLE  :  "; LINE TITLE$
  	INPUT "JOURNAL:  "; LINE JOURNAL$
  	INPUT "ISSUE  :  "; LINE ISSUE$
  	AUTHOR$=LEFT$(AUTHOR$+BLANK$,AUTH.LEN%)
        TITLE$=LEFT$(TITLE$+BLANK$,TITL.LEN%)
  	JOURNAL$=LEFT$(JOURNAL$+BLANK$,JOUR.LEN%)
  	ISSUE$=LEFT$(ISSUE$+BLANK$,ISS.LEN%)
  
6.33E1  BUFF$=AUTHOR$+TITLE$+JOURNAL$+ISSUE$
  	PRINT "ENTER KEYWORDS AND RATINGS, I.E., KEYWORD1 0.5 KEYWORD2 0.6"

	REM GET KEYWORDS AND RATINGS--CODE INTO ASCII HEX
  	CMD$="" : KEY%=0 : ERR%=0
  	INPUT ">";LINE CMD$
  	IF (LEN(CMD$)=0) THEN 6.31E1
  	SW%=-1 : I%=1 : W.NUM%=0
  	WHILE (I%<LEN(CMD$))
    	IF KEY%>=MAXDESC% THEN 6.39E1
    	WHILE (MID$(CMD$,I%,1)=" ")
      	I%=I%+1
    	WEND
    	DUM$=RIGHT$(CMD$,LEN(CMD$)-I%+1)
    	IF NOT SW% THEN 6.36E1
      	W.NUM%=W.NUM%+1
      	GOSUB 19E1
      	IF ERR% THEN 6.33E1
      	BUFF$=BUFF$+FN.INT.TWO$(KB%):SW%=0
      	GOTO 6.38E1

6.36E1  CH$=LEFT$(DUM$,1)
	IF (CH$<>".") AND ((CH$<"0") OR (CH$>"9")) THEN \
           PRINT "KEYWORDS MAY NOT CONTAIN BLANKS--RE-ENTER LINE" :GOTO 6.33E1
        IF LEFT$(DUM$,1)="." THEN RAT=VAL("0"+DUM$) ELSE RAT=VAL(DUM$)
        BUFF$=BUFF$+FN.REALONE$(RAT):KEY%=KEY%+1:SW%=-1

6.38E1  WHILE (MID$(CMD$,I%,1)<>" ") AND (I%<LEN(CMD$))
        I%=I%+1
        WEND
        WEND

6.31E1  BUFF$=BUFF$+"FFF"
	PRINT USING "&"; #2; BUFF$
  	BIB.LEN%=BIB.LEN+1
  	WEND

4E1 	REM READ AND DISPLAY ARTICLES, SAVING OR DELETING AS WE GO
	R.REC%=2 : DEL%=0
	READ #2,1; LINE BUFF$
	PRINT "TO DELETE RESPOND TO THE PROMPT ' > ' WITH AT LEAST TWO"
	PRINT "DEPRESSIONS OF LB SIGN #, THEN RETURN.  DEPRESS ONLY THE"
	PRINT "RETURN TO SAVE, ENTER E THEN RETURN TO EXIT TO MENU":PRINT
	IF END #2 THEN 4.4E1
	WHILE TRUE%
  	READ #2,R.REC%; LINE BUFF$
  	PRINT MID$(BUFF$,AUTH.LEN%+TITL.LEN%+1,JOUR.LEN%);
  	PRINT TAB(JOUR.LEN%+2); MID$(BUFF$,AUTH.LEN%+1,TITL.LEN%)
  	PRINT MID$(BUFF$,AUTH.LEN%+TITL.LEN%+JOUR.LEN%+1,ISS.LEN%);
  	PRINT TAB(JOUR.LEN%+2); LEFT$(BUFF$,AUTH.LEN%):PRINT
  	INPUT "> "; LINE ANS$
  	IF UCASE$(LEFT$(ANS$,1))="E" THEN 4.4E1
  	IF LEFT$(ANS$,2)="##" THEN \
    	PRINT USING "&"; #2,R.REC%;"ZZZZZ"+RIGHT$(BUFF$,RLEN%-7): \
    	DEL%=DEL%+1:R.REC%=R.REC%+1
  	WEND

4.4E1   PRINT DEL%; " ARTICLES MARKED DELETED":CLOSE 2:GOTO 6.7E1

6.34E1  PRINT "FILE FULL--NO MORE ARTICLES CAN BE WRITTEN"

6.32E1  PRINT BIB.LEN%; " ARTICLES ON FILE"
	PRINT USING "######"; #2; BIB.LEN%
	CLOSE 2:GOTO 6.7E1

6.1E1   PRINT "NO VOCABULARY FILE NAMED ";LIBNAME$

6.7E1   INPUT "PRESS RETURN TO GO BACK TO MENU "; LINE ANS$:RETURN

6.39E1  REM TOO MANY KEYWORDS ENTERED
	KEY%=0:BUFF$=LEFT$(BUFF$,AUTH.LEN%+TITL.LEN%+JOUR.LEN%+ISS.LEN%)
	PRINT "NO MORE THAN ";MAXDESC%; " KEYWORDS PER ARTICLE CAN"
	PRINT "BE ENTERED.  PLEASE SHORTEN LIST AND RE-ENTER.":GOTO 6.33E1

19E1    REM BREAKOUT KEYWORD AND FIND ITS NUMBER
        IR%=1
	WHILE MID$(DUM$,IR%,1)<>" " 
	IR%=IR%+1 
	WEND
	WORD$=LEFT$(DUM$,IR%-1)+BLANK$:WORD$=LEFT$(WORD$,KWD.LEN%)
        WORD$=UCASE$(WORD$)

	REM BINARY SEARCH FOR WORD IN KEYWORD ARRAY
	IB%=1 : JB%=VOC.LEN%

19.1E1  KB%=(IB%+JB%)/2
	IF WORD$>KEYWD$(KB%) THEN IB%=KB%+1 ELSE JB%=KB%-1
	IF (WORD$<>KEYWD$(KB%)) AND (IB%<=JB%) THEN 19.1E1
	IF WORD$<>KEYWD$(KB%) THEN 19.3E1
	I%=IR%+I%-1:RETURN

19.3E1  PRINT USING "CANNOT FIND KEYWORD \####"; W.NUM%
        INPUT "PRESS RETURN TO RE-ENTER COMPLETE LINE "; LINE ANS$
	ERR%=-1 : RETURN
