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
|
{
/***************************************************************************
filereferencelist.pas
---------------------
***************************************************************************/
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
TFileReferenceList stores reference counters for a set of filenames and
can create a search path of all files.
}
unit FileReferenceList;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Laz_AVL_Tree,
// Codetools
FileProcs,
// LazUtils
LazFileUtils, LazTracer;
type
{ TFileReference }
TFileReference = class
private
fFilename: string;
fReferenceCount: integer;
public
property Filename: string read fFilename;
property ReferenceCount: integer read FReferenceCount;
end;
{ TFileReferenceList }
TFileReferenceFlag = (
frfSearchPathValid,
frfChanged
);
TFileReferenceFlags = set of TFileReferenceFlag;
TFileReferenceList = class
private
FOnChanged: TNotifyEvent;
FTimeStamp: integer;
FTree: TAvlTree; // tree of TFileReference sorted for filename
FFlags: TFileReferenceFlags;
FSearchPath: string;
FUpdateLock: integer;
procedure UpdateSearchPath;
procedure IncreaseTimeStamp;
procedure Invalidate;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure BeginUpdate;
procedure EndUpdate;
procedure AddFilename(const Filename: string);
procedure RemoveFilename(const Filename: string);
function GetFileReference(const Filename: string): TFileReference;
function CreateSearchPathFromAllFiles: string;
function CreateFileList: TStringList;
property TimeStamp: integer read FTimeStamp;
property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
property UpdateLock: integer read FUpdateLock;
end;
implementation
function CompareFileReferences(Data1, Data2: Pointer): integer;
var
FileRef1: TFileReference absolute Data1;
FileRef2: TFileReference absolute Data2;
begin
Result:=CompareFilenames(FileRef1.Filename,FileRef2.Filename);
end;
function CompareFileNameAndReference(Key, Data: Pointer): integer;
var
FileRef: TFileReference absolute Data;
begin
Result:=CompareFilenames(AnsiString(Key),FileRef.Filename);
end;
{ TFileReferenceList }
procedure TFileReferenceList.UpdateSearchPath;
var
SearchPathLen: Integer;
ANode: TAvlTreeNode;
StartPos: Integer;
CurFileLen: Integer;
CurFileName: String;
begin
if (frfSearchPathValid in FFlags) then exit;
FSearchPath:='';
if FTree<>nil then begin
// count length
SearchPathLen:=0;
ANode:=FTree.FindLowest;
while ANode<>nil do begin
CurFileName:=TFileReference(ANode.Data).Filename;
CurFileLen:=length(CurFileName);
if CurFileLen>0 then begin
// add semicolon
if SearchPathLen>0 then inc(SearchPathLen);
// add path
inc(SearchPathLen,CurFileLen);
end;
ANode:=FTree.FindSuccessor(ANode);
end;
// create search path
SetLength(FSearchPath,SearchPathLen);
StartPos:=1;
ANode:=FTree.FindLowest;
while ANode<>nil do begin
CurFileName:=TFileReference(ANode.Data).Filename;
CurFileLen:=length(CurFileName);
if CurFileLen>0 then begin
// add semicolon
if StartPos>1 then begin
FSearchPath[StartPos]:=';';
inc(StartPos);
end;
// add path
Move(CurFileName[1],FSearchPath[StartPos],CurFileLen);
inc(StartPos,CurFileLen);
end;
ANode:=FTree.FindSuccessor(ANode);
end;
if StartPos<>length(FSearchPath)+1 then
RaiseGDBException('TFileReferenceList.UpdateSearchPath');
end;
Include(FFlags,frfSearchPathValid);
end;
procedure TFileReferenceList.IncreaseTimeStamp;
begin
CTIncreaseChangeStamp(FTimeStamp);
//DebugLn('TFileReferenceList.IncreaseTimeStamp ',dbgs(FTimeStamp));
end;
procedure TFileReferenceList.Invalidate;
begin
IncreaseTimeStamp;
Exclude(FFlags,frfSearchPathValid);
if FUpdateLock>0 then
Include(FFlags,frfChanged)
else if Assigned(OnChanged) then
OnChanged(Self);
end;
constructor TFileReferenceList.Create;
begin
FTimeStamp:=CTInvalidChangeStamp;
end;
destructor TFileReferenceList.Destroy;
begin
Clear;
FreeAndNil(FTree);
inherited Destroy;
end;
procedure TFileReferenceList.Clear;
begin
if (FTree<>nil) and (FTree.Count>0) then begin
FTree.FreeAndClear;
Invalidate;
end;
end;
procedure TFileReferenceList.BeginUpdate;
begin
inc(FUpdateLock);
end;
procedure TFileReferenceList.EndUpdate;
begin
if FUpdateLock<=0 then RaiseGDBException('TFileReferenceList.EndUpdate');
dec(FUpdateLock);
if (frfChanged in FFlags) then begin
Exclude(FFlags,frfChanged);
if Assigned(OnChanged) then
OnChanged(Self);
end;
end;
procedure TFileReferenceList.AddFilename(const Filename: string);
var
ANode: TAvlTreeNode;
NewFileRef: TFileReference;
begin
if Filename='' then exit;
if FTree<>nil then begin
ANode:=FTree.FindKey(Pointer(Filename),@CompareFileNameAndReference);
if ANode<>nil then begin
inc(TFileReference(ANode.Data).fReferenceCount);
exit;
end;
end;
NewFileRef:=TFileReference.Create;
NewFileRef.fFilename:=Filename;
inc(NewFileRef.fReferenceCount);
if FTree=nil then FTree:=TAvlTree.Create(@CompareFileReferences);
FTree.Add(NewFileRef);
Invalidate;
end;
procedure TFileReferenceList.RemoveFilename(const Filename: string);
var
ANode: TAvlTreeNode;
CurFileRef: TFileReference;
begin
if Filename='' then exit;
if FTree=nil then exit;
ANode:=FTree.FindKey(Pointer(Filename),@CompareFileNameAndReference);
if ANode=nil then exit;
CurFileRef:=TFileReference(ANode.Data);
dec(CurFileRef.fReferenceCount);
if CurFileRef.fReferenceCount=0 then begin
FTree.Delete(ANode);
CurFileRef.Free;
Invalidate;
end;
end;
function TFileReferenceList.GetFileReference(const Filename: string): TFileReference;
var
ANode: TAvlTreeNode;
begin
Result:=nil;
if FTree=nil then exit;
ANode:=FTree.FindKey(Pointer(Filename),@CompareFileNameAndReference);
if ANode=nil then exit;
Result:=TFileReference(ANode.Data);
end;
function TFileReferenceList.CreateSearchPathFromAllFiles: string;
begin
UpdateSearchPath;
Result:=FSearchPath;
end;
function TFileReferenceList.CreateFileList: TStringList;
var
ANode: TAvlTreeNode;
begin
Result:=TStringList.Create;
if FTree=nil then exit;
ANode:=FTree.FindLowest;
while ANode<>nil do begin
Result.Add(TFileReference(ANode.Data).Filename);
ANode:=FTree.FindSuccessor(ANode);
end;
end;
end.
|