The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
binary_or_text.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <[email protected]>
3  Copyright (C) 2005 - 2016 by Guillaume Melquiond <[email protected]>
4  Part of the Battle for Wesnoth Project http://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 
21 #include "global.hpp"
22 
23 #include "binary_or_text.hpp"
24 #include "config.hpp"
25 #include "log.hpp"
26 #include "wesconfig.h"
27 #include "serialization/parser.hpp"
28 
29 
30 #include <boost/iostreams/filter/bzip2.hpp>
31 #include <boost/iostreams/filter/gzip.hpp>
32 
33 static lg::log_domain log_config("config");
34 #define ERR_CF LOG_STREAM(err, log_config)
35 
37  std::ostream &out, compression::format compress) :
38  filter_(),
39  out_ptr_(compress ? &filter_ : &out), //ternary indirection creates a temporary
40  out_(*out_ptr_), //now MSVC will allow binding to the reference member
41  compress_(compress),
42  level_(0),
43  textdomain_(PACKAGE)
44 {
46  filter_.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(9)));
47  filter_.push(out);
48 
49  } else if(compress_ == compression::BZIP2) {
50  filter_.push(boost::iostreams::bzip2_compressor(boost::iostreams::bzip2_params()));
51  filter_.push(out);
52  }
53 }
55  std::ostream &out, bool compress, int level) :
56  filter_(),
57  out_ptr_(compress ? &filter_ : &out), //ternary indirection creates a temporary
58  out_(*out_ptr_), //now MSVC will allow binding to the reference member
59  compress_(compress ? compression::GZIP : compression::NONE),
60  level_(0),
61  textdomain_(PACKAGE)
62 {
63  if(compress_) {
64  if (level >=0)
65  filter_.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(level)));
66  else
67  filter_.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params()));
68 
69  filter_.push(out);
70  }
71 }
72 
74 {
75  //we only need this for gzip but we also do it for bz2 for unification.
77  {
78  // prevent empty gz files because of https://svn.boost.org/trac/boost/ticket/5237
79  out_ << "\n";
80  }
81 }
82 
83 void config_writer::write(const config &cfg)
84 {
85  ::write(out_, cfg, level_);
86 }
87 
88 void config_writer::write_child(const std::string &key, const config &cfg)
89 {
90  open_child(key);
91  ::write(out_, cfg, level_);
92  close_child(key);
93 }
94 
96 {
98 }
99 
101 {
103 }
104 
106 {
107  return out_.good();
108 }
109 
bool good() const
void write(const config &cfg)
GLint level
Definition: glew.h:1220
config_writer(std::ostream &out, compression::format compress)
static lg::log_domain log_config("config")
void write_open_child(std::ostream &out, const std::string &child, unsigned int level)
Definition: parser.cpp:565
compression::format compress_
Definitions for the interface to Wesnoth Markup Language (WML).
void write_close_child(std::ostream &out, const std::string &child, unsigned int level)
Definition: parser.cpp:570
unsigned int level_
void write_child(const std::string &key, const config &cfg)
gui::filter_textbox & filter_
Definition: dialogs.cpp:99
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.
~config_writer()
Default implementation, but defined out-of-line for efficiency reasons.
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
boost::iostreams::filtering_stream< boost::iostreams::output > filter_