stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject svn commit: r320814 - /incubator/stdcxx/trunk/examples/manual/time_manip.cpp
Date Thu, 13 Oct 2005 17:25:26 GMT
Author: sebor
Date: Thu Oct 13 10:25:23 2005
New Revision: 320814

URL: http://svn.apache.org/viewcvs?rev=320814&view=rev
Log:
2005-10-13  Martin Sebor  <sebor@roguewave.com>

	* time_manip.cpp (time_get_manip): Made ctor explicit.
	(time_put_manip): Same.
	(get_time): Explicitly invoked the time_get_manip ctor instead
	of relying on implicit user-defined conversion.
	(put_time): Same.
	(main): Added a missing std:: qualification.
	Improved error message and exited with a non-zero status
	on error.


Modified:
    incubator/stdcxx/trunk/examples/manual/time_manip.cpp

Modified: incubator/stdcxx/trunk/examples/manual/time_manip.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/examples/manual/time_manip.cpp?rev=320814&r1=320813&r2=320814&view=diff
==============================================================================
--- incubator/stdcxx/trunk/examples/manual/time_manip.cpp (original)
+++ incubator/stdcxx/trunk/examples/manual/time_manip.cpp Thu Oct 13 10:25:23 2005
@@ -21,10 +21,10 @@
  * 
  **************************************************************************/
 
-#include <ctime>
-#include <iomanip>
-#include <iostream>
-#include <locale>
+#include <ctime>      // for localtime(), time()
+#include <iomanip>    // for __rw_smanip
+#include <iostream>   // for cout
+#include <locale>     // for time_get, time_put
 
 #include <examples.h>
 
@@ -35,7 +35,7 @@
 {
     std::tm *tmb_;
 
-    time_get_manip (std::tm *tmb)
+    explicit time_get_manip (std::tm *tmb)
         : tmb_ (tmb) { }
 
     template <class Traits>
@@ -52,11 +52,10 @@
 
         // extract the time from the stream using an extension
         // of this implementation
-        const Iter end =
-            tg.get (Iter (strm.rdbuf ()), Iter (), strm, err, tmb_,
-                    fmt, fmtend);
+        tg.get (Iter (strm.rdbuf ()), Iter (), strm, err, tmb_, fmt, fmtend);
 
-        strm.setstate (err);
+        if (std::ios_base::goodbit != err)
+            strm.setstate (err);
     }
 };
 
@@ -67,7 +66,7 @@
 {
     const std::tm *tmb_;
 
-    time_put_manip (const std::tm *tmb)
+    explicit time_put_manip (const std::tm *tmb)
         : tmb_ (tmb) { }
 
     template <class Traits>
@@ -80,9 +79,8 @@
         const charT* const fmtend = fmt + Traits::length (fmt);
 
         const TimePut &tp = std::use_facet<TimePut>(strm.getloc ());
-        const Iter end =
-            tp.put (Iter (strm.rdbuf ()), strm, strm.fill (), tmb_,
-                    fmt, fmtend);
+        const Iter end = tp.put (Iter (strm.rdbuf ()), strm, strm.fill (),
+                                 tmb_, fmt, fmtend);
 
         if (end.failed ())
             strm.setstate (std::ios_base::badbit);
@@ -96,10 +94,13 @@
 get_time (std::tm *tmb, const charT *fmt)
 {
     // return an object of the manipulator implementation type
-    // which will store the function arguments until its function
+    // that will store the function arguments until its function
     // call operator is invoked by the extraction operator for
     // std::__rw_smanip
-    return std::__rw_smanip<time_get_manip<charT>, const charT*>(tmb, fmt);
+    typedef time_get_manip<charT>                   GetTime;
+    typedef std::__rw_smanip<GetTime, const charT*> Manip;
+
+    return Manip (GetTime (tmb), fmt);
 }
 
 
@@ -109,10 +110,13 @@
 put_time (const std::tm *tmb, const charT *fmt)
 {
     // return an object of the manipulator implementation type
-    // which will store the function arguments until its function
+    // that will store the function arguments until its function
     // call operator is invoked by the insertion operator for
     // std::__rw_smanip
-    return std::__rw_smanip<time_put_manip<charT>, const charT*>(tmb, fmt);
+    typedef time_put_manip<charT>                   PutTime;
+    typedef std::__rw_smanip<PutTime, const charT*> Manip;
+
+    return Manip (PutTime (tmb), fmt);
 }
 
 
@@ -122,25 +126,31 @@
     // the extration format specifier string
     const char* const get_fmt = argc < 2 ? "%c" : argv [1];
 
-    // the seconf optional command line argument specifies
+    // the second optional command line argument specifies
     // the insertion format specifier string
     const char* const put_fmt = argc < 3 ? "%c" : argv [2];
 
     // obtain the local time
-    const std::time_t t = time (0);
+    const std::time_t t = std::time (0);
     std::tm* const tmb = std::localtime (&t);
 
     // set cout's locale to the one specified in the environment
     std::cout.imbue (std::locale (""));
 
-    // output the local time using the specified format
+    // output the local time using the specified output format
     std::cout << put_time (tmb, put_fmt) << '\n';
 
     // input time in the specified input format using the "C" locale
     std::cin >> get_time (tmb, get_fmt);
 
-    // output the extracted time using the output format
-    std::cout << put_time (tmb, put_fmt) << '\n';
+    // on success output the extracted time using the specified
+    // output format; otherwise output an error message
+    if (std::cin.good ())
+        std::cout << put_time (tmb, put_fmt) << '\n';
+    else {
+        std::cerr << "get_time (std::tm*, \"" << get_fmt << "\") failed\n";
+        return 1;
+    }
 
     return 0;
 }



Mime
View raw message