comparison dep/pugixml/docs/samples/load_memory.cpp @ 367:8d45d892be88 default tip

*: instead of pugixml, use Qt XML features this means we have one extra Qt dependency though...
author Paper <paper@tflc.us>
date Sun, 17 Nov 2024 22:55:47 -0500
parents 886f66775f31
children
comparison
equal deleted inserted replaced
366:886f66775f31 367:8d45d892be88
1 #include "pugixml.hpp"
2
3 #include <iostream>
4 #include <cstring>
5
6 int main()
7 {
8 // tag::decl[]
9 const char source[] = "<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>";
10 size_t size = sizeof(source);
11 // end::decl[]
12
13 pugi::xml_document doc;
14
15 {
16 // tag::load_buffer[]
17 // You can use load_buffer to load document from immutable memory block:
18 pugi::xml_parse_result result = doc.load_buffer(source, size);
19 // end::load_buffer[]
20
21 std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
22 }
23
24 {
25 // tag::load_buffer_inplace_begin[]
26 // You can use load_buffer_inplace to load document from mutable memory block; the block's lifetime must exceed that of document
27 char* buffer = new char[size];
28 memcpy(buffer, source, size);
29
30 // The block can be allocated by any method; the block is modified during parsing
31 pugi::xml_parse_result result = doc.load_buffer_inplace(buffer, size);
32 // end::load_buffer_inplace_begin[]
33
34 std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
35
36 // tag::load_buffer_inplace_end[]
37 // You have to destroy the block yourself after the document is no longer used
38 delete[] buffer;
39 // end::load_buffer_inplace_end[]
40 }
41
42 {
43 // tag::load_buffer_inplace_own[]
44 // You can use load_buffer_inplace_own to load document from mutable memory block and to pass the ownership of this block
45 // The block has to be allocated via pugixml allocation function - using i.e. operator new here is incorrect
46 char* buffer = static_cast<char*>(pugi::get_memory_allocation_function()(size));
47 memcpy(buffer, source, size);
48
49 // The block will be deleted by the document
50 pugi::xml_parse_result result = doc.load_buffer_inplace_own(buffer, size);
51 // end::load_buffer_inplace_own[]
52
53 std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
54 }
55
56 {
57 // tag::load_string[]
58 // You can use load to load document from null-terminated strings, for example literals:
59 pugi::xml_parse_result result = doc.load_string("<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>");
60 // end::load_string[]
61
62 std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
63 }
64 }
65
66 // vim:et