From 3117219e7c343645d7cb3d3c19fbc6a126522695 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Thu, 24 Jun 2010 12:28:17 +0000 Subject: docs: Extracted sample code in a separate file, added stream loading sample prototype git-svn-id: http://pugixml.googlecode.com/svn/trunk@534 99668b35-9821-0410-8761-19e4c4f06640 --- docs/manual.qbk | 21 ++------ docs/samples/custom_memory_management.cpp | 25 ++++++++++ docs/samples/load_stream.cpp | 68 ++++++++++++++++++++++++++ docs/samples/weekly-shift_jis.xml | 78 ++++++++++++++++++++++++++++++ docs/samples/weekly-utf-16.xml | Bin 0 -> 3186 bytes docs/samples/weekly-utf-8.xml | 78 ++++++++++++++++++++++++++++++ 6 files changed, 254 insertions(+), 16 deletions(-) create mode 100644 docs/samples/custom_memory_management.cpp create mode 100644 docs/samples/load_stream.cpp create mode 100644 docs/samples/weekly-shift_jis.xml create mode 100644 docs/samples/weekly-utf-16.xml create mode 100644 docs/samples/weekly-utf-8.xml diff --git a/docs/manual.qbk b/docs/manual.qbk index 8b3bea6..5668906 100644 --- a/docs/manual.qbk +++ b/docs/manual.qbk @@ -375,22 +375,11 @@ You can use the following accessor functions to change or get current memory man Allocation function is called with the size (in bytes) as an argument and should return a pointer to memory block with alignment that is suitable for pointer storage and size that is greater or equal to the requested one. If the allocation fails, the function has to return null pointer (throwing an exception from allocation function results in undefined behavior). Deallocation function is called with the pointer that was returned by the previous call or with a null pointer; null pointer deallocation should be handled as a no-op. If memory management functions are not thread-safe, library thread safety is not guaranteed. -This is a simple example of custom memory management: - - void* custom_allocate(size_t size) - { - return new (std::nothrow) char[size]; - } - - void custom_deallocate(void* ptr) - { - delete[] static_cast(ptr); - } - - int main() - { - pugi::set_memory_management_functions(custom_allocate, custom_deallocate); - } +This is a simple example of custom memory management ([@samples/custom_memory_management.cpp]) + +[import samples/custom_memory_management.cpp] +[code_custom_memory_management_decl] +[code_custom_memory_management_call] When setting new memory management functions, care must be taken to make sure that there are no live pugixml objects. Otherwise when the objects are destroyed, the new deallocation function will be called with the memory obtained by the old allocation function, resulting in undefined behavior. diff --git a/docs/samples/custom_memory_management.cpp b/docs/samples/custom_memory_management.cpp new file mode 100644 index 0000000..01a2acf --- /dev/null +++ b/docs/samples/custom_memory_management.cpp @@ -0,0 +1,25 @@ +#include "pugixml.hpp" + +#include + +//[code_custom_memory_management_decl +void* custom_allocate(size_t size) +{ + return new (std::nothrow) char[size]; +} + +void custom_deallocate(void* ptr) +{ + delete[] static_cast(ptr); +} +//] + +int main() +{ +//[code_custom_memory_management_call + pugi::set_memory_management_functions(custom_allocate, custom_deallocate); +//] + + pugi::xml_document doc; + doc.load(""); +} diff --git a/docs/samples/load_stream.cpp b/docs/samples/load_stream.cpp new file mode 100644 index 0000000..4aba8bd --- /dev/null +++ b/docs/samples/load_stream.cpp @@ -0,0 +1,68 @@ +#include "pugixml.hpp" + +#include +#include +#include + +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; +} + +int main() +{ + pugi::xml_document doc; + + { + 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 (WHAT?)) + std::wifstream stream("weekly-utf-8.xml"); + 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); + } + + { + // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-16 file from a wide stream + // at all; you'll have to provide a UTF16 locale (WHAT??) + std::wifstream stream("weekly-utf-16.xml"); + pugi::xml_parse_result result = doc.load(stream); + + // first character of root name: U+0000, year: + print_doc("UTF16 file from wide stream", doc, result); + } + + { + // Since encoding names are non-standard, you can't load the Shift-JIS (or any other non-ASCII) file + // from a wide stream portably; this code assumes Microsoft C Runtime Libraries (WHAT???) + std::wifstream stream("weekly-shift_jis.xml"); + stream.imbue(std::locale(".932")); + 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); + } +} diff --git a/docs/samples/weekly-shift_jis.xml b/docs/samples/weekly-shift_jis.xml new file mode 100644 index 0000000..7421455 --- /dev/null +++ b/docs/samples/weekly-shift_jis.xml @@ -0,0 +1,78 @@ + + + +<週報> + <年月週> + <年度>1997 + <月度>1 + <週>1 + + + <氏名> + <氏>山田 + <名>太郎 + + + <業務報告リスト> + <業務報告> + <業務名>XMLエディターの作成 + <業務コード>X3355-23 + <工数管理> + <見積もり工数>1600 + <実績工数>320 + <当月見積もり工数>160 + <当月実績工数>24 + + <予定項目リスト> + <予定項目> +

