home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d079 / m2error.lha / M2Error / fast.mod < prev    next >
Encoding:
Modula Implementation  |  1987-06-04  |  4.4 KB  |  168 lines

  1. (*
  2.  
  3.         This module implements fast text I/O. In the
  4.         first version only reading is implemented.
  5.         
  6.         Created: 3/26/87 by Richie Bielak
  7.         
  8.         Modified:
  9.  
  10.         This program is PD. It can be freely copied and modified,
  11.         but please leave my name in....Thanks
  12.  
  13. *)
  14. IMPLEMENTATION MODULE FastTextIO;
  15.  
  16.  
  17. FROM SYSTEM     IMPORT NULL, ADR, ADDRESS, TSIZE;
  18. FROM Memory     IMPORT AllocMem, FreeMem, MemPublic, MemReqSet;
  19. FROM DOSFiles   IMPORT Open, Close,  Read, ModeOldFile, FileHandle;
  20. FROM DOSLibrary IMPORT DOSName, DOSBase;
  21. FROM Libraries  IMPORT OpenLibrary;
  22.  
  23. CONST
  24.       BuffSize = 1024;
  25.       EOL = 12C; (* LF ends a line *)
  26.  
  27. TYPE
  28.       FastFile = POINTER TO FFileDescriptor;
  29.       FFileDescriptor = RECORD
  30.                           indx   : CARDINAL;
  31.                           count  : LONGINT;
  32.                           buff   : ARRAY [0..BuffSize-1] OF CHAR;
  33.                           f      : FileHandle;
  34.                           EOF    : BOOLEAN;
  35.                         END;
  36.  
  37.  
  38. (* +++++++++++++++++++++++++++++++++++++++ *)   
  39. (* Read data from the file into the buffer *)
  40. PROCEDURE FillBuffer (F : FastFile);
  41.   BEGIN
  42.     WITH F^ DO
  43.       count := Read (f, ADR (buff), BuffSize);
  44.       IF count <= 0 THEN
  45.         EOF := TRUE
  46.       ELSE
  47.         indx := 0;
  48.       END
  49.     END;
  50.   END FillBuffer;
  51.  
  52. (* +++++++++++++++++++++++++++++++++++++ *)
  53. (* Open a fast file. Make sure the file  *)
  54. (* exists first.                         *)
  55. PROCEDURE OpenFF (VAR F : FastFile; name : ARRAY OF CHAR;
  56.                   VAR suc : BOOLEAN);
  57.   VAR tf : FileHandle;
  58.   BEGIN
  59.     suc := TRUE;
  60.     tf := Open (name, ModeOldFile);
  61.     IF tf = 0 THEN suc := FALSE 
  62.     ELSE
  63.       (* OK, file is there, allocate a descriptor *)
  64.       F := AllocMem (TSIZE (FFileDescriptor), MemReqSet{MemPublic});
  65.       (* If allocation failed, clean up *)
  66.       IF F = NULL THEN 
  67.         suc := FALSE;
  68.         Close (tf);
  69.       (* Now set up the descriptor *)
  70.       ELSE
  71.         WITH F^ DO
  72.           indx := 0; count := 0;
  73.           f := tf; EOF := FALSE; 
  74.         END;
  75.       END
  76.     END
  77.   END OpenFF;
  78.  
  79.    
  80. (* +++++++++++++++++++++++++++++++++++++ *)
  81. PROCEDURE CloseFF (VAR F : FastFile);
  82.   BEGIN
  83.     (* Close the file *)
  84.     Close (F^.f);
  85.     FreeMem (F, TSIZE (FFileDescriptor));
  86.   END CloseFF;
  87.  
  88.  
  89.  
  90. (* +++++++++++++++++++++++++++++++++++++ *)   
  91. PROCEDURE ReadLine (F : FastFile; VAR OutBuff : ARRAY OF CHAR);
  92.   VAR dindx : CARDINAL;
  93.   BEGIN
  94.     WITH F^ DO
  95.       dindx := 0;
  96.       (* Move a line of data into the output string *)
  97.       LOOP
  98.         (* First test for all the wierd end conditions *)
  99.         (* If buffer emptied, re-fill it *)
  100.         IF count = 0 THEN 
  101.            FillBuffer (F); 
  102.           IF EOF THEN EXIT END; (* End-of-file *)
  103.         END;
  104.         (* EOL we're done *)
  105.         IF buff[indx] = EOL THEN EXIT END;
  106.         (* End of destination array *)
  107.         IF dindx > HIGH (OutBuff) THEN EXIT END;
  108.         (* Now we can move data *)
  109.         OutBuff [dindx] := buff [indx];
  110.         INC (indx); INC (dindx); 
  111.         DEC (count);
  112.       END; (* LOOP *)
  113.       (* Skip past EOL *)
  114.       IF buff [indx] = EOL THEN 
  115.         DEC(count); INC (indx); 
  116.       END;
  117.       (* If possible, mark the end of the string *)
  118.       IF dindx <= HIGH (OutBuff) THEN OutBuff[dindx] := 0C END;
  119.     END
  120.   END ReadLine;
  121.  
  122. (* +++++++++++++++++++++++++++++++++++++ *)
  123. PROCEDURE EofFF (F : FastFile) : BOOLEAN;
  124.   BEGIN
  125.     RETURN F^.EOF
  126.   END EofFF;
  127.  
  128. (* +++++++++++++++++++++++++++++++++++++ *)
  129. PROCEDURE SkipOneLine (F : FastFile);
  130.   BEGIN
  131.     WITH F^ DO
  132.       LOOP
  133.         (* First test for all the wierd end conditions *)
  134.         (* If buffer emptied, re-fill it *)
  135.         IF count = 0 THEN 
  136.           FillBuffer (F); 
  137.           IF EOF THEN EXIT END; (* End-of-file *)
  138.         END;
  139.         (* EOL we're done, but skip past EOL *)
  140.         IF buff[indx] = EOL THEN
  141.           INC (indx); DEC (count); 
  142.           EXIT
  143.         END;
  144.         INC (indx);  DEC (count); (* Not EOL, continue on *)
  145.       END; (* LOOP *)
  146.     END;
  147.   END SkipOneLine;
  148.  
  149. (* +++++++++++++++++++++++++++++++++++++ *)
  150. PROCEDURE SkipLines (F : FastFile; lcount : CARDINAL);
  151.   BEGIN
  152.     WITH F^ DO
  153.       FOR lcount := lcount TO 1 BY -1 DO
  154.         SkipOneLine (F);
  155.         (* If we hit EOF, before all the lines are  *)
  156.         (* skipped just return.                     *)
  157.         IF EOF THEN RETURN END;
  158.       END;
  159.     END;
  160.   END SkipLines;
  161.  
  162. BEGIN
  163.   (* Make sure that DOS library is opened *)
  164.   IF DOSBase = NULL THEN
  165.     DOSBase := OpenLibrary (DOSName, 0);
  166.   END;
  167. END FastTextIO.
  168.