source: tools/Lazarus translator/UTextFileStream.pas

Last change on this file was 4, checked in by george, 16 years ago
  • Přidáno: Překládací program přímo pro Lazarus namísto Delphi.
  • Upraveno: Překladový soubor je nyní kódován v UTF-8.
File size: 2.1 KB
Line 
1// TTextFileStream class by Chronos 12.9.2005
2// Homepage: http://jirihajda.zdechov.net/
3
4unit UTextFileStream;
5
6{$mode objfpc}{$H+}
7
8interface
9
10uses Classes, Dialogs, SysUtils;
11
12type
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
30implementation
31
32{ TTextFileStream }
33
34function TTextFileStream.Eof: Boolean;
35begin
36 Eof := ((Position - Length(FBuffer)) = Size);
37end;
38
39function TTextFileStream.ReadLn: string;
40var
41 BufferLength: LongInt;
42 NewBuffer: string;
43 Readed: Integer;
44begin
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;
60end;
61
62function TTextFileStream.RowsCount: Integer;
63begin
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);
72end;
73
74constructor TTextFileStream.Create(const AFileName: string; Mode: Word);
75begin
76 inherited Create(AFileName, Mode);
77 FBuffer := '';
78end;
79
80function TTextFileStream.Seek(const Offset: Int64;
81 Origin: TSeekOrigin): Int64;
82begin
83 if Origin = soCurrent then
84 Result := inherited Seek(Offset - Length(FBuffer), Origin)
85 else Result := inherited Seek(Offset, Origin);
86 FBuffer := '';
87end;
88
89procedure TTextFileStream.WriteLn(Text: string);
90const
91 NewLine = #13#10;
92begin
93 Seek(0, soCurrent);
94 Write(Text[1], Length(Text));
95 Write(NewLine, 2);
96end;
97
98end.
Note: See TracBrowser for help on using the repository browser.