Re: [Vala] posix.vapi additions



Michael 'Mickey' Lauer wrote:
I found the need for open(2) and friends. Please add to posix.vapi:

    [CCode (cheader_filename = "fcntl.h")]
    public const int O_ACCMODE;
    [CCode (cheader_filename = "fcntl.h")]
    public const int O_RDONLY;

...
    [CCode (cheader_filename = "unistd.h")]
    public int open(string pathname, int flags);

    [CCode (cheader_filename = "unistd.h")]
    public ssize_t read(int fd, void* buf, size_t count);

    [CCode (cheader_filename = "unistd.h")]
    public ssize_t write(int fd, void* buf, size_t count);

    [CCode (cheader_filename = "unistd.h")]
    public int close(int fd);

Hi,

I'd suggest binding it this way:

---------------------------------------------------------------------
[Flags]
[CCode (cheader_filename = "fcntl.h", cprefix = "O_", cname = "int")]
public enum OpenFlags {
        ACCMODE,
        RDONLY,
        WRONLY,
        RDWR,
        CREAT,
        EXCL,
        NOCTTY,
        TRUNC,
        APPEND,
        NONBLOCK,
        NDELAY,
        SYNC,
        FSYNC,
        ASYNC
}

[SimpleType]
[CCode (cheader_filename = "unistd.h", cname = "int",
        free_function = "close", cprefix = "")]
struct FileDescriptor {

        [CCode (cname = "open")]
        public FileDescriptor (string pathname, OpenFlags flags);

        public ssize_t read (void* buf, size_t count);
        public ssize_t write (void* buf, size_t count);
}
---------------------------------------------------------------------

Then the code will look more object oriented:


using Posix;

static int main () {
        var file = FileDescriptor ("data.text", OpenFlags.RDONLY);
        file.read (/*...*/);
        file.write (/*...*/);
        // file closed automatically
        return 0;
}


Regards,

Frederik



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]