summaryrefslogtreecommitdiff
path: root/docs/samples
diff options
context:
space:
mode:
authorarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2010-06-28 21:38:46 +0000
committerarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2010-06-28 21:38:46 +0000
commit8b1a9951559fc6420aad68c4f4e23f8b470cec0c (patch)
tree03041d4da413407b3a4adfe92201c66daa9c5e11 /docs/samples
parent39187a1f04d3e1fbaa6465a262a9bfd146c5287a (diff)
docs: Added memory and stream loading documentation
git-svn-id: http://pugixml.googlecode.com/svn/trunk@551 99668b35-9821-0410-8761-19e4c4f06640
Diffstat (limited to 'docs/samples')
-rw-r--r--docs/samples/load_memory.cpp62
-rw-r--r--docs/samples/load_stream.cpp2
2 files changed, 64 insertions, 0 deletions
diff --git a/docs/samples/load_memory.cpp b/docs/samples/load_memory.cpp
new file mode 100644
index 0000000..b8d898f
--- /dev/null
+++ b/docs/samples/load_memory.cpp
@@ -0,0 +1,62 @@
+#include "pugixml.hpp"
+
+#include <iostream>
+
+int main()
+{
+//[code_load_memory_decl
+ const char source[] = "<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>";
+ size_t size = sizeof(source);
+//]
+
+ pugi::xml_document doc;
+
+ {
+ //[code_load_memory_buffer
+ // You can use load_buffer to load document from immutable memory block:
+ pugi::xml_parse_result result = doc.load_buffer(source, size);
+ //]
+
+ std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
+ }
+
+ {
+ //[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
+ char* buffer = new char[size];
+ memcpy(buffer, source, size);
+
+ // The block can be allocated by any method; the block is modified during parsing
+ pugi::xml_parse_result result = doc.load_buffer_inplace(buffer, size);
+
+ //<-
+ std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
+ //->
+ // You have to destroy the block yourself after the document is no longer used
+ delete[] buffer;
+ //]
+ }
+
+ {
+ //[code_load_memory_buffer_inplace_own
+ // You can use load_buffer_inplace_own to load document from mutable memory block and to pass the ownership of this block
+ // The block has to be allocated via pugixml allocation function - using i.e. operator new here is incorrect
+ char* buffer = static_cast<char*>(pugi::get_memory_allocation_function()(size));
+ memcpy(buffer, source, size);
+
+ // The block will be deleted by the document
+ pugi::xml_parse_result result = doc.load_buffer_inplace_own(buffer, size);
+ //]
+
+ std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
+ }
+
+ {
+ //[code_load_memory_string
+ // You can use load to load document from null-terminated strings, for example literals:
+ pugi::xml_parse_result result = doc.load("<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>");
+ //]
+
+ std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
+ }
+}
diff --git a/docs/samples/load_stream.cpp b/docs/samples/load_stream.cpp
index f97949d..830ba4b 100644
--- a/docs/samples/load_stream.cpp
+++ b/docs/samples/load_stream.cpp
@@ -33,8 +33,10 @@ int main()
pugi::xml_document doc;
{
+ //[code_load_stream
std::ifstream stream("weekly-utf-8.xml");
pugi::xml_parse_result result = doc.load(stream);
+ //]
// first character of root name: U+9031, year: 1997
print_doc("UTF8 file from narrow stream", doc, result);