Commit 90efad4b authored by twanvl's avatar twanvl

The writer now writes less unused blocks, especially for unused DelayedIndexMaps

parent afabe2a5
...@@ -59,7 +59,12 @@ void Reader::handle(DelayedIndexMapsData<Key,Value>& d) { ...@@ -59,7 +59,12 @@ void Reader::handle(DelayedIndexMapsData<Key,Value>& d) {
template <typename Key, typename Value> template <typename Key, typename Value>
void Writer::handle(const DelayedIndexMapsData<Key,Value>& d) { void Writer::handle(const DelayedIndexMapsData<Key,Value>& d) {
if (!d.unread_data.empty()) { if (!d.unread_data.empty()) {
handle(d.unread_data); // TODO: how to handle filenames if (d.unread_data == _("\n")) {
// this is not interesting, it is only used to make unread_data nonempty (see above)
// we don't need to write it
} else {
handle(d.unread_data); // TODO: how to handle filenames
}
} else { } else {
handle(d.read_data); handle(d.read_data);
} }
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
// ----------------------------------------------------------------------------- : Writer // ----------------------------------------------------------------------------- : Writer
Writer::Writer(const OutputStreamP& output, Version file_app_version) Writer::Writer(const OutputStreamP& output, Version file_app_version)
: indentation(0), just_opened(false) : indentation(0)
, output(output), stream(*output) , output(output), stream(*output)
{ {
stream.WriteString(BYTE_ORDER_MARK); stream.WriteString(BYTE_ORDER_MARK);
...@@ -27,27 +27,35 @@ Writer::Writer(const OutputStreamP& output, Version file_app_version) ...@@ -27,27 +27,35 @@ Writer::Writer(const OutputStreamP& output, Version file_app_version)
void Writer::enterBlock(const Char* name) { void Writer::enterBlock(const Char* name) {
// indenting into a sub-block?
if (just_opened) {
writeKey();
stream.WriteString(_(":\n"));
}
// don't write the key yet // don't write the key yet
indentation += 1; pending_opened.push_back(name);
opened_key = cannocial_name_form(name);
just_opened = true;
} }
void Writer::exitBlock() { void Writer::exitBlock() {
assert(indentation > 0); if (pending_opened.empty()) {
indentation -= 1; assert(indentation > 0);
just_opened = false; indentation -= 1;
} else {
// this block was apparently empty, ignore it
pending_opened.pop_back();
}
} }
void Writer::writeKey() { void Writer::writePending() {
writeIndentation(); // In enterBlock we have delayed the actual writing of the keys until this point
writeUTF8(stream, opened_key); // here we write all the pending keys, and increase indentation along the way.
for (size_t i = 0 ; i < pending_opened.size() ; ++i) {
if (i > 0) {
// before entering a sub-block, write a colon after the parent's name
stream.WriteString(_(":\n"));
}
indentation += 1;
writeIndentation();
writeUTF8(stream, cannocial_name_form(pending_opened[i]));
}
pending_opened.clear();
} }
void Writer::writeIndentation() { void Writer::writeIndentation() {
for(int i = 1 ; i < indentation ; ++i) { for(int i = 1 ; i < indentation ; ++i) {
stream.PutChar(_('\t')); stream.PutChar(_('\t'));
...@@ -57,15 +65,14 @@ void Writer::writeIndentation() { ...@@ -57,15 +65,14 @@ void Writer::writeIndentation() {
// ----------------------------------------------------------------------------- : Handling basic types // ----------------------------------------------------------------------------- : Handling basic types
void Writer::handle(const String& value) { void Writer::handle(const String& value) {
if (!just_opened) { if (pending_opened.empty()) {
throw InternalError(_("Can only write a value in a key that was just opened")); throw InternalError(_("Can only write a value in a key that was just opened"));
} }
writePending();
// write indentation and key // write indentation and key
writeKey();
writeUTF8(stream, _(": "));
if (value.find_first_of(_('\n')) != String::npos || (!value.empty() && isSpace(value.GetChar(0)))) { if (value.find_first_of(_('\n')) != String::npos || (!value.empty() && isSpace(value.GetChar(0)))) {
// multiline string, or contains leading whitespace // multiline string, or contains leading whitespace
stream.PutChar(_('\n')); stream.WriteString(_(":\n"));
indentation += 1; indentation += 1;
// split lines, and write each line // split lines, and write each line
size_t start = 0, end, size = value.size(); size_t start = 0, end, size = value.size();
...@@ -87,10 +94,10 @@ void Writer::handle(const String& value) { ...@@ -87,10 +94,10 @@ void Writer::handle(const String& value) {
} }
indentation -= 1; indentation -= 1;
} else { } else {
stream.WriteString(_(": "));
writeUTF8(stream, value); writeUTF8(stream, value);
} }
stream.PutChar(_('\n')); stream.PutChar(_('\n'));
just_opened = false;
} }
template <> void Writer::handle(const int& value) { template <> void Writer::handle(const int& value) {
......
...@@ -77,10 +77,8 @@ class Writer { ...@@ -77,10 +77,8 @@ class Writer {
// --------------------------------------------------- : Data // --------------------------------------------------- : Data
/// Indentation of the current block /// Indentation of the current block
int indentation; int indentation;
/// Did we just open a block (i.e. not written any lines of it)? /// Blocks opened to which nothing has been written
bool just_opened; vector<const Char*> pending_opened;
/// Last key opened
String opened_key;
/// Output stream we are writing to /// Output stream we are writing to
OutputStreamP output; OutputStreamP output;
...@@ -94,8 +92,8 @@ class Writer { ...@@ -94,8 +92,8 @@ class Writer {
/// Leave the block we are in /// Leave the block we are in
void exitBlock(); void exitBlock();
/// Write the opened_key and the required indentation /// Write the pending_opened with the required indentation
void writeKey(); void writePending();
/// Output some taps to represent the indentation level /// Output some taps to represent the indentation level
void writeIndentation(); void writeIndentation();
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment