1 | // TTextFileStream class by Chronos 12.9.2005
|
---|
2 | // Homepage: http://jirihajda.zdechov.net/
|
---|
3 |
|
---|
4 | unit UTextFileStream;
|
---|
5 |
|
---|
6 | interface
|
---|
7 |
|
---|
8 | uses Classes, Dialogs, SysUtils;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
25 | implementation
|
---|
26 |
|
---|
27 | { TTextFileStream }
|
---|
28 |
|
---|
29 | function TTextFileStream.Eof: Boolean;
|
---|
30 | begin
|
---|
31 | Eof := ((Position - Length(FBuffer)) = Size);
|
---|
32 | end;
|
---|
33 |
|
---|
34 | function TTextFileStream.ReadLn: string;
|
---|
35 | const
|
---|
36 | BufferLength = 10000;
|
---|
37 | var
|
---|
38 | NewBuffer: string;
|
---|
39 | Readed: Integer;
|
---|
40 | begin
|
---|
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;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | function TTextFileStream.RowsCount: Integer;
|
---|
58 | begin
|
---|
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);
|
---|
67 | end;
|
---|
68 |
|
---|
69 | function TTextFileStream.Seek(const Offset: Int64;
|
---|
70 | Origin: TSeekOrigin): Int64;
|
---|
71 | begin
|
---|
72 | if Origin = soCurrent then
|
---|
73 | Result := inherited Seek(Offset - Length(FBuffer), Origin)
|
---|
74 | else Result := inherited Seek(Offset, Origin);
|
---|
75 | FBuffer := '';
|
---|
76 | end;
|
---|
77 |
|
---|
78 | procedure TTextFileStream.WriteLn(Text: string);
|
---|
79 | const
|
---|
80 | NewLine = #13#10;
|
---|
81 | begin
|
---|
82 | Seek(0, soCurrent);
|
---|
83 | Write(Text[1], Length(Text));
|
---|
84 | Write(NewLine, 2);
|
---|
85 | end;
|
---|
86 |
|
---|
87 | end.
|
---|