Merge branch 'bzip2'

This commit is contained in:
Alexander van Gessel 2012-10-08 15:30:56 +01:00
parent 9526f9e3bf
commit 3ae335c5fe
8 changed files with 99 additions and 14 deletions

View file

@ -318,6 +318,7 @@ if env["prereqs"]:
conf.CheckGettextLibintl() and \
conf.CheckBoost("iostreams", require_version = "1.34.1") and \
conf.CheckBoostIostreamsGZip() and \
conf.CheckBoostIostreamsBZip2() and \
conf.CheckBoost("smart_ptr", header_only = True) and \
conf.CheckSDL(require_version = '1.2.7') and \
conf.CheckSDL('SDL_net') and \

View file

@ -139,4 +139,34 @@ def CheckBoostIostreamsGZip(context):
context.Result("no")
return False
config_checks = { "CheckBoost" : CheckBoost, "CheckBoostIostreamsGZip" : CheckBoostIostreamsGZip }
def CheckBoostIostreamsBZip2(context):
env = context.env
backup = env.Clone().Dictionary()
context.Message("Checking for bzip2 support in Boost Iostreams... ")
test_program = """
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
int main()
{
boost::iostreams::filtering_stream<boost::iostreams::output> filter;
filter.push(boost::iostreams::bzip2_compressor(boost::iostreams::bzip2_params()));
}
\n"""
for libbz2 in ["", "bz2"]:
env.Append(LIBS = [libbz2])
comment = ""
if libbz2:
comment = " //Trying to link against '%s'.\n" % libbz2
if context.TryLink(comment + test_program, ".cpp"):
context.Result("yes")
return True
else:
env.Replace(**backup)
context.Result("no")
return False
config_checks = { "CheckBoost" : CheckBoost, "CheckBoostIostreamsGZip" : CheckBoostIostreamsGZip, "CheckBoostIostreamsBZip2" : CheckBoostIostreamsBZip2 }

View file

@ -878,6 +878,17 @@ bool is_gzip_file(const std::string& filename)
&& filename.substr(filename.length() - 3) == ".gz");
}
/**
* Returns true if the file ends with '.bz2'.
*
* @param filename The name to test.
*/
bool is_bzip2_file(const std::string& filename)
{
return (filename.length() > 4
&& filename.substr(filename.length() - 4) == ".bz2");
}
file_tree_checksum::file_tree_checksum()
: nfiles(0), sum_size(0), modified(0)
{}

View file

