The Battle for Wesnoth  1.17.17+dev
binary_or_text.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2005 - 2023
3  by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4  Copyright (C) 2003 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 /**
18  * @file
19  * Read/Write file in binary (compressed) or text-format (uncompressed).
20  */
21 
23 
24 #include "config.hpp"
25 #include "log.hpp"
26 #include "serialization/parser.hpp"
27 #include "wesconfig.h"
28 
29 #include <boost/iostreams/filter/bzip2.hpp>
30 #include <boost/iostreams/filter/gzip.hpp>
31 
32 static lg::log_domain log_config("config");
33 #define ERR_CF LOG_STREAM(err, log_config)
34 
36  : filter_()
37  , out_ptr_(compress != compression::format::none ? &filter_ : &out) // ternary indirection creates a temporary
38  , out_(*out_ptr_) // now MSVC will allow binding to the reference member
39  , compress_(compress)
40  , level_(0)
41  , textdomain_(PACKAGE)
42 {
44  filter_.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(9)));
45  filter_.push(out);
47  filter_.push(boost::iostreams::bzip2_compressor(boost::iostreams::bzip2_params()));
48  filter_.push(out);
49  }
50 }
51 
52 config_writer::config_writer(std::ostream& out, bool compress, int level)
53  : filter_()
54  , out_ptr_(compress ? &filter_ : &out) // ternary indirection creates a temporary
55  , out_(*out_ptr_) // now MSVC will allow binding to the reference member
56  , compress_(compress ? compression::format::gzip : compression::format::none)
57  , level_(0)
58  , textdomain_(PACKAGE)
59 {
61  if(level >= 0) {
62  filter_.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(level)));
63  } else {
64  filter_.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params()));
65  }
66 
67  filter_.push(out);
68  }
69 }
70 
72 {
73  // we only need this for gzip but we also do it for bz2 for unification.
75  // prevent empty gz files because of https://svn.boost.org/trac/boost/ticket/5237
76  out_ << "\n";
77  }
78 }
79 
80 void config_writer::write(const config& cfg)
81 {
82  ::write(out_, cfg, level_);
83 }
84 
85 void config_writer::write_child(const std::string& key, const config& cfg)
86 {
87  open_child(key);
88  ::write(out_, cfg, level_);
89  close_child(key);
90 }
91 
92 void config_writer::open_child(const std::string& key)
93 {
95 }
96 
97 void config_writer::close_child(const std::string& key)
98 {
100 }
101 
103 {
104  return out_.good();
105 }
static lg::log_domain log_config("config")
boost::iostreams::filtering_stream< boost::iostreams::output > filter_
void close_child(const std::string &key)
bool good() const
unsigned int level_
void write(const config &cfg)
~config_writer()
Default implementation, but defined out-of-line for efficiency reasons.
std::ostream & out_
void write_child(const std::string &key, const config &cfg)
compression::format compress_
config_writer(std::ostream &out, compression::format compress)
void open_child(const std::string &key)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
Standard logging facilities (interface).
void write_close_child(std::ostream &out, const std::string &child, unsigned int level)
Definition: parser.cpp:708
void write_open_child(std::ostream &out, const std::string &child, unsigned int level)
Definition: parser.cpp:703
Some defines: VERSION, PACKAGE, MIN_SAVEGAME_VERSION.
#define PACKAGE
Definition: wesconfig.h:23