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 <ctime>
00024 #include <ios>
00025 #include <locale>
00026 #include <string>
00027 #include <vector>
00028 
00029 namespace sbuild
00030 {
00031 
00033   typedef std::vector<std::string> string_list;
00034 
00036   class date_base
00037   {
00038   public:
00039     typedef std::tm *(*break_time_func)(const time_t *timep, std:: tm *result);
00040 
00041     date_base (time_t          unix_time,
00042                break_time_func break_time):
00043       unix_time(unix_time),
00044       break_time(break_time)
00045     {}
00046 
00047     ~date_base ()
00048     {}
00049 
00050     template <class charT, class traits>
00051     friend
00052     std::basic_ostream<charT,traits>&
00053     operator << (std::basic_ostream<charT,traits>& stream,
00054                  date_base const&                  dt)
00055     {
00056       std::ios_base::iostate err = std::ios_base::goodbit;
00057 
00058       std::tm dtm;
00059       if ((dt.break_time(&dt.unix_time, &dtm)) == 0)
00060         {
00061           err = std::ios_base::badbit;
00062         }
00063       else
00064         {
00065           try
00066             {
00067               typename std::basic_ostream<charT, traits>::sentry sentry(stream);
00068               if (sentry)
00069                 {
00070                   const char nfmt[] = "%d %b %Y";
00071                   charT wfmt[sizeof(nfmt)/sizeof(nfmt[0])];
00072                   std::use_facet<std::ctype<charT> >(stream.getloc())
00073                     .widen(nfmt, nfmt + (sizeof(nfmt)/sizeof(nfmt[0])) - 1, wfmt);
00074 
00075                   typedef std::time_put<charT,std::ostreambuf_iterator<charT,traits> >
00076                     time_type;
00077                   if (std::use_facet<time_type>(stream.getloc())
00078                       .put(stream, stream, stream.fill(),
00079                            &dtm, wfmt + 0, wfmt + sizeof(wfmt)/sizeof(wfmt[0]) - 1)
00080                       .failed())
00081                     {
00082                       err = std::ios_base::badbit;
00083                     }
00084                   stream.width(0);
00085                 }
00086             }
00087           catch (...)
00088             {
00089               bool flag = false;
00090               try
00091                 {
00092                   stream.setstate(std::ios::failbit);
00093                 }
00094               catch (std::ios_base::failure)
00095                 {
00096                   flag = true;
00097                 }
00098               if (flag)
00099                 throw;
00100             }
00101         }
00102 
00103       if (err)
00104         stream.setstate(err);
00105 
00106       return stream;
00107     }
00108 
00109   private:
00110     time_t          unix_time;
00111     break_time_func break_time;
00112   };
00113 
00114   class gmdate : public date_base
00115   {
00116   public:
00117     gmdate (time_t          unix_time):
00118       date_base(unix_time, gmtime_r)
00119     {}
00120 
00121     ~gmdate ()
00122     {}
00123   };
00124 
00125   class date : public date_base
00126   {
00127   public:
00128     date (time_t          unix_time):
00129       date_base(unix_time, localtime_r)
00130     {}
00131 
00132     ~date ()
00133     {}
00134   };
00135 
00136 }
00137 
00138 #endif /* SBUILD_TYPES_H */
00139 
00140 /*
00141  * Local Variables:
00142  * mode:C++
00143  * End:
00144  */

Generated on Mon Jun 26 12:50:25 2006 for schroot by  doxygen 1.4.7