@ -132,6 +132,13 @@ time_t file_create_time(const std::string& fname);
/** Returns true if the file ends with '.gz'. */
bool is_gzip_file(const std::string& filename);
/** Returns true if the file ends with '.bz2'. */
bool is_bzip2_file(const std::string& filename);
inline bool is_compressed_file(const std::string& filename) {
return is_gzip_file(filename) || is_bzip2_file(filename);
}
struct file_tree_checksum
{
file_tree_checksum();

View file

@ -75,7 +75,8 @@ static void safe_exit(int res) {
}
// maybe this should go in a util file somewhere?
static void gzip_codec(const std::string & input_file, const std::string & output_file, bool encode)
template <typename filter>
static void codec(const std::string & input_file, const std::string & output_file)
{
try {
std::ofstream ofile(output_file.c_str(), std::ios_base::out
@ -83,10 +84,7 @@ static void gzip_codec(const std::string & input_file, const std::string & outpu
std::ifstream ifile(input_file.c_str(),
std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
if(encode)
in.push(boost::iostreams::gzip_compressor());
else
in.push(boost::iostreams::gzip_decompressor());
in.push(filter());
in.push(ifile);
boost::iostreams::copy(in, ofile);
ifile.close();
@ -98,12 +96,12 @@ static void gzip_codec(const std::string & input_file, const std::string & outpu
static void gzip_encode(const std::string & input_file, const std::string & output_file)
{
gzip_codec(input_file, output_file, true);
codec<boost::iostreams::gzip_compressor>(input_file, output_file);
}
static void gzip_decode(const std::string & input_file, const std::string & output_file)
{
gzip_codec(input_file, output_file, false);
codec<boost::iostreams::gzip_decompressor>(input_file, output_file);
}
static void handle_preprocess_command(const commandline_options& cmdline_opts)

View file

@ -409,12 +409,22 @@ void read_save_file(const std::string& name, config& cfg, std::string* error_log
if (file_stream->fail()) {
file_stream = istream_file(get_saves_dir() + "/" + name);
}
if(file_stream->fail() && !is_gzip_file(modified_name)) {
if(file_stream->fail() && !is_compressed_file(modified_name)) {
file_stream = istream_file(get_saves_dir() + "/" + modified_name + ".gz");
if (file_stream->fail()) {
file_stream = istream_file(get_saves_dir() + "/" + name + ".gz");
}
modified_name += ".gz";
if (!file_stream->fail()) {
modified_name += ".gz";
} else {
file_stream = istream_file(get_saves_dir() + "/" + modified_name + ".bz2");
if (file_stream->fail()) {
file_stream = istream_file(get_saves_dir() + "/" + name + ".bz2");
}
if (!file_stream->fail()) {
modified_name += ".bz2";
}
}
}
cfg.clear();
@ -425,6 +435,8 @@ void read_save_file(const std::string& name, config& cfg, std::string* error_log
*/
if(is_gzip_file(modified_name)) {
read_gz(cfg, *file_stream);
} else if(is_bzip2_file(modified_name)) {
read_bz2(cfg, *file_stream);
} else {
read(cfg, *file_stream);
}

View file

@ -36,6 +36,7 @@
#include <stack>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/variant.hpp>
@ -380,7 +381,8 @@ void read(config &cfg, const std::string &in, abstract_validator * validator)
parser(cfg, ss, validator)();
}
void read_gz(config &cfg, std::istream &file, abstract_validator * validator)
template <typename decompressor>
void read_compressed(config &cfg, std::istream &file, abstract_validator * validator)
{
//an empty gzip file seems to confuse boost on msvc
//so return early if this is the case
@ -388,7 +390,7 @@ void read_gz(config &cfg, std::istream &file, abstract_validator * validator)
return;
}
boost::iostreams::filtering_stream<boost::iostreams::input> filter;
filter.push(boost::iostreams::gzip_decompressor());
filter.push(decompressor());
filter.push(file);
/*
@ -403,6 +405,16 @@ void read_gz(config &cfg, std::istream &file, abstract_validator * validator)
parser(cfg, filter,validator)();
}
void read_gz(config &cfg, std::istream &file, abstract_validator * validator)
{
read_compressed<boost::iostreams::gzip_decompressor>(cfg, file, validator);
}
void read_bz2(config &cfg, std::istream &file, abstract_validator * validator)
{
read_compressed<boost::iostreams::bzip2_decompressor>(cfg, file, validator);
}
static std::string escaped_string(const std::string &value)
{
std::string res;
@ -527,11 +539,22 @@ void write(std::ostream &out, config const &cfg, unsigned int level)
write_internal(cfg, out, textdomain, level);
}
void write_gz(std::ostream &out, config const &cfg)
template <typename compressor>
void write_compressed(std::ostream &out, config const &cfg)
{
boost::iostreams::filtering_stream<boost::iostreams::output> filter;
filter.push(boost::iostreams::gzip_compressor());
filter.push(compressor());
filter.push(out);
write(filter, cfg);
}
void write_gz(std::ostream &out, config const &cfg)
{
write_compressed<boost::iostreams::gzip_compressor>(out, cfg);
}
void write_bz2(std::ostream &out, config const &cfg)
{
write_compressed<boost::iostreams::bzip2_compressor>(out, cfg);
}

View file

@ -31,9 +31,12 @@ void read(config &cfg, const std::string &in,
abstract_validator * validator = NULL); // Throws config::error
void read_gz(config &cfg, std::istream &in,
abstract_validator * validator = NULL);
void read_bz2(config &cfg, std::istream &in,
abstract_validator * validator = NULL);
void write(std::ostream &out, config const &cfg, unsigned int level=0);
void write_gz(std::ostream &out, config const &cfg);
void write_bz2(std::ostream &out, config const &cfg);
void write_key_val(std::ostream &out, const std::string &key, const config::attribute_value &value, unsigned level, std::string &textdomain);
void write_open_child(std::ostream &out, const std::string &child, unsigned int level);
void write_close_child(std::ostream &out, const std::string &child, unsigned int level);