1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
| unit uTextFileReader;
interface
uses Windows, SysUtils, Classes;
type
PTextFileReaderIndex = ^TTextFileReaderIndex;
TTextFileReaderIndex = packed record
OffSet: Int64;
Length: Integer; // une Chaine Delphi ne peut pas dépasser cette taille
end;
type
TTextFileReader = class(TObject)
protected
FFileName: string;
FIndexName: string;
FTextFile: TFileStream;
FIndexFile: TFileStream; // un File of aurait été plus pratique mais limité à 2Go, donc environ 178 millions de lignes, hors un fichier texte de 4Go, peut en contenir bien plus ...
FIndexed: Boolean;
FAutoIndexed: Boolean;
FTextSize: Int64;
FIndexCount: Int64;
FIndexRecSize: Byte;
FLinearPosition: Int64;
function GetIndexed: Boolean;
procedure SetIndexed(const Value: Boolean);
function GetCount: Int64;
function GetEOF: Boolean;
function ReadLineFromIndexRec(IndexRec: TTextFileReaderIndex): string;
function ReadIndex(Index: Cardinal): TTextFileReaderIndex;
function ReadLineRaw(Index: Cardinal; OffSet: Int64 = 0): string;
public
constructor Create(const AFileName: string);
destructor Destroy; override;
procedure BuildLinesIndexes();
function ReadLine(Index: Cardinal): string; // 4 Milliard de Ligne ?
procedure ReadLines(IndexBegin, IndexEnd: Cardinal; Lines: TStrings; DoClear: Boolean = True);
function ReadString(): string;
procedure ReadStrings(ACount: Cardinal; Lines: TStrings; DoClear: Boolean = True);
property Count: Int64 read GetCount;
property OEF: Boolean read GetEOF;
property FileName: string read FFileName;
property IndexName: string read FIndexName;
property TextFile: TFileStream read FTextFile;
property Indexed: Boolean read GetIndexed write SetIndexed;
property AutoIndexed: Boolean read FAutoIndexed write FAutoIndexed;
end;
ETextFileReaderErrorIndex = class(Exception);
implementation
resourcestring
SIndexNotExist = 'Le Fichier d''Index n''existe pas !';
{ TTextFileReader }
{ TTextFileReader - Constructeurs }
constructor TTextFileReader.Create(const AFileName: string);
begin
inherited Create();
FIndexCount := -1;
FIndexRecSize := SizeOf(TTextFileReaderIndex);
FFileName := AFileName;
FIndexName := FFileName+'.idx';
FTextFile := TFileStream.Create(FileName, fmOpenRead, fmShareDenyWrite);
FIndexFile := nil;
FTextSize := FTextFile.Size;
FLinearPosition := 0;
Indexed := False;
AutoIndexed := False;
end;
destructor TTextFileReader.Destroy;
begin
if Assigned(FIndexFile) then
begin
FIndexFile.Free();
FIndexFile := nil;
end;
if Assigned(FTextFile) then
begin
FTextFile.Free();
FTextFile := nil;
end;
inherited;
end;
{ TTextFileReader - Méthodes Publiques }
procedure TTextFileReader.BuildLinesIndexes();
const
BUF_SIZE = 1024;
REC_BUF_SIZE = 65536;
LF: Byte = 10;
CR: Byte = 13;
var
TextBuf: array[0..BUF_SIZE-1] of Byte;
IndexRec: PTextFileReaderIndex;
IndexesRec: array[0..REC_BUF_SIZE-1] of TTextFileReaderIndex;
iBuf, Readed: Integer;
AByte: Byte;
LastIsCR: Boolean;
iRec: Integer;
IRSize: Integer;
begin
FIndexFile := TFileStream.Create(FIndexName, fmCreate, fmShareExclusive);
try
// Positionnement au début du Fichier
FTextFile.Seek(0, soFromBeginning);
// Compteur/Index/Drapeau à Zéro
iRec := 0;
IRSize := REC_BUF_SIZE * SizeOf(IndexRec^);
ZeroMemory(@IndexesRec, IRSize);
IndexRec := @IndexesRec[iRec];
LastIsCR := False;
// Boucle jusqu'à la fin
while (FTextFile.Position < FTextSize) do
begin
Readed := FTextFile.Read(TextBuf, BUF_SIZE);
for iBuf := 0 to Readed - 1 do
begin
AByte := TextBuf[iBuf];
if (AByte = LF) then
begin
IndexRec^.Length := (FTextFile.Position - Readed + iBuf) - IndexRec^.OffSet;
if LastIsCR then
Dec(IndexRec^.Length);
if iRec = REC_BUF_SIZE then
begin
FIndexFile.Write(IndexesRec, IRSize);
iRec := 0;
end else
Inc(iRec);
IndexRec := @IndexesRec[iRec];
IndexRec^.OffSet := FTextFile.Position - Readed + iBuf + 1; // + 1 car on inclu pas le LF
end;
LastIsCR := (AByte = CR);
end;
end;
if IndexRec^.OffSet < FTextSize then
begin
IndexRec^.Length := FTextFile.Position - IndexRec^.OffSet;
if iRec = REC_BUF_SIZE then
begin
FIndexFile.Write(IndexesRec, IRSize);
iRec := 0;
end else
Inc(iRec);
end;
if iRec > 0 then
FIndexFile.Write(IndexesRec, iRec * SizeOf(IndexRec^));
finally
FIndexFile.Free();
FIndexFile := nil;
end;
end;
function TTextFileReader.ReadLine(Index: Cardinal): string;
begin
if FIndexed then
Result := ReadLineFromIndexRec(ReadIndex(Index))
else
Result := ReadLineRaw(Index);
end;
procedure TTextFileReader.ReadLines(IndexBegin, IndexEnd: Cardinal; Lines: TStrings; DoClear: Boolean = True);
var
Index: Cardinal; // le For Delphi 6 ne gère pas le Int64
begin
if Assigned(Lines) then
begin
if DoClear then
Lines.Clear();
for Index := IndexBegin to IndexEnd do
Lines.Add(ReadLine(Index));
end;
end;
function TTextFileReader.ReadString: string;
begin
Result := ReadLineRaw(0, FLinearPosition);
end;
procedure TTextFileReader.ReadStrings(ACount: Cardinal; Lines: TStrings; DoClear: Boolean = True);
var
Index: Cardinal; // le For Delphi 6 ne gère pas le Int64
begin
if Assigned(Lines) then
begin
if DoClear then
Lines.Clear();
for Index := 1 to ACount do
Lines.Add(ReadString());
end;
end;
{ TTextFileReader - Méthodes d'Accès }
function TTextFileReader.GetIndexed: Boolean;
begin
Result := FIndexed and Assigned(FIndexFile);
end;
procedure TTextFileReader.SetIndexed(const Value: Boolean);
begin
if FIndexed <> Value then
begin
if Assigned(FIndexFile) then
begin
FIndexFile.Free();
FIndexFile := nil;
end;
FIndexed := Value;
if FIndexed then
begin
if not FileExists(FIndexName) then
if AutoIndexed then
BuildLinesIndexes()
else
raise ETextFileReaderErrorIndex.Create(SIndexNotExist);
FIndexFile := TFileStream.Create(FIndexName, fmOpenRead, fmShareDenyWrite);
FIndexCount := FIndexFile.Size div FIndexRecSize;
end;
end;
end;
function TTextFileReader.GetCount: Int64;
begin
Result := FIndexCount;
end;
function TTextFileReader.GetEOF: Boolean;
begin
Result := FLinearPosition >= FTextSize;
end;
{ TTextFileReader - Méthodes Privées }
function TTextFileReader.ReadIndex(Index: Cardinal): TTextFileReaderIndex;
begin
if FIndexed and Assigned(FIndexFile) and (Index < FIndexCount) then
begin
FIndexFile.Seek(Index * FIndexRecSize, soFromBeginning);
FIndexFile.Read(Result, FIndexRecSize);
end else
ZeroMemory(@Result, FIndexRecSize);
end;
function TTextFileReader.ReadLineFromIndexRec(IndexRec: TTextFileReaderIndex): string;
begin
if (IndexRec.OffSet >= 0) and (IndexRec.Length > 0) and (IndexRec.OffSet + IndexRec.Length <= FTextSize)then
begin
SetLength(Result, IndexRec.Length);
FTextFile.Seek(IndexRec.OffSet, soFromBeginning);
FTextFile.Read(Result[1], IndexRec.Length);
end else
Result := '';
end;
function TTextFileReader.ReadLineRaw(Index: Cardinal; OffSet: Int64 = 0): string;
const
BUF_SIZE = 1024;
LF: Byte = 10;
CR: Byte = 13;
var
TextBuf: array[0..BUF_SIZE-1] of Byte;
IndexRec: TTextFileReaderIndex;
iBuf, Readed: Integer;
AByte: Byte;
LastIsCR: Boolean;
LineReaded: Cardinal;
begin
// Positionnement au début du Fichier ou sur le Curseur de lecture linéaire
FTextFile.Seek(OffSet, soFromBeginning);
// Compteur/Index/Drapeau à Zéro
IndexRec.OffSet := OffSet;
IndexRec.Length := 0;
LastIsCR := False;
LineReaded := 0;
// Boucle jusqu'à la fin
while (FTextFile.Position < FTextSize) do
begin
Readed := FTextFile.Read(TextBuf, BUF_SIZE);
for iBuf := 0 to Readed - 1 do
begin
AByte := TextBuf[iBuf];
if (AByte = LF) then
begin
FLinearPosition := FTextFile.Position - Readed + iBuf;
IndexRec.Length := FLinearPosition - IndexRec.OffSet;
Inc(FLinearPosition); // car on inclu pas le LF dans la prochaine ligne
if LastIsCR then
Dec(IndexRec.Length);
if (LineReaded = Index) then
begin
Result := ReadLineFromIndexRec(IndexRec);
Exit;
end;
IndexRec.OffSet := FLinearPosition;
Inc(LineReaded);
end;
LastIsCR := (AByte = CR);
end;
end;
if IndexRec.OffSet < FTextSize then
begin
IndexRec.Length := FTextFile.Position - IndexRec.OffSet;
FLinearPosition := FTextFile.Position + 1; // + 1 car on inclu pas le LF
if (LineReaded = Index) then
begin
Result := ReadLineFromIndexRec(IndexRec);
Exit;
end;
end;
Result := '';
end;
end. |
Partager