diff options
author | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-06-24 12:28:17 +0000 |
---|---|---|
committer | arseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640> | 2010-06-24 12:28:17 +0000 |
commit | 3117219e7c343645d7cb3d3c19fbc6a126522695 (patch) | |
tree | 274e9a5508aae06e72602b663f08b1cae720c5cc | |
parent | 8d6177ebd8d7cc54839d80525936aa76746e4d33 (diff) |
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
-rw-r--r-- | docs/manual.qbk | 21 | ||||
-rw-r--r-- | docs/samples/custom_memory_management.cpp | 25 | ||||
-rw-r--r-- | docs/samples/load_stream.cpp | 68 | ||||
-rw-r--r-- | docs/samples/weekly-shift_jis.xml | 78 | ||||
-rw-r--r-- | docs/samples/weekly-utf-16.xml | bin | 0 -> 3186 bytes | |||
-rw-r--r-- | docs/samples/weekly-utf-8.xml | 78 |
6 files changed, 254 insertions, 16 deletions
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<char*>(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 <new>
+
+//[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<char*>(ptr);
+}
+//]
+
+int main()
+{
+//[code_custom_memory_management_call
+ pugi::set_memory_management_functions(custom_allocate, custom_deallocate);
+//]
+
+ pugi::xml_document doc;
+ doc.load("<node/>");
+}
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 <fstream>
+#include <iomanip>
+#include <iostream>
+
+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 @@ +<?xml version="1.0" encoding="Shift_JIS"?>
+<!DOCTYPE �T�� SYSTEM "weekly-shift_jis.dtd">
+<!-- �T��T���v�� -->
+<�T��>
+ <�N���T>
+ <�N�x>1997</�N�x>
+ <���x>1</���x>
+ <�T>1</�T>
+ </�N���T>
+
+ <����>
+ <��>�R�c</��>
+ <��>���Y</��>
+ </����>
+
+ <�Ɩ����X�g>
+ <�Ɩ���>
+ <�Ɩ���>XML�G�f�B�^�[�̍쐬</�Ɩ���>
+ <�Ɩ��R�[�h>X3355-23</�Ɩ��R�[�h>
+ <�H���Ǘ�>
+ <����H��>1600</����H��>
+ <���эH��>320</���эH��>
+ <��������H��>160</��������H��>
+ <�������эH��>24</�������эH��>
+ </�H���Ǘ�>
+ <�\�荀�ڃ��X�g>
+ <�\�荀��>
+ <P>XML�G�f�B�^�[�̊�{�d�l�̍쐬</P>
+ </�\�荀��>
+ </�\�荀�ڃ��X�g>
+ <���{�������X�g>
+ <���{����>
+ <P>XML�G�f�B�^�[�̊�{�d�l�̍쐬</P>
+ </���{����>
+ <���{����>
+ <P>�������А��i�̋@�\����</P>
+ </���{����>
+ </���{�������X�g>
+ <�㒷�ւ̗v���������X�g>
+ <�㒷�ւ̗v������>
+ <P>���ɂȂ�</P>
+ </�㒷�ւ̗v������>
+ </�㒷�ւ̗v���������X�g>
+ <���_��>
+ <P>XML�Ƃ͉����킩��Ȃ��B</P>
+ </���_��>
+ </�Ɩ���>
+
+ <�Ɩ���>
+ <�Ɩ���>�����G���W���̊J��</�Ɩ���>
+ <�Ɩ��R�[�h>S8821-76</�Ɩ��R�[�h>
+ <�H���Ǘ�>
+ <����H��>120</����H��>
+ <���эH��>6</���эH��>
+ <��������H��>32</��������H��>
+ <�������эH��>2</�������эH��>
+ </�H���Ǘ�>
+ <�\�荀�ڃ��X�g>
+ <�\�荀��>
+ <P><A href="http://www.goo.ne.jp">goo</A>�̋@�\�ׂĂ݂�</P>
+ </�\�荀��>
+ </�\�荀�ڃ��X�g>
+ <���{�������X�g>
+ <���{����>
+ <P>�X�ɁA�ǂ����������G���W�������邩��������</P>
+ </���{����>
+ </���{�������X�g>
+ <�㒷�ւ̗v���������X�g>
+ <�㒷�ւ̗v������>
+ <P>�J��������̂͂߂�ǂ��Ȃ̂ŁAYahoo!�����ĉ������B</P>
+ </�㒷�ւ̗v������>
+ </�㒷�ւ̗v���������X�g>
+ <���_��>
+ <P>�����G���W���ŎԂ𑖂点�邱�Ƃ��ł��Ȃ��B�i�v�����j</P>
+ </���_��>
+ </�Ɩ���>
+ </�Ɩ����X�g>
+</�T��>
diff --git a/docs/samples/weekly-utf-16.xml b/docs/samples/weekly-utf-16.xml Binary files differnew file mode 100644 index 0000000..6c8622a --- /dev/null +++ b/docs/samples/weekly-utf-16.xml 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 @@ +<?xml version="1.0"?>
+<!DOCTYPE 週報 SYSTEM "weekly-utf-8.dtd">
+<!-- 週報サンプル -->
+<週報>
+ <年月週>
+ <年度>1997</年度>
+ <月度>1</月度>
+ <週>1</週>
+ </年月週>
+
+ <氏名>
+ <氏>山田</氏>
+ <名>太郎</名>
+ </氏名>
+
+ <業務報告リスト>
+ <業務報告>
+ <業務名>XMLエディターの作成</業務名>
+ <業務コード>X3355-23</業務コード>
+ <工数管理>
+ <見積もり工数>1600</見積もり工数>
+ <実績工数>320</実績工数>
+ <当月見積もり工数>160</当月見積もり工数>
+ <当月実績工数>24</当月実績工数>
+ </工数管理>
+ <予定項目リスト>
+ <予定項目>
+ <P>XMLエディターの基本仕様の作成</P>
+ </予定項目>
+ </予定項目リスト>
+ <実施事項リスト>
+ <実施事項>
+ <P>XMLエディターの基本仕様の作成</P>
+ </実施事項>
+ <実施事項>
+ <P>競合他社製品の機能調査</P>
+ </実施事項>
+ </実施事項リスト>
+ <上長への要請事項リスト>
+ <上長への要請事項>
+ <P>特になし</P>
+ </上長への要請事項>
+ </上長への要請事項リスト>
+ <問題点対策>
+ <P>XMLとは何かわからない。</P>
+ </問題点対策>
+ </業務報告>
+
+ <業務報告>
+ <業務名>検索エンジンの開発</業務名>
+ <業務コード>S8821-76</業務コード>
+ <工数管理>
+ <見積もり工数>120</見積もり工数>
+ <実績工数>6</実績工数>
+ <当月見積もり工数>32</当月見積もり工数>
+ <当月実績工数>2</当月実績工数>
+ </工数管理>
+ <予定項目リスト>
+ <予定項目>
+ <P><A href="http://www.goo.ne.jp">goo</A>の機能を調べてみる</P>
+ </予定項目>
+ </予定項目リスト>
+ <実施事項リスト>
+ <実施事項>
+ <P>更に、どういう検索エンジンがあるか調査する</P>
+ </実施事項>
+ </実施事項リスト>
+ <上長への要請事項リスト>
+ <上長への要請事項>
+ <P>開発をするのはめんどうなので、Yahoo!を買収して下さい。</P>
+ </上長への要請事項>
+ </上長への要請事項リスト>
+ <問題点対策>
+ <P>検索エンジンで車を走らせることができない。(要調査)</P>
+ </問題点対策>
+ </業務報告>
+ </業務報告リスト>
+</週報>
|