[libxml++] Stop exceptions when using std::cout and UTF-8.



commit 6f91e4489fba79f70c33029d2d9a76fd03fe6276
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Mar 30 18:06:26 2010 +0200

    Stop exceptions when using std::cout and UTF-8.
    
    * examples/*/main.cc: Initialize the global C and C++ locale to prevent
        exceptions when ouputing a ustring (with non-ASCII UTF-8) to std::cout.
        We don't see this problem when writing gtkmm apps because gtk_init() (via
        Gtk::Main) initializes the C locale correctly.
    
        Thanks to Daniel Elstner for the solution (he will document it properly
        in the Glib::ustring API reference) and to Nic Reveles and others for
        noticing the problem.

 ChangeLog                             |   13 +++++++++++++
 examples/dom_build/main.cc            |    4 ++++
 examples/dom_parse_entities/main.cc   |    4 ++++
 examples/dom_parser/example.xml       |    1 +
 examples/dom_parser/main.cc           |    6 +++++-
 examples/dom_parser_raw/main.cc       |    4 ++++
 examples/dom_read_write/main.cc       |    4 ++++
 examples/dom_xpath/main.cc            |    4 ++++
 examples/dtdvalidation/main.cc        |    4 ++++
 examples/import_node/main.cc          |    4 ++++
 examples/sax_exception/main.cc        |    4 ++++
 examples/sax_parser/main.cc           |    4 ++++
 examples/sax_parser_build_dom/main.cc |    4 ++++
 examples/sax_parser_entities/main.cc  |    4 ++++
 examples/schemavalidation/main.cc     |    4 ++++
 examples/textreader/main.cc           |    4 ++++
 16 files changed, 71 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 783ec27..09f091e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2010-03-30  Murray Cumming  <murrayc murrayc com>>
+
+    Stop exceptions when using std::cout and UTF-8.
+
+	* examples/*/main.cc: Initialize the global C and C++ locale to prevent 
+    exceptions when ouputing a ustring (with non-ASCII UTF-8) to std::cout.
+    We don't see this problem when writing gtkmm apps because gtk_init() (via 
+    Gtk::Main) initializes the C locale correctly.
+
+    Thanks to Daniel Elstner for the solution (he will document it properly 
+    in the Glib::ustring API reference) and to Nic Reveles and others for 
+    noticing the problem.
+
 2010-03-30  David King  <davidk openismus com>
 
 	Update pkg-config file
diff --git a/examples/dom_build/main.cc b/examples/dom_build/main.cc
index f29ac41..82cdc0b 100644
--- a/examples/dom_build/main.cc
+++ b/examples/dom_build/main.cc
@@ -31,6 +31,10 @@
 int
 main(int /* argc */, char** /* argv */)
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
   try
   {
diff --git a/examples/dom_parse_entities/main.cc b/examples/dom_parse_entities/main.cc
index a64b6b0..9a326a4 100644
--- a/examples/dom_parse_entities/main.cc
+++ b/examples/dom_parse_entities/main.cc
@@ -61,6 +61,10 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
 
 int main(int argc, char* argv[])
 {
+  // Set the global C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   std::string filepath;
   if(argc > 1 )
     filepath = argv[1]; //Allow the user to specify a different XML file to parse.
diff --git a/examples/dom_parser/example.xml b/examples/dom_parser/example.xml
index 95c9305..4745f79 100644
--- a/examples/dom_parser/example.xml
+++ b/examples/dom_parser/example.xml
@@ -8,5 +8,6 @@
   <examplechild id="2">
     <child_of_child/>
   </examplechild>
+  <examplechildtext>Ð?Ñ?ивеÑ?!</examplechildtext>
 </example>
 
diff --git a/examples/dom_parser/main.cc b/examples/dom_parser/main.cc
index 5eacbf2..160a45b 100644
--- a/examples/dom_parser/main.cc
+++ b/examples/dom_parser/main.cc
@@ -120,12 +120,16 @@ void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
 
 int main(int argc, char* argv[])
 {
+  // Set the global C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   std::string filepath;
   if(argc > 1 )
     filepath = argv[1]; //Allow the user to specify a different XML file to parse.
   else
     filepath = "example.xml";
-  
+ 
   #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
   try
   {
diff --git a/examples/dom_parser_raw/main.cc b/examples/dom_parser_raw/main.cc
index 14e31ee..8353f60 100644
--- a/examples/dom_parser_raw/main.cc
+++ b/examples/dom_parser_raw/main.cc
@@ -60,6 +60,10 @@ std::string read_from_disk(const std::string& filepath)
 
 int main(int argc, char* argv[])
 {
+  // Set the global C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   std::string filepath;
   if(argc > 1 )
     filepath = argv[1]; //Allow the user to specify a different XML file to parse.
diff --git a/examples/dom_read_write/main.cc b/examples/dom_read_write/main.cc
index d765a07..cc298ea 100644
--- a/examples/dom_read_write/main.cc
+++ b/examples/dom_read_write/main.cc
@@ -31,6 +31,10 @@
 int
 main(int argc, char* argv[])
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   //Parse command-line arguments:
   std::string filepath_in, filepath_out;
   if(argc > 1 )
diff --git a/examples/dom_xpath/main.cc b/examples/dom_xpath/main.cc
index d6fae13..e38fa99 100644
--- a/examples/dom_xpath/main.cc
+++ b/examples/dom_xpath/main.cc
@@ -46,6 +46,10 @@ void xpath_test(const xmlpp::Node* node, const Glib::ustring& xpath)
 
 int main(int argc, char* argv[])
 {
+  // Set the global C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   std::string filepath;
   if(argc > 1 )
     filepath = argv[1]; //Allow the user to specify a different XML file to parse.
diff --git a/examples/dtdvalidation/main.cc b/examples/dtdvalidation/main.cc
index 5294f3f..74b3ec1 100644
--- a/examples/dtdvalidation/main.cc
+++ b/examples/dtdvalidation/main.cc
@@ -30,6 +30,10 @@
 
 int main(int argc, char* argv[])
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   std::string dtdfilepath;
   if(argc > 1)
     dtdfilepath = argv[1]; //Allow the user to specify a different dtd file to use.
diff --git a/examples/import_node/main.cc b/examples/import_node/main.cc
index 019338d..8a0b44a 100644
--- a/examples/import_node/main.cc
+++ b/examples/import_node/main.cc
@@ -9,6 +9,10 @@ using namespace std;
 
 int main (int /* argc */, char** /* argv */)
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
   try
   {
diff --git a/examples/sax_exception/main.cc b/examples/sax_exception/main.cc
index 547616f..b316ea7 100644
--- a/examples/sax_exception/main.cc
+++ b/examples/sax_exception/main.cc
@@ -32,6 +32,10 @@
 
 int main(int /* argc */, char** /* argv */)
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   MySaxParser parser;
   
   #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
diff --git a/examples/sax_parser/main.cc b/examples/sax_parser/main.cc
index 194fa0f..cb4ad22 100644
--- a/examples/sax_parser/main.cc
+++ b/examples/sax_parser/main.cc
@@ -31,6 +31,10 @@
 int
 main(int argc, char* argv[])
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   std::string filepath;
   if(argc > 1 )
     filepath = argv[1]; //Allow the user to specify a different XML file to parse.
diff --git a/examples/sax_parser_build_dom/main.cc b/examples/sax_parser_build_dom/main.cc
index f453966..8292cc9 100644
--- a/examples/sax_parser_build_dom/main.cc
+++ b/examples/sax_parser_build_dom/main.cc
@@ -37,6 +37,10 @@
 int
 main(int argc, char* argv[])
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   std::string filepath;
   if(argc > 1 )
     filepath = argv[1]; //Allow the user to specify a different XML file to parse.
diff --git a/examples/sax_parser_entities/main.cc b/examples/sax_parser_entities/main.cc
index 247ac10..e0e8df2 100644
--- a/examples/sax_parser_entities/main.cc
+++ b/examples/sax_parser_entities/main.cc
@@ -31,6 +31,10 @@
 int
 main(int argc, char* argv[])
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   std::string filepath;
   if(argc > 1 )
     filepath = argv[1]; //Allow the user to specify a different XML file to parse.
diff --git a/examples/schemavalidation/main.cc b/examples/schemavalidation/main.cc
index 1f19586..18023b0 100644
--- a/examples/schemavalidation/main.cc
+++ b/examples/schemavalidation/main.cc
@@ -31,6 +31,10 @@
 
 int main(int argc, char* argv[])
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   std::string schemafilepath("example.xsd"),
               docfilepath("example.xml");
 
diff --git a/examples/textreader/main.cc b/examples/textreader/main.cc
index 05fbae0..0016d73 100644
--- a/examples/textreader/main.cc
+++ b/examples/textreader/main.cc
@@ -44,6 +44,10 @@ std::ostream & operator<<(std::ostream & o, indent const & in)
 
 int main(int /* argc */, char** /* argv */)
 {
+  // Set the global C and C++ locale to the user-configured locale,
+  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
+  std::locale::global(std::locale(""));
+
   #ifdef LIBXMLCPP_EXCEPTIONS_ENABLED
   try
   {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]