annotate dep/pugixml/docs/samples/load_stream.cpp @ 198:bc1ae1810855

dep/animia: switch from using classes to global functions the old idea was ok, but sort of hackish; this method doesn't use classes at all, and this way (especially important!) we can do wayland stuff AND x11 at the same time, which wasn't really possible without stupid workarounds in the other method
author Paper <mrpapersonic@gmail.com>
date Sun, 24 Dec 2023 02:59:42 -0500
parents d10b6c6b432e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #include "pugixml.hpp"
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 #include <fstream>
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <iomanip>
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include <iostream>
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 void print_doc(const char* message, const pugi::xml_document& doc, const pugi::xml_parse_result& result)
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 std::cout
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 << message
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 << "\t: load result '" << result.description() << "'"
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 << ", first character of root name: U+" << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << pugi::as_wide(doc.first_child().name())[0]
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 << ", year: " << doc.first_child().first_child().first_child().child_value()
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 << std::endl;
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 bool try_imbue(std::wistream& stream, const char* name)
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 try
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 stream.imbue(std::locale(name));
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 return true;
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 catch (const std::exception&)
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 return false;
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 int main()
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 pugi::xml_document doc;
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 // tag::code[]
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 std::ifstream stream("weekly-utf-8.xml");
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 pugi::xml_parse_result result = doc.load(stream);
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 // end::code[]
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41 // first character of root name: U+9031, year: 1997
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 print_doc("UTF8 file from narrow stream", doc, result);
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 std::ifstream stream("weekly-utf-16.xml");
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 pugi::xml_parse_result result = doc.load(stream);
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 // first character of root name: U+9031, year: 1997
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 print_doc("UTF16 file from narrow stream", doc, result);
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-8 file from a wide stream
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 // directly if you have localized characters; you'll have to provide a UTF8 locale (there is no
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 // standard one; you can use utf8_codecvt_facet from Boost or codecvt_utf8 from C++0x)
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 std::wifstream stream("weekly-utf-8.xml");
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 if (try_imbue(stream, "en_US.UTF-8")) // try Linux encoding
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 pugi::xml_parse_result result = doc.load(stream);
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 // first character of root name: U+00E9, year: 1997
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 print_doc("UTF8 file from wide stream", doc, result);
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 else
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 std::cout << "UTF-8 locale is not available\n";
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-16 file from a wide stream without
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74 // using custom codecvt; you can use codecvt_utf16 from C++0x
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 // Since encoding names are non-standard, you can't load the Shift-JIS (or any other non-ASCII) file
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 // from a wide stream portably
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80 std::wifstream stream("weekly-shift_jis.xml");
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
81
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
82 if (try_imbue(stream, ".932") || // try Microsoft encoding
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83 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
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 pugi::xml_parse_result result = doc.load(stream);
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 // first character of root name: U+9031, year: 1997
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 print_doc("Shift-JIS file from wide stream", doc, result);
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 else
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 {
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 std::cout << "Shift-JIS locale is not available\n";
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
95 }
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
96
d10b6c6b432e add xml lib, we will need to use it eventually
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
97 // vim:et