summaryrefslogtreecommitdiff
path: root/docs/samples/load_memory.cpp
blob: 1185944d430d52d73328ccb49e3c22baf46cfbe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "pugixml.hpp"

#include <iostream>
#include <cstring>

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; the block's 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;
    }
}

// vim:et