The Battle for Wesnoth
1.17.14+dev
serialization
binary_or_text.cpp
Go to the documentation of this file.
1
/*
2
Copyright (C) 2005 - 2022
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
22
#include "
serialization/binary_or_text.hpp
"
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
35
config_writer::config_writer
(std::ostream& out,
compression::format
compress)
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
{
43
if
(
compress_
==
compression::format::gzip
) {
44
filter_
.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(9)));
45
filter_
.push(out);
46
}
else
if
(
compress_
==
compression::format::bzip2
) {
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
{
60
if
(
compress_
!=
compression::format::none
) {
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
71
config_writer::~config_writer
()
72
{
73
// we only need this for gzip but we also do it for bz2 for unification.
74
if
(
compress_
==
compression::format::gzip
||
compress_
==
compression::format::bzip2
) {
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
{
94
::write_open_child
(
out_
, key,
level_
++);
95
}
96
97
void
config_writer::close_child
(
const
std::string& key)
98
{
99
::write_close_child
(
out_
, key, --
level_
);
100
}
101
102
bool
config_writer::good
()
const
103
{
104
return
out_
.good();
105
}
config_writer::good
bool good() const
Definition:
binary_or_text.cpp:102
write_close_child
void write_close_child(std::ostream &out, const std::string &child, unsigned int level)
Definition:
parser.cpp:708
config_writer::write
void write(const config &cfg)
Definition:
binary_or_text.cpp:80
config_writer::config_writer
config_writer(std::ostream &out, compression::format compress)
Definition:
binary_or_text.cpp:35
config_writer::textdomain_
std::string textdomain_
Definition:
binary_or_text.hpp:57
log_config
static lg::log_domain log_config("config")
lg::log_domain
Definition:
log.hpp:114
compression
Definition:
compression.hpp:20
config_writer::compress_
compression::format compress_
Definition:
binary_or_text.hpp:55
config.hpp
Definitions for the interface to Wesnoth Markup Language (WML).
config_writer::level_
unsigned int level_
Definition:
binary_or_text.hpp:56
config_writer::write_child
void write_child(const std::string &key, const config &cfg)
Definition:
binary_or_text.cpp:85
loading_stage::none
config_writer::close_child
void close_child(const std::string &key)
Definition:
binary_or_text.cpp:97
config_writer::out_
std::ostream & out_
Definition:
binary_or_text.hpp:54
config_writer::open_child
void open_child(const std::string &key)
Definition:
binary_or_text.cpp:92
compression::format::bzip2
PACKAGE
#define PACKAGE
Definition:
wesconfig.h:23
wesconfig.h
Some defines: VERSION, PACKAGE, MIN_SAVEGAME_VERSION.
game_config::images::level
std::string level
Definition:
game_config.cpp:226
compression::format::none
compression::format
format
Definition:
compression.hpp:22
binary_or_text.hpp
config_writer::~config_writer
~config_writer()
Default implementation, but defined out-of-line for efficiency reasons.
Definition:
binary_or_text.cpp:71
write_open_child
void write_open_child(std::ostream &out, const std::string &child, unsigned int level)
Definition:
parser.cpp:703
compression::format::gzip
parser.hpp
log.hpp
Standard logging facilities (interface).
config_writer::out_ptr_
std::ostream * out_ptr_
Definition:
binary_or_text.hpp:53
config
A config object defines a single node in a WML file, with access to child nodes.
Definition:
config.hpp:60
config_writer::filter_
boost::iostreams::filtering_stream< boost::iostreams::output > filter_
Definition:
binary_or_text.hpp:52
Generated by
1.8.13