Index: data/core/about.cfg
===================================================================
--- data/core/about.cfg	(revision 55891)
+++ data/core/about.cfg	(working copy)
@@ -1132,6 +1132,10 @@
         name = "Petr Sobotka (Pietro)"
     [/entry]
     [entry]
+        name = "Philipp Matthias Schäfer"
+        email = "philipp.schaefer@gmail.com"
+    [/entry]
+    [entry]
         name = "Pierre Bourdon (delroth)"
         email = "root_AT_delroth.is-a-geek.org"
     [/entry]
Index: src/game.cpp
===================================================================
--- src/game.cpp	(revision 55891)
+++ src/game.cpp	(working copy)
@@ -19,6 +19,7 @@
 #include "addon/manager.hpp"
 #include "addon/manager_ui.hpp"
 #include "commandline_options.hpp"
+#include "file_compression.hpp"
 #include "game_controller.hpp"
 #include "game_controller_new.hpp"
 #include "gui/dialogs/title_screen.hpp"
@@ -38,13 +39,9 @@
 
 #include <cerrno>
 #include <clocale>
-#include <fstream>
 #include <libintl.h>
 
 #include <boost/foreach.hpp>
-#include <boost/iostreams/copy.hpp>
-#include <boost/iostreams/filter/bzip2.hpp>
-#include <boost/iostreams/filter/gzip.hpp>
 
 #ifdef HAVE_VISUAL_LEAK_DETECTOR
 #include "vld.h"
@@ -75,70 +72,6 @@
 	exit(res);
 }
 
