source: icGREP/icgrep-devel/icgrep/combine/pugixml/docs/samples/load_stream.cpp @ 5516

Last change on this file since 5516 was 5516, checked in by faldebey, 21 months ago

added combinatorial testing tool

File size: 3.0 KB
Line 
1#include "pugixml.hpp"
2
3#include <fstream>
4#include <iomanip>
5#include <iostream>
6
7void print_doc(const char* message, const pugi::xml_document& doc, const pugi::xml_parse_result& result)
8{
9    std::cout
10        << message
11        << "\t: load result '" << result.description() << "'"
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]
13        << ", year: " << doc.first_child().first_child().first_child().child_value()
14        << std::endl;
15}
16
17bool try_imbue(std::wistream& stream, const char* name)
18{
19    try
20    {
21        stream.imbue(std::locale(name));
22
23        return true;
24    }
25    catch (const std::exception&)
26    {
27        return false;
28    }
29}
30
31int main()
32{
33    pugi::xml_document doc;
34
35    {
36    // tag::code[]
37        std::ifstream stream("weekly-utf-8.xml");
38        pugi::xml_parse_result result = doc.load(stream);
39    // end::code[]
40
41        // first character of root name: U+9031, year: 1997
42        print_doc("UTF8 file from narrow stream", doc, result);
43    }
44
45    {
46        std::ifstream stream("weekly-utf-16.xml");
47        pugi::xml_parse_result result = doc.load(stream);
48
49        // first character of root name: U+9031, year: 1997
50        print_doc("UTF16 file from narrow stream", doc, result);
51    }
52
53    {
54        // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-8 file from a wide stream
55        // directly if you have localized characters; you'll have to provide a UTF8 locale (there is no
56        // standard one; you can use utf8_codecvt_facet from Boost or codecvt_utf8 from C++0x)
57        std::wifstream stream("weekly-utf-8.xml");
58
59        if (try_imbue(stream, "en_US.UTF-8")) // try Linux encoding
60        {
61            pugi::xml_parse_result result = doc.load(stream);
62
63            // first character of root name: U+00E9, year: 1997
64            print_doc("UTF8 file from wide stream", doc, result);
65        }
66        else
67        {
68            std::cout << "UTF-8 locale is not available\n";
69        }
70    }
71
72    {
73        // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-16 file from a wide stream without
74        // using custom codecvt; you can use codecvt_utf16 from C++0x
75    }
76
77    {
78        // Since encoding names are non-standard, you can't load the Shift-JIS (or any other non-ASCII) file
79        // from a wide stream portably
80        std::wifstream stream("weekly-shift_jis.xml");
81
82        if (try_imbue(stream, ".932") || // try Microsoft encoding
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
84        {
85            pugi::xml_parse_result result = doc.load(stream);
86
87            // first character of root name: U+9031, year: 1997
88            print_doc("Shift-JIS file from wide stream", doc, result);
89        }
90        else
91        {
92            std::cout << "Shift-JIS locale is not available\n";
93        }
94    }
95}
96
97// vim:et
Note: See TracBrowser for help on using the repository browser.