'**************************************************************************** 'LineInput is a faster alternative to LINE INPUT. It uses a 4k buffer to 'read a lot of data at once from the file and then breaks it up in to lines. 'FileName$ = the name of the file to read from, passed to it by the calling ' routine. The calling routine is responsible for making sure ' the file exists. 'LineInput$ = the line returned to the caller. Same as it would get from ' LINE INPUT. 'Done% = a flag to tell the caller that all lines have been read. ' Can also be used by the calling routine to tell this function ' to quit before the whole file is read. ' 'LineInput works by reading a long string (BigString$) and then looking 'for a LF (chr$(10)) marking the end of a line. It cuts off everything to 'the left of the LF, returns it to the caller, and keeps everything else 'ready to be called again. It assumes the LF is part of a CRLF pair and 'discards both bytes. ' 'Converted to PB3 February 27, 1993 '**************************************************************************** FUNCTION LineInput$ (FileName$, Done%) STATIC 'if calling routine sends Done% as True, it's ready to quit before the whole 'file is read, so close file and reset flags. IF ISTRUE Done% THEN CLOSE #FileNum% Reading% = 0 Done% = 0 BigString$ = "" Save$ = "" EXIT FUNCTION END IF 'if this is the first pass through this sub, set flag to show that it's been 'here, set some variables to their initial values, and open the file. The 'file will stay open until the last pass through. IF ISFALSE Reading% THEN BufferSize% = 4096 Reading% = -1 Done% = 0 LineEnd% = 0 EnterChar$ = CHR$(10) FileNum% = FREEFILE OPEN FileName$ FOR BINARY AS #FileNum% 'Remaining& = how much of the file has not been read into BigString yet. 'at this point, that means the whole file. Remaining& = LOF(FileNum%) END IF DO 'LineEnd% is the position in the string of the first LF (CHR$(10)) LineEnd% = INSTR(BigString$, EnterChar$) 'if it's zero, there are no more complete lines in this BigString. But there 'is most likely a partial line. If there is more to read (Remaining& > 0), 'then save whatever fragment is left in BigString$, read some more from the 'file, and add it to the saved fragment. IF LineEnd% > 0 THEN LineInput$ = LEFT$(BigString$, LineEnd% - 2) BigString$ = MID$(BigString$, LineEnd% + 1) EXIT LOOP 'no LineEnd% found: ELSE IF Remaining& THEN Save$ = BigString$ IF Remaining& < BufferSize% THEN BufferSize% = Remaining& 'add BigString$ to the piece left over from before, refigure remaining 'amount to be read, and start over: GET$ #FileNum%, BufferSize%, BigString$ BigString$ = Save$ + BigString$ DECR Remaining&, BufferSize% IF ISFALSE Remaining& THEN 'Some software uses a chr$(26) as an end-of-file marker. I don't need it 'so throw it away. IF RIGHT$(BigString$,1) = CHR$(26) THEN BigString$ = LEFT$(BigString$, LEN(BigString$) - 1) END IF END IF ELSE 'if there is no Remaining&, then the fragment is the last line. This could 'happen if the last line does not end with a carriage return. LineInput$ = BigString$ BigString$ = "" exit loop END IF END IF LOOP 'if its out of BigString and no more remains, set Done flag and close file. IF BigString$ = "" THEN IF ISFALSE Remaining& THEN CLOSE #FileNum% Done% = -1 Reading% = 0 END IF END IF END FUNCTION