docs: Use AsciiDoc-compatible comments in samples

This commit is contained in:
Arseny Kapoulkine 2015-03-21 21:03:01 -07:00
parent 23e9beb003
commit 1a450b302a
26 changed files with 109 additions and 106 deletions

View File

@ -2,7 +2,7 @@
#include <new>
//[code_custom_memory_management_decl
// tag::decl[]
void* custom_allocate(size_t size)
{
return new (std::nothrow) char[size];
@ -12,13 +12,13 @@ void custom_deallocate(void* ptr)
{
delete[] static_cast<char*>(ptr);
}
//]
// end::decl[]
int main()
{
//[code_custom_memory_management_call
// tag::call[]
pugi::set_memory_management_functions(custom_allocate, custom_deallocate);
//]
// end::call[]
pugi::xml_document doc;
doc.load_string("<node/>");

View File

@ -3,7 +3,7 @@
#include <string.h>
#include <iostream>
//[code_include
// tag::code[]
bool load_preprocess(pugi::xml_document& doc, const char* path);
bool preprocess(pugi::xml_node node)
@ -51,7 +51,7 @@ bool load_preprocess(pugi::xml_document& doc, const char* path)
return result ? preprocess(doc) : false;
}
//]
// end::code[]
int main()
{

View File

@ -4,19 +4,21 @@
void check_xml(const char* source)
{
//[code_load_error_handling
// tag::code[]
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(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";
}
//]
// end::code[]
}
int main()

View File

@ -4,13 +4,13 @@
int main()
{
//[code_load_file
// tag::code[]
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file("tree.xml");
std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
//]
// end::code[]
}
// vim:et

View File

@ -5,41 +5,42 @@
int main()
{
//[code_load_memory_decl
// tag::decl[]
const char source[] = "<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>";
size_t size = sizeof(source);
//]
// end::decl[]
pugi::xml_document doc;
{
//[code_load_memory_buffer
// tag::load_buffer[]
// You can use load_buffer to load document from immutable memory block:
pugi::xml_parse_result result = doc.load_buffer(source, size);
//]
// end::load_buffer[]
std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
}
{
//[code_load_memory_buffer_inplace
// tag::load_buffer_inplace_begin[]
// 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);
// end::load_buffer_inplace_begin[]
//<-
std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
//->
// tag::load_buffer_inplace_end[]
// You have to destroy the block yourself after the document is no longer used
delete[] buffer;
//]
// end::load_buffer_inplace_end[]
}
{
//[code_load_memory_buffer_inplace_own
// tag::load_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));
@ -47,16 +48,16 @@ int main()
// The block will be deleted by the document
pugi::xml_parse_result result = doc.load_buffer_inplace_own(buffer, size);
//]
// end::load_buffer_inplace_own[]
std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
}
{
//[code_load_memory_string
// tag::load_string[]
// You can use load to load document from null-terminated strings, for example literals:
pugi::xml_parse_result result = doc.load_string("<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>");
//]
// end::load_string[]
std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
}

View File

@ -6,7 +6,7 @@ int main()
{
pugi::xml_document doc;
//[code_load_options
// tag::code[]
const char* source = "<!--comment--><node>&lt;</node>";
// Parsing with default options; note that comment node is not added to the tree, and entity reference &lt; is expanded
@ -24,7 +24,7 @@ int main()
// Parsing with minimal option mask; comment node is not added to the tree, and &lt; is not expanded
doc.load_string(source, pugi::parse_minimal);
std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n";
//]
// end::code[]
}
// vim:et

View File

@ -33,10 +33,10 @@ int main()
pugi::xml_document doc;
{
//[code_load_stream
// tag::code[]
std::ifstream stream("weekly-utf-8.xml");
pugi::xml_parse_result result = doc.load(stream);
//]
// end::code[]
// first character of root name: U+9031, year: 1997
print_doc("UTF8 file from narrow stream", doc, result);

View File

@ -6,7 +6,7 @@ int main()
{
pugi::xml_document doc;
//[code_modify_add
// tag::code[]
// add node with some name
pugi::xml_node node = doc.append_child("node");
@ -21,7 +21,7 @@ int main()
param.append_attribute("name") = "version";
param.append_attribute("value") = 1.1;
param.insert_attribute_after("type", param.attribute("name")) = "float";
//]
// end::code[]
doc.print(std::cout);
}

View File

@ -8,7 +8,7 @@ int main()
pugi::xml_document doc;
if (!doc.load_string("<node id='123'>text</node><!-- comment -->", pugi::parse_default | pugi::parse_comments)) return -1;
//[code_modify_base_node
// tag::node[]
pugi::xml_node node = doc.child("node");
// change node name
@ -21,9 +21,9 @@ int main()
// we can't change value of the element or name of the comment
std::cout << node.set_value("1") << ", " << doc.last_child().set_name("2") << std::endl;
//]
// end::node[]
//[code_modify_base_attr
// tag::attr[]
pugi::xml_attribute attr = node.attribute("id");
// change attribute name/value
@ -37,7 +37,7 @@ int main()
// we can also use assignment operators for more concise code
attr = true;
std::cout << "final attribute value: " << attr.value() << std::endl;
//]
// end::attr[]
}
// vim:et

View File

@ -7,7 +7,7 @@ int main()
pugi::xml_document doc;
if (!doc.load_string("<node><description>Simple node</description><param name='id' value='123'/></node>")) return -1;
//[code_modify_remove
// tag::code[]
// remove description node with the whole subtree
pugi::xml_node node = doc.child("node");
node.remove_child("description");
@ -19,7 +19,7 @@ int main()
// we can also remove nodes/attributes by handles
pugi::xml_attribute id = param.attribute("name");
param.remove_attribute(id);
//]
// end::code[]
doc.print(std::cout);
}

View File

@ -4,7 +4,7 @@
#include <iostream>
#include <cstring>
//[code_save_custom_writer
// tag::code[]
struct xml_string_writer: pugi::xml_writer
{
std::string result;
@ -14,7 +14,7 @@ struct xml_string_writer: pugi::xml_writer
result.append(static_cast<const char*>(data), size);
}
};
//]
// end::code[]
struct xml_memory_writer: pugi::xml_writer
{

View File

@ -4,7 +4,7 @@
int main()
{
//[code_save_declaration
// tag::code[]
// get a test document
pugi::xml_document doc;
doc.load_string("<foo bar='baz'><call>hey</call></foo>");
@ -21,7 +21,7 @@ int main()
// </foo>
doc.save(std::cout);
std::cout << std::endl;
//]
// end::code[]
}
// vim:et

View File

@ -8,10 +8,10 @@ int main()
pugi::xml_document doc;
doc.load_string("<foo bar='baz'>hey</foo>");
//[code_save_file
// tag::code[]
// save document to file
std::cout << "Saving result: " << doc.save_file("save_file_output.xml") << std::endl;
//]
// end::code[]
}
// vim:et

View File

@ -4,7 +4,7 @@
int main()
{
//[code_save_options
// tag::code[]
// get a test document
pugi::xml_document doc;
doc.load_string("<foo bar='baz'><call>hey</call></foo>");
@ -42,7 +42,7 @@ int main()
// <foo bar="baz"><call>hey</call></foo>
doc.save(std::cout, "\t", pugi::format_raw | pugi::format_no_declaration);
std::cout << std::endl;
//]
// end::code[]
}
// vim:et

View File

@ -8,11 +8,11 @@ int main()
pugi::xml_document doc;
doc.load_string("<foo bar='baz'><call>hey</call></foo>");
//[code_save_stream
// tag::code[]
// save document to standard output
std::cout << "Document:\n";
doc.save(std::cout);
//]
// end::code[]
}
// vim:et

View File

@ -4,7 +4,7 @@
int main()
{
//[code_save_subtree
// tag::code[]
// get a test document
pugi::xml_document doc;
doc.load_string("<foo bar='baz'><call>hey</call></foo>");
@ -20,7 +20,7 @@ int main()
// print a subtree to standard output (prints <call>hey</call>)
doc.child("foo").child("call").print(std::cout, "", pugi::format_raw);
std::cout << std::endl;
//]
// end::code[]
}
// vim:et

View File

@ -11,23 +11,23 @@ int main()
pugi::xml_node project = doc.child("project");
//[code_text_access
// tag::access[]
std::cout << "Project name: " << project.child("name").text().get() << std::endl;
std::cout << "Project version: " << project.child("version").text().as_double() << std::endl;
std::cout << "Project visibility: " << (project.child("public").text().as_bool(/* def= */ true) ? "public" : "private") << std::endl;
std::cout << "Project description: " << project.child("description").text().get() << std::endl;
//]
// end::access[]
std::cout << std::endl;
//[code_text_modify
// tag::modify[]
// change project version
project.child("version").text() = 1.2;
// add description element and set the contents
// note that we do not have to explicitly add the node_pcdata child
project.append_child("description").text().set("a test project");
//]
// end::modify[]
doc.save(std::cout);
}

View File

@ -10,7 +10,7 @@ int main()
pugi::xml_node tools = doc.child("Profile").child("Tools");
//[code_traverse_base_basic
// tag::basic[]
for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling())
{
std::cout << "Tool:";
@ -22,11 +22,11 @@ int main()
std::cout << std::endl;
}
//]
// end::basic[]
std::cout << std::endl;
//[code_traverse_base_data
// tag::data[]
for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
{
std::cout << "Tool " << tool.attribute("Filename").value();
@ -34,18 +34,18 @@ int main()
std::cout << ", Timeout " << tool.attribute("Timeout").as_int();
std::cout << ", Description '" << tool.child_value("Description") << "'\n";
}
//]
// end::data[]
std::cout << std::endl;
//[code_traverse_base_contents
// tag::contents[]
std::cout << "Tool for *.dae generation: " << tools.find_child_by_attribute("Tool", "OutputFileMasks", "*.dae").attribute("Filename").value() << "\n";
for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
{
std::cout << "Tool " << tool.attribute("Filename").value() << "\n";
}
//]
// end::contents[]
}
// vim:et

View File

@ -9,7 +9,7 @@ int main()
pugi::xml_node tools = doc.child("Profile").child("Tools");
//[code_traverse_iter
// tag::code[]
for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
{
std::cout << "Tool:";
@ -21,7 +21,7 @@ int main()
std::cout << std::endl;
}
//]
// end::code[]
}
// vim:et

View File

@ -3,7 +3,7 @@
#include <string.h>
#include <iostream>
//[code_traverse_predicate_decl
// tag::decl[]
bool small_timeout(pugi::xml_node node)
{
return node.attribute("Timeout").as_int() < 20;
@ -21,7 +21,7 @@ struct allow_remote_predicate
return node.attribute("AllowRemote").as_bool();
}
};
//]
// end::decl[]
int main()
{
@ -30,7 +30,7 @@ int main()
pugi::xml_node tools = doc.child("Profile").child("Tools");
//[code_traverse_predicate_find
// tag::find[]
// Find child via predicate (looks for direct children only)
std::cout << tools.find_child(allow_remote_predicate()).attribute("Filename").value() << std::endl;
@ -42,7 +42,7 @@ int main()
// We can use simple functions instead of function objects
std::cout << tools.find_child(small_timeout).attribute("Filename").value() << std::endl;
//]
// end::find[]
}
// vim:et

View File

@ -1,32 +1,32 @@
#include "pugixml.hpp"
#include <iostream>
int main()
{
pugi::xml_document doc;
if (!doc.load_file("xgconsole.xml")) return -1;
pugi::xml_node tools = doc.child("Profile").child("Tools");
//[code_traverse_rangefor
for (pugi::xml_node tool: tools.children("Tool"))
{
std::cout << "Tool:";
for (pugi::xml_attribute attr: tool.attributes())
{
std::cout << " " << attr.name() << "=" << attr.value();
}
for (pugi::xml_node child: tool.children())
{
std::cout << ", child " << child.name();
}
std::cout << std::endl;
}
//]
}
// vim:et
#include "pugixml.hpp"
#include <iostream>
int main()
{
pugi::xml_document doc;
if (!doc.load_file("xgconsole.xml")) return -1;
pugi::xml_node tools = doc.child("Profile").child("Tools");
// tag::code[]
for (pugi::xml_node tool: tools.children("Tool"))
{
std::cout << "Tool:";
for (pugi::xml_attribute attr: tool.attributes())
{
std::cout << " " << attr.name() << "=" << attr.value();
}
for (pugi::xml_node child: tool.children())
{
std::cout << ", child " << child.name();
}
std::cout << std::endl;
}
// end::code[]
}
// vim:et

View File

@ -7,7 +7,7 @@ const char* node_types[] =
"null", "document", "element", "pcdata", "cdata", "comment", "pi", "declaration"
};
//[code_traverse_walker_impl
// tag::impl[]
struct simple_walker: pugi::xml_tree_walker
{
virtual bool for_each(pugi::xml_node& node)
@ -19,17 +19,17 @@ struct simple_walker: pugi::xml_tree_walker
return true; // continue traversal
}
};
//]
// end::impl[]
int main()
{
pugi::xml_document doc;
if (!doc.load_file("tree.xml")) return -1;
//[code_traverse_walker_traverse
// tag::traverse[]
simple_walker walker;
doc.traverse(walker);
//]
// end::traverse[]
}
// vim:et

View File

@ -7,7 +7,7 @@ int main()
pugi::xml_document doc;
if (!doc.load_file("xgconsole.xml")) return -1;
//[code_xpath_error
// tag::code[]
// Exception is thrown for incorrect query syntax
try
{
@ -37,7 +37,7 @@ int main()
{
std::cout << "Select failed: " << e.what() << std::endl;
}
//]
// end::code[]
}
// vim:et

View File

@ -8,7 +8,7 @@ int main()
pugi::xml_document doc;
if (!doc.load_file("xgconsole.xml")) return -1;
//[code_xpath_query
// tag::code[]
// Select nodes via compiled query
pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']");
@ -30,7 +30,7 @@ int main()
if (query_name_valid.evaluate_boolean(tool)) std::cout << s << std::endl;
}
//]
// end::code[]
}
// vim:et

View File

@ -7,7 +7,7 @@ int main()
pugi::xml_document doc;
if (!doc.load_file("xgconsole.xml")) return -1;
//[code_xpath_select
// tag::code[]
pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']");
std::cout << "Tools:\n";
@ -22,7 +22,7 @@ int main()
if (build_tool)
std::cout << "Build tool: " << build_tool.node().attribute("Filename").value() << "\n";
//]
// end::code[]
}
// vim:et

View File

@ -8,7 +8,7 @@ int main()
pugi::xml_document doc;
if (!doc.load_file("xgconsole.xml")) return -1;
//[code_xpath_variables
// tag::code[]
// Select nodes via compiled query
pugi::xpath_variable_set vars;
vars.add("remote", pugi::xpath_type_boolean);
@ -32,7 +32,7 @@ int main()
std::cout << "Local tool imm: ";
tools_local_imm[0].node().print(std::cout);
//]
// end::code[]
}
// vim:et