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
| type
TSearchDuplicatesLineIndexes = class(TStringList)
private
function GetInteger(IndexLine, IndexInteger: Integer): Integer;
procedure PutInteger(IndexLine, IndexInteger: Integer; const Value: Integer);
function GetCountInteger(IndexLine: Integer): Integer;
public
procedure Clear; override;
function AddInteger(const Line: string; Value: Integer): Integer; overload;
procedure AddInteger(IndexLine, Value: Integer); overload;
property CountInteger[IndexLine: Integer]: Integer read GetCountInteger;
property Integers[IndexLine, IndexInteger: Integer]: Integer read GetInteger write PutInteger;
end;
function SearchDuplicatesLines(const FileName: string; Duplicates: TSearchDuplicatesLineIndexes): Integer;
type
PLongRec = ^LongRec;
TOffsetLine = packed record
BeginOffSet: Int64;
LineLength: Integer;
Skip: Boolean;
DuplicateIndex: Integer;
CheckSum: Integer;
end;
TOffsetLineEx = packed record
BeginOffSet: Int64;
LineLength: Integer;
Skip: Boolean;
DuplicateIndex: Integer;
CheckSum: Integer;
EndOffSet: Int64;
Is13: Boolean;
Is10: Boolean;
end;
var
OffSetLines: Array of TOffsetLine;
procedure CreateLineOffSet();
const
BUF_SIZE: Integer = 32000;
var
FileHandle: Integer;
FileLength: Int64;
SearchBuf, RememberBuf: array of Byte;
iSearchBufPos, iReaded: Int64;
AmtTransferred, RememberLen: Integer;
CurrentOSL: TOffsetLineEx;
procedure NewOSL(AOffSet: Int64);
var
I, ICS, BO: Integer;
pCS: PLongRec;
WordByte: Word;
begin
SetLength(OffSetLines, Length(OffSetLines) + 1);
CurrentOSL.EndOffSet := AOffSet;
CurrentOSL.LineLength := CurrentOSL.EndOffSet - CurrentOSL.BeginOffSet + 1;
if CurrentOSL.Is13 then Dec(CurrentOSL.LineLength);
if CurrentOSL.Is10 then Dec(CurrentOSL.LineLength);
ICS := 0;
pCS := PLongRec(@CurrentOSL.CheckSum);
for I := 0 to RememberLen - 1 do
begin
pCS.Bytes[ICS] := (pCS.Bytes[ICS] + RememberBuf[I]) and $000000FF;
Inc(ICS);
if ICS = 4 then
ICS := 0;
end;
if RememberLen > 0 then
BO := 0
else
BO := CurrentOSL.BeginOffSet - iReaded;
for I := BO to BO + CurrentOSL.LineLength - RememberLen do
begin
pCS.Bytes[ICS] := (pCS.Bytes[ICS] + SearchBuf[I]) and $000000FF;
Inc(ICS);
if ICS = 4 then
ICS := 0;
end;
CopyMemory(@OffSetLines[High(OffSetLines)], @CurrentOSL, SizeOf(TOffsetLine));
ZeroMemory(@CurrentOSL, SizeOf(CurrentOSL));
CurrentOSL.BeginOffSet := AOffSet + 1;
CurrentOSL.DuplicateIndex := -1;
RememberLen := 0;
end;
begin
SetLength(SearchBuf, BUF_SIZE);
SetLength(RememberBuf, BUF_SIZE);
RememberLen := 0;
Result := 0;
iReaded := 0;
FileLength := 0;
ZeroMemory(@CurrentOSL, SizeOf(CurrentOSL));
CurrentOSL.DuplicateIndex := -1;
FileHandle := FileOpen(FileName, fmOpenRead);
try
FileLength := FileSeek(FileHandle, FileLength, FILE_END);
FileSeek(FileHandle, 0, FILE_BEGIN);
while iReaded < FileLength do
begin
AmtTransferred := FileRead(FileHandle, SearchBuf[0], BUF_SIZE); // [0] parce que c'est un tableau dynamique
iSearchBufPos := 0;
if iReaded > 0 then
begin
if CurrentOSL.Is13 then
begin
CurrentOSL.Is10 := SearchBuf[iSearchBufPos] = 10;
if CurrentOSL.Is10 then
begin
NewOSL(iReaded+iSearchBufPos);
Inc(iSearchBufPos);
end;
end;
end;
while iSearchBufPos < AmtTransferred do
begin
CurrentOSL.Is13 := SearchBuf[iSearchBufPos] = 13;
if CurrentOSL.Is13 then
begin
Inc(iSearchBufPos);
if iSearchBufPos < AmtTransferred then
begin
CurrentOSL.Is10 := SearchBuf[iSearchBufPos] = 10;
if not CurrentOSL.Is10 then
Dec(iSearchBufPos);
NewOSL(iReaded+iSearchBufPos);
end;
end else
begin
CurrentOSL.Is10 := SearchBuf[iSearchBufPos] = 10;
if CurrentOSL.Is10 then
begin
NewOSL(iReaded+iSearchBufPos);
end;
end;
Inc(iSearchBufPos);
end;
RememberLen := AmtTransferred - CurrentOSL.BeginOffSet + iReaded;
if RememberLen > 0 then
CopyMemory(@RememberBuf[0], @SearchBuf[CurrentOSL.BeginOffSet - iReaded], RememberLen);
Inc(iReaded, AmtTransferred);
end;
if (FileLength > 0) and not (CurrentOSL.Is13 or CurrentOSL.Is10) then
NewOSL(FileLength - 1);
finally
FileClose(FileHandle);
end;
end;
procedure CompareLength();
var
iOSL, iOSLSub: Integer;
begin
for iOSL := Low(OffSetLines) to High(OffSetLines) - 1 do
begin
OffSetLines[iOSL].Skip := True;
for iOSLSub := iOSL + 1 to High(OffSetLines) do
begin
if (OffSetLines[iOSL].LineLength = OffSetLines[iOSLSub].LineLength)
and (OffSetLines[iOSL].CheckSum = OffSetLines[iOSLSub].CheckSum) then
begin
OffSetLines[iOSL].Skip := False;
Break;
end;
end;
end;
end;
procedure CompareStrings();
var
FileHandle: Integer;
FileLength, FilePosition: Int64;
Buf, BufSub: Array of Byte;
BufStr: string;
BufLen: Integer;
iOSL, iOSLSub: Integer;
begin
FileHandle := FileOpen(FileName, fmOpenRead);
try
for iOSL := Low(OffSetLines) to High(OffSetLines) - 1 do
begin
if OffSetLines[iOSL].Skip then
Continue;
BufLen := OffSetLines[iOSL].LineLength;
SetLength(Buf, BufLen);
FileSeek(FileHandle, OffSetLines[iOSL].BeginOffSet, FILE_BEGIN);
FileRead(FileHandle, Buf[1], BufLen);
SetLength(BufStr, BufLen);
CopyMemory(@BufStr[1], @Buf[1], BufLen);
SetLength(BufSub, BufLen);
for iOSLSub := iOSL + 1 to High(OffSetLines) do
begin
if OffSetLines[iOSLSub].Skip then
Continue;
if OffSetLines[iOSL].LineLength = OffSetLines[iOSLSub].LineLength then
begin
FileSeek(FileHandle, OffSetLines[iOSLSub].BeginOffSet, FILE_BEGIN);
FileRead(FileHandle, BufSub[1], BufLen);
if CompareMem(@Buf[1], @BufSub[1], BufLen) then
//if Buf = BufSub then
begin
OffSetLines[iOSLSub].Skip := True;
if OffSetLines[iOSL].DuplicateIndex < 0 then
OffSetLines[iOSL].DuplicateIndex := Duplicates.AddInteger(BufStr, iOSL);
Duplicates.AddInteger(OffSetLines[iOSL].DuplicateIndex, iOSLSub);
end;
end;
end;
SetLength(Buf, 0);
BufStr := '';
SetLength(BufSub, 0);
end;
finally
FileClose(FileHandle);
end;
end;
begin
Result := -1;
if not Assigned(Duplicates) then
Exit;
CreateLineOffSet();
//CompareLength();
CompareStrings();
Result := Duplicates.Count;
end;
{ TSearchDuplicatesLineIndexes }
procedure TSearchDuplicatesLineIndexes.Clear;
var
I: Integer;
begin
for I := 0 to Count - 1 do
if Assigned(Objects[I]) then
Objects[I].Free();
inherited Clear();
end;
function TSearchDuplicatesLineIndexes.AddInteger(const Line: string; Value: Integer): Integer;
begin
Result := IndexOf(Line);
if Result < 0 then
Result := AddObject(Line, TList.Create());
TList(Objects[Result]).Add(Pointer(Value));
end;
procedure TSearchDuplicatesLineIndexes.AddInteger(IndexLine, Value: Integer);
begin
TList(Objects[IndexLine]).Add(Pointer(Value));
end;
function TSearchDuplicatesLineIndexes.GetInteger(IndexLine, IndexInteger: Integer): Integer;
begin
Result := Integer(TList(Objects[IndexLine]).Items[IndexInteger]);
end;
procedure TSearchDuplicatesLineIndexes.PutInteger(IndexLine, IndexInteger: Integer; const Value: Integer);
begin
TList(Objects[IndexLine]).Items[IndexInteger] := Pointer(Value);
end;
function TSearchDuplicatesLineIndexes.GetCountInteger(IndexLine: Integer): Integer;
begin
Result := TList(Objects[IndexLine]).Count;
end;
{ TSearchDuplicatesLineIndexes - END } |
Partager