summaryrefslogtreecommitdiff
path: root/docs/samples
diff options
context:
space:
mode:
authorarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2010-06-24 12:28:17 +0000
committerarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2010-06-24 12:28:17 +0000
commit3117219e7c343645d7cb3d3c19fbc6a126522695 (patch)
tree274e9a5508aae06e72602b663f08b1cae720c5cc /docs/samples
parent8d6177ebd8d7cc54839d80525936aa76746e4d33 (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
Diffstat (limited to 'docs/samples')
-rw-r--r--docs/samples/custom_memory_management.cpp25
-rw-r--r--docs/samples/load_stream.cpp68
-rw-r--r--docs/samples/weekly-shift_jis.xml78
-rw-r--r--docs/samples/weekly-utf-16.xmlbin0 -> 3186 bytes
-rw-r--r--docs/samples/weekly-utf-8.xml78
5 files changed, 249 insertions, 0 deletions
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
new file mode 100644
index 0000000..6c8622a
--- /dev/null
+++ b/docs/samples/weekly-utf-16.xml
Binary files 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 @@
+<?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>
+ </問題点対策>
+ </業務報告>
+ </業務報告リスト>
+</週報>