How to check return value of DWScript FileCreate function?
Asked Answered
B

1

7

Using DWScript, I don't see obvious way of checking the return value of the FileCreate function.

Example (not working) script:

function TestFileCreate : Boolean;
var
    F : File;
begin
    F := FileCreate('MyTestFile.txt');
    Result := (F = -1);        // Not working!
    Result := (F.Handle = -1); // Not working!
end;

Extract from DWScript source code:

procedure TFileCreateFunc.DoEvalAsVariant(const args : TExprBaseListExec; var result : Variant);
var
   h : THandle;
   i : IdwsFileHandle;
begin
   h:=FileCreate(args.AsFileName[0]);
   i:=TdwsFileHandle.Create(h);
   Result:=IUnknown(i);
end;

As you can see, internally, Delphi CreateFile is called and the result value is -1 when it fails. This numeric value is converted to a IdwsFileHandle.

Bushbuck answered 2/2, 2016 at 14:52 Comment(0)
S
2

They were intended to raise an exception in case of failure. This has now beed fixed!

Extended so that now exception is triggered only in case of use of an invalid file, except for two functions: FileIsValid and FileClose. Also added helpers so that file functions can be used like methods (ie. "FileIsValid(f)" can also be written as "f.IsValid")

Stonybroke answered 15/2, 2016 at 10:25 Comment(2)
I accept the solution "as is" but I had preferred to have a testable return value just like the original FileCreate function.Bushbuck
Hmmm, I will probably change to that and add the missing stuff, raising the exception may cause regressions in current code (previously the exception was raised when trying to use the file, and people may have placed try..except around that rather than the Open/Create)Stonybroke

© 2022 - 2024 — McMap. All rights reserved.