Factor out base/indented_stream.h

This commit is contained in:
Joe Thornber
2014-01-31 13:43:39 +00:00
parent 8531a2befa
commit 344f4b1e08
4 changed files with 109 additions and 104 deletions

View File

@@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.
#include "xml_format.h"
#include "base/indented_stream.h"
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
@@ -41,8 +42,7 @@ namespace {
class xml_emitter : public emitter {
public:
xml_emitter(ostream &out)
: out_(out),
indent_(0) {
: out_(out) {
}
void begin_superblock(string const &uuid,
@@ -51,7 +51,7 @@ namespace {
uint32_t data_block_size,
uint64_t nr_data_blocks,
boost::optional<uint64_t> metadata_snap) {
indent();
out_.indent();
out_ << "<superblock uuid=\"" << uuid << "\""
<< " time=\"" << time << "\""
<< " transaction=\"" << trans_id << "\""
@@ -61,14 +61,13 @@ namespace {
if (metadata_snap)
out_ << "\" metadata_snap=\"" << *metadata_snap;
out_ << "\">"
<< endl;
inc();
out_ << "\">" << endl;
out_.inc();
}
void end_superblock() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</superblock>" << endl;
}
@@ -77,40 +76,40 @@ namespace {
uint64_t trans_id,
uint64_t creation_time,
uint64_t snap_time) {
indent();
out_.indent();
out_ << "<device dev_id=\"" << dev_id << "\""
<< " mapped_blocks=\"" << mapped_blocks << "\""
<< " transaction=\"" << trans_id << "\""
<< " creation_time=\"" << creation_time << "\""
<< " snap_time=\"" << snap_time << "\">" << endl;
inc();
out_.inc();
}
void end_device() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</device>" << endl;
}
void begin_named_mapping(string const &name) {
indent();
out_.indent();
out_ << "<named_mapping>" << endl;
inc();
out_.inc();
}
void end_named_mapping() {
dec();
indent();
out_.dec();
out_.indent();
out_ << "</named_mapping>" << endl;
}
void identifier(string const &name) {
indent();
out_.indent();
out_ << "<identifier name=\"" << name << "\"/>" << endl;
}
void range_map(uint64_t origin_begin, uint64_t data_begin, uint32_t time, uint64_t len) {
indent();
out_.indent();
out_ << "<range_mapping origin_begin=\"" << origin_begin << "\""
<< " data_begin=\"" << data_begin << "\""
@@ -120,7 +119,7 @@ namespace {
}
void single_map(uint64_t origin_block, uint64_t data_block, uint32_t time) {
indent();
out_.indent();
out_ << "<single_mapping origin_block=\"" << origin_block << "\""
<< " data_block=\"" << data_block << "\""
@@ -129,21 +128,7 @@ namespace {
}
private:
void indent() {
for (unsigned i = 0; i < indent_ * 2; i++)
out_ << ' ';
}
void inc() {
indent_++;
}
void dec() {
indent_--;
}
ostream &out_;
unsigned indent_;
indented_stream out_;
};
//------------------------------------------------