-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dinit-iostream & dinit-fstream: Custom new special-purpose libraries
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
1 parent
2bf8bcc
commit c2f6379
Showing
4 changed files
with
1,877 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
#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 (path == nullptr || path[0] == '\0') { | ||
errno = EINVAL; | ||
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 (path == nullptr || path[0] == '\0') { | ||
errno = EINVAL; | ||
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 */ |
Oops, something went wrong.