The Battle for Wesnoth
1.19.5+dev
serialization
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
22
#include "
serialization/binary_or_text.hpp
"
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
34
config_writer::config_writer
(std::ostream& out,
compression::format
compress)
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
{
42
if
(
compress_
==
compression::format::gzip
) {
43
filter_
.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(9)));
44
filter_
.push(out);
45
}
else
if
(
compress_
==
compression::format::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
::
format
::gzip :
compression
::
format
::none)
56
, level_(0)
57
, textdomain_(
PACKAGE
)
58
{
59
if
(
compress_
!=
compression::format::none
) {
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
70
config_writer::~config_writer
()
71
{
72
// we only need this for gzip but we also do it for bz2 for unification.
73
if
(
compress_
==
compression::format::gzip
||
compress_
==
compression::format::bzip2
) {
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
{
93
::write_open_child
(
out_
, key,
level_
++);
94
}
95
96
void
config_writer::close_child
(
const
std::string& key)
97
{
98
::write_close_child
(
out_
, key, --
level_
);
99
}
100
101
bool
config_writer::good
()
const
102
{
103
return
out_
.good();
104
}
log_config
static lg::log_domain log_config("config")
binary_or_text.hpp
config_writer::filter_
boost::iostreams::filtering_stream< boost::iostreams::output > filter_
Definition:
binary_or_text.hpp:51
config_writer::close_child
void close_child(const std::string &key)
Definition:
binary_or_text.cpp:96
config_writer::good
bool good() const
Definition:
binary_or_text.cpp:101
config_writer::level_
unsigned int level_
Definition:
binary_or_text.hpp:55
config_writer::write
void write(const config &cfg)
Definition:
binary_or_text.cpp:79
config_writer::~config_writer
~config_writer()
Default implementation, but defined out-of-line for efficiency reasons.
Definition:
binary_or_text.cpp:70
config_writer::out_
std::ostream & out_
Definition:
binary_or_text.hpp:53
config_writer::write_child
void write_child(const std::string &key, const config &cfg)
Definition:
binary_or_text.cpp:84
config_writer::compress_
compression::format compress_
Definition:
binary_or_text.hpp:54
config_writer::config_writer
config_writer(std::ostream &out, compression::format compress)
Definition:
binary_or_text.cpp:34
config_writer::open_child
void open_child(const std::string &key)
Definition:
binary_or_text.cpp:91
config
A config object defines a single node in a WML file, with access to child nodes.
Definition:
config.hpp:172
lg::log_domain
Definition:
log.hpp:125
log.hpp
Standard logging facilities (interface).
compression
Definition:
compression.hpp:21
compression::format
format
Definition:
compression.hpp:22
compression::format::bzip2
@ bzip2
compression::format::none
@ none
compression::format::gzip
@ gzip
game_config::images::level
std::string level
Definition:
game_config.cpp:210
write_close_child
void write_close_child(std::ostream &out, const std::string &child, unsigned int level)
Definition:
parser.cpp:703
write_open_child
void write_open_child(std::ostream &out, const std::string &child, unsigned int level)
Definition:
parser.cpp:698
parser.hpp
wesconfig.h
Some defines: VERSION, PACKAGE, MIN_SAVEGAME_VERSION.
PACKAGE
#define PACKAGE
Definition:
wesconfig.h:23
Generated by
1.9.1