The Battle for Wesnoth  1.19.5+dev
binary_or_text.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2005 - 2024
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 "log.hpp"
25 #include "serialization/parser.hpp"
26 #include "wesconfig.h"
27 
28 #include <boost/iostreams/filter/bzip2.hpp>
29 #include <boost/iostreams/filter/gzip.hpp>
30 
31 static lg::log_domain log_config("config");
32 #define ERR_CF LOG_STREAM(err, log_config)
33 
35  : filter_()
36  , out_ptr_(compress != compression::format::none ? &filter_ : &out) // ternary indirection creates a temporary
37  , out_(*out_ptr_) // now MSVC will allow binding to the reference member
38  , compress_(compress)
39  , level_(0)
40  , textdomain_(PACKAGE)
41 {
43  filter_.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(9)));
44  filter_.push(out);
46  filter_.push(boost::iostreams::bzip2_compressor(boost::iostreams::bzip2_params()));
47  filter_.push(out);
48  }
49 }
50 
51 config_writer::config_writer(std::ostream& out, bool compress, int level)
52  : filter_()
53  , out_ptr_(compress ? &filter_ : &out) // ternary indirection creates a temporary
54  , out_(*out_ptr_) // now MSVC will allow binding to the reference member
55  , compress_(compress ? compression::format::gzip : compression::format::none)
56  , level_(0)
57  , textdomain_(PACKAGE)
58 {
60  if(level >= 0) {
61  filter_.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(level)));
62  } else {
63  filter_.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params()));
64  }
65 
66  filter_.push(out);
67  }
68 }
69 
71 {
72  // we only need this for gzip but we also do it for bz2 for unification.
74  // prevent empty gz files because of https://svn.boost.org/trac/boost/ticket/5237
75  out_ << "\n";
76  }
77 }
78 
79 void config_writer::write(const config& cfg)
80 {
81  ::write(out_, cfg, level_);
82 }
83 
84 void config_writer::write_child(const std::string& key, const config& cfg)
85 {
86  open_child(key);
87  ::write(out_, cfg, level_);
88  close_child(key);
89 }
90 
91 void config_writer::open_child(const std::string& key)
92 {
94 }
95 
96 void config_writer::close_child(const std::string& key)
97 {
99 }
100 
102 {
103  return out_.good();
104 }
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:172
Standard logging facilities (interface).
void write_close_child(std::ostream &out, const std::string &child, unsigned int level)
Definition: parser.cpp:703
void write_open_child(std::ostream &out, const std::string &child, unsigned int level)
Definition: parser.cpp:698
Some defines: VERSION, PACKAGE, MIN_SAVEGAME_VERSION.
#define PACKAGE
Definition: wesconfig.h:23