Add support for reading gzip savegames, writing them isn't supported yet.

This commit is contained in:
Mark de Wever 2007-12-04 17:39:43 +00:00
parent e60afcb6f1
commit 44a04421b1
6 changed files with 31 additions and 2 deletions

View file

@ -2028,7 +2028,7 @@ static int play_game(int argc, char** argv)
}
const std::string input_file(argv[arg + 1]);
if(input_file.substr(input_file.length() - 3) != ".gz") {
if(! is_gzip_file(input_file)) {
std::cerr << "file '" << input_file << "'isn't a .gz file\n";
return 2;

View file

@ -736,7 +736,11 @@ void read_save_file(const std::string& name, config& cfg, std::string* error_log
cfg.clear();
try{
detect_format_and_read(cfg, *file_stream, error_log);
if(is_gzip_file(name)) {
read_gz(cfg, *file_stream, error_log);
} else {
detect_format_and_read(cfg, *file_stream, error_log);
}
} catch (config::error &err)
{
std::cerr << err.message;

View file

@ -83,3 +83,9 @@ bool config_writer::good() const
return out_.good();
}
bool is_gzip_file(const std::string& filename)
{
return (filename.length() > 3
&& filename.substr(filename.length() - 3) == ".gz");
}

View file

@ -52,4 +52,8 @@ private:
unsigned int level_;
std::string textdomain_;
};
bool is_gzip_file(const std::string& filename);
#endif

View file

@ -36,6 +36,10 @@
#include <sstream>
#include <stack>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#define ERR_CF LOG_STREAM(err, config)
#define WRN_CF LOG_STREAM(warn, config)
#define LOG_CF LOG_STREAM(info, config)
@ -347,6 +351,16 @@ void read(config &cfg, std::string &in, std::string* error_log)
parser(cfg, in)(error_log);
}
void read_gz(config &cfg, std::istream &file, std::string* error_log)
{
boost::iostreams::filtering_stream<boost::iostreams::input> filter;
filter.push(boost::iostreams::gzip_decompressor());
filter.push(file);
parser(cfg, filter)(error_log);
}
static char const *AttributeEquals = "=";
static char const *TranslatableAttributePrefix = "_ \"";

View file

@ -30,6 +30,7 @@ class t_string;
// Read data in, clobbering existing data.
void read(config &cfg, std::istream &in, std::string* error_log = NULL); // Throws config::error
void read(config &cfg, std::string &in, std::string* error_log = NULL); // Throws config::error
void read_gz(config &cfg, std::istream &in, std::string* error_log=NULL);
void write(std::ostream &out, config const &cfg, unsigned int level=0);
void write_key_val(std::ostream &out, const std::string &key, const t_string &value, unsigned int level, std::string &textdomain);