diff options
author | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-06-30 12:47:19 +0000 |
---|---|---|
committer | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-06-30 12:47:19 +0000 |
commit | e73b54e60d5b8bc4234d6c4cf4fa978e175d68cb (patch) | |
tree | e95b3b15488dac3a28bb9b0a8e6bbc79d81587c5 | |
parent | 8b1a9951559fc6420aad68c4f4e23f8b470cec0c (diff) |
docs: Fixed sample comment, adding load error handling sample, added custom writer sample
git-svn-id: http://pugixml.googlecode.com/svn/trunk@552 99668b35-9821-0410-8761-19e4c4f06640
-rw-r--r-- | docs/samples/load_error_handling.cpp | 29 | ||||
-rw-r--r-- | docs/samples/load_memory.cpp | 2 | ||||
-rw-r--r-- | docs/samples/save_custom_writer.cpp | 112 |
3 files changed, 142 insertions, 1 deletions
diff --git a/docs/samples/load_error_handling.cpp b/docs/samples/load_error_handling.cpp new file mode 100644 index 0000000..9b274ba --- /dev/null +++ b/docs/samples/load_error_handling.cpp @@ -0,0 +1,29 @@ +#include "pugixml.hpp"
+
+#include <iostream>
+
+void check_xml(const char* source)
+{
+//[code_load_error_handling
+ pugi::xml_document doc;
+ pugi::xml_parse_result result = doc.load(source);
+
+ if (result)
+ std::cout << "XML [" << source << "] parsed without errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n\n";
+ else
+ {
+ std::cout << "XML [" << source << "] parsed with errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n";
+ std::cout << "Error description: " << result.description() << "\n";
+ std::cout << "Error offset: " << result.offset << " (error at [..." << (source + result.offset) << "]\n\n";
+ }
+//]
+}
+
+int main()
+{
+ check_xml("<node attr='value'><child>text</child></node>");
+ check_xml("<node attr='value'><child>text</chil></node>");
+ check_xml("<node attr='value'><child>text</child>");
+ check_xml("<node attr='value\"><child>text</child></node>");
+ check_xml("<node attr='value'><#tag /></node>");
+}
diff --git a/docs/samples/load_memory.cpp b/docs/samples/load_memory.cpp index b8d898f..5f975b2 100644 --- a/docs/samples/load_memory.cpp +++ b/docs/samples/load_memory.cpp @@ -22,7 +22,7 @@ int main() {
//[code_load_memory_buffer_inplace
- // You can use load_buffer_inplace to load document from mutable memory block; memory blocks lifetime must exceed that of document
+ // You can use load_buffer_inplace to load document from mutable memory block; the block's lifetime must exceed that of document
char* buffer = new char[size];
memcpy(buffer, source, size);
diff --git a/docs/samples/save_custom_writer.cpp b/docs/samples/save_custom_writer.cpp new file mode 100644 index 0000000..978c583 --- /dev/null +++ b/docs/samples/save_custom_writer.cpp @@ -0,0 +1,112 @@ +#include "pugixml.hpp"
+
+#include <string>
+
+#include <stdio.h>
+
+struct xml_string_writer: pugi::xml_writer
+{
+ std::string result;
+
+ 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;
+
+ size_t result;
+
+ xml_memory_writer(): buffer(0), capacity(0), 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;
+ }
+
+ 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);
+ }
+
+ result += size;
+ }
+};
+
+std::string node_to_string(pugi::xml_node node)
+{
+ xml_string_writer writer;
+ node.print(writer);
+
+ return writer.result;
+}
+
+char* node_to_buffer(pugi::xml_node node, char* buffer, size_t size)
+{
+ if (size == 0) return buffer;
+
+ // leave one character for null terminator
+ xml_memory_writer writer(buffer, size - 1);
+ node.print(writer);
+
+ // null terminate
+ buffer[writer.written_size()] = 0;
+
+ return buffer;
+}
+
+char* node_to_buffer_heap(pugi::xml_node node)
+{
+ // 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];
+
+ // second pass: actual printing
+ xml_memory_writer writer(buffer, counter.result);
+ node.print(writer);
+
+ // null terminate
+ buffer[writer.written_size()] = 0;
+
+ return buffer;
+}
+
+int main()
+{
+ // 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 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 heap-allocated buffer (two passes)
+ char* heap_buf = node_to_buffer_heap(doc);
+ printf("contents: [%s]\n", heap_buf);
+ delete[] heap_buf;
+}
|