' Fast Indexed Help indexer
' This program creates an index to the input .TXT file
' Written: April 2nd, 1990
' Modified: November 1991
' Language used: QuickBASIC 4.5
' Author: George Toft

'============== Type Definition Section ===============
DEFSNG A-Z
DEFINT D
   
'============== Constant Definition Section ===========
CONST FALSE = 0, TRUE = NOT FALSE

'============== Subroutine Definition Section =========
DECLARE SUB DRAWWINDOW (Top AS INTEGER, Left AS INTEGER, Bottom
AS INTEGER, Right AS INTEGER, Border AS INTEGER, LineStyle AS
INTEGER, Bkgnd AS INTEGER)
DECLARE SUB INC (Arg AS SINGLE)
DECLARE SUB PRINTSTRING (Y AS INTEGER, X AS INTEGER, text AS
STRING)
DECLARE SUB SetAttrib (Top AS INTEGER, Left AS INTEGER, Bottom AS
INTEGER, Right AS INTEGER, Attrib AS INTEGER)

'============== Execution Section =====================
ErrorInFile = FALSE

'--- Get name of file to index ---
CLS
FileToIndex$ = LEFT$(COMMAND$, INSTR(COMMAND$ + " ", " "))

'--- Verify file exists ---
ON ERROR GOTO FileError
OPEN FileToIndex$ + ".TXT" FOR INPUT AS 1
CLOSE 1
ON ERROR GOTO 0

'--- If file exists, then run program; else quit
IF (NOT ErrorInFile) THEN
    '--- Create viewing windows ---
    CLS
    DRAWWINDOW 9, 4, 11, 10, &H7, DOUBLELINE, &H7
    DRAWWINDOW 9, 19, 11, 27, &H7, DOUBLELINE, &H7
    DRAWWINDOW 14, 4, 16, 76, &H7, DOUBLELINE, &H7
    '--- open text file for input ---
    OPEN FileToIndex$ + ".TXT" FOR INPUT AS 1
        '--- create the index file ---
        OPEN FileToIndex$ + ".IDX" FOR RANDOM AS 2 LEN = 5
        FIELD #2, 5 AS CountField$
            ' The first record in the index will always
            ' point to the first byte in the text file,
            ' so make record #1 equal to 1 and start
            ' processing with record 2
            LSET CountField$ = MKS$(1)
            PUT #2, 1
            record = 2
            Count = 0
            ' Process text until the end of the text file
            ' is reached.
            DO WHILE NOT EOF(1)
                LINE INPUT #1, line$
                ' Count keeps a running total of bytes read
                ' from the text file.  By saving Count
                ' after reading each line in the text file,
                ' Count point to the beginning of the next
                ' line in the text file, and an index to
                ' the text file is created.  Add two to
                ' Count to account for CR+LF at the end
                ' of the input line.
                Count = Count + LEN(line$) + 2
                ' This record points to the beginning of
                ' the next line of text (or the end of this
                ' line of text - 1).
                LSET CountField$ = MKS$(Count)
                PUT #2, record
                ' Display the current record number, the
                ' index, abd the first 60 characters of
                ' the input line.
                PRINTSTRING 10, 5, STR$(record)
                PRINTSTRING 10, 20, STR$(Count)
                PRINTSTRING 15, 5, SPACE$(60)
                PRINTSTRING 15, 5, LEFT$(line$, 60)
                ' Advance to the next record and
                ' continue processing.
                INC record
            LOOP
        CLOSE 2
    CLOSE 1
    CLS
    PRINT "Indexing complete:"
    PRINT "Total number of records ="; record
    PRINT "Total number of bytes in help file ="; Count - 1
    PRINT
ELSE
    PRINT "Error in filename to be indexed ("; FileToIndex$;
".TXT)."
    PRINT "Ensure you enter the filename without the "
    PRINT "extension when you invoke INDEX.  For example:"
    PRINT "type 'INDEX HELP' to index HELP.TXT and create"
    PRINT "the index HELP.IDX"
    PRINT "Index terminated in error."
END IF
END

'============== Error Trap section ====================
FileError:
    ErrorInFile = TRUE
    RESUME NEXT
DEFINT A-C, E-Z
'$Page
'
SUB DRAWWINDOW (Top AS INTEGER, Left AS INTEGER, Bottom AS
INTEGER, Right AS INTEGER, Border AS INTEGER, LineStyle AS
INTEGER, Bkgnd AS INTEGER)

    SetAttrib Top, Left, Bottom, Right, Border
    SELECT CASE LineStyle
        CASE SingleLine
            PRINTSTRING Top, Left, "" + STRING$(Right - Left -
1, "") + ""
            FOR i = Top + 1 TO Bottom - 1
                PRINTSTRING i, Left, "" + SPACE$(Right - Left -
1) + ""
            NEXT i
            PRINTSTRING Bottom, Left, "" + STRING$(Right - Left
- 1, "") + ""
      
        CASE DOUBLELINE
            PRINTSTRING Top, Left, "" + STRING$(Right - Left -
1, "") + ""
            FOR i = Top + 1 TO Bottom - 1
                PRINTSTRING i, Left, "" + SPACE$(Right - Left -
1) + ""
            NEXT i
            PRINTSTRING Bottom, Left, "" + STRING$(Right - Left
- 1, "") + ""

    END SELECT
    SetAttrib Top + 1, Left + 1, Bottom - 1, Right - 1, Bkgnd
END SUB' DrawWindow

DEFSNG A-Z
'$Page
'
SUB INC (Arg AS SINGLE)
    Arg = Arg + 1
END SUB' IINC

DEFINT A-Z
'$Page
'
'
SUB PRINTCENTER (text AS STRING)
    ' prints text in center of screen at current row
    PRINTSTRING CSRLIN, 41 + POS(0) - LEN(text) \ 2, text
    PRINT
END SUB' PRINTCENTER

'$Page
'
SUB PRINTSTRING (Y AS INTEGER, X AS INTEGER, text AS STRING)
    DEF SEG = &HB800
    FOR i = 0 TO LEN(text) - 1
        POKE (Y * 80 + X + i - 1) * 2, ASC(MID$(text, i + 1, 1))
    NEXT i
END SUB' PrintString

'$Page
'
SUB SetAttrib (Top AS INTEGER, Left AS INTEGER, Bottom AS
INTEGER, Right AS INTEGER, Attrib AS INTEGER)
    DEF SEG = &HB800
    FOR i = Top TO Bottom
        FOR j = Left TO Right
            POKE (i * 80 + j - 1) * 2 + 1, Attrib
        NEXT j
    NEXT i
END SUB' SetAttrib


