mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-29 14:14:45 +03:00
Userland: Use Buffered<T> in gunzip.
This commit is contained in:
parent
359fcf348f
commit
612c1bc84d
Notes:
sideshowbarker
2024-07-19 02:53:59 +09:00
Author: https://github.com/asynts Commit: https://github.com/SerenityOS/serenity/commit/612c1bc84d6 Pull-request: https://github.com/SerenityOS/serenity/pull/3405
@ -26,6 +26,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Buffered.h>
|
||||
#include <AK/Stream.h>
|
||||
#include <LibCore/File.h>
|
||||
|
||||
@ -50,6 +51,18 @@ public:
|
||||
return InputFileStream { file_result.value() };
|
||||
}
|
||||
|
||||
static Result<Buffered<InputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::ReadOnly, mode_t permissions = 0644)
|
||||
{
|
||||
ASSERT((mode & 0xf) == IODevice::OpenMode::ReadOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
|
||||
|
||||
auto file_result = File::open(filename, mode, permissions);
|
||||
|
||||
if (file_result.is_error())
|
||||
return file_result.error();
|
||||
|
||||
return Buffered<InputFileStream> { file_result.value() };
|
||||
}
|
||||
|
||||
size_t read(Bytes bytes) override
|
||||
{
|
||||
size_t nread = 0;
|
||||
@ -145,11 +158,28 @@ public:
|
||||
return OutputFileStream { file_result.value() };
|
||||
}
|
||||
|
||||
static Result<Buffered<OutputFileStream>, String> open_buffered(StringView filename, IODevice::OpenMode mode = IODevice::OpenMode::WriteOnly, mode_t permissions = 0644)
|
||||
{
|
||||
ASSERT((mode & 0xf) == IODevice::OpenMode::WriteOnly || (mode & 0xf) == IODevice::OpenMode::ReadWrite);
|
||||
|
||||
auto file_result = File::open(filename, mode, permissions);
|
||||
|
||||
if (file_result.is_error())
|
||||
return file_result.error();
|
||||
|
||||
return Buffered<OutputFileStream> { file_result.value() };
|
||||
}
|
||||
|
||||
static OutputFileStream stdout()
|
||||
{
|
||||
return OutputFileStream { Core::File::stdout() };
|
||||
}
|
||||
|
||||
static Buffered<OutputFileStream> stdout_buffered()
|
||||
{
|
||||
return Buffered<OutputFileStream> { Core::File::stdout() };
|
||||
}
|
||||
|
||||
size_t write(ReadonlyBytes bytes) override
|
||||
{
|
||||
if (!m_file->write(bytes.data(), bytes.size())) {
|
||||
|
@ -28,7 +28,7 @@
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/FileStream.h>
|
||||
|
||||
static void decompress_file(Core::InputFileStream input_stream, Core::OutputFileStream output_stream)
|
||||
static void decompress_file(Buffered<Core::InputFileStream>& input_stream, Buffered<Core::OutputFileStream>& output_stream)
|
||||
{
|
||||
auto gzip_stream = Compress::GzipDecompressor { input_stream };
|
||||
|
||||
@ -38,9 +38,6 @@ static void decompress_file(Core::InputFileStream input_stream, Core::OutputFile
|
||||
const auto nread = gzip_stream.read({ buffer, sizeof(buffer) });
|
||||
output_stream.write_or_error({ buffer, nread });
|
||||
}
|
||||
|
||||
input_stream.close();
|
||||
output_stream.close();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
@ -64,12 +61,13 @@ int main(int argc, char** argv)
|
||||
const auto input_filename = filename;
|
||||
const auto output_filename = filename.substring_view(0, filename.length() - 3);
|
||||
|
||||
auto input_stream_result = Core::InputFileStream::open(input_filename);
|
||||
auto input_stream_result = Core::InputFileStream::open_buffered(input_filename);
|
||||
|
||||
if (write_to_stdout) {
|
||||
decompress_file(input_stream_result.value(), Core::OutputFileStream::stdout());
|
||||
auto stdout = Core::OutputFileStream::stdout_buffered();
|
||||
decompress_file(input_stream_result.value(), stdout);
|
||||
} else {
|
||||
auto output_stream_result = Core::OutputFileStream::open(output_filename);
|
||||
auto output_stream_result = Core::OutputFileStream::open_buffered(output_filename);
|
||||
decompress_file(input_stream_result.value(), output_stream_result.value());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user