[libxml++] Example programs: Fix return codes and print errors on std::cerr.
- From: Kjell Ahlstedt <kjellahl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libxml++] Example programs: Fix return codes and print errors on std::cerr.
- Date: Wed, 20 Jun 2012 09:02:44 +0000 (UTC)
commit 949e0ecb01c79015c1d9daf7f01eadb3cd5cb146
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date: Tue Jun 19 13:10:42 2012 +0200
Example programs: Fix return codes and print errors on std::cerr.
* examples/*/main.cc: Return EXIT_FAILURE in case of failure. Print error
messages on std::cerr. The example programs can then be run by 'make check'.
Bug #678390.
ChangeLog | 8 +++++
examples/dom_build/main.cc | 6 +++-
examples/dom_parse_entities/main.cc | 8 +++--
examples/dom_parser/main.cc | 9 +++---
examples/dom_parser_raw/main.cc | 7 +++--
examples/dom_read_write/main.cc | 8 +++---
examples/dom_xpath/main.cc | 36 +++++++++++++++---------
examples/dtdvalidation/main.cc | 15 +++++++---
examples/import_node/main.cc | 4 +-
examples/sax_exception/main.cc | 12 +++++++-
examples/sax_parser/main.cc | 8 +++--
examples/sax_parser_build_dom/main.cc | 6 +++-
examples/sax_parser_entities/main.cc | 6 +++-
examples/schemavalidation/main.cc | 49 +++++++++++++++++---------------
examples/textreader/main.cc | 5 +++-
15 files changed, 117 insertions(+), 70 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 4279a7d..2a60fa5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2012-06-19 Kjell Ahlstedt <kjell ahlstedt bredband net>
+
+ Example programs: Fix return codes and print errors on std::cerr.
+
+ * examples/*/main.cc: Return EXIT_FAILURE in case of failure. Print error
+ messages on std::cerr. The example programs can then be run by 'make check'.
+ Bug #678390.
+
2012-04-20 Kjell Ahlstedt <kjell ahlstedt bredband net>
Node: Add functions eval_to_[boolean|number|string]().
diff --git a/examples/dom_build/main.cc b/examples/dom_build/main.cc
index 367620e..63f9ec7 100644
--- a/examples/dom_build/main.cc
+++ b/examples/dom_build/main.cc
@@ -25,6 +25,7 @@
#include <libxml++/libxml++.h>
#include <iostream>
+#include <stdlib.h>
int
main(int /* argc */, char** /* argv */)
@@ -72,9 +73,10 @@ main(int /* argc */, char** /* argv */)
}
catch(const std::exception& ex)
{
- std::cout << "Exception caught: " << ex.what() << std::endl;
+ std::cerr << "Exception caught: " << ex.what() << std::endl;
+ return EXIT_FAILURE;
}
- return 0;
+ return EXIT_SUCCESS;
}
diff --git a/examples/dom_parse_entities/main.cc b/examples/dom_parse_entities/main.cc
index 9f67a59..1525f51 100644
--- a/examples/dom_parse_entities/main.cc
+++ b/examples/dom_parse_entities/main.cc
@@ -24,8 +24,8 @@
#endif
#include <libxml++/libxml++.h>
-
#include <iostream>
+#include <stdlib.h>
void print_node(const xmlpp::Node* node, bool substitute_entities, unsigned int indentation = 0)
{
@@ -78,6 +78,7 @@ int main(int argc, char* argv[])
filepath = "example.xml";
// Parse first without, then with, entity substitution.
+ int return_code = EXIT_SUCCESS;
bool substitute_entities = false;
while (true)
{
@@ -101,7 +102,8 @@ int main(int argc, char* argv[])
}
catch(const std::exception& ex)
{
- std::cout << "Exception caught: " << ex.what() << std::endl;
+ std::cerr << "Exception caught: " << ex.what() << std::endl;
+ return_code = EXIT_FAILURE;
}
if (substitute_entities) break;
@@ -109,6 +111,6 @@ int main(int argc, char* argv[])
substitute_entities = true;
}
- return 0;
+ return return_code;
}
diff --git a/examples/dom_parser/main.cc b/examples/dom_parser/main.cc
index 54785f3..40cb9b6 100644
--- a/examples/dom_parser/main.cc
+++ b/examples/dom_parser/main.cc
@@ -24,8 +24,8 @@
#endif
#include <libxml++/libxml++.h>
-
#include <iostream>
+#include <stdlib.h>
void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
{
@@ -142,7 +142,7 @@ int main(int argc, char* argv[])
<< " -t Throw messages in an exception" << std::endl
<< " -e Write messages to stderr" << std::endl
<< " -E Do not substitute entities" << std::endl;
- return 1;
+ return EXIT_FAILURE;
}
argi++;
}
@@ -171,9 +171,10 @@ int main(int argc, char* argv[])
}
catch(const std::exception& ex)
{
- std::cout << "Exception caught: " << ex.what() << std::endl;
+ std::cerr << "Exception caught: " << ex.what() << std::endl;
+ return EXIT_FAILURE;
}
- return 0;
+ return EXIT_SUCCESS;
}
diff --git a/examples/dom_parser_raw/main.cc b/examples/dom_parser_raw/main.cc
index 0db852f..77d783b 100644
--- a/examples/dom_parser_raw/main.cc
+++ b/examples/dom_parser_raw/main.cc
@@ -24,7 +24,7 @@
#include <iostream>
#include <fstream>
#include <glibmm/convert.h>
-
+#include <stdlib.h>
void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
{
@@ -109,9 +109,10 @@ int main(int argc, char* argv[])
}
catch(const std::exception& ex)
{
- std::cout << "Exception caught: " << ex.what() << std::endl;
+ std::cerr << "Exception caught: " << ex.what() << std::endl;
+ return EXIT_FAILURE;
}
- return 0;
+ return EXIT_SUCCESS;
}
diff --git a/examples/dom_read_write/main.cc b/examples/dom_read_write/main.cc
index 0ac5628..bf7f3fe 100644
--- a/examples/dom_read_write/main.cc
+++ b/examples/dom_read_write/main.cc
@@ -24,9 +24,8 @@
#endif
#include <libxml++/libxml++.h>
-
#include <iostream>
-
+#include <stdlib.h>
int
main(int argc, char* argv[])
@@ -62,9 +61,10 @@ main(int argc, char* argv[])
}
catch(const std::exception& ex)
{
- std::cout << "Exception caught: " << ex.what() << std::endl;
+ std::cerr << "Exception caught: " << ex.what() << std::endl;
+ return EXIT_FAILURE;
}
- return 0;
+ return EXIT_SUCCESS;
}
diff --git a/examples/dom_xpath/main.cc b/examples/dom_xpath/main.cc
index e713a2b..d2c2d8e 100644
--- a/examples/dom_xpath/main.cc
+++ b/examples/dom_xpath/main.cc
@@ -42,8 +42,9 @@ Glib::ustring result_type_to_ustring(xmlpp::XPathResultType result_type)
}
}
-void xpath_test(const xmlpp::Node* node, const Glib::ustring& xpath)
+bool xpath_test(const xmlpp::Node* node, const Glib::ustring& xpath)
{
+ bool result = true;
std::cout << std::endl; //Separate tests by an empty line.
std::cout << "searching with xpath '" << xpath << "' in root node: " << std::endl;
@@ -82,7 +83,8 @@ void xpath_test(const xmlpp::Node* node, const Glib::ustring& xpath)
}
catch (const xmlpp::exception& ex)
{
- std::cout << "Exception caught from find: " << ex.what() << std::endl;
+ std::cerr << "Exception caught from find: " << ex.what() << std::endl;
+ result = false;
}
try
@@ -95,8 +97,10 @@ void xpath_test(const xmlpp::Node* node, const Glib::ustring& xpath)
}
catch (const xmlpp::exception& ex)
{
- std::cout << "Exception caught from eval: " << ex.what() << std::endl;
+ std::cerr << "Exception caught from eval: " << ex.what() << std::endl;
+ result = false;
}
+ return result;
}
int main(int argc, char* argv[])
@@ -111,6 +115,7 @@ int main(int argc, char* argv[])
else
filepath = "example.xml";
+ bool result = true;
try
{
xmlpp::DomParser parser(filepath);
@@ -121,21 +126,23 @@ int main(int argc, char* argv[])
if(root)
{
// Find all sections, no matter where:
- xpath_test(root, "//section");
+ result &= xpath_test(root, "//section");
// Find the title node (if there is one):
- xpath_test(root, "title");
+ result &= xpath_test(root, "title");
// Find all literal text, in any paragraph:
- xpath_test(root, "//para/literal");
+ result &= xpath_test(root, "//para/literal");
// Evaluate some XPath expressions with result types other than nodeset:
- xpath_test(root, "boolean(//para/literal)");
- xpath_test(root, "number(//para/literal)+2");
- xpath_test(root, "concat(string(title),\" !\")");
+ // These tests shall fail.
+ std::cerr << "Expecting 3 exceptions" << std::endl;
+ result &= !xpath_test(root, "boolean(//para/literal)");
+ result &= !xpath_test(root, "number(//para/literal)+2");
+ result &= !xpath_test(root, "concat(string(title),\" !\")");
// Don't find anything:
- xpath_test(root, "/wont_find");
+ result &= xpath_test(root, "/wont_find");
std::cout << std::endl;
@@ -148,15 +155,16 @@ int main(int argc, char* argv[])
std::cout << "searching for unresolved internal references "
<< "(see docbook manual):" << std::endl;
- xpath_test(root, "//@id");
- xpath_test(root, "//xref/@linkend");
+ result &= xpath_test(root, "//@id");
+ result &= xpath_test(root, "//xref/@linkend");
}
}
}
catch(const std::exception& ex)
{
- std::cout << "Exception caught: " << ex.what() << std::endl;
+ std::cerr << "Exception caught: " << ex.what() << std::endl;
+ result = false;
}
- return EXIT_SUCCESS;
+ return result ? EXIT_SUCCESS : EXIT_FAILURE;
}
diff --git a/examples/dtdvalidation/main.cc b/examples/dtdvalidation/main.cc
index 84b0511..b235b14 100644
--- a/examples/dtdvalidation/main.cc
+++ b/examples/dtdvalidation/main.cc
@@ -25,8 +25,8 @@
#endif
#include <libxml++/libxml++.h>
-
#include <iostream>
+#include <stdlib.h>
int main(int argc, char* argv[])
{
@@ -40,6 +40,7 @@ int main(int argc, char* argv[])
else
dtdfilepath = "example.dtd";
+ int return_code = EXIT_SUCCESS;
xmlpp::Document document;
/* xmlpp::Element* nodeRoot = */document.create_root_node("incorrect");
@@ -49,12 +50,13 @@ int main(int argc, char* argv[])
try
{
- validator.validate( &document );
- std::cout << "Validation successful" << std::endl;
+ validator.validate( &document ); // Shall fail
+ std::cerr << "Validation successful (not expected)" << std::endl;
+ return_code = EXIT_FAILURE;
}
catch( const xmlpp::validity_error& )
{
- std::cout << "Error validating the document" << std::endl;
+ std::cout << "Error validating the document (expected)" << std::endl;
}
/* xmlpp::Element* nodeRoot2 = */document.create_root_node("example");
@@ -70,12 +72,15 @@ int main(int argc, char* argv[])
}
catch( const xmlpp::validity_error& )
{
- std::cout << "Error validating the document" << std::endl;
+ std::cerr << "Error validating the document" << std::endl;
+ return_code = EXIT_FAILURE;
}
}
catch( const xmlpp::parse_error& )
{
std::cerr << "Error parsing the dtd" << std::endl;
+ return_code = EXIT_FAILURE;
}
+ return return_code;
}
diff --git a/examples/import_node/main.cc b/examples/import_node/main.cc
index d6d5f30..8c210d4 100644
--- a/examples/import_node/main.cc
+++ b/examples/import_node/main.cc
@@ -43,7 +43,7 @@ int main (int /* argc */, char** /* argv */)
Element* first_child2 = dynamic_cast<Element*>(child_list2.front());
if (!first_child2)
{
- cout << "first_child2 == 0" << endl;
+ cerr << "first_child2 == 0" << endl;
return EXIT_FAILURE;
}
TextNode* text_to_add = first_child2->get_child_text();
@@ -56,11 +56,11 @@ int main (int /* argc */, char** /* argv */)
// print out the new doc1
string doc1_string = doc1->write_to_string_formatted();
cout << doc1_string;
- return EXIT_SUCCESS;
}
catch (std::exception& ex)
{
cerr << "Caught exception " << ex.what() << endl;
return EXIT_FAILURE;
}
+ return EXIT_SUCCESS;
}
diff --git a/examples/sax_exception/main.cc b/examples/sax_exception/main.cc
index e64610a..f5b2c57 100644
--- a/examples/sax_exception/main.cc
+++ b/examples/sax_exception/main.cc
@@ -29,6 +29,7 @@
#include "myparser.h"
#include <iostream>
+#include <stdlib.h>
int main(int /* argc */, char** /* argv */)
{
@@ -42,11 +43,18 @@ int main(int /* argc */, char** /* argv */)
{
parser.parse_file("example.xml");
}
+ catch(const MyException& ex)
+ {
+ std::cout << "Exception caught (expected): " << ex.what() << std::endl;
+ return EXIT_SUCCESS;
+ }
catch(const std::exception& ex)
{
- std::cout << "Exception caught: " << ex.what() << std::endl;
+ std::cerr << "Exception caught: " << ex.what() << std::endl;
+ return EXIT_FAILURE;
}
- return 0;
+ std::cerr << "No exception caught" << std::endl;
+ return EXIT_FAILURE;
}
diff --git a/examples/sax_parser/main.cc b/examples/sax_parser/main.cc
index bf1c088..ffe82e6 100644
--- a/examples/sax_parser/main.cc
+++ b/examples/sax_parser/main.cc
@@ -25,6 +25,7 @@
#include <fstream>
#include <iostream>
+#include <stdlib.h>
#include "myparser.h"
@@ -42,6 +43,7 @@ main(int argc, char* argv[])
filepath = "example.xml";
// Parse the entire document in one go:
+ int return_code = EXIT_SUCCESS;
try
{
MySaxParser parser;
@@ -50,7 +52,8 @@ main(int argc, char* argv[])
}
catch(const xmlpp::exception& ex)
{
- std::cout << "libxml++ exception: " << ex.what() << std::endl;
+ std::cerr << "libxml++ exception: " << ex.what() << std::endl;
+ return_code = EXIT_FAILURE;
}
@@ -84,7 +87,6 @@ main(int argc, char* argv[])
*/
}
-
- return 0;
+ return return_code;
}
diff --git a/examples/sax_parser_build_dom/main.cc b/examples/sax_parser_build_dom/main.cc
index c2f019d..a033c87 100644
--- a/examples/sax_parser_build_dom/main.cc
+++ b/examples/sax_parser_build_dom/main.cc
@@ -27,6 +27,7 @@
#include <fstream>
#include <iostream>
+#include <stdlib.h>
#include "svgparser.h"
#include "svgdocument.h"
@@ -70,9 +71,10 @@ main(int argc, char* argv[])
}
catch(const xmlpp::exception& ex)
{
- std::cout << "libxml++ exception: " << ex.what() << std::endl;
+ std::cerr << "libxml++ exception: " << ex.what() << std::endl;
+ return EXIT_FAILURE;
}
- return 0;
+ return EXIT_SUCCESS;
}
diff --git a/examples/sax_parser_entities/main.cc b/examples/sax_parser_entities/main.cc
index 7075fce..1466c8d 100644
--- a/examples/sax_parser_entities/main.cc
+++ b/examples/sax_parser_entities/main.cc
@@ -25,6 +25,7 @@
#include <fstream>
#include <iostream>
+#include <stdlib.h>
#include "myparser.h"
@@ -49,9 +50,10 @@ main(int argc, char* argv[])
}
catch(const xmlpp::exception& ex)
{
- std::cout << "libxml++ exception: " << ex.what() << std::endl;
+ std::cerr << "libxml++ exception: " << ex.what() << std::endl;
+ return EXIT_FAILURE;
}
- return 0;
+ return EXIT_SUCCESS;
}
diff --git a/examples/schemavalidation/main.cc b/examples/schemavalidation/main.cc
index 2206660..1c5d38a 100644
--- a/examples/schemavalidation/main.cc
+++ b/examples/schemavalidation/main.cc
@@ -25,9 +25,8 @@
#endif
#include <libxml++/libxml++.h>
-
#include <iostream>
-
+#include <stdlib.h>
int main(int argc, char* argv[])
{
@@ -39,35 +38,39 @@ int main(int argc, char* argv[])
docfilepath("example.xml");
if(argc!=1 && argc!=3)
+ {
std::cout << "usage : " << argv[0] << " [document schema]" << std::endl;
- else
+ return EXIT_FAILURE;
+ }
+
+ if(argc == 3)
{
- if(argc == 3)
- {
- docfilepath = argv[1];
- schemafilepath = argv[2];
- }
+ docfilepath = argv[1];
+ schemafilepath = argv[2];
+ }
+
+ try
+ {
+ xmlpp::DomParser parser(docfilepath);
+ xmlpp::SchemaValidator validator(schemafilepath);
try
{
- xmlpp::DomParser parser(docfilepath);
- xmlpp::SchemaValidator validator(schemafilepath);
-
- try
- {
- validator.validate( parser.get_document() );
- std::cout << "Valid document" << std::endl;
- }
- catch( const xmlpp::validity_error& error)
- {
- std::cout << "Error validating the document" << std::endl;
- std::cout << error.what();
- }
+ validator.validate( parser.get_document() );
+ std::cout << "Valid document" << std::endl;
}
- catch( const xmlpp::parse_error& )
+ catch( const xmlpp::validity_error& error)
{
- std::cerr << "Error parsing the schema" << std::endl;
+ std::cerr << "Error validating the document" << std::endl;
+ std::cerr << error.what();
+ return EXIT_FAILURE;
}
}
+ catch( const xmlpp::parse_error& error)
+ {
+ std::cerr << "Error parsing the schema: " << error.what() << std::endl;
+ return EXIT_FAILURE;
+ }
+ return EXIT_SUCCESS;
}
diff --git a/examples/textreader/main.cc b/examples/textreader/main.cc
index a6957e7..5b97f32 100644
--- a/examples/textreader/main.cc
+++ b/examples/textreader/main.cc
@@ -27,6 +27,7 @@
#include <libxml++/parsers/textreader.h>
#include <iostream>
+#include <stdlib.h>
struct indent {
int depth_;
@@ -83,7 +84,9 @@ int main(int /* argc */, char** /* argv */)
}
catch(const std::exception& e)
{
- std::cout << "Exception caught: " << e.what() << std::endl;
+ std::cerr << "Exception caught: " << e.what() << std::endl;
+ return EXIT_FAILURE;
}
+ return EXIT_SUCCESS;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]