Skip to content

Commit

Permalink
dinit-iostream & dinit-fstream: Custom new special-purpose libraries
Browse files Browse the repository at this point in the history
These are replacement for C++ standard library iostream/fstream.

See dinit-iostream.h and dinit-fstream.h for more information and the usage.

Signed-off-by: Mobin "Hojjat" Aydinfar <[email protected]>
Reviewed-by: Davin McCall <EMAIL HIDDEN>
  • Loading branch information
mobin-2008 committed Dec 17, 2023
1 parent 2bf8bcc commit 60197e3
Show file tree
Hide file tree
Showing 4 changed files with 1,937 additions and 0 deletions.
165 changes: 165 additions & 0 deletions src/dinit-fstream.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <cstring>

#include <dinit-fstream.h>

// See dinit-fstream.h for inforamtion about the usage

namespace dio {

// class fstream_base
int fstream_base::open(const char *path, const int flags) noexcept
{
if (strlen(path) == 0) return -1; // Path is empty
int fd = bp_sys::open(path, flags);
if (fd < 0) return -1; // Failed
are_we_open = true;
return fd;
}

int fstream_base::open(const char *path, const int flags, const mode_t mode) noexcept
{
if (strlen(path) == 0) return -1; // Path is empty
int fd = bp_sys::open(path, flags, mode);
if (fd < 0) return -1; // Failed
are_we_open = true;
return fd;
}

bool fstream_base::is_open() noexcept
{
return are_we_open;
}

int fstream_base::close(const int fd) noexcept
{
if (!is_open()) return -1;

int r = bp_sys::close(fd);
if (r < 0) return fd; // Failed
are_we_open = false;
return -1; // -1 means successful
}

// class ofstream
bool ofstream::open() noexcept
{
setfd(fstream_base::open(path, O_WRONLY));
return (getfd() >= 0);
}

bool ofstream::open(const int flags) noexcept
{
setfd(fstream_base::open(path, O_WRONLY | flags));
return (getfd() >= 0);
}

bool ofstream::open(const int flags, const mode_t mode) noexcept
{
setfd(fstream_base::open(path, O_WRONLY | flags, mode));
return (getfd() >= 0);
}

bool ofstream::open(const char *dpath) noexcept
{
path = dpath;
open();
return (getfd() >= 0);
}

bool ofstream::open(const char *dpath, const int flags) noexcept
{
path = dpath;
open(flags);
return (getfd() >= 0);
}

bool ofstream::open(const char *dpath, const int flags, const mode_t mode) noexcept
{
path = dpath;
open(flags, mode);
return (getfd() >= 0);
}

bool ofstream::close() noexcept
{
if (is_open()) setfd(fstream_base::close(getfd()));
return (getfd() < 0);
}

bool ofstream::operator!() noexcept
{
return !is_open();
}

ofstream::operator bool() noexcept
{
return is_open();
}

ofstream::~ofstream() noexcept
{
close();
}

// class ifstream
bool ifstream::open() noexcept
{
setfd(fstream_base::open(path, O_RDONLY));
return (getfd() >= 0);
}

bool ifstream::open(const int flags) noexcept
{
setfd(fstream_base::open(path, O_RDONLY | flags));
return (getfd() >= 0);
}

bool ifstream::open(const int flags, const mode_t mode) noexcept
{
setfd(fstream_base::open(path, O_RDONLY | flags, mode));
return (getfd() >= 0);
}

bool ifstream::open(const char *dpath) noexcept
{
path = dpath;
open();
return (getfd() >= 0);
}

bool ifstream::open(const char *dpath, const int flags) noexcept
{
path = dpath;
open(flags);
return (getfd() >= 0);
}

bool ifstream::open(const char *dpath, const int flags, const mode_t mode) noexcept
{
path = dpath;
open(flags, mode);
return (getfd() >= 0);
}

bool ifstream::close() noexcept
{
if (is_open()) setfd(fstream_base::close(getfd()));
return (getfd() < 0);
}

bool ifstream::operator!() noexcept
{
return !is_open();
}

ifstream::operator bool() noexcept
{
return is_open();
}

ifstream::~ifstream() noexcept
{
close();
}

} /* dio namespace */
Loading

0 comments on commit 60197e3

Please sign in to comment.