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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
{$mode objfpc}
{$h+}
unit testu;
Interface
uses
dos;
{ ---------------------------------------------------------------------
utility functions, shared by several programs of the test suite
---------------------------------------------------------------------}
type
TCharSet = set of char;
TVerboseLevel=(V_Abort,V_Error,V_Warning,V_Normal,V_Debug,V_SQL);
TConfig = record
NeedOptions,
DelOptions,
NeedCPU,
SkipCPU,
SkipEmu,
NeedTarget,
SkipTarget,
MinVersion,
MaxVersion,
KnownRunNote,
KnownCompileNote,
RecompileOpt: string;
ResultCode : longint;
KnownRunError : longint;
KnownCompileError : longint;
NeedRecompile : boolean;
NeedLibrary : boolean;
NeededAfter : boolean;
IsInteractive : boolean;
IsKnownRunError,
IsKnownCompileError : boolean;
NoRun : boolean;
UsesGraph : boolean;
ShouldFail : boolean;
Timeout : longint;
Category : string;
Note : string;
Files : string;
ConfigFileSrc : string;
ConfigFileDst : string;
WpoParas : string;
WpoPasses : longint;
DelFiles : string;
end;
Const
DoVerbose : boolean = false;
DoSQL : boolean = false;
MaxLogSize : LongInt = 50000;
procedure TrimB(var s:string);
procedure TrimE(var s:string);
function upper(const s : string) : string;
procedure Verbose(lvl:TVerboseLevel;const s:string);
function GetConfig(const fn:string;var r:TConfig):boolean;
Function GetFileContents (FN : String) : String;
const
{ Constants used in IsAbsolute function }
TargetHasDosStyleDirectories : boolean = false;
TargetAmigaLike : boolean = false;
TargetIsMacOS : boolean = false;
TargetIsUnix : boolean = false;
{ File path helper functions }
function SplitPath(const s:string):string;
function SplitBasePath(const s:string): string;
Function SplitFileName(const s:string):string;
Function SplitFileBase(const s:string):string;
Function SplitFileExt(const s:string):string;
Function FileExists (Const F : String) : Boolean;
Function PathExists (Const F : String) : Boolean;
Function IsAbsolute (Const F : String) : boolean;
function GetToken(var s: string; Delims: TCharSet = [' ']):string;
Implementation
function GetToken(var s: string; Delims: TCharSet = [' ']):string;
var
i : longint;
p: PChar;
begin
p:=PChar(s);
i:=0;
while (p^ <> #0) and not (p^ in Delims) do begin
Inc(p);
Inc(i);
end;
GetToken:=Copy(s,1,i);
Delete(s,1,i+1);
end;
function SplitPath(const s:string):string;
var
i : longint;
begin
i:=Length(s);
while (i>0) and not(s[i] in ['/','\'{$IFDEF MACOS},':'{$ENDIF}]) do
dec(i);
SplitPath:=Copy(s,1,i);
end;
function SplitBasePath(const s:string): string;
var
i : longint;
begin
i:=1;
while (i<length(s)) and not(s[i] in ['/','\'{$IFDEF MACOS},':'{$ENDIF}]) do
inc(i);
if s[i] in ['/','\'{$IFDEF MACOS},':'{$ENDIF}] then
dec(i);
SplitBasePath:=Copy(s,1,i);
end;
Function SplitFileName(const s:string):string;
var
p : dirstr;
n : namestr;
e : extstr;
begin
FSplit(s,p,n,e);
SplitFileName:=n+e;
end;
Function SplitFileBase(const s:string):string;
var
p : dirstr;
n : namestr;
e : extstr;
begin
FSplit(s,p,n,e);
SplitFileBase:=n;
end;
Function SplitFileExt(const s:string):string;
var
p : dirstr;
n : namestr;
e : extstr;
begin
FSplit(s,p,n,e);
SplitFileExt:=e;
end;
Function FileExists (Const F : String) : Boolean;
{
Returns True if the file exists, False if not.
}
Var
info : searchrec;
begin
FindFirst (F,anyfile,Info);
FileExists:=DosError=0;
FindClose (Info);
end;
Function PathExists (Const F : String) : Boolean;
{
Returns True if the file exists, False if not.
}
Var
info : searchrec;
begin
FindFirst (F,anyfile,Info);
PathExists:=(DosError=0) and (Info.Attr and Directory=Directory);
FindClose (Info);
end;
{ extracted from rtl/macos/macutils.inc }
function IsMacFullPath (const path: string): Boolean;
begin
if Pos(':', path) = 0 then {its partial}
IsMacFullPath := false
else if path[1] = ':' then
IsMacFullPath := false
else
IsMacFullPath := true
end;
Function IsAbsolute (Const F : String) : boolean;
{
Returns True if the name F is a absolute file name
}
begin
IsAbsolute:=false;
if TargetHasDosStyleDirectories then
begin
if (F[1]='/') or (F[1]='\') then
IsAbsolute:=true;
if (Length(F)>2) and (F[2]=':') and ((F[3]='\') or (F[3]='/')) then
IsAbsolute:=true;
end
else if TargetAmigaLike then
begin
if (length(F)>0) and (Pos(':',F) <> 0) then
IsAbsolute:=true;
end
else if TargetIsMacOS then
begin
IsAbsolute:=IsMacFullPath(F);
end
{ generic case }
else if (F[1]='/') then
IsAbsolute:=true;
end;
procedure Verbose(lvl:TVerboseLevel;const s:string);
begin
case lvl of
V_Normal :
writeln(s);
V_Debug :
if DoVerbose then
writeln('Debug: ',s);
V_SQL :
if DoSQL then
writeln('SQL: ',s);
V_Warning :
writeln('Warning: ',s);
V_Error :
begin
writeln('Error: ',s);
halt(1);
end;
V_Abort :
begin
writeln('Abort: ',s);
halt(0);
end;
end;
end;
procedure TrimB(var s:string);
begin
while (s<>'') and (s[1] in [' ',#9]) do
delete(s,1,1);
end;
procedure TrimE(var s:string);
begin
while (s<>'') and (s[length(s)] in [' ',#9]) do
delete(s,length(s),1);
end;
function upper(const s : string) : string;
var
i,l : longint;
begin
L:=Length(S);
SetLength(upper,l);
for i:=1 to l do
if s[i] in ['a'..'z'] then
upper[i]:=char(byte(s[i])-32)
else
upper[i]:=s[i];
end;
function GetConfig(const fn:string;var r:TConfig):boolean;
var
t : text;
part,code : integer;
l : longint;
s,res : string;
function GetEntry(const entry:string):boolean;
var
i : longint;
begin
Getentry:=false;
Res:='';
if Upper(Copy(s,1,length(entry)))=Upper(entry) then
begin
Delete(s,1,length(entry));
TrimB(s);
if (s<>'') then
begin
if (s[1]='=') then
begin
delete(s,1,1);
i:=pos('}',s);
if i=0 then
i:=255
else
dec(i);
res:=Copy(s,1,i);
TrimB(res);
TrimE(res);
end;
Verbose(V_Debug,'Config: '+Entry+' = "'+Res+'"');
GetEntry:=true;
end;
end;
end;
begin
FillChar(r,sizeof(r),0);
GetConfig:=false;
Verbose(V_Debug,'Reading '+fn);
assign(t,fn);
{$I-}
reset(t);
{$I+}
if ioresult<>0 then
begin
Verbose(V_Error,'Can''t open '+fn);
exit;
end;
r.Note:='';
while not eof(t) do
begin
readln(t,s);
if Copy(s,1,3)=#$EF#$BB#$BF then
delete(s,1,3);
TrimB(s);
if s<>'' then
begin
if s[1]='{' then
begin
delete(s,1,1);
TrimB(s);
if (s<>'') and (s[1]='%') then
begin
delete(s,1,1);
if GetEntry('OPT') then
r.NeedOptions:=res
else
if GetEntry('DELOPT') then
r.DelOptions:=res
else
if GetEntry('TARGET') then
r.NeedTarget:=res
else
if GetEntry('SKIPTARGET') then
r.SkipTarget:=res
else
if GetEntry('CPU') then
r.NeedCPU:=res
else
if GetEntry('SKIPCPU') then
r.SkipCPU:=res
else
if GetEntry('SKIPEMU') then
r.SkipEmu:=res
else
if GetEntry('VERSION') then
r.MinVersion:=res
else
if GetEntry('MAXVERSION') then
r.MaxVersion:=res
else
if GetEntry('RESULT') then
Val(res,r.ResultCode,code)
else
if GetEntry('GRAPH') then
r.UsesGraph:=true
else
if GetEntry('FAIL') then
r.ShouldFail:=true
else
if GetEntry('RECOMPILE') then
begin
r.NeedRecompile:=true;
r.RecompileOpt:=res;
end
else
if GetEntry('NORUN') then
r.NoRun:=true
else
if GetEntry('NEEDLIBRARY') then
r.NeedLibrary:=true
else
if GetEntry('NEEDEDAFTER') then
r.NeededAfter:=true
else
if GetEntry('KNOWNRUNERROR') then
begin
r.IsKnownRunError:=true;
if res<>'' then
begin
val(res,l,code);
if code>1 then
begin
part:=code;
val(copy(res,1,code-1),l,code);
delete(res,1,part);
end;
if code=0 then
r.KnownRunError:=l;
if res<>'' then
r.KnownRunNote:=res;
end;
end
else
if GetEntry('KNOWNCOMPILEERROR') then
begin
r.IsKnownCompileError:=true;
if res<>'' then
begin
val(res,l,code);
if code>1 then
begin
part:=code;
val(copy(res,1,code-1),l,code);
delete(res,1,part);
end;
if code=0 then
r.KnownCompileError:=l;
if res<>'' then
r.KnownCompileNote:=res;
end;
end
else
if GetEntry('INTERACTIVE') then
r.IsInteractive:=true
else
if GetEntry('NOTE') then
begin
R.Note:='Note: '+res;
Verbose(V_Normal,r.Note);
end
else
if GetEntry('TIMEOUT') then
Val(res,r.Timeout,code)
else
if GetEntry('FILES') then
r.Files:=res
else
if GetEntry('CONFIGFILE') then
begin
l:=Pos(' ',res);
if l>0 then
begin
r.ConfigFileSrc:=Copy(res,1,l-1);
r.ConfigFileDst:=Copy(res,l+1,Length(res)-l+1);
if r.ConfigFileSrc='' then
Verbose(V_Error,'Config file source is empty');
if r.ConfigFileDst='' then
Verbose(V_Error,'Config file destination is empty');
end
else
begin
r.ConfigFileSrc:=res;
r.ConfigFileDst:=res;
end;
end
else
if GetEntry('WPOPARAS') then
r.wpoparas:=res
else
if GetEntry('WPOPASSES') then
val(res,r.wpopasses,code)
else
if GetEntry('DELFILES') then
r.DelFiles:=res
else
Verbose(V_Error,'Unknown entry: '+s);
end;
end
else
break;
end;
end;
close(t);
GetConfig:=true;
end;
Function GetFileContents (FN : String) : String;
Var
F : Text;
S : String;
begin
Result:='';
Assign(F,FN);
{$I-}
Reset(F);
If IOResult<>0 then
Exit;
{$I+}
While Not(EOF(F)) do
begin
ReadLn(F,S);
if length(Result)<MaxLogSize then
Result:=Result+S+LineEnding;
end;
Close(F);
end;
end.
|