Recent

Author Topic: FileSeek appears to be documented different than implementation  (Read 939 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 406
Acording to RTL reference guide
76.15.111 FileSeek
Synopsis: Set the current file position on a file handle.
Errors: On error, -1 is returned.
Description: FileSeek sets the file pointer on position Offset, starting from Origin....If successful, the function returns the new file position, relative to the beginning of the file.

The following code can be found in the development(trunc) of rtl/unix/sysutils.pp:
Code: Pascal  [Select][+][-]
  1. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  2.  
  3. Var
  4.   I : Int64;
  5.  
  6. begin
  7.   I:=FileSeek(Handle,int64(FOffset),Origin);
  8.   if I>High(Longint) then
  9.      Raise EInOutError.CreateFmt(SErrPosToBigForLongint,[I]);
  10.   result:=I;
  11. end;

As can be seen, on error the function may return -1 but it may raise exceptions, too. Should the documentation be updated?
Should I understand that the appropriate programming practice for using FileSeek is including it in a "try if FileSeek<>-1 then begin...end; except/finnaly end;" block?
Wouldn't be better if the function would return a negative value different than -1 instead of that exception, for example error code -2 or something?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: FileSeek appears to be documented different than implementation
« Reply #1 on: April 23, 2022, 04:31:06 pm »
The documentation https://www.freepascal.org/docs-html/current/rtl/sysutils/fileseek.html also says that there are two implementations of FileSeek(), one of which takes a LongInt and the other an Int64.

I suggest removing that Int64() cast and retesting.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

lagprogramming

  • Sr. Member
  • ****
  • Posts: 406
Re: FileSeek appears to be documented different than implementation
« Reply #2 on: April 23, 2022, 05:22:51 pm »
Let's assume that in a code that should work on different platforms FileSeek is called using zero and fsFromEnd.
Code: Pascal  [Select][+][-]
  1. FileSeek(FileHandle, 0, fsFromEnd);
As an example, in a linux-x86_64 environment the assembly window shows a call to <SYSUTILS_$$_FILESEEK$LONGINT$LONGINT$LONGINT$$LONGINT>
Even though the above code is legitimate, probably depending on the target platform, if FileHandle points to a file larger than High(Longint) then FileSeek(FileHandle, 0, fsFromEnd) might raise an exception.
"Write once, compile anywhere!" :-\

AlexTP

  • Hero Member
  • *****
  • Posts: 2402
    • UVviewsoft
Re: FileSeek appears to be documented different than implementation
« Reply #3 on: April 24, 2022, 08:55:25 am »
Quote
As can be seen, on error the function may return -1 but it may raise exceptions, too. Should the documentation be updated?
I don't see the error in the FileSeek. Why doc should be changed? What is wrong with it? -1 result will be handled ok.

Here one 'overload' of FileSeek calls another 'overload' with Int64.
« Last Edit: April 24, 2022, 09:02:47 am by AlexTP »

 

TinyPortal © 2005-2018