The Battle for Wesnoth  1.15.2+dev
binary_or_text.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <dave@whitevine.net>
3  Copyright (C) 2005 - 2018 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Read/Write file in binary (compressed) or text-format (uncompressed).
19  */
20 
22 
23 #include "config.hpp"
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 ? &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);
45  } else if(compress_ == compression::BZIP2) {
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::GZIP : compression::NONE)
56  , level_(0)
58 {
59  if(compress_) {
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 }
bool good() const
void write_close_child(std::ostream &out, const std::string &child, unsigned int level)
Definition: parser.cpp:706
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:701
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:68
boost::iostreams::filtering_stream< boost::iostreams::output > filter_