binary_or_text.cpp

Go to the documentation of this file.
00001 /* $Id: binary_or_text.cpp 23842 2008-02-16 08:47:16Z mordante $ */
00002 /*
00003    Copyright (C) 2003 by David White <dave@whitevine.net>
00004    Copyright (C) 2005 - 2008 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License version 2
00009    or at your option any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 //! @file serialization/binary_or_text.cpp 
00017 //! Read/Write file in binary (compressed) or text-format (uncompressed).
00018 
00019 #include "global.hpp"
00020 
00021 #include "binary_or_text.hpp"
00022 #include "config.hpp"
00023 #include "filesystem.hpp"
00024 #include "serialization/binary_wml.hpp"
00025 #include "serialization/parser.hpp"
00026 
00027 #include <sstream>
00028 
00029 #include <boost/iostreams/filter/gzip.hpp>
00030 
00031 bool detect_format_and_read(config &cfg, std::istream &in, std::string* error_log)
00032 {
00033     unsigned char c = in.peek();
00034     if (c < 4) {
00035         read_compressed(cfg, in);
00036         return true;
00037     } else {
00038         read(cfg, in, error_log);
00039         return false;
00040     }
00041 }
00042 
00043 void write_possibly_compressed(std::ostream &out, config &cfg, bool compress)
00044 {
00045     if (compress)
00046         write_compressed(out, cfg);
00047     else
00048         write(out, cfg);
00049 }
00050 
00051 config_writer::config_writer(
00052     std::ostream &out, bool compress, const std::string &textdomain) :
00053         filter_(),
00054         out_ptr_(compress ? &filter_ : &out), //ternary indirection creates a temporary
00055         out_(*out_ptr_), //now MSVC will allow binding to the reference member
00056         compress_(compress), 
00057         level_(0), 
00058         textdomain_(textdomain)
00059 {
00060     if(compress_) {
00061         filter_.push(boost::iostreams::gzip_compressor());
00062         filter_.push(out);
00063     }
00064 }
00065 
00066 void config_writer::write(const config &cfg)
00067 {
00068 	::write(out_, cfg, level_);
00069 }
00070 
00071 void config_writer::write_child(const std::string &key, const config &cfg)
00072 {
00073     open_child(key);
00074 	::write(out_, cfg, level_);
00075     close_child(key);
00076 }
00077 
00078 void config_writer::write_key_val(const std::string &key, const std::string &value)
00079 {
00080 	::write_key_val(out_, key, value, level_, textdomain_);
00081 }
00082 
00083 void config_writer::open_child(const std::string &key)
00084 {
00085 	::write_open_child(out_, key, level_++);
00086 }
00087 
00088 void config_writer::close_child(const std::string &key)
00089 {
00090 	::write_close_child(out_, key, --level_);
00091 }
00092 
00093 bool config_writer::good() const
00094 {
00095     return out_.good();
00096 }
00097 

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs