source: tools/Delphi translator/UTextFileStream.pas

Last change on this file was 1, checked in by george, 16 years ago
  • Přidáno: Rozpracovaný překlad Lazarusu 0.9.25 a 0.9.26.
  • Přidáno: Nástroj pro překládání naprogramovaný v Turbo Delphi.
File size: 1.8 KB
Line 
1// TTextFileStream class by Chronos 12.9.2005
2// Homepage: http://jirihajda.zdechov.net/
3
4unit UTextFileStream;
5
6interface
7
8uses Classes, Dialogs, SysUtils;
9
10type
11
12 TTextFileStream = class(TFileStream)
13 private
14 FBuffer: string;
15 protected
16
17 public
18 function Eof: Boolean;
19 procedure WriteLn(Text: string);
20 function ReadLn: string;
21 function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
22 function RowsCount: Integer;
23 end;
24
25implementation
26
27{ TTextFileStream }
28
29function TTextFileStream.Eof: Boolean;
30begin
31 Eof := ((Position - Length(FBuffer)) = Size);
32end;
33
34function TTextFileStream.ReadLn: string;
35const
36 BufferLength = 10000;
37var
38 NewBuffer: string;
39 Readed: Integer;
40begin
41 Readed := 1;
42 while (Pos(#13, FBuffer) = 0) and (Readed > 0) do begin
43 SetLength(NewBuffer, BufferLength + 2);
44 Readed := Read(NewBuffer[1], BufferLength);
45 SetLength(NewBuffer, Readed);
46 FBuffer := FBuffer + NewBuffer;
47 end;
48 if Pos(#13, FBuffer) > 0 then begin
49 Result := Copy(FBuffer, 1, Pos(#13, FBuffer) - 1);
50 Delete(FBuffer, 1, Pos(#13, FBuffer) + 1);
51 end else begin
52 Result := FBuffer;
53 FBuffer := '';
54 end;
55end;
56
57function TTextFileStream.RowsCount: Integer;
58begin
59 Result := 1;
60 FBuffer := '';
61 Seek(0, soBeginning);
62 while not Eof do begin
63 ReadLn;
64 Inc(Result);
65 end;
66 Seek(0, soBeginning);
67end;
68
69function TTextFileStream.Seek(const Offset: Int64;
70 Origin: TSeekOrigin): Int64;
71begin
72 if Origin = soCurrent then
73 Result := inherited Seek(Offset - Length(FBuffer), Origin)
74 else Result := inherited Seek(Offset, Origin);
75 FBuffer := '';
76end;
77
78procedure TTextFileStream.WriteLn(Text: string);
79const
80 NewLine = #13#10;
81begin
82 Seek(0, soCurrent);
83 Write(Text[1], Length(Text));
84 Write(NewLine, 2);
85end;
86
87end.
Note: See TracBrowser for help on using the repository browser.