aboutsummaryrefslogtreecommitdiffstats
path: root/test/run_gunzip.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2014-08-07 17:14:58 +0200
committerMax Kellermann <max@duempel.org>2014-08-08 20:11:29 +0200
commita67a881715ca521347cc86631d770c9e6755c877 (patch)
tree762e32cadee52ea2a229010673e11d5b830b48b5 /test/run_gunzip.cxx
parent7a048f004dc1c7d35518e38c75861bc167ade945 (diff)
downloadmpd-a67a881715ca521347cc86631d770c9e6755c877.tar.gz
mpd-a67a881715ca521347cc86631d770c9e6755c877.tar.xz
mpd-a67a881715ca521347cc86631d770c9e6755c877.zip
input/GunzipInputStream: new class
Diffstat (limited to 'test/run_gunzip.cxx')
-rw-r--r--test/run_gunzip.cxx76
1 files changed, 76 insertions, 0 deletions
diff --git a/test/run_gunzip.cxx b/test/run_gunzip.cxx
new file mode 100644
index 000000000..51bdb532e
--- /dev/null
+++ b/test/run_gunzip.cxx
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2003-2014 The Music Player Daemon Project
+ * http://www.musicpd.org
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+#include "fs/io/GunzipReader.hxx"
+#include "fs/io/FileReader.hxx"
+#include "fs/io/StdioOutputStream.hxx"
+#include "util/Error.hxx"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static bool
+Copy(OutputStream &dest, Reader &src, Error &error)
+{
+ while (true) {
+ char buffer[4096];
+ size_t nbytes = src.Read(buffer, sizeof(buffer), error);
+ if (nbytes == 0)
+ return !error.IsDefined();
+
+ if (!dest.Write(buffer, nbytes, error))
+ return false;
+ }
+}
+
+static bool
+CopyGunzip(OutputStream &dest, Reader &_src, Error &error)
+{
+ GunzipReader src(_src, error);
+ return src.IsDefined() && Copy(dest, src, error);
+}
+
+static bool
+CopyGunzip(FILE *_dest, Path src_path, Error &error)
+{
+ StdioOutputStream dest(_dest);
+ FileReader src(src_path, error);
+ return src.IsDefined() && CopyGunzip(dest, src, error);
+}
+
+int
+main(int argc, gcc_unused char **argv)
+{
+ if (argc != 2) {
+ fprintf(stderr, "Usage: run_gunzip PATH\n");
+ return EXIT_FAILURE;
+ }
+
+ Path path = Path::FromFS(argv[1]);
+
+ Error error;
+ if (!CopyGunzip(stdout, path, error)) {
+ fprintf(stderr, "%s\n", error.GetMessage());
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}