mpd/test/run_gunzip.cxx

58 lines
1.1 KiB
C++
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
2014-08-07 17:14:58 +02:00
2021-12-03 14:02:07 +01:00
#include "lib/zlib/GunzipReader.hxx"
#include "io/FileReader.hxx"
#include "io/StdioOutputStream.hxx"
#include "fs/NarrowPath.hxx"
2018-07-17 21:56:43 +02:00
#include "util/PrintException.hxx"
2014-08-07 17:14:58 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void
Copy(OutputStream &dest, Reader &src)
2014-08-07 17:14:58 +02:00
{
while (true) {
2023-05-15 11:00:21 +02:00
std::byte buffer[4096];
2023-10-05 10:25:16 +02:00
std::size_t nbytes = src.Read(buffer);
2014-08-07 17:14:58 +02:00
if (nbytes == 0)
break;
2014-08-07 17:14:58 +02:00
2023-05-15 11:00:21 +02:00
dest.Write(std::span{buffer}.first(nbytes));
2014-08-07 17:14:58 +02:00
}
}
static void
CopyGunzip(OutputStream &dest, Reader &_src)
2014-08-07 17:14:58 +02:00
{
GunzipReader src(_src);
Copy(dest, src);
2014-08-07 17:14:58 +02:00
}
static void
CopyGunzip(FILE *_dest, Path src_path)
2014-08-07 17:14:58 +02:00
{
StdioOutputStream dest(_dest);
FileReader src(src_path);
CopyGunzip(dest, src);
2014-08-07 17:14:58 +02:00
}
int
main(int argc, [[maybe_unused]] char **argv)
2018-07-17 21:56:43 +02:00
try {
2014-08-07 17:14:58 +02:00
if (argc != 2) {
fprintf(stderr, "Usage: run_gunzip PATH\n");
return EXIT_FAILURE;
}
FromNarrowPath path = argv[1];
2014-08-07 17:14:58 +02:00
2018-07-17 21:56:43 +02:00
CopyGunzip(stdout, path);
return EXIT_SUCCESS;
} catch (...) {
PrintException(std::current_exception());
return EXIT_FAILURE;
2014-08-07 17:14:58 +02:00
}