summaryrefslogtreecommitdiff
path: root/docs/samples/save_custom_writer.cpp
diff options
context:
space:
mode:
authorarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2010-07-11 13:29:12 +0000
committerarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2010-07-11 13:29:12 +0000
commitfb507ab2d6f9eaa090671e40b582354b16f3821c (patch)
tree4cf47721ff98432595ea8704e2e8d421d1f3c913 /docs/samples/save_custom_writer.cpp
parentf73df8d06e67f4a1d00b427d752fb0deab11e553 (diff)
docs: Replaced all tabs with 4 spaces (guaranteed tab size)
git-svn-id: http://pugixml.googlecode.com/svn/trunk@591 99668b35-9821-0410-8761-19e4c4f06640
Diffstat (limited to 'docs/samples/save_custom_writer.cpp')
-rw-r--r--docs/samples/save_custom_writer.cpp128
1 files changed, 65 insertions, 63 deletions
diff --git a/docs/samples/save_custom_writer.cpp b/docs/samples/save_custom_writer.cpp
index 0c7669c..c0999e1 100644
--- a/docs/samples/save_custom_writer.cpp
+++ b/docs/samples/save_custom_writer.cpp
@@ -7,108 +7,110 @@
//[code_save_custom_writer
struct xml_string_writer: pugi::xml_writer
{
- std::string result;
+ std::string result;
- virtual void write(const void* data, size_t size)
- {
- result += std::string(static_cast<const char*>(data), size);
- }
+ virtual void write(const void* data, size_t size)
+ {
+ result += std::string(static_cast<const char*>(data), size);
+ }
};
//]
struct xml_memory_writer: pugi::xml_writer
{
- char* buffer;
- size_t capacity;
+ char* buffer;
+ size_t capacity;
- size_t result;
+ size_t result;
- xml_memory_writer(): buffer(0), capacity(0), result(0)
- {
- }
+ xml_memory_writer(): buffer(0), capacity(0), result(0)
+ {
+ }
- xml_memory_writer(char* buffer, size_t capacity): buffer(buffer), capacity(capacity), result(0)
- {
- }
+ xml_memory_writer(char* buffer, size_t capacity): buffer(buffer), capacity(capacity), result(0)
+ {
+ }
- size_t written_size() const
- {
- return result < capacity ? result : capacity;
- }
+ size_t written_size() const
+ {
+ return result < capacity ? result : capacity;
+ }
- virtual void write(const void* data, size_t size)
- {
- if (result < capacity)
- {
- size_t chunk = (capacity - result < size) ? capacity - result : size;
+ virtual void write(const void* data, size_t size)
+ {
+ if (result < capacity)
+ {
+ size_t chunk = (capacity - result < size) ? capacity - result : size;
- memcpy(buffer + result, data, chunk);
- }
+ memcpy(buffer + result, data, chunk);
+ }
- result += size;
- }
+ result += size;
+ }
};
std::string node_to_string(pugi::xml_node node)
{
- xml_string_writer writer;
- node.print(writer);
+ xml_string_writer writer;
+ node.print(writer);
- return writer.result;
+ return writer.result;
}
char* node_to_buffer(pugi::xml_node node, char* buffer, size_t size)
{
- if (size == 0) return buffer;
+ if (size == 0) return buffer;
- // leave one character for null terminator
- xml_memory_writer writer(buffer, size - 1);
- node.print(writer);
+ // leave one character for null terminator
+ xml_memory_writer writer(buffer, size - 1);
+ node.print(writer);
- // null terminate
- buffer[writer.written_size()] = 0;
+ // null terminate
+ buffer[writer.written_size()] = 0;
- return buffer;
+ return buffer;
}
char* node_to_buffer_heap(pugi::xml_node node)
{
- // first pass: get required memory size
- xml_memory_writer counter;
- node.print(counter);
+ // first pass: get required memory size
+ xml_memory_writer counter;
+ node.print(counter);
- // allocate necessary size (+1 for null termination)
- char* buffer = new char[counter.result + 1];
+ // allocate necessary size (+1 for null termination)
+ char* buffer = new char[counter.result + 1];
- // second pass: actual printing
- xml_memory_writer writer(buffer, counter.result);
- node.print(writer);
+ // second pass: actual printing
+ xml_memory_writer writer(buffer, counter.result);
+ node.print(writer);
- // null terminate
- buffer[writer.written_size()] = 0;
+ // null terminate
+ buffer[writer.written_size()] = 0;
- return buffer;
+ return buffer;
}
int main()
{
- // get a test document
- pugi::xml_document doc;
- doc.load("<foo bar='baz'>hey</foo>");
+ // get a test document
+ pugi::xml_document doc;
+ doc.load("<foo bar='baz'>hey</foo>");
- // get contents as std::string (single pass)
- printf("contents: [%s]\n", node_to_string(doc).c_str());
+ // get contents as std::string (single pass)
+ printf("contents: [%s]\n", node_to_string(doc).c_str());
- // get contents into fixed-size buffer (single pass)
- char large_buf[128];
- printf("contents: [%s]\n", node_to_buffer(doc, large_buf, sizeof(large_buf)));
+ // get contents into fixed-size buffer (single pass)
+ char large_buf[128];
+ printf("contents: [%s]\n", node_to_buffer(doc, large_buf, sizeof(large_buf)));
- // get contents into fixed-size buffer (single pass, shows truncating behavior)
- char small_buf[22];
- printf("contents: [%s]\n", node_to_buffer(doc, small_buf, sizeof(small_buf)));
+ // get contents into fixed-size buffer (single pass, shows truncating behavior)
+ char small_buf[22];
+ printf("contents: [%s]\n", node_to_buffer(doc, small_buf, sizeof(small_buf)));
- // get contents into heap-allocated buffer (two passes)
- char* heap_buf = node_to_buffer_heap(doc);
- printf("contents: [%s]\n", heap_buf);
- delete[] heap_buf;
+ // get contents into heap-allocated buffer (two passes)
+ char* heap_buf = node_to_buffer_heap(doc);
+ printf("contents: [%s]\n", heap_buf);
+ delete[] heap_buf;
}
+
+// vim:et