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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
unit PackageDependencyIntf;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
// LazUtils
LazFileUtils;
type
{ TPkgVersion }
TPkgVersionValid = (
pvtNone,
pvtMajor,
pvtMinor,
pvtRelease,
pvtBuild
);
TPkgVersion = class
private
FMajor: integer;
FMinor: integer;
FRelease: integer;
FBuild: integer;
FValid: TPkgVersionValid;
FOnChange: TNotifyEvent;
function GetAsString: String;
procedure SetAsString(const AValue: String);
public
procedure Clear;
function Compare(Version2: TPkgVersion): integer;
function CompareMask(ExactVersion: TPkgVersion): integer;
procedure Assign(Source: TPkgVersion);
//function AsString: string;
function AsWord: string;
function GetIsNullVersion: Boolean;
function ReadString(const s: string): boolean;
procedure SetValues(NewMajor, NewMinor, NewRelease, NewBuild: integer;
NewValid: TPkgVersionValid = pvtBuild);
function VersionBound(v: integer): integer;
public
property AsString: String read GetAsString write SetAsString;
property Major: Integer read FMajor write FMajor;
property Minor: Integer read FMinor write FMinor;
property Release: Integer read FRelease write FRelease;
property Build: Integer read FBuild write FBuild;
property IsNullVersion: Boolean read GetIsNullVersion;
property Valid: TPkgVersionValid read FValid write FValid;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
{ PkgDependency flags }
TPkgDependencyFlag = (
pdfMinVersion, // >= MinVersion
pdfMaxVersion // <= MaxVersion
);
TPkgDependencyFlags = set of TPkgDependencyFlag;
{ TPkgDependencyBase }
TPkgDependencyBase = class
private
protected
FFlags: TPkgDependencyFlags;
FMaxVersion: TPkgVersion;
FMinVersion: TPkgVersion;
FPackageName: string;
FRemoved: boolean;
//FRequiredPackage: TIDEPackage;
procedure SetPackageName(const AValue: string); virtual; abstract;
public
constructor Create;
destructor Destroy; override;
procedure Clear; virtual;
function IsMakingSense: boolean;
function IsCompatible(const Version: TPkgVersion): boolean; overload;
function IsCompatible(const PkgName: string; const Version: TPkgVersion): boolean; overload;
// API for iterating dependencies.
function NextUsedByDependency: TPkgDependencyBase; virtual; abstract;
function PrevUsedByDependency: TPkgDependencyBase; virtual; abstract;
function NextRequiresDependency: TPkgDependencyBase; virtual; abstract;
function PrevRequiresDependency: TPkgDependencyBase; virtual; abstract;
// API for adding / removing dependencies.
procedure AddRequiresDep(var FirstDependency: TPkgDependencyBase); virtual; abstract;
procedure AddUsedByDep(var FirstDependency: TPkgDependencyBase); virtual; abstract;
procedure RemoveRequiresDep(var FirstDependency: TPkgDependencyBase); virtual; abstract;
procedure RemoveUsedByDep(var FirstDependency: TPkgDependencyBase); virtual; abstract;
public
property Flags: TPkgDependencyFlags read FFlags write FFlags;
property MinVersion: TPkgVersion read FMinVersion write FMinVersion;
property MaxVersion: TPkgVersion read FMaxVersion write FMaxVersion;
property PackageName: string read FPackageName write SetPackageName;
property Removed: boolean read FRemoved write FRemoved;
end;
function LazIsValidIdent(const Ident: string; AllowDots: Boolean = False;
StrictDots: Boolean = False): Boolean;
function IsValidUnitName(AUnitName: String): Boolean; inline;
function IsValidPkgName(APkgName: String): Boolean; inline;
function PackageFileNameIsValid(const AFilename: string): boolean;
implementation
function LazIsValidIdent(const Ident: string; AllowDots: Boolean = False;
StrictDots: Boolean = False): Boolean;
// This is a copy of IsValidIdent from FPC 3.1.
// ToDo: Switch to using IsValidIdent from FPC 3.2 when it is the minimum requirement.
const
Alpha = ['A'..'Z', 'a'..'z', '_'];
AlphaNum = Alpha + ['0'..'9'];
Dot = '.';
var
First: Boolean;
I, Len: Integer;
begin
Len := Length(Ident);
if Len < 1 then
Exit(False);
First := True;
for I := 1 to Len do
begin
if First then
begin
Result := Ident[I] in Alpha;
First := False;
end
else if AllowDots and (Ident[I] = Dot) then
begin
if StrictDots then
begin
Result := I < Len;
First := True;
end;
end
else
Result := Ident[I] in AlphaNum;
if not Result then
Break;
end;
end;
function IsValidUnitName(AUnitName: String): Boolean;
begin
Result := LazIsValidIdent(AUnitName, True, True);
end;
function IsValidPkgName(APkgName: String): Boolean;
begin
Result := LazIsValidIdent(APkgName, True, True);
end;
function PackageFileNameIsValid(const AFilename: string): boolean;
var
PkgName: String;
begin
Result:=false;
if CompareFileExt(AFilename,'.lpk',false)<>0 then exit;
PkgName:=ExtractFileNameOnly(AFilename);
if (PkgName='') or (not IsValidPkgName(PkgName)) then exit;
Result:=true;
end;
{ TPkgVersion }
procedure TPkgVersion.Clear;
begin
SetValues(0,0,0,0,pvtBuild);
end;
function TPkgVersion.Compare(Version2: TPkgVersion): integer;
begin
Result:=FMajor-Version2.FMajor;
if Result<>0 then exit;
Result:=FMinor-Version2.FMinor;
if Result<>0 then exit;
Result:=FRelease-Version2.FRelease;
if Result<>0 then exit;
Result:=FBuild-Version2.FBuild;
end;
function TPkgVersion.CompareMask(ExactVersion: TPkgVersion): integer;
begin
if FValid=pvtNone then exit(0);
Result:=FMajor-ExactVersion.FMajor;
if Result<>0 then exit;
if FValid=pvtMajor then exit;
Result:=FMinor-ExactVersion.FMinor;
if Result<>0 then exit;
if FValid=pvtMinor then exit;
Result:=FRelease-ExactVersion.FRelease;
if Result<>0 then exit;
if FValid=pvtRelease then exit;
Result:=FBuild-ExactVersion.FBuild;
end;
procedure TPkgVersion.Assign(Source: TPkgVersion);
begin
SetValues(Source.FMajor,Source.FMinor,Source.FRelease,Source.FBuild,Source.FValid);
end;
function TPkgVersion.GetAsString: String;
begin
Result:=IntToStr(FMajor)+'.'+IntToStr(FMinor);
if (FBuild<>0) then
Result:=Result+'.'+IntToStr(FRelease)+'.'+IntToStr(FBuild)
else if (FRelease<>0) then
Result:=Result+'.'+IntToStr(FRelease)
end;
procedure TPkgVersion.SetAsString(const AValue: String);
var
Version: String;
P, I, V: Integer;
begin
Clear;
if AValue = '' then Exit;
I := 0;
Version := Trim(AValue) + '.';
repeat
Inc(I);
P := Pos('.', Version);
if P <> 0 then
begin
V := StrToIntDef(Copy(Version, 1, P-1), 0);
case I of
1: FMajor := V;
2: FMinor := V;
3: FRelease := V;
4: FBuild := V;
end;
Delete(Version, 1, P);
end;
until (Version = '') or (P = 0) or (I > 4);
end;
function TPkgVersion.AsWord: string;
begin
Result:=IntToStr(FMajor)+'_'+IntToStr(FMinor);
if (FBuild<>0) then
Result:=Result+'_'+IntToStr(FRelease)+'_'+IntToStr(FBuild)
else if (FRelease<>0) then
Result:=Result+'_'+IntToStr(FRelease)
end;
function TPkgVersion.GetIsNullVersion: Boolean;
begin
Result := (FMajor = 0) and (FMinor = 0) and (FRelease = 0) and (FBuild = 0);
end;
function TPkgVersion.ReadString(const s: string): boolean;
var
ints: array[1..4] of integer;
i: integer;
CurPos: Integer;
StartPos: Integer;
NewValid: TPkgVersionValid;
begin
Result:=false;
CurPos:=1;
NewValid:=pvtNone;
for i:=1 to 4 do begin
ints[i]:=0;
if CurPos<length(s) then begin
if i>Low(ints) then begin
// read point
if s[CurPos]<>'.' then exit;
inc(CurPos);
end;
// read int
StartPos:=CurPos;
while (CurPos<=length(s)) and (i<=9999)
and (s[CurPos] in ['0'..'9']) do begin
ints[i]:=ints[i]*10+ord(s[CurPos])-ord('0');
inc(CurPos);
end;
if (StartPos=CurPos) then exit;
NewValid:=succ(NewValid);
end;
end;
if CurPos<=length(s) then exit;
SetValues(ints[1],ints[2],ints[3],ints[4],NewValid);
Result:=true;
end;
procedure TPkgVersion.SetValues(NewMajor, NewMinor, NewRelease,
NewBuild: integer; NewValid: TPkgVersionValid);
begin
NewMajor:=VersionBound(NewMajor);
NewMinor:=VersionBound(NewMinor);
NewRelease:=VersionBound(NewRelease);
NewBuild:=VersionBound(NewBuild);
if (NewMajor=FMajor) and (NewMinor=FMinor) and (NewRelease=FRelease)
and (NewBuild=FBuild) and (NewValid=FValid) then exit;
FMajor:=NewMajor;
FMinor:=NewMinor;
FRelease:=NewRelease;
FBuild:=NewBuild;
FValid:=NewValid;
if Assigned(FOnChange) then FOnChange(Self);
end;
function TPkgVersion.VersionBound(v: integer): integer;
begin
if v>9999 then
Result:=9999
else if v<0 then
Result:=0
else
Result:=v;
end;
{ TLazPackageID }
constructor TPkgDependencyBase.Create;
begin
inherited Create;
MinVersion:=TPkgVersion.Create;
MaxVersion:=TPkgVersion.Create;
Clear;
end;
destructor TPkgDependencyBase.Destroy;
begin
PackageName:='';
FreeAndNil(fMinVersion);
FreeAndNil(fMaxVersion);
inherited Destroy;
end;
procedure TPkgDependencyBase.Clear;
begin
PackageName:='';
FRemoved:=false;
FFlags:=[];
FMaxVersion.Clear;
FMinVersion.Clear;
end;
function TPkgDependencyBase.IsMakingSense: boolean;
begin
Result:=IsValidPkgName(PackageName);
if Result
and (pdfMinVersion in FFlags) and (pdfMaxVersion in FFlags)
and (MinVersion.Compare(MaxVersion)>0) then
Result:=false;
end;
function TPkgDependencyBase.IsCompatible(const Version: TPkgVersion): boolean;
begin
if ((pdfMinVersion in FFlags) and (MinVersion.Compare(Version)>0))
or ((pdfMaxVersion in FFlags) and (MaxVersion.Compare(Version)<0)) then
Result:=false
else
Result:=true;
end;
function TPkgDependencyBase.IsCompatible(const PkgName: string; const Version: TPkgVersion): boolean;
begin
Result:=(CompareText(PkgName,PackageName)=0) and IsCompatible(Version);
end;
end.
|