ladybird/LibGUI/GFile.cpp
Andreas Kling 8e3d0a23d5 LibGUI: Add GTCPSocket and base class GSocket (inherits from GIODevice.)
And use these to do the line-by-line reading automagically instead of having
that logic in IRCClient. This will definitely come in handy.
2019-03-18 14:09:58 +01:00

43 lines
916 B
C++

#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;
}