-// maybe this should go in a util file somewhere?
-template <typename filter>
-static void encode(const std::string & input_file, const std::string & output_file)
-{
-	try {
-		std::ifstream ifile(input_file.c_str(),
-				std::ios_base::in | std::ios_base::binary);
-		ifile.peek(); // We need to touch the stream to set the eof bit
-		if(!ifile.good()) {
-			std::cerr << "Input file " << input_file << " is not good for reading. Exiting to prevent bzip2 from segfaulting\n";
-			safe_exit(1);
-		}
-		std::ofstream ofile(output_file.c_str(), std::ios_base::out
-				| std::ios_base::binary);
-		boost::iostreams::filtering_stream<boost::iostreams::output> stream;
-		stream.push(filter());
-		stream.push(ofile);
-		boost::iostreams::copy(ifile, stream);
-		ifile.close();
-		safe_exit(remove(input_file.c_str()));
-	}  catch(io_exception& e) {
-		std::cerr << "IO error: " << e.what() << "\n";
-	}
-}
-
-template <typename filter>
-static void decode(const std::string & input_file, const std::string & output_file)
-{
-	try {
-		std::ofstream ofile(output_file.c_str(), std::ios_base::out
-				| std::ios_base::binary);
-		std::ifstream ifile(input_file.c_str(),
-				std::ios_base::in | std::ios_base::binary);
-		boost::iostreams::filtering_stream<boost::iostreams::input> stream;
-		stream.push(filter());
-		stream.push(ifile);
-		boost::iostreams::copy(stream, ofile);
-		ifile.close();
-		safe_exit(remove(input_file.c_str()));
-	}  catch(io_exception& e) {
-		std::cerr << "IO error: " << e.what() << "\n";
-	}
-}
-
-static void gzip_encode(const std::string & input_file, const std::string & output_file)
-{
-	encode<boost::iostreams::gzip_compressor>(input_file, output_file);
-}
-
-static void gzip_decode(const std::string & input_file, const std::string & output_file)
-{
-	decode<boost::iostreams::gzip_decompressor>(input_file, output_file);
-}
-
-static void bzip2_encode(const std::string & input_file, const std::string & output_file)
-{
-	encode<boost::iostreams::bzip2_compressor>(input_file, output_file);
-}
-
-static void bzip2_decode(const std::string & input_file, const std::string & output_file)
-{
-	decode<boost::iostreams::bzip2_decompressor>(input_file, output_file);
-}
-
 static void handle_preprocess_command(const commandline_options& cmdline_opts)
 {
 	std::string output_macros_file;
@@ -304,34 +237,34 @@
 		game_config::debug_lua = true;
 	}
 	if(cmdline_opts.gunzip) {
-		const std::string input_file(*cmdline_opts.gunzip);
-		if(!is_gzip_file(input_file)) {
-			std::cerr << "file '" << input_file << "'isn't a .gz file\n";
+		std::string const in_path(*cmdline_opts.gunzip);
+		if(!is_gzip_file(in_path)) {
+			std::cerr << "File '" << in_path << "'isn't a .gz file!" <<
+				std::endl;
 			return 2;
 		}
-		const std::string output_file(
-			input_file, 0, input_file.length() - 3);
-		gzip_decode(input_file, output_file);
+		std::string const out_path(in_path, 0, in_path.length() - 3);
+		safe_exit(gzip_decompress(in_path, out_path));
 	}
 	if(cmdline_opts.bunzip2) {
-		const std::string input_file(*cmdline_opts.bunzip2);
-		if(!is_bzip2_file(input_file)) {
-			std::cerr << "file '" << input_file << "'isn't a .bz2 file\n";
+		std::string const in_path(*cmdline_opts.bunzip2);
+		if(!is_bzip2_file(in_path)) {
+			std::cerr << "File '" << in_path << "'isn't a .bz2 file!" <<
+				std::endl;
 			return 2;
 		}
-		const std::string output_file(
-			input_file, 0, input_file.length() - 4);
-		bzip2_decode(input_file, output_file);
+		std::string const out_path(in_path, 0, in_path.length() - 4);
+		safe_exit(bzip2_decompress(in_path, out_path));
 	}
 	if(cmdline_opts.gzip) {
-		const std::string input_file(*cmdline_opts.gzip);
-		const std::string output_file(*cmdline_opts.gzip + ".gz");
-		gzip_encode(input_file, output_file);
+		std::string const in_path(*cmdline_opts.gzip);
+		std::string const out_path(in_path + ".gz");
+		safe_exit(gzip_compress(in_path, out_path));
 	}
 	if(cmdline_opts.bzip2) {
-		const std::string input_file(*cmdline_opts.bzip2);
-		const std::string output_file(*cmdline_opts.bzip2 + ".bz2");
-		bzip2_encode(input_file, output_file);
+		std::string const in_path(*cmdline_opts.bzip2);
+		std::string const out_path(in_path + ".bz2");
+		safe_exit(bzip2_compress(in_path, out_path));
 	}
 	if(cmdline_opts.help) {
 		std::cout << cmdline_opts;
Index: src/SConscript
===================================================================
--- src/SConscript	(revision 55891)
+++ src/SConscript	(working copy)
@@ -245,6 +245,7 @@
     editor/editor_preferences.cpp
     editor/toolkit/editor_toolkit.cpp
     filechooser.cpp
+    file_compression.cpp
     floating_textbox.cpp
     formula.cpp
     formula_debugger.cpp
Index: src/CMakeLists.txt
===================================================================
--- src/CMakeLists.txt	(revision 55891)
+++ src/CMakeLists.txt	(working copy)
@@ -671,6 +671,7 @@
 	editor/toolkit/editor_toolkit.cpp
 	editor/map/context_manager.cpp
 	filechooser.cpp
+	file_compression.cpp
 	floating_textbox.cpp
 	formula.cpp
 	formula_debugger.cpp
Index: src/file_compression.cpp
===================================================================
--- src/file_compression.cpp	(revision 0)
+++ src/file_compression.cpp	(working copy)
@@ -0,0 +1,78 @@
+#include <fstream>
+#include <iostream>
+#include <boost/iostreams/copy.hpp>
+#include <boost/iostreams/filter/bzip2.hpp>
+#include <boost/iostreams/filter/gzip.hpp>
+#include <boost/iostreams/filtering_stream.hpp>
+#include "file_compression.hpp"
+
+namespace {
+
+/**
+ * change a file by passing it through a filter.
+ *
+ * @tparam filter A filter of the boost::iostreams library.
+ *
+ * @param in_path Path to the input file.
+ * @param out_path Path to the output file.
+ *
+ * @returns An exit code that is supposed to be passed on to ::safe_exit (see
+ *          file game.cpp)
+ */
+template <typename filter>
+inline int filter_file(std::string const & in_path,
+					   std::string const & out_path)
+{
+	std::ifstream ifile(in_path.c_str(),
+						std::ios_base::in | std::ios_base::binary);
+
+	// The following code is necessary in some cases to prevent segmentation
+	// faults.
+	ifile.peek();
+	if(!ifile.good()) {
+		std::cerr << "Cannot read from file '" << in_path <<
+			"'. Exiting to prevent a segmentation fault!\n";
+		return 1;
+	}
+
+	std::ofstream ofile(out_path.c_str(),
+						std::ios_base::out | std::ios_base::binary);
+	boost::iostreams::filtering_ostream stream;
+	stream.push(filter());
+	stream.push(ofile);
+	boost::iostreams::copy(ifile, stream);
+	ifile.close();
+	return remove(in_path.c_str());
+}
+
+}
+
+int gzip_compress(std::string const & in_path,
+				  std::string const & out_path)
+{
+	return filter_file<boost::iostreams::gzip_compressor>(in_path,
+														  out_path);
+}
+
+int gzip_decompress(std::string const & in_path,
+					std::string const & out_path)
+{
+	return filter_file<boost::iostreams::gzip_decompressor>(in_path,
+															out_path);
+}
+
+int bzip2_compress(std::string const & in_path,
+				   std::string const & out_path)
+{
+	return filter_file<boost::iostreams::bzip2_compressor>(in_path,
+														   out_path);
+}
+
+int bzip2_decompress(std::string const & in_path,
+					 std::string const & out_path)
+{
+	return filter_file<boost::iostreams::bzip2_decompressor>(in_path,
+															 out_path);
+}
+
+

Property changes on: src/file_compression.cpp
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Revision
\ No newline at end of property
Added: svn:eolstyle
## -0,0 +1 ##
+native
\ No newline at end of property
Index: src/file_compression.hpp
===================================================================
--- src/file_compression.hpp	(revision 0)
+++ src/file_compression.hpp	(working copy)
@@ -0,0 +1,72 @@
+/* $Id$ */
+/*
+   Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
+   Part of the Battle for Wesnoth Project http://www.wesnoth.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.
+
+   See the COPYING file for more details.
+*/
+
+#include <string>
+
+/**
+ * @file
+ * Provides functions handling (de)compression of files.
+ *
+ * These functions are used by the feature provided via the --gzip, --gunzip,
+ * --bzip2, and --bunzip command line arguments.
+ */
+
+/**
+ * Compress a file using gzip.
+ *
+ * @param in_path Path to the input file.
+ * @param out_path Path to the output file.
+ *
+ * @returns An exit code that is supposed to be passed on to ::safe_exit (see
+ *          file game.cpp)
+ */
+int gzip_compress(std::string const & in_path,
+				  std::string const & out_path);
+
+/**
+ * Decompress a file using gzip.
+ *
+ * @param in_path Path to the input file.
+ * @param out_path Path to the output file.
+ *
+ * @returns An exit code that is supposed to be passed on to ::safe_exit (see
+ *          file game.cpp)
+ */
+int gzip_decompress(std::string const & in_path,
+					std::string const & out_path);
+
+/**
+ * Compress a file using bzip.
+ *
+ * @param in_path Path to the input file.
+ * @param out_path Path to the output file.
+ *
+ * @returns An exit code that is supposed to be passed on to ::safe_exit (see
+ *          file game.cpp)
+ */
+int bzip2_compress(std::string const & in_path,
+				   std::string const & out_path);
+
+/**
+ * Decompress a file using bzip.
+ *
+ * @param in_path Path to the input file.
+ * @param out_path Path to the output file.
+ *
+ * @returns An exit code that is supposed to be passed on to ::safe_exit (see
+ *          file game.cpp)
+ */
+int bzip2_decompress(std::string const & in_path,
+					 std::string const & out_path);

Property changes on: src/file_compression.hpp
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Revision
\ No newline at end of property
Added: svn:eolstyle
## -0,0 +1 ##
+native
\ No newline at end of property
