diff options
author | demostanis <demostanis@protonmail.com> | 2022-08-22 13:24:56 +0200 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2022-09-09 14:11:52 +0100 |
commit | 1c5f6003d7b26bb012d17628217510652d60ae27 (patch) | |
tree | 0a44540583e567ef444680019a0dfac12e930922 /Userland/Libraries | |
parent | b66ceafabaa174e77c3285ce4288ee1759a4a8fe (diff) | |
download | serenity-1c5f6003d7b26bb012d17628217510652d60ae27.zip |
LibCore: Add File::open_file_or_standard_stream()
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCore/Stream.cpp | 15 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Stream.h | 2 |
2 files changed, 17 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index 5debcb987a..d6462efbbd 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -153,6 +153,21 @@ bool File::exists(StringView filename) return !Core::System::stat(filename).is_error(); } +ErrorOr<NonnullOwnPtr<File>> File::open_file_or_standard_stream(StringView filename, OpenMode mode) +{ + if (!filename.is_empty() && filename != "-"sv) + return File::open(filename, mode); + + switch (mode) { + case OpenMode::Read: + return File::adopt_fd(STDIN_FILENO, mode); + case OpenMode::Write: + return File::adopt_fd(STDOUT_FILENO, mode); + default: + VERIFY_NOT_REACHED(); + } +} + int File::open_mode_to_options(OpenMode mode) { int flags = 0; diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index f21dd8a109..da99054337 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -185,6 +185,8 @@ public: static ErrorOr<NonnullOwnPtr<File>> adopt_fd(int fd, OpenMode); static bool exists(StringView filename); + static ErrorOr<NonnullOwnPtr<File>> open_file_or_standard_stream(StringView filename, OpenMode mode); + File(File&& other) { operator=(move(other)); } File& operator=(File&& other) |