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