Example #1
0
bool ConfigFile::saveToStream(WriteStream &stream) {
	for (List<Section>::iterator i = _sections.begin(); i != _sections.end(); ++i) {
		// Write out the section comment, if any
		if (! i->comment.empty()) {
			stream.writeString(i->comment);
		}

		// Write out the section name
		stream.writeByte('[');
		stream.writeString(i->name);
		stream.writeByte(']');
		stream.writeByte('\n');

		// Write out the key/value pairs
		for (List<KeyValue>::iterator kv = i->keys.begin(); kv != i->keys.end(); ++kv) {
			// Write out the comment, if any
			if (! kv->comment.empty()) {
				stream.writeString(kv->comment);
			}
			// Write out the key/value pair
			stream.writeString(kv->key);
			stream.writeByte('=');
			stream.writeString(kv->value);
			stream.writeByte('\n');
		}
	}

	stream.flush();
	return !stream.err();
}
Example #2
0
void ConfigFile::save(WriteStream &stream) const {
	// Write file prologue
	if (!_prologue.empty()) {
		stream.writeString(_prologue);
		stream.writeByte('\n');
		stream.writeByte('\n');
	}

	// Domains
	for (DomainList::const_iterator domain = _domainList.begin(); domain != _domainList.end(); ++domain) {
		// Write domain prologue
		if (!(*domain)->_prologue.empty()) {
			stream.writeString((*domain)->_prologue);
			stream.writeByte('\n');
		}

		// Write domain name
		stream.writeByte('[');
		stream.writeString((*domain)->_name);
		stream.writeByte(']');

		// Write domain comment
		if (!(*domain)->_comment.empty()) {
			stream.writeByte(' ');
			stream.writeString((*domain)->_comment);
		}

		stream.writeByte('\n');

		// Lines
		for (ConfigDomain::LineList::const_iterator line = (*domain)->_lines.begin(); line != (*domain)->_lines.end(); ++line) {
			// Write key
			if (line->key != (*domain)->_keys.end()) {
				stream.writeString(line->key->first);
				stream.writeByte('=');
				stream.writeString(line->key->second);
				if (!line->comment.empty())
					stream.writeByte(' ');
			}

			// Write comment
			if (!line->comment.empty())
				stream.writeString(line->comment);

			stream.writeByte('\n');
		}

		stream.writeByte('\n');
	}

	// Write the epilogue
	if (!_epilogue.empty()) {
		stream.writeString(_epilogue);
		stream.writeByte('\n');
	}

	if (!stream.flush() || stream.err())
		throw Exception(kWriteError);
}