XMLエディターの基本仕様の作成

+ + + <実施事項リスト> + <実施事項> +

XMLエディターの基本仕様の作成

+ + <実施事項> +

競合他社製品の機能調査

+ + + <上長への要請事項リスト> + <上長への要請事項> +

特になし

+ + + <問題点対策> +

XMLとは何かわからない。

+ + + + <業務報告> + <業務名>検索エンジンの開発 + <業務コード>S8821-76 + <工数管理> + <見積もり工数>120 + <実績工数>6 + <当月見積もり工数>32 + <当月実績工数>2 + + <予定項目リスト> + <予定項目> +

gooの機能を調べてみる

+ + + <実施事項リスト> + <実施事項> +

更に、どういう検索エンジンがあるか調査する

+ + + <上長への要請事項リスト> + <上長への要請事項> +

開発をするのはめんどうなので、Yahoo!を買収して下さい。

+ + + <問題点対策> +

検索エンジンで車を走らせることができない。(要調査)

+ + + + diff --git a/docs/samples/weekly-utf-16.xml b/docs/samples/weekly-utf-16.xml new file mode 100644 index 0000000..6c8622a Binary files /dev/null and b/docs/samples/weekly-utf-16.xml differ diff --git a/docs/samples/weekly-utf-8.xml b/docs/samples/weekly-utf-8.xml new file mode 100644 index 0000000..497f572 --- /dev/null +++ b/docs/samples/weekly-utf-8.xml @@ -0,0 +1,78 @@ + + + +<騾ア蝣ア> + <蟷エ譛磯ア> + <蟷エ蠎ヲ>1997 + <譛亥コヲ>1 + <騾ア>1 + + + <豌丞錐> + <豌>螻ア逕ー + <蜷>螟ェ驛 + + + <讌ュ蜍吝ア蜻翫Μ繧ケ繝> + <讌ュ蜍吝ア蜻> + <讌ュ蜍吝錐>XML繧ィ繝繧」繧ソ繝シ縺ョ菴懈 + <讌ュ蜍吶さ繝シ繝>X3355-23 + <蟾・謨ー邂。逅> + <隕狗ゥ阪b繧雁キ・謨ー>1600 + <螳溽クセ蟾・謨ー>320 + <蠖捺怦隕狗ゥ阪b繧雁キ・謨ー>160 + <蠖捺怦螳溽クセ蟾・謨ー>24 + + <莠亥ョ夐逶ョ繝ェ繧ケ繝> + <莠亥ョ夐逶ョ> +

XML繧ィ繝繧」繧ソ繝シ縺ョ蝓コ譛ャ莉墓ァ倥ョ菴懈

+ + + <螳滓命莠矩繝ェ繧ケ繝> + <螳滓命莠矩> +

XML繧ィ繝繧」繧ソ繝シ縺ョ蝓コ譛ャ莉墓ァ倥ョ菴懈

+ + <螳滓命莠矩> +

遶カ蜷井サ也、セ陬ス蜩√ョ讖溯ス隱ソ譟サ

+ + + <荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝> + <荳企聞縺ク縺ョ隕∬ォ倶コ矩> +

迚ケ縺ォ縺ェ縺

+ + + <蝠城。檎せ蟇セ遲> +

XML縺ィ縺ッ菴輔°繧上°繧峨↑縺縲

+ + + + <讌ュ蜍吝ア蜻> + <讌ュ蜍吝錐>讀懃エ「繧ィ繝ウ繧ク繝ウ縺ョ髢狗匱 + <讌ュ蜍吶さ繝シ繝>S8821-76 + <蟾・謨ー邂。逅> + <隕狗ゥ阪b繧雁キ・謨ー>120 + <螳溽クセ蟾・謨ー>6 + <蠖捺怦隕狗ゥ阪b繧雁キ・謨ー>32 + <蠖捺怦螳溽クセ蟾・謨ー>2 + + <莠亥ョ夐逶ョ繝ェ繧ケ繝> + <莠亥ョ夐逶ョ> +

goo縺ョ讖溯ス繧定ェソ縺ケ縺ヲ縺ソ繧

+ + + <螳滓命莠矩繝ェ繧ケ繝> + <螳滓命莠矩> +

譖エ縺ォ縲√←縺縺縺讀懃エ「繧ィ繝ウ繧ク繝ウ縺後≠繧九°隱ソ譟サ縺吶k

+ + + <荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝> + <荳企聞縺ク縺ョ隕∬ォ倶コ矩> +

髢狗匱繧偵☆繧九ョ縺ッ繧√s縺ゥ縺縺ェ縺ョ縺ァ縲〆ahoo!繧定イキ蜿弱@縺ヲ荳九&縺縲

+ + + <蝠城。檎せ蟇セ遲> +

讀懃エ「繧ィ繝ウ繧ク繝ウ縺ァ霆翫r襍ー繧峨○繧九%縺ィ縺後〒縺阪↑縺縲ゑシ郁ヲ∬ェソ譟サシ

+ + + + -- cgit v1.2.3