stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject svn commit: r654286 - in /stdcxx/branches/4.2.x/examples/manual: failure.cpp in/failure.in out/failure.out
Date Wed, 07 May 2008 21:24:24 GMT
Author: sebor
Date: Wed May  7 14:24:23 2008
New Revision: 654286

URL: http://svn.apache.org/viewvc?rev=654286&view=rev
Log:
2008-05-07  Martin Sebor  <sebor@roguewave.com>

	* examples/manual/failure.cpp (main): Demonstrated the (optional)
	extensions, classes std::ios::badbit_set, std::ios::eofbit_set,
	and std::ios::failbit_set.
	* examples/manual/in/failure.in: Added "interesting" input file.
	* examples/manual/in/failure.in: Set expected output.

Added:
    stdcxx/branches/4.2.x/examples/manual/in/failure.in   (with props)
Modified:
    stdcxx/branches/4.2.x/examples/manual/failure.cpp
    stdcxx/branches/4.2.x/examples/manual/out/failure.out

Modified: stdcxx/branches/4.2.x/examples/manual/failure.cpp
URL: http://svn.apache.org/viewvc/stdcxx/branches/4.2.x/examples/manual/failure.cpp?rev=654286&r1=654285&r2=654286&view=diff
==============================================================================
--- stdcxx/branches/4.2.x/examples/manual/failure.cpp (original)
+++ stdcxx/branches/4.2.x/examples/manual/failure.cpp Wed May  7 14:24:23 2008
@@ -1,6 +1,7 @@
 /**************************************************************************
  *
- * failure.cpp - Example program demonstrating ios::failure.
+ * failure.cpp - Example program demonstrating the use of ios::failure
+ *               and its extendsions provided by this implementation.
  *
  * $Id$
  *
@@ -22,7 +23,7 @@
  * implied.   See  the License  for  the  specific language  governing
  * permissions and limitations under the License.
  *
- * Copyright 1994-2006 Rogue Wave Software.
+ * Copyright 1994-2008 Rogue Wave Software, Inc.
  * 
  **************************************************************************/
 
@@ -31,39 +32,78 @@
 
 #include <examples.h>
 
+
 #ifndef _RWSTD_NO_EXCEPTIONS
 
 int main ()
 {
-   try {
-       // Enable exceptions in cin.
-       std::cin.exceptions (std::ios::eofbit);
-
-       // Clear all bits and set eofbit.
-       std::cin.clear (std::ios::eofbit);
-   }
-   catch (const std::ios::failure &e) {
-       std::cout << "Caught an exception: " << e.what () << std::endl;
-   }
-   catch (const std::exception &e) {
-       std::cout << "Caught an exception: " << e.what () << std::endl;
-
-       return 1;   // Indicate failure.
-   }
-   catch (...) {
-       std::cout << "Caught an unknown exception" << std::endl;
-
-       return 1;   // Indicate failure.
-   }
+    const std::ios::iostate allbits =
+        std::ios::badbit | std::ios::eofbit | std::ios::failbit;
+                
+    // Enable all exceptions in cin.
+    std::cin.exceptions (allbits);
+
+    while (!std::cin.eof ()) {
+
+        // Clear state bits.
+        std::cin.clear ();
+
+        try {
+            short i;
+            char  ch;
+
+            // Try to extract a character and an integer.
+            std::cin >> ch >> i;
+
+            std::cout << ch << ' ' << i << '\n';
+        }
+
+#ifndef _RWSTD_NO_EXT_FAILURE
+
+        // Classes badbit_set, eofbit_set, and failbit_set are
+        // derived from failure and are optional extensions of
+        // this implementation.
+
+        catch (const std::ios::badbit_set &e) {
+            std::cout << "Caught std::ios::badbit_set: "
+                      << e.what () << '\n';
+        }
+        catch (const std::ios::eofbit_set &e) {
+            std::cout << "Caught std::ios::eofbit_set: "
+                      << e.what () << '\n';
+        }
+        catch (const std::ios::failbit_set &e) {
+            std::cout << "Caught std::ios::failbit_set: "
+                      << e.what () << '\n';
+        }
+
+#endif   // _RWSTD_NO_EXT_FAILURE
+
+        catch (const std::ios::failure &e) {
+            std::cout << "Caught std::ios::failure: "
+                      << e.what () << '\n';
+        }
+        catch (const std::exception &e) {
+            std::cout << "Caught std::exception: "
+                      << e.what () << '\n';
+
+            return 1;   // Indicate error.
+        }
+        catch (...) {
+            std::cout << "Caught an unknown exception\n";
+
+            return 1;   // Indicate error.
+        }
+    }
 
-   return 0;
+    return 0;
 }
 
-#else
+#else   // if defined (_RWSTD_NO_EXCEPTIONS)
 
 int main ()
 {
-    std::cout << "Exceptions not supported." << std::endl;
+    std::cout << "Exceptions not supported\n";
 
     return 0;
 }

Added: stdcxx/branches/4.2.x/examples/manual/in/failure.in
URL: http://svn.apache.org/viewvc/stdcxx/branches/4.2.x/examples/manual/in/failure.in?rev=654286&view=auto
==============================================================================
--- stdcxx/branches/4.2.x/examples/manual/in/failure.in (added)
+++ stdcxx/branches/4.2.x/examples/manual/in/failure.in Wed May  7 14:24:23 2008
@@ -0,0 +1,4 @@
+a+1234
+b-1234567890
+c+2345
+d-

Propchange: stdcxx/branches/4.2.x/examples/manual/in/failure.in
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: stdcxx/branches/4.2.x/examples/manual/out/failure.out
URL: http://svn.apache.org/viewvc/stdcxx/branches/4.2.x/examples/manual/out/failure.out?rev=654286&r1=654285&r2=654286&view=diff
==============================================================================
--- stdcxx/branches/4.2.x/examples/manual/out/failure.out (original)
+++ stdcxx/branches/4.2.x/examples/manual/out/failure.out Wed May  7 14:24:23 2008
@@ -1 +1,5 @@
-Caught an exception: std::cin: stream object has set ios::eofbit
+a 1234
+Caught std::ios::failbit_set: std::cin: stream object has set ios::failbit
+c 2345
+Caught std::ios::failbit_set: std::cin: stream object has set ios::failbit
+Caught std::ios::failure: std::cin: stream object has set failbit, eofbit



Mime
View raw message