home *** CD-ROM | disk | FTP | other *** search
Modula Implementation | 1987-06-04 | 4.4 KB | 168 lines |
- (*
-
- This module implements fast text I/O. In the
- first version only reading is implemented.
-
- Created: 3/26/87 by Richie Bielak
-
- Modified:
-
- This program is PD. It can be freely copied and modified,
- but please leave my name in....Thanks
-
- *)
- IMPLEMENTATION MODULE FastTextIO;
-
-
- FROM SYSTEM IMPORT NULL, ADR, ADDRESS, TSIZE;
- FROM Memory IMPORT AllocMem, FreeMem, MemPublic, MemReqSet;
- FROM DOSFiles IMPORT Open, Close, Read, ModeOldFile, FileHandle;
- FROM DOSLibrary IMPORT DOSName, DOSBase;
- FROM Libraries IMPORT OpenLibrary;
-
- CONST
- BuffSize = 1024;
- EOL = 12C; (* LF ends a line *)
-
- TYPE
- FastFile = POINTER TO FFileDescriptor;
- FFileDescriptor = RECORD
- indx : CARDINAL;
- count : LONGINT;
- buff : ARRAY [0..BuffSize-1] OF CHAR;
- f : FileHandle;
- EOF : BOOLEAN;
- END;
-
-
- (* +++++++++++++++++++++++++++++++++++++++ *)
- (* Read data from the file into the buffer *)
- PROCEDURE FillBuffer (F : FastFile);
- BEGIN
- WITH F^ DO
- count := Read (f, ADR (buff), BuffSize);
- IF count <= 0 THEN
- EOF := TRUE
- ELSE
- indx := 0;
- END
- END;
- END FillBuffer;
-
- (* +++++++++++++++++++++++++++++++++++++ *)
- (* Open a fast file. Make sure the file *)
- (* exists first. *)
- PROCEDURE OpenFF (VAR F : FastFile; name : ARRAY OF CHAR;
- VAR suc : BOOLEAN);
- VAR tf : FileHandle;
- BEGIN
- suc := TRUE;
- tf := Open (name, ModeOldFile);
- IF tf = 0 THEN suc := FALSE
- ELSE
- (* OK, file is there, allocate a descriptor *)
- F := AllocMem (TSIZE (FFileDescriptor), MemReqSet{MemPublic});
- (* If allocation failed, clean up *)
- IF F = NULL THEN
- suc := FALSE;
- Close (tf);
- (* Now set up the descriptor *)
- ELSE
- WITH F^ DO
- indx := 0; count := 0;
- f := tf; EOF := FALSE;
- END;
- END
- END
- END OpenFF;
-
-
- (* +++++++++++++++++++++++++++++++++++++ *)
- PROCEDURE CloseFF (VAR F : FastFile);
- BEGIN
- (* Close the file *)
- Close (F^.f);
- FreeMem (F, TSIZE (FFileDescriptor));
- END CloseFF;
-
-
-
- (* +++++++++++++++++++++++++++++++++++++ *)
- PROCEDURE ReadLine (F : FastFile; VAR OutBuff : ARRAY OF CHAR);
- VAR dindx : CARDINAL;
- BEGIN
- WITH F^ DO
- dindx := 0;
- (* Move a line of data into the output string *)
- LOOP
- (* First test for all the wierd end conditions *)
- (* If buffer emptied, re-fill it *)
- IF count = 0 THEN
- FillBuffer (F);
- IF EOF THEN EXIT END; (* End-of-file *)
- END;
- (* EOL we're done *)
- IF buff[indx] = EOL THEN EXIT END;
- (* End of destination array *)
- IF dindx > HIGH (OutBuff) THEN EXIT END;
- (* Now we can move data *)
- OutBuff [dindx] := buff [indx];
- INC (indx); INC (dindx);
- DEC (count);
- END; (* LOOP *)
- (* Skip past EOL *)
- IF buff [indx] = EOL THEN
- DEC(count); INC (indx);
- END;
- (* If possible, mark the end of the string *)
- IF dindx <= HIGH (OutBuff) THEN OutBuff[dindx] := 0C END;
- END
- END ReadLine;
-
- (* +++++++++++++++++++++++++++++++++++++ *)
- PROCEDURE EofFF (F : FastFile) : BOOLEAN;
- BEGIN
- RETURN F^.EOF
- END EofFF;
-
- (* +++++++++++++++++++++++++++++++++++++ *)
- PROCEDURE SkipOneLine (F : FastFile);
- BEGIN
- WITH F^ DO
- LOOP
- (* First test for all the wierd end conditions *)
- (* If buffer emptied, re-fill it *)
- IF count = 0 THEN
- FillBuffer (F);
- IF EOF THEN EXIT END; (* End-of-file *)
- END;
- (* EOL we're done, but skip past EOL *)
- IF buff[indx] = EOL THEN
- INC (indx); DEC (count);
- EXIT
- END;
- INC (indx); DEC (count); (* Not EOL, continue on *)
- END; (* LOOP *)
- END;
- END SkipOneLine;
-
- (* +++++++++++++++++++++++++++++++++++++ *)
- PROCEDURE SkipLines (F : FastFile; lcount : CARDINAL);
- BEGIN
- WITH F^ DO
- FOR lcount := lcount TO 1 BY -1 DO
- SkipOneLine (F);
- (* If we hit EOF, before all the lines are *)
- (* skipped just return. *)
- IF EOF THEN RETURN END;
- END;
- END;
- END SkipLines;
-
- BEGIN
- (* Make sure that DOS library is opened *)
- IF DOSBase = NULL THEN
- DOSBase := OpenLibrary (DOSName, 0);
- END;
- END FastTextIO.
-