File.Seek for AGS script

Started by Crimson Wizard, Fri 29/08/2014 15:54:18

Previous topic - Next topic

Crimson Wizard

I am implementing a Seek function for the File class in AGS script, and would like to know opinion on its parameters.
Should it follow the C fseek standart in the sense of declaration?

Code: ags

enum StreamSeek
{
   eSeekBegin,
   eSeekCurrent,
   eSeekEnd
};

managed struct File
{
...
    import bool Seek(int offset, StreamSeek whence = eSeekCurrent);

    readonly import attribute int Position;
...
}


Is the function protoype (return value, order of arguments) is good, and is the default value convenient?

monkey0506

My only critique would be that AGS doesn't really have generic streams, so maybe FileSeek would be a better enum name.

Oh, and "whence" does feel a bit antiquated (from a native speaker's POV). Maybe just "from".

Crimson Wizard

I am thinking also to change the return value to integer instead and let Seek return new position, or -1 if something dire happened.

monkey0506

That would make plenty of sense to me. ;)

While you're at it, if I could, I'd like to suggest you add File.Copy and File.Rename as well. These functions would help really make the file class complete.

Radiant

This sounds fine to me, although the only parameter I've ever used in C is SeekBegin. Generally speaking, following C/C++ standards would be a good thing.

What I'm interested in though is exception handling. If the file handle is invalid, the offset is negative, and particularly if the offset is greater than the file size, will this return -1? Throw a message box? Crash to desktop?

Crimson Wizard

#5
Seek returns current Position, and Position property returns -1 for all errors.

The declaration we use now is:
Code: ags

enum FileSeek {
  eSeekBegin = 0,
  eSeekCurrent = 1,
  eSeekEnd = 2
};

<...>

/// Moves file cursor by specified offset, returns new position.
import int Seek(int offset, FileSeek origin = eSeekCurrent);
/// Gets current cursor position inside the file.
readonly import attribute int Position;


We will be releasing another alpha (3.4.0 this time) soon with File.Seek included.

SMF spam blocked by CleanTalk