'===========================================================================
' Subject: FILE BROWSER                       Date: 05-25-96 (00:00)       
'  Author: Dave Gjessing                      Code: QB, QBasic, PDS        
'  Origin: dgjess@freenet.columbus.oh.us    Packet: TEXT.ABC
'===========================================================================
'Needed a subroutine to replace -> SHELL "type [filespec] | more"
'BF seems to work pretty well. It writes a copy of the text file to a
'temporary structured file, which can then be jumped around in with
'ease and speed.

'Please note that this is mainly intended as a subroutine to deal with
'text files from known sources (in my case, ones created by the program
'using the sub). I have run into unexpected problems with certain text
'files when using BF as a stand-alone program. This particular version
'will (for instance) fly off the handle at a CHR$(12)... <G>. But, as
'long as you know what files BF will run into, it seems to be pretty
'stable.

'Shameless plug: BF is incorporated in my program ShareCon, which can be
'found at http://www.simtel.net/pub/simtelnet/msdos/database/sc1-2e.zip
'end of plug

'define data type for temporary file...

TYPE txline
ln AS STRING * 80
END TYPE
DIM text AS txline

filename$ = COMMAND$

IF filename$ = "" OR filename$ = "?" OR filename$ = "/?" THEN
PRINT "(B)rowse (F)ile.BAS    Dave Gjessing 5/25/96"
PRINT "A smooth-scrolling text file viewer subroutine (or complete program"
PRINT "as presented here)."
PRINT "BF can only handle lines 80 characters in length (or less)."
PRINT
PRINT "to use, type -> BF [filespec]"
END
END IF

ON ERROR GOTO fail  'it is assumed that error will be bad file name
OPEN filename$ FOR INPUT AS #1

spread = 23     'pass the spread as an argument when using BF as a subroutine

low = 1
high = spread
counter = 1
tl = 1

CLS     'if no error, clear the screen and begin

COLOR 2, 0
LOCATE 25, 1: PRINT CHR$(4); " PGUP - PGDN - ARROWS - HOME - END - ESC "; CHR$(4);
COLOR 15, 0
PRINT " "; RIGHT$(filename$, 35); " ";
COLOR 7, 0

OPEN "temp.$$$" FOR RANDOM AS #2 LEN = LEN(text)

DO UNTIL EOF(1)             'copy file 1 (a plain ASCII text file)
LINE INPUT #1, line$        'into a nice neat structured file for
text.ln = line$             'random access (file 2)
PUT #2, counter, text
counter = counter + 1
LOOP

CLOSE 1     'done with actual file we want to look at (browse temp file)

size = LOF(2) / LEN(text)   'find size of file

again:      'display selected screenful of file on screen
tl = 1
IF low < 1 THEN low = 1
FOR x = low TO high
GET #2, x, text
LOCATE tl, 1: PRINT text.ln
tl = tl + 1
NEXT x
tl = 1

'move around within the temporary file...

DO
op$ = INKEY$
op$ = UCASE$(op$)
  
   IF op$ = CHR$(0) + CHR$(71) THEN 'home key
   low = 1
   high = spread
   GOTO again
   END IF

   IF op$ = CHR$(0) + CHR$(79) THEN 'end key
   high = size
   low = (high - spread) + 1
   GOTO again
   END IF

   IF op$ = CHR$(0) + CHR$(81) THEN 'page down key
   low = low + spread
   high = high + spread
    IF high > size THEN
    high = size
    low = (high - spread) + 1
    END IF
   GOTO again
   END IF
  
   IF op$ = CHR$(0) + CHR$(73) THEN 'page up key
   low = low - spread
   high = high - spread
      IF low < 1 THEN low = 1: high = (low + spread) - 1
   GOTO again
   END IF

   IF op$ = CHR$(0) + "P" THEN 'down arrow
   low = low + 1
   high = high + 1
   GOTO again
   END IF

   IF op$ = CHR$(0) + "H" THEN 'up arrow
   low = low - 1
   high = high - 1
      IF low < 1 THEN low = 1: high = spread
   GOTO again
   END IF
   
   IF op$ = CHR$(27) THEN           'escape key
   CLOSE
   KILL "temp.$$$"
   CLS
   END
   END IF

LOOP

fail:       'report expected error - don't clear the screen
PRINT
PRINT "file -> "; filename$; " not found"
END

