sbuild-types.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2008  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 3 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, see
00015  * <http://www.gnu.org/licenses/>.
00016  *
00017  *********************************************************************/
00018 
00019 #ifndef SBUILD_TYPES_H
00020 #define SBUILD_TYPES_H
00021 
00022 #include <cassert>
00023 #include <ctime>
00024 #include <ios>
00025 #include <locale>
00026 #include <set>
00027 #include <string>
00028 #include <vector>
00029 
00030 namespace sbuild
00031 {
00032 
00034   typedef std::vector<std::string> string_list;
00035 
00037   typedef std::set<std::string> string_set;
00038 
00042   class date_base
00043   {
00044   public:
00046     typedef std::tm *(*break_time_func)(const time_t *timep, std:: tm *result);
00047 
00054     date_base (time_t          unix_time,
00055                break_time_func break_time):
00056       unix_time(unix_time),
00057       break_time(break_time)
00058     {}
00059 
00061     virtual ~date_base ()
00062     {}
00063 
00071     template <class charT, class traits>
00072     friend
00073     std::basic_ostream<charT,traits>&
00074     operator << (std::basic_ostream<charT,traits>& stream,
00075                  date_base const&                  dt)
00076     {
00077       std::ios_base::iostate err = std::ios_base::goodbit;
00078 
00079       std::tm dtm;
00080       if ((dt.break_time(&dt.unix_time, &dtm)) == 0)
00081         {
00082           err = std::ios_base::badbit;
00083         }
00084       else
00085         {
00086           try
00087             {
00088               typename std::basic_ostream<charT, traits>::sentry sentry(stream);
00089               if (sentry)
00090                 {
00091                   const std::basic_string<char>
00092                     nfmt(dt.get_date_format());
00093                   std::basic_string<charT> wfmt(nfmt.size(), 0);
00094                   assert(nfmt.size() == wfmt.size());
00095                   const char *nptr = nfmt.c_str();
00096                   charT *wptr = const_cast<charT *>(wfmt.c_str());
00097 
00098                   std::use_facet<std::ctype<charT> >(stream.getloc())
00099                     .widen(nptr, nptr + nfmt.size(), wptr);
00100 
00101                   typedef std::time_put<charT,std::ostreambuf_iterator<charT,traits> >
00102                     time_type;
00103                   if (std::use_facet<time_type>(stream.getloc())
00104                       .put(stream, stream, stream.fill(),
00105                            &dtm,
00106                            wptr, wptr + wfmt.size())
00107                       .failed())
00108                     {
00109                       err = std::ios_base::badbit;
00110                     }
00111                   stream.width(0);
00112                 }
00113             }
00114           catch (...)
00115             {
00116               bool flag = false;
00117               try
00118                 {
00119                   stream.setstate(std::ios::failbit);
00120                 }
00121               catch (std::ios_base::failure const& discard)
00122                 {
00123                   flag = true;
00124                 }
00125               if (flag)
00126                 throw;
00127             }
00128         }
00129 
00130       if (err)
00131         stream.setstate(err);
00132 
00133       return stream;
00134     }
00135 
00136   private:
00143     virtual const char *
00144     get_date_format () const;
00145 
00147     time_t          unix_time;
00149     break_time_func break_time;
00150   };
00151 
00155   class gmdate : public date_base
00156   {
00157   public:
00163     gmdate (time_t          unix_time):
00164       date_base(unix_time, gmtime_r)
00165     {}
00166 
00168     virtual ~gmdate ()
00169     {}
00170   };
00171 
00175   class date : public date_base
00176   {
00177   public:
00183     date (time_t           unix_time):
00184       date_base(unix_time, localtime_r)
00185     {}
00186 
00188     virtual ~date ()
00189     {}
00190   };
00191 
00195   class isodate : public date_base
00196   {
00197   public:
00203     isodate (time_t        unix_time):
00204       date_base(unix_time, gmtime_r)
00205     {}
00206 
00208     virtual ~isodate ()
00209     {}
00210 
00211   private:
00212     virtual const char *
00213     get_date_format () const;
00214   };
00215 
00216 }
00217 
00218 #endif /* SBUILD_TYPES_H */
00219 
00220 /*
00221  * Local Variables:
00222  * mode:C++
00223  * End:
00224  */

Generated on Sun Mar 22 22:18:14 2009 for sbuild by  doxygen 1.5.8