1 | // TTextFileStream class by Chronos 12.9.2005
|
---|
2 | // Homepage: http://jirihajda.zdechov.net/
|
---|
3 |
|
---|
4 | unit UTextFileStream;
|
---|
5 |
|
---|
6 | {$mode objfpc}{$H+}
|
---|
7 |
|
---|
8 | interface
|
---|
9 |
|
---|
10 | uses Classes, Dialogs, SysUtils;
|
---|
11 |
|
---|
12 | type
|
---|
13 |
|
---|
14 | { TTextFileStream }
|
---|
15 |
|
---|
16 | TTextFileStream = class(TFileStream)
|
---|
17 | private
|
---|
18 | FBuffer: string;
|
---|
19 | protected
|
---|
20 |
|
---|
21 | public
|
---|
22 | function Eof: Boolean;
|
---|
23 | procedure WriteLn(Text: string);
|
---|
24 | function ReadLn: string;
|
---|
25 | function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
|
---|
26 | function RowsCount: Integer;
|
---|
27 | constructor Create(const AFileName: string; Mode: Word);
|
---|
28 | end;
|
---|
29 |
|
---|
30 | implementation
|
---|
31 |
|
---|
32 | { TTextFileStream }
|
---|
33 |
|
---|
34 | function TTextFileStream.Eof: Boolean;
|
---|
35 | begin
|
---|
36 | Eof := ((Position - Length(FBuffer)) = Size);
|
---|
37 | end;
|
---|
38 |
|
---|
39 | function TTextFileStream.ReadLn: string;
|
---|
40 | var
|
---|
41 | BufferLength: LongInt;
|
---|
42 | NewBuffer: string;
|
---|
43 | Readed: Integer;
|
---|
44 | begin
|
---|
45 | BufferLength := 10000;
|
---|
46 | Readed := 1;
|
---|
47 | while (Pos(#13, FBuffer) = 0) and (Readed > 0) do begin
|
---|
48 | SetLength(NewBuffer, BufferLength + 10);
|
---|
49 | Readed := Read(NewBuffer[1], BufferLength);
|
---|
50 | SetLength(NewBuffer, Readed);
|
---|
51 | FBuffer := FBuffer + NewBuffer;
|
---|
52 | end;
|
---|
53 | if Pos(#13, FBuffer) > 0 then begin
|
---|
54 | Result := Copy(FBuffer, 1, Pos(#13, FBuffer) - 1);
|
---|
55 | Delete(FBuffer, 1, Pos(#13, FBuffer) + 1);
|
---|
56 | end else begin
|
---|
57 | Result := FBuffer;
|
---|
58 | FBuffer := '';
|
---|
59 | end;
|
---|
60 | end;
|
---|
61 |
|
---|
62 | function TTextFileStream.RowsCount: Integer;
|
---|
63 | begin
|
---|
64 | Result := 1;
|
---|
65 | FBuffer := '';
|
---|
66 | Seek(0, soBeginning);
|
---|
67 | while not Eof do begin
|
---|
68 | ReadLn;
|
---|
69 | Inc(Result);
|
---|
70 | end;
|
---|
71 | Seek(0, soBeginning);
|
---|
72 | end;
|
---|
73 |
|
---|
74 | constructor TTextFileStream.Create(const AFileName: string; Mode: Word);
|
---|
75 | begin
|
---|
76 | inherited Create(AFileName, Mode);
|
---|
77 | FBuffer := '';
|
---|
78 | end;
|
---|
79 |
|
---|
80 | function TTextFileStream.Seek(const Offset: Int64;
|
---|
81 | Origin: TSeekOrigin): Int64;
|
---|
82 | begin
|
---|
83 | if Origin = soCurrent then
|
---|
84 | Result := inherited Seek(Offset - Length(FBuffer), Origin)
|
---|
85 | else Result := inherited Seek(Offset, Origin);
|
---|
86 | FBuffer := '';
|
---|
87 | end;
|
---|
88 |
|
---|
89 | procedure TTextFileStream.WriteLn(Text: string);
|
---|
90 | const
|
---|
91 | NewLine = #13#10;
|
---|
92 | begin
|
---|
93 | Seek(0, soCurrent);
|
---|
94 | Write(Text[1], Length(Text));
|
---|
95 | Write(NewLine, 2);
|
---|
96 | end;
|
---|
97 |
|
---|
98 | end.
|
---|