sbuild-types.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2006  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software; you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation; either version 2 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
00017  *
00018  *********************************************************************/
00019 
00020 #ifndef SBUILD_TYPES_H
00021 #define SBUILD_TYPES_H
00022 
00023 #include <cassert>
00024 #include <ctime>
00025 #include <ios>
00026 #include <locale>
00027 #include <string>
00028 #include <vector>
00029 
00030 namespace sbuild
00031 {
00032 
00034   typedef std::vector<std::string> string_list;
00035 
00039   class date_base
00040   {
00041   public:
00043     typedef std::tm *(*break_time_func)(const time_t *timep, std:: tm *result);
00044 
00051     date_base (time_t          unix_time,
00052                break_time_func break_time):
00053       unix_time(unix_time),
00054       break_time(break_time)
00055     {}
00056 
00058     ~date_base ()
00059     {}
00060 
00068     template <class charT, class traits>
00069     friend
00070     std::basic_ostream<charT,traits>&
00071     operator << (std::basic_ostream<charT,traits>& stream,
00072                  date_base const&                  dt)
00073     {
00074       std::ios_base::iostate err = std::ios_base::goodbit;
00075 
00076       std::tm dtm;
00077       if ((dt.break_time(&dt.unix_time, &dtm)) == 0)
00078         {
00079           err = std::ios_base::badbit;
00080         }
00081       else
00082         {
00083           try
00084             {
00085               typename std::basic_ostream<charT, traits>::sentry sentry(stream);
00086               if (sentry)
00087                 {
00088                   const std::basic_string<char>
00089                     nfmt(date_base::get_date_format());
00090                   std::basic_string<charT> wfmt(nfmt.size(), 0);
00091                   assert(nfmt.size() == wfmt.size());
00092                   const char *nptr = nfmt.c_str();
00093                   charT *wptr = const_cast<charT *>(wfmt.c_str());
00094 
00095                   std::use_facet<std::ctype<charT> >(stream.getloc())
00096                     .widen(nptr, nptr + nfmt.size(), wptr);
00097 
00098                   typedef std::time_put<charT,std::ostreambuf_iterator<charT,traits> >
00099                     time_type;
00100                   if (std::use_facet<time_type>(stream.getloc())
00101                       .put(stream, stream, stream.fill(),
00102                            &dtm,
00103                            wptr, wptr + wfmt.size())
00104                       .failed())
00105                     {
00106                       err = std::ios_base::badbit;
00107                     }
00108                   stream.width(0);
00109                 }
00110             }
00111           catch (...)
00112             {
00113               bool flag = false;
00114               try
00115                 {
00116                   stream.setstate(std::ios::failbit);
00117                 }
00118               catch (std::ios_base::failure const& discard)
00119                 {
00120                   flag = true;
00121                 }
00122               if (flag)
00123                 throw;
00124             }
00125         }
00126 
00127       if (err)
00128         stream.setstate(err);
00129 
00130       return stream;
00131     }
00132 
00133   private:
00140     static const char *
00141     get_date_format ();
00142 
00144     time_t          unix_time;
00146     break_time_func break_time;
00147   };
00148 
00152   class gmdate : public date_base
00153   {
00154   public:
00160     gmdate (time_t          unix_time):
00161       date_base(unix_time, gmtime_r)
00162     {}
00163 
00165     ~gmdate ()
00166     {}
00167   };
00168 
00172   class date : public date_base
00173   {
00174   public:
00180     date (time_t          unix_time):
00181       date_base(unix_time, localtime_r)
00182     {}
00183 
00185     ~date ()
00186     {}
00187   };
00188 
00189 }
00190 
00191 #endif /* SBUILD_TYPES_H */
00192 
00193 /*
00194  * Local Variables:
00195  * mode:C++
00196  * End:
00197  */

Generated on Sat Jan 27 16:11:03 2007 for schroot by  doxygen 1.5.1