From fb507ab2d6f9eaa090671e40b582354b16f3821c Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Sun, 11 Jul 2010 13:29:12 +0000 Subject: docs: Replaced all tabs with 4 spaces (guaranteed tab size) git-svn-id: http://pugixml.googlecode.com/svn/trunk@591 99668b35-9821-0410-8761-19e4c4f06640 --- docs/samples/custom_memory_management.cpp | 12 ++- docs/samples/include.cpp | 68 ++++++------- docs/samples/load_error_handling.cpp | 32 +++--- docs/samples/load_file.cpp | 8 +- docs/samples/load_memory.cpp | 106 ++++++++++---------- docs/samples/load_options.cpp | 30 +++--- docs/samples/load_stream.cpp | 158 +++++++++++++++--------------- docs/samples/modify_add.cpp | 44 +++++---- docs/samples/modify_base.cpp | 48 ++++----- docs/samples/modify_remove.cpp | 28 +++--- docs/samples/save_custom_writer.cpp | 128 ++++++++++++------------ docs/samples/save_file.cpp | 18 ++-- docs/samples/save_options.cpp | 80 +++++++-------- docs/samples/save_stream.cpp | 18 ++-- docs/samples/save_subtree.cpp | 30 +++--- docs/samples/traverse_base.cpp | 4 +- docs/samples/traverse_iter.cpp | 4 +- docs/samples/traverse_predicate.cpp | 4 +- docs/samples/traverse_walker.cpp | 4 +- docs/samples/xpath_error.cpp | 60 ++++++------ docs/samples/xpath_query.cpp | 38 +++---- docs/samples/xpath_select.cpp | 24 ++--- 22 files changed, 495 insertions(+), 451 deletions(-) (limited to 'docs/samples') diff --git a/docs/samples/custom_memory_management.cpp b/docs/samples/custom_memory_management.cpp index 01a2acf..7e69528 100644 --- a/docs/samples/custom_memory_management.cpp +++ b/docs/samples/custom_memory_management.cpp @@ -5,21 +5,23 @@ //[code_custom_memory_management_decl void* custom_allocate(size_t size) { - return new (std::nothrow) char[size]; + return new (std::nothrow) char[size]; } void custom_deallocate(void* ptr) { - delete[] static_cast(ptr); + delete[] static_cast(ptr); } //] int main() { //[code_custom_memory_management_call - pugi::set_memory_management_functions(custom_allocate, custom_deallocate); + pugi::set_memory_management_functions(custom_allocate, custom_deallocate); //] - pugi::xml_document doc; - doc.load(""); + pugi::xml_document doc; + doc.load(""); } + +// vim:et diff --git a/docs/samples/include.cpp b/docs/samples/include.cpp index 528d016..0d80887 100644 --- a/docs/samples/include.cpp +++ b/docs/samples/include.cpp @@ -8,55 +8,57 @@ bool load_preprocess(pugi::xml_document& doc, const char* path); bool preprocess(pugi::xml_node node) { - for (pugi::xml_node child = node.first_child(); child; ) - { - if (child.type() == pugi::node_pi && strcmp(child.name(), "include") == 0) - { - pugi::xml_node include = child; + for (pugi::xml_node child = node.first_child(); child; ) + { + if (child.type() == pugi::node_pi && strcmp(child.name(), "include") == 0) + { + pugi::xml_node include = child; - // load new preprocessed document (note: ideally this should handle relative paths) - const char* path = include.value(); + // load new preprocessed document (note: ideally this should handle relative paths) + const char* path = include.value(); - pugi::xml_document doc; - if (!load_preprocess(doc, path)) return false; + pugi::xml_document doc; + if (!load_preprocess(doc, path)) return false; - // insert the comment marker above include directive - node.insert_child_before(pugi::node_comment, include).set_value(path); + // insert the comment marker above include directive + node.insert_child_before(pugi::node_comment, include).set_value(path); - // copy the document above the include directive (this retains the original order!) - for (pugi::xml_node ic = doc.first_child(); ic; ic = ic.next_sibling()) - { - node.insert_copy_before(ic, include); - } + // copy the document above the include directive (this retains the original order!) + for (pugi::xml_node ic = doc.first_child(); ic; ic = ic.next_sibling()) + { + node.insert_copy_before(ic, include); + } - // remove the include node and move to the next child - child = child.next_sibling(); + // remove the include node and move to the next child + child = child.next_sibling(); - node.remove_child(include); - } - else - { - if (!preprocess(child)) return false; + node.remove_child(include); + } + else + { + if (!preprocess(child)) return false; - child = child.next_sibling(); - } - } + child = child.next_sibling(); + } + } - return true; + return true; } bool load_preprocess(pugi::xml_document& doc, const char* path) { - pugi::xml_parse_result result = doc.load_file(path, pugi::parse_default | pugi::parse_pi); // for - - return result ? preprocess(doc) : false; + pugi::xml_parse_result result = doc.load_file(path, pugi::parse_default | pugi::parse_pi); // for + + return result ? preprocess(doc) : false; } //] int main() { - pugi::xml_document doc; - if (!load_preprocess(doc, "character.xml")) return -1; + pugi::xml_document doc; + if (!load_preprocess(doc, "character.xml")) return -1; - doc.print(std::cout); + doc.print(std::cout); } + +// vim:et diff --git a/docs/samples/load_error_handling.cpp b/docs/samples/load_error_handling.cpp index 9b274ba..c7f44b8 100644 --- a/docs/samples/load_error_handling.cpp +++ b/docs/samples/load_error_handling.cpp @@ -5,25 +5,27 @@ void check_xml(const char* source) { //[code_load_error_handling - pugi::xml_document doc; - pugi::xml_parse_result result = doc.load(source); + 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"; - } + 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("text"); - check_xml("text"); - check_xml("text"); - check_xml("<#tag />"); + check_xml("text"); + check_xml("text"); + check_xml("text"); + check_xml("<#tag />"); } + +// vim:et diff --git a/docs/samples/load_file.cpp b/docs/samples/load_file.cpp index 1a102c2..3e77f04 100644 --- a/docs/samples/load_file.cpp +++ b/docs/samples/load_file.cpp @@ -5,10 +5,12 @@ int main() { //[code_load_file - pugi::xml_document doc; + pugi::xml_document doc; - pugi::xml_parse_result result = doc.load_file("tree.xml"); + 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; + std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl; //] } + +// vim:et diff --git a/docs/samples/load_memory.cpp b/docs/samples/load_memory.cpp index 5f975b2..67d5dee 100644 --- a/docs/samples/load_memory.cpp +++ b/docs/samples/load_memory.cpp @@ -5,58 +5,60 @@ int main() { //[code_load_memory_decl - const char source[] = "0 0 1 1"; - size_t size = sizeof(source); + const char source[] = "0 0 1 1"; + 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(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("0 0 1 1"); - //] - - std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl; - } + 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(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("0 0 1 1"); + //] + + std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl; + } } + +// vim:et diff --git a/docs/samples/load_options.cpp b/docs/samples/load_options.cpp index 82273aa..c192914 100644 --- a/docs/samples/load_options.cpp +++ b/docs/samples/load_options.cpp @@ -4,25 +4,27 @@ int main() { - pugi::xml_document doc; + pugi::xml_document doc; //[code_load_options - const char* source = "<"; + const char* source = "<"; - // Parsing with default options; note that comment node is not added to the tree, and entity reference < is expanded - doc.load(source); - std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; + // Parsing with default options; note that comment node is not added to the tree, and entity reference < is expanded + doc.load(source); + std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; - // Parsing with additional parse_comments option; comment node is now added to the tree - doc.load(source, pugi::parse_default | pugi::parse_comments); - std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; + // Parsing with additional parse_comments option; comment node is now added to the tree + doc.load(source, pugi::parse_default | pugi::parse_comments); + std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; - // Parsing with additional parse_comments option and without the (default) parse_escapes option; < is not expanded - doc.load(source, (pugi::parse_default | pugi::parse_comments) & ~pugi::parse_escapes); - std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; + // Parsing with additional parse_comments option and without the (default) parse_escapes option; < is not expanded + doc.load(source, (pugi::parse_default | pugi::parse_comments) & ~pugi::parse_escapes); + std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; - // Parsing with minimal option mask; comment node is not added to the tree, and < is not expanded - doc.load(source, pugi::parse_minimal); - std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; + // Parsing with minimal option mask; comment node is not added to the tree, and < is not expanded + doc.load(source, pugi::parse_minimal); + std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; //] } + +// vim:et diff --git a/docs/samples/load_stream.cpp b/docs/samples/load_stream.cpp index 830ba4b..c717eeb 100644 --- a/docs/samples/load_stream.cpp +++ b/docs/samples/load_stream.cpp @@ -6,90 +6,92 @@ void print_doc(const char* message, const pugi::xml_document& doc, const pugi::xml_parse_result& result) { - std::cout - << message - << "\t: load result '" << result.description() << "'" - << ", first character of root name: U+" << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << pugi::as_wide(doc.first_child().name())[0] - << ", year: " << doc.first_child().first_child().first_child().child_value() - << std::endl; + std::cout + << message + << "\t: load result '" << result.description() << "'" + << ", first character of root name: U+" << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << pugi::as_wide(doc.first_child().name())[0] + << ", year: " << doc.first_child().first_child().first_child().child_value() + << std::endl; } bool try_imbue(std::wistream& stream, const char* name) { - try - { - stream.imbue(std::locale(name)); - - return true; - } - catch (const std::exception&) - { - return false; - } + try + { + stream.imbue(std::locale(name)); + + return true; + } + catch (const std::exception&) + { + return false; + } } 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); - } - - { - std::ifstream stream("weekly-utf-16.xml"); - pugi::xml_parse_result result = doc.load(stream); - - // first character of root name: U+9031, year: 1997 - print_doc("UTF16 file from narrow stream", doc, result); - } - - { - // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-8 file from a wide stream - // directly if you have localized characters; you'll have to provide a UTF8 locale (there is no - // standard one; you can use utf8_codecvt_facet from Boost or codecvt_utf8 from C++0x) - std::wifstream stream("weekly-utf-8.xml"); - - if (try_imbue(stream, "en_US.UTF-8")) // try Linux encoding - { - pugi::xml_parse_result result = doc.load(stream); - - // first character of root name: U+00E9, year: 1997 - print_doc("UTF8 file from wide stream", doc, result); - } - else - { - std::cout << "UTF-8 locale is not available\n"; - } - } - - { - // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-16 file from a wide stream without - // using custom codecvt; you can use codecvt_utf16 from C++0x - } - - { - // Since encoding names are non-standard, you can't load the Shift-JIS (or any other non-ASCII) file - // from a wide stream portably - std::wifstream stream("weekly-shift_jis.xml"); - - if (try_imbue(stream, ".932") || // try Microsoft encoding - try_imbue(stream, "ja_JP.SJIS")) // try Linux encoding; run "localedef -i ja_JP -c -f SHIFT_JIS /usr/lib/locale/ja_JP.SJIS" to get it - { - pugi::xml_parse_result result = doc.load(stream); - - // first character of root name: U+9031, year: 1997 - print_doc("Shift-JIS file from wide stream", doc, result); - } - else - { - std::cout << "Shift-JIS locale is not available\n"; - } - } + 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); + } + + { + std::ifstream stream("weekly-utf-16.xml"); + pugi::xml_parse_result result = doc.load(stream); + + // first character of root name: U+9031, year: 1997 + print_doc("UTF16 file from narrow stream", doc, result); + } + + { + // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-8 file from a wide stream + // directly if you have localized characters; you'll have to provide a UTF8 locale (there is no + // standard one; you can use utf8_codecvt_facet from Boost or codecvt_utf8 from C++0x) + std::wifstream stream("weekly-utf-8.xml"); + + if (try_imbue(stream, "en_US.UTF-8")) // try Linux encoding + { + pugi::xml_parse_result result = doc.load(stream); + + // first character of root name: U+00E9, year: 1997 + print_doc("UTF8 file from wide stream", doc, result); + } + else + { + std::cout << "UTF-8 locale is not available\n"; + } + } + + { + // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-16 file from a wide stream without + // using custom codecvt; you can use codecvt_utf16 from C++0x + } + + { + // Since encoding names are non-standard, you can't load the Shift-JIS (or any other non-ASCII) file + // from a wide stream portably + std::wifstream stream("weekly-shift_jis.xml"); + + if (try_imbue(stream, ".932") || // try Microsoft encoding + try_imbue(stream, "ja_JP.SJIS")) // try Linux encoding; run "localedef -i ja_JP -c -f SHIFT_JIS /usr/lib/locale/ja_JP.SJIS" to get it + { + pugi::xml_parse_result result = doc.load(stream); + + // first character of root name: U+9031, year: 1997 + print_doc("Shift-JIS file from wide stream", doc, result); + } + else + { + std::cout << "Shift-JIS locale is not available\n"; + } + } } + +// vim:et diff --git a/docs/samples/modify_add.cpp b/docs/samples/modify_add.cpp index 88c8fe6..76619b3 100644 --- a/docs/samples/modify_add.cpp +++ b/docs/samples/modify_add.cpp @@ -4,27 +4,29 @@ int main() { - pugi::xml_document doc; + pugi::xml_document doc; //[code_modify_add - // add node with some name - pugi::xml_node node = doc.append_child(); - node.set_name("node"); - - // add description node with text child - pugi::xml_node descr = node.append_child(); - descr.set_name("description"); - descr.append_child(pugi::node_pcdata).set_value("Simple node"); - - // add param node before the description - pugi::xml_node param = node.insert_child_before(pugi::node_element, descr); - param.set_name("param"); - - // add attributes to param node - param.append_attribute("name") = "version"; - param.append_attribute("value") = 1.1; - param.insert_attribute_after("type", param.attribute("name")) = "float"; - //] - - doc.print(std::cout); + // add node with some name + pugi::xml_node node = doc.append_child(); + node.set_name("node"); + + // add description node with text child + pugi::xml_node descr = node.append_child(); + descr.set_name("description"); + descr.append_child(pugi::node_pcdata).set_value("Simple node"); + + // add param node before the description + pugi::xml_node param = node.insert_child_before(pugi::node_element, descr); + param.set_name("param"); + + // add attributes to param node + param.append_attribute("name") = "version"; + param.append_attribute("value") = 1.1; + param.insert_attribute_after("type", param.attribute("name")) = "float"; + //] + + doc.print(std::cout); } + +// vim:et diff --git a/docs/samples/modify_base.cpp b/docs/samples/modify_base.cpp index 4213cff..7d52bd1 100644 --- a/docs/samples/modify_base.cpp +++ b/docs/samples/modify_base.cpp @@ -5,37 +5,39 @@ int main() { - pugi::xml_document doc; + pugi::xml_document doc; if (!doc.load("text", pugi::parse_default | pugi::parse_comments)) return -1; //[code_modify_base_node - pugi::xml_node node = doc.child("node"); + pugi::xml_node node = doc.child("node"); - // change node name - std::cout << node.set_name("notnode"); - std::cout << ", new node name: " << node.name() << std::endl; + // change node name + std::cout << node.set_name("notnode"); + std::cout << ", new node name: " << node.name() << std::endl; - // change comment text - std::cout << doc.last_child().set_value("useless comment"); - std::cout << ", new comment text: " << doc.last_child().value() << std::endl; + // change comment text + std::cout << doc.last_child().set_value("useless comment"); + std::cout << ", new comment text: " << doc.last_child().value() << std::endl; - // 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; - //] + // 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; + //] - //[code_modify_base_attr - pugi::xml_attribute attr = node.attribute("id"); + //[code_modify_base_attr + pugi::xml_attribute attr = node.attribute("id"); - // change attribute name/value - std::cout << attr.set_name("key") << ", " << attr.set_value("345"); - std::cout << ", new attribute: " << attr.name() << "=" << attr.value() << std::endl; + // change attribute name/value + std::cout << attr.set_name("key") << ", " << attr.set_value("345"); + std::cout << ", new attribute: " << attr.name() << "=" << attr.value() << std::endl; - // we can use numbers or booleans - attr.set_value(1.234); - std::cout << "new attribute value: " << attr.value() << std::endl; + // we can use numbers or booleans + attr.set_value(1.234); + std::cout << "new attribute value: " << attr.value() << std::endl; - // we can also use assignment operators for more concise code - attr = true; - std::cout << "final attribute value: " << attr.value() << std::endl; - //] + // we can also use assignment operators for more concise code + attr = true; + std::cout << "final attribute value: " << attr.value() << std::endl; + //] } + +// vim:et diff --git a/docs/samples/modify_remove.cpp b/docs/samples/modify_remove.cpp index 21dcc32..015b0da 100644 --- a/docs/samples/modify_remove.cpp +++ b/docs/samples/modify_remove.cpp @@ -4,22 +4,24 @@ int main() { - pugi::xml_document doc; - if (!doc.load("Simple node")) return -1; + pugi::xml_document doc; + if (!doc.load("Simple node")) return -1; //[code_modify_remove - // remove description node with the whole subtree - pugi::xml_node node = doc.child("node"); - node.remove_child("description"); + // remove description node with the whole subtree + pugi::xml_node node = doc.child("node"); + node.remove_child("description"); - // remove id attribute - pugi::xml_node param = node.child("param"); - param.remove_attribute("value"); + // remove id attribute + pugi::xml_node param = node.child("param"); + param.remove_attribute("value"); - // we can also remove nodes/attributes by handles - pugi::xml_attribute id = param.attribute("name"); - param.remove_attribute(id); - //] + // we can also remove nodes/attributes by handles + pugi::xml_attribute id = param.attribute("name"); + param.remove_attribute(id); + //] - doc.print(std::cout); + doc.print(std::cout); } + +// vim:et diff --git a/docs/samples/save_custom_writer.cpp b/docs/samples/save_custom_writer.cpp index 0c7669c..c0999e1 100644 --- a/docs/samples/save_custom_writer.cpp +++ b/docs/samples/save_custom_writer.cpp @@ -7,108 +7,110 @@ //[code_save_custom_writer struct xml_string_writer: pugi::xml_writer { - std::string result; + std::string result; - virtual void write(const void* data, size_t size) - { - result += std::string(static_cast(data), size); - } + virtual void write(const void* data, size_t size) + { + result += std::string(static_cast(data), size); + } }; //] struct xml_memory_writer: pugi::xml_writer { - char* buffer; - size_t capacity; + char* buffer; + size_t capacity; - size_t result; + size_t result; - xml_memory_writer(): buffer(0), capacity(0), result(0) - { - } + xml_memory_writer(): buffer(0), capacity(0), result(0) + { + } - xml_memory_writer(char* buffer, size_t capacity): buffer(buffer), capacity(capacity), 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; - } + 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; + 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); - } + memcpy(buffer + result, data, chunk); + } - result += size; - } + result += size; + } }; std::string node_to_string(pugi::xml_node node) { - xml_string_writer writer; - node.print(writer); + xml_string_writer writer; + node.print(writer); - return writer.result; + return writer.result; } char* node_to_buffer(pugi::xml_node node, char* buffer, size_t size) { - if (size == 0) return buffer; + if (size == 0) return buffer; - // leave one character for null terminator - xml_memory_writer writer(buffer, size - 1); - node.print(writer); + // leave one character for null terminator + xml_memory_writer writer(buffer, size - 1); + node.print(writer); - // null terminate - buffer[writer.written_size()] = 0; + // null terminate + buffer[writer.written_size()] = 0; - return buffer; + return buffer; } char* node_to_buffer_heap(pugi::xml_node node) { - // first pass: get required memory size - xml_memory_writer counter; - node.print(counter); + // 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]; + // 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); + // second pass: actual printing + xml_memory_writer writer(buffer, counter.result); + node.print(writer); - // null terminate - buffer[writer.written_size()] = 0; + // null terminate + buffer[writer.written_size()] = 0; - return buffer; + return buffer; } int main() { - // get a test document - pugi::xml_document doc; - doc.load("hey"); + // get a test document + pugi::xml_document doc; + doc.load("hey"); - // get contents as std::string (single pass) - printf("contents: [%s]\n", node_to_string(doc).c_str()); + // 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) + 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 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; + // 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; } + +// vim:et diff --git a/docs/samples/save_file.cpp b/docs/samples/save_file.cpp index 44bcfd1..c30e986 100644 --- a/docs/samples/save_file.cpp +++ b/docs/samples/save_file.cpp @@ -4,12 +4,14 @@ int main() { - // get a test document - pugi::xml_document doc; - doc.load("hey"); - - //[code_save_file - // save document to file - std::cout << "Saving result: " << doc.save_file("save_file_output.xml") << std::endl; - //] + // get a test document + pugi::xml_document doc; + doc.load("hey"); + + //[code_save_file + // save document to file + std::cout << "Saving result: " << doc.save_file("save_file_output.xml") << std::endl; + //] } + +// vim:et diff --git a/docs/samples/save_options.cpp b/docs/samples/save_options.cpp index 5b4ccdb..45ac096 100644 --- a/docs/samples/save_options.cpp +++ b/docs/samples/save_options.cpp @@ -4,43 +4,45 @@ int main() { - //[code_save_options - // get a test document - pugi::xml_document doc; - doc.load("hey"); - - // default options; prints - // - // - // hey - // - doc.save(std::cout); - std::cout << std::endl; - - // default options with custom indentation string; prints - // - // - // --hey - // - doc.save(std::cout, "--"); - std::cout << std::endl; - - // default options without indentation; prints - // - // - // hey - // - doc.save(std::cout, "\t", pugi::format_default & ~pugi::format_indent); // can also pass "" instead of indentation string for the same effect - std::cout << std::endl; - - // raw output; prints - // hey - doc.save(std::cout, "\t", pugi::format_raw); - std::cout << std::endl << std::endl; - - // raw output without declaration; prints - // hey - doc.save(std::cout, "\t", pugi::format_raw | pugi::format_no_declaration); - std::cout << std::endl; - //] + //[code_save_options + // get a test document + pugi::xml_document doc; + doc.load("hey"); + + // default options; prints + // + // + // hey + // + doc.save(std::cout); + std::cout << std::endl; + + // default options with custom indentation string; prints + // + // + // --hey + // + doc.save(std::cout, "--"); + std::cout << std::endl; + + // default options without indentation; prints + // + // + // hey + // + doc.save(std::cout, "\t", pugi::format_default & ~pugi::format_indent); // can also pass "" instead of indentation string for the same effect + std::cout << std::endl; + + // raw output; prints + // hey + doc.save(std::cout, "\t", pugi::format_raw); + std::cout << std::endl << std::endl; + + // raw output without declaration; prints + // hey + doc.save(std::cout, "\t", pugi::format_raw | pugi::format_no_declaration); + std::cout << std::endl; + //] } + +// vim:et diff --git a/docs/samples/save_stream.cpp b/docs/samples/save_stream.cpp index 65be76b..39e99eb 100644 --- a/docs/samples/save_stream.cpp +++ b/docs/samples/save_stream.cpp @@ -4,13 +4,15 @@ int main() { - // get a test document - pugi::xml_document doc; - doc.load("hey"); + // get a test document + pugi::xml_document doc; + doc.load("hey"); - //[code_save_stream - // save document to standard output - std::cout << "Document:\n"; - doc.save(std::cout); - //] + //[code_save_stream + // save document to standard output + std::cout << "Document:\n"; + doc.save(std::cout); + //] } + +// vim:et diff --git a/docs/samples/save_subtree.cpp b/docs/samples/save_subtree.cpp index fc48905..a1c9f2f 100644 --- a/docs/samples/save_subtree.cpp +++ b/docs/samples/save_subtree.cpp @@ -4,21 +4,23 @@ int main() { - //[code_save_subtree - // get a test document - pugi::xml_document doc; - doc.load("hey"); + //[code_save_subtree + // get a test document + pugi::xml_document doc; + doc.load("hey"); - // print document to standard output (prints hey) - doc.save(std::cout, "", pugi::format_raw); - std::cout << std::endl; + // print document to standard output (prints hey) + doc.save(std::cout, "", pugi::format_raw); + std::cout << std::endl; - // print document to standard output as a regular node (prints hey) - doc.print(std::cout, "", pugi::format_raw); - std::cout << std::endl; + // print document to standard output as a regular node (prints hey) + doc.print(std::cout, "", pugi::format_raw); + std::cout << std::endl; - // print a subtree to standard output (prints hey) - doc.child("foo").child("call").print(std::cout, "", pugi::format_raw); - std::cout << std::endl; - //] + // print a subtree to standard output (prints hey) + doc.child("foo").child("call").print(std::cout, "", pugi::format_raw); + std::cout << std::endl; + //] } + +// vim:et diff --git a/docs/samples/traverse_base.cpp b/docs/samples/traverse_base.cpp index 7559859..b6f6d2e 100644 --- a/docs/samples/traverse_base.cpp +++ b/docs/samples/traverse_base.cpp @@ -5,7 +5,7 @@ int main() { - pugi::xml_document doc; + pugi::xml_document doc; if (!doc.load_file("xgconsole.xml")) return -1; pugi::xml_node tools = doc.child("Profile").child("Tools"); @@ -47,3 +47,5 @@ int main() } //] } + +// vim:et diff --git a/docs/samples/traverse_iter.cpp b/docs/samples/traverse_iter.cpp index b134f2f..935540f 100644 --- a/docs/samples/traverse_iter.cpp +++ b/docs/samples/traverse_iter.cpp @@ -4,7 +4,7 @@ int main() { - pugi::xml_document doc; + pugi::xml_document doc; if (!doc.load_file("xgconsole.xml")) return -1; pugi::xml_node tools = doc.child("Profile").child("Tools"); @@ -23,3 +23,5 @@ int main() } //] } + +// vim:et diff --git a/docs/samples/traverse_predicate.cpp b/docs/samples/traverse_predicate.cpp index 19fa32f..e565597 100644 --- a/docs/samples/traverse_predicate.cpp +++ b/docs/samples/traverse_predicate.cpp @@ -25,7 +25,7 @@ struct allow_remote_predicate int main() { - pugi::xml_document doc; + pugi::xml_document doc; if (!doc.load_file("xgconsole.xml")) return -1; pugi::xml_node tools = doc.child("Profile").child("Tools"); @@ -44,3 +44,5 @@ int main() std::cout << tools.find_child(small_timeout).attribute("Filename").value() << std::endl; //] } + +// vim:et diff --git a/docs/samples/traverse_walker.cpp b/docs/samples/traverse_walker.cpp index f712eee..9387fa0 100644 --- a/docs/samples/traverse_walker.cpp +++ b/docs/samples/traverse_walker.cpp @@ -23,7 +23,7 @@ struct simple_walker: pugi::xml_tree_walker int main() { - pugi::xml_document doc; + pugi::xml_document doc; if (!doc.load_file("tree.xml")) return -1; //[code_traverse_walker_traverse @@ -31,3 +31,5 @@ int main() doc.traverse(walker); //] } + +// vim:et diff --git a/docs/samples/xpath_error.cpp b/docs/samples/xpath_error.cpp index 1ecb41b..3415d31 100644 --- a/docs/samples/xpath_error.cpp +++ b/docs/samples/xpath_error.cpp @@ -4,38 +4,40 @@ int main() { - pugi::xml_document doc; - if (!doc.load_file("xgconsole.xml")) return -1; + pugi::xml_document doc; + if (!doc.load_file("xgconsole.xml")) return -1; //[code_xpath_error - // Exception is thrown for incorrect query syntax - try - { - doc.select_nodes("//nodes[#true()]"); - } - catch (const pugi::xpath_exception& e) - { - std::cout << "Select failed: " << e.what() << std::endl; - } + // Exception is thrown for incorrect query syntax + try + { + doc.select_nodes("//nodes[#true()]"); + } + catch (const pugi::xpath_exception& e) + { + std::cout << "Select failed: " << e.what() << std::endl; + } - // Exception is thrown for incorrect query semantics - try - { - doc.select_nodes("(123)/next"); - } - catch (const pugi::xpath_exception& e) - { - std::cout << "Select failed: " << e.what() << std::endl; - } + // Exception is thrown for incorrect query semantics + try + { + doc.select_nodes("(123)/next"); + } + catch (const pugi::xpath_exception& e) + { + std::cout << "Select failed: " << e.what() << std::endl; + } - // Exception is thrown for query with incorrect return type - try - { - doc.select_nodes("123"); - } - catch (const pugi::xpath_exception& e) - { - std::cout << "Select failed: " << e.what() << std::endl; - } + // Exception is thrown for query with incorrect return type + try + { + doc.select_nodes("123"); + } + catch (const pugi::xpath_exception& e) + { + std::cout << "Select failed: " << e.what() << std::endl; + } //] } + +// vim:et diff --git a/docs/samples/xpath_query.cpp b/docs/samples/xpath_query.cpp index ad62690..b14a0b0 100644 --- a/docs/samples/xpath_query.cpp +++ b/docs/samples/xpath_query.cpp @@ -5,30 +5,32 @@ int main() { - pugi::xml_document doc; - if (!doc.load_file("xgconsole.xml")) return -1; + pugi::xml_document doc; + if (!doc.load_file("xgconsole.xml")) return -1; //[code_xpath_query - // Select nodes via compiled query - pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']"); + // Select nodes via compiled query + pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']"); - pugi::xpath_node_set tools = query_remote_tools.evaluate_node_set(doc); - std::cout << "Remote tool: "; - tools[2].node().print(std::cout); + pugi::xpath_node_set tools = query_remote_tools.evaluate_node_set(doc); + std::cout << "Remote tool: "; + tools[2].node().print(std::cout); - // Evaluate numbers via compiled query - pugi::xpath_query query_timeouts("sum(//Tool/@Timeout)"); - std::cout << query_timeouts.evaluate_number(doc) << std::endl; + // Evaluate numbers via compiled query + pugi::xpath_query query_timeouts("sum(//Tool/@Timeout)"); + std::cout << query_timeouts.evaluate_number(doc) << std::endl; - // Evaluate strings via compiled query for different context nodes - pugi::xpath_query query_name_valid("string-length(substring-before(@Filename, '_')) > 0 and @OutputFileMasks"); - pugi::xpath_query query_name("concat(substring-before(@Filename, '_'), ' produces ', @OutputFileMasks)"); + // Evaluate strings via compiled query for different context nodes + pugi::xpath_query query_name_valid("string-length(substring-before(@Filename, '_')) > 0 and @OutputFileMasks"); + pugi::xpath_query query_name("concat(substring-before(@Filename, '_'), ' produces ', @OutputFileMasks)"); - for (pugi::xml_node tool = doc.first_element_by_path("Profile/Tools/Tool"); tool; tool = tool.next_sibling()) - { - std::string s = query_name.evaluate_string(tool); + for (pugi::xml_node tool = doc.first_element_by_path("Profile/Tools/Tool"); tool; tool = tool.next_sibling()) + { + std::string s = query_name.evaluate_string(tool); - if (query_name_valid.evaluate_boolean(tool)) std::cout << s << std::endl; - } + if (query_name_valid.evaluate_boolean(tool)) std::cout << s << std::endl; + } //] } + +// vim:et diff --git a/docs/samples/xpath_select.cpp b/docs/samples/xpath_select.cpp index 8025e04..5645fd3 100644 --- a/docs/samples/xpath_select.cpp +++ b/docs/samples/xpath_select.cpp @@ -4,22 +4,24 @@ int main() { - pugi::xml_document doc; - if (!doc.load_file("xgconsole.xml")) return -1; + pugi::xml_document doc; + if (!doc.load_file("xgconsole.xml")) return -1; //[code_xpath_select - pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']"); + pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']"); - std::cout << "Tools:"; + std::cout << "Tools:"; - for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it) - { - pugi::xpath_node node = *it; - std::cout << " " << node.node().attribute("Filename").value(); - } + for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it) + { + pugi::xpath_node node = *it; + std::cout << " " << node.node().attribute("Filename").value(); + } - pugi::xpath_node build_tool = doc.select_single_node("//Tool[contains(Description, 'build system')]"); + pugi::xpath_node build_tool = doc.select_single_node("//Tool[contains(Description, 'build system')]"); - std::cout << "\nBuild tool: " << build_tool.node().attribute("Filename").value() << "\n"; + std::cout << "\nBuild tool: " << build_tool.node().attribute("Filename").value() << "\n"; //] } + +// vim:et -- cgit v1.2.3