File.seek() offset parameter should be enum, not int.

Bastiaan Veelo Bastiaan at Veelo.net
Mon Apr 8 13:56:24 UTC 2019


Hi,

I'm just returning from a debugging session of an error that in 
my eyes was caused by a badly implemented File.seek() from 
Phobos, which has the signature

   /**
     Calls $(HTTP 
cplusplus.com/reference/clibrary/cstdio/fseek.html, fseek)
     for the file handle.

     Throws: `Exception` if the file is not opened.
             `ErrnoException` if the call to `fseek` fails.
   */
   void seek(long offset, int origin = SEEK_SET) @trusted
   { /*...*/ }

Unfamiliar with the ins and outs of the C stdlib, and in absence 
if sufficient Phobos documentation, I assumed origin to be a 
position in bytes. I need the offset from the current position, 
so I used a call to File.tell() in place of origin. I assumed 
SEEK_SET to be defined as a negative number. My program 
terminated without any message as soon as origin == 4 (on 
Windows).

I now know that origin MUST be any of SEEK_SET, SEEK_CUR or 
SEEK_END, and nothing else. These are defined in core.stdc.stdio, 
but they aren't linked to the Phobos documentation of seek.

My misunderstanding is caused by Phobos unnecessarily propagating 
the shortcomings of C into its API.

I am prepared to contribute a PR to fix this, please let me know 
if my plan has any holes:

1) Change the enum containing SEEK_* to a named enum,
2) Change the type of the origin parameter to this enum.
3) Document.

Would we need to keep the original signature around as an 
overload to prevent code breakage? Maybe not? But if so, would 
that work without conflicts?

Thanks,
Bastiaan.


More information about the Digitalmars-d mailing list