mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:27:45 +00:00
LibCore: Add standard_{output, input, error} functions to File::Stream
This commit is contained in:
parent
e0cccaa583
commit
d9360676cd
2 changed files with 18 additions and 2 deletions
|
@ -148,6 +148,19 @@ bool File::exists(StringView filename)
|
|||
return !Core::System::stat(filename).is_error();
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<File>> File::standard_input()
|
||||
{
|
||||
return File::adopt_fd(STDIN_FILENO, OpenMode::Read, ShouldCloseFileDescriptor::No);
|
||||
}
|
||||
ErrorOr<NonnullOwnPtr<File>> File::standard_output()
|
||||
{
|
||||
return File::adopt_fd(STDOUT_FILENO, OpenMode::Write, ShouldCloseFileDescriptor::No);
|
||||
}
|
||||
ErrorOr<NonnullOwnPtr<File>> File::standard_error()
|
||||
{
|
||||
return File::adopt_fd(STDERR_FILENO, OpenMode::Write, ShouldCloseFileDescriptor::No);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<File>> File::open_file_or_standard_stream(StringView filename, OpenMode mode)
|
||||
{
|
||||
if (!filename.is_empty() && filename != "-"sv)
|
||||
|
@ -155,9 +168,9 @@ ErrorOr<NonnullOwnPtr<File>> File::open_file_or_standard_stream(StringView filen
|
|||
|
||||
switch (mode) {
|
||||
case OpenMode::Read:
|
||||
return File::adopt_fd(STDIN_FILENO, mode);
|
||||
return standard_input();
|
||||
case OpenMode::Write:
|
||||
return File::adopt_fd(STDOUT_FILENO, mode);
|
||||
return standard_output();
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
|
|
@ -198,6 +198,9 @@ public:
|
|||
static ErrorOr<NonnullOwnPtr<File>> adopt_fd(int fd, OpenMode, ShouldCloseFileDescriptor = ShouldCloseFileDescriptor::Yes);
|
||||
static bool exists(StringView filename);
|
||||
|
||||
static ErrorOr<NonnullOwnPtr<File>> standard_input();
|
||||
static ErrorOr<NonnullOwnPtr<File>> standard_output();
|
||||
static ErrorOr<NonnullOwnPtr<File>> standard_error();
|
||||
static ErrorOr<NonnullOwnPtr<File>> open_file_or_standard_stream(StringView filename, OpenMode mode);
|
||||
|
||||
File(File&& other) { operator=(move(other)); }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue