summaryrefslogtreecommitdiff
path: root/LibGUI/GFile.cpp
blob: e0ff42e0bb5913ee059fbd1e736cf187b855f88a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <LibGUI/GFile.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

GFile::GFile(const String& filename)
    : m_filename(filename)
{
}

GFile::~GFile()
{
    if (mode() != NotOpen)
        close();
}

bool GFile::open(GIODevice::OpenMode mode)
{
    int flags = 0;
    if ((mode & GIODevice::ReadWrite) == GIODevice::ReadWrite) {
        flags |= O_RDWR | O_CREAT;
    } else if (mode & GIODevice::ReadOnly) {
        flags |= O_RDONLY;
    } else if (mode & GIODevice::WriteOnly) {
        flags |= O_WRONLY | O_CREAT;
    }
    if (mode & GIODevice::Append)
        flags |= O_APPEND;
    if (mode & GIODevice::Truncate)
        flags |= O_TRUNC;
    if (mode & GIODevice::MustBeNew)
        flags |= O_EXCL;
    int fd = ::open(m_filename.characters(), flags, 0666);
    if (fd < 0) {
        set_error(errno);
        return false;
    }

    set_fd(fd);
    set_mode(mode);
    return true;
}