mirror of
https://github.com/RGBCube/serenity
synced 2025-05-19 04:05:07 +00:00
LibGUI: Add GFile and base class GIODevice.
Working with the LibC API's is tedious, so let's add some comfy C++ API's.
This commit is contained in:
parent
ef05d8cbf6
commit
ce7017e1ec
6 changed files with 230 additions and 9 deletions
56
LibGUI/GFile.cpp
Normal file
56
LibGUI/GFile.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
#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;
|
||||
}
|
||||
|
||||
bool GFile::close()
|
||||
{
|
||||
if (fd() < 0 || mode() == NotOpen)
|
||||
return false;
|
||||
int rc = ::close(fd());
|
||||
if (rc < 0) {
|
||||
set_error(rc);
|
||||
return false;
|
||||
}
|
||||
set_fd(-1);
|
||||
set_mode(GIODevice::NotOpen);
|
||||
return true;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue