I'm trying to make a class in Delphi that handles files. I have a property that returns the size of the file and another one that returns the position of the file. I don't know if any error can happen with these calls. Should I raise an exception?
My code is:
function TFile.GetSize: Int64;
var
FileSizeHi, FileSizeLo: Cardinal;
begin
FileSizeLo := GetFileSize(FHandle, @FileSizeHi);
if (FileSizeLo = INVALID_FILE_SIZE) and (GetLastError = NO_ERROR) then
Result := $FFFFFFFF
else
Result := FileSizeLo or Int64(FileSizeHi) shl 32;
end;
function TFile.GetPosition: Int64;
var
FilePosHi, FilePosLo: Cardinal;
begin
FilePosHi := 0;
FilePosLo := 0;
FilePosLo := SetFilePointer(FHandle, FilePosLo, @FilePosHi, FILE_CURRENT);
if (FilePosLo = INVALID_SET_FILE_POINTER) and (GetLastError = NO_ERROR) then
Result := $FFFFFFFF
else
Result := FilePosLo or Int64(FilePosHi) shl 32;
end;
I don't know what error could happen when I call GetFileSize or SetFilePointer (without moving the file pointer).
Int64Rec
to do right awayInt64Rec(Result).Lo:= GetFileSize( FHandle, @Int64Rec(Result).Hi );
and/orif Int64Rec(Result).Lo= MAXDWORD
without any intermediate variables.