sbuild  1.6.10
sbuild-types.h
1 /* Copyright © 2005-2008 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_TYPES_H
20 #define SBUILD_TYPES_H
21 
22 #include <cassert>
23 #include <ctime>
24 #include <ios>
25 #include <locale>
26 #include <map>
27 #include <set>
28 #include <string>
29 #include <vector>
30 
34 namespace sbuild
35 {
36 
38  typedef std::vector<std::string> string_list;
39 
41  typedef std::set<std::string> string_set;
42 
44  typedef std::map<std::string, std::string> string_map;
45 
49  class date_base
50  {
51  public:
53  typedef std::tm *(*break_time_func)(const time_t *timep, std:: tm *result);
54 
63  unix_time(unix_time),
64  break_time(break_time)
65  {}
66 
68  virtual ~date_base ()
69  {}
70 
78  template <class charT, class traits>
79  friend
80  std::basic_ostream<charT,traits>&
81  operator << (std::basic_ostream<charT,traits>& stream,
82  date_base const& dt)
83  {
84  std::ios_base::iostate err = std::ios_base::goodbit;
85 
86  std::tm dtm;
87  if ((dt.break_time(&dt.unix_time, &dtm)) == 0)
88  {
89  err = std::ios_base::badbit;
90  }
91  else
92  {
93  try
94  {
95  typename std::basic_ostream<charT, traits>::sentry sentry(stream);
96  if (sentry)
97  {
98  const std::basic_string<char>
99  nfmt(dt.get_date_format());
100  std::basic_string<charT> wfmt(nfmt.size(), 0);
101  assert(nfmt.size() == wfmt.size());
102  const char *nptr = nfmt.c_str();
103  charT *wptr = const_cast<charT *>(wfmt.c_str());
104 
105  std::use_facet<std::ctype<charT> >(stream.getloc())
106  .widen(nptr, nptr + nfmt.size(), wptr);
107 
108  typedef std::time_put<charT,std::ostreambuf_iterator<charT,traits> >
109  time_type;
110  if (std::use_facet<time_type>(stream.getloc())
111  .put(stream, stream, stream.fill(),
112  &dtm,
113  wptr, wptr + wfmt.size())
114  .failed())
115  {
116  err = std::ios_base::badbit;
117  }
118  stream.width(0);
119  }
120  }
121  catch (...)
122  {
123  bool flag = false;
124  try
125  {
126  stream.setstate(std::ios::failbit);
127  }
128  catch (std::ios_base::failure const& discard)
129  {
130  flag = true;
131  }
132  if (flag)
133  throw;
134  }
135  }
136 
137  if (err)
138  stream.setstate(err);
139 
140  return stream;
141  }
142 
143  private:
150  virtual const char *
151  get_date_format () const;
152 
154  time_t unix_time;
157  };
158 
162  class gmdate : public date_base
163  {
164  public:
170  gmdate (time_t unix_time):
171  date_base(unix_time, gmtime_r)
172  {}
173 
175  virtual ~gmdate ()
176  {}
177  };
178 
182  class date : public date_base
183  {
184  public:
190  date (time_t unix_time):
191  date_base(unix_time, localtime_r)
192  {}
193 
195  virtual ~date ()
196  {}
197  };
198 
202  class isodate : public date_base
203  {
204  public:
210  isodate (time_t unix_time):
211  date_base(unix_time, gmtime_r)
212  {}
213 
215  virtual ~isodate ()
216  {}
217 
218  private:
219  virtual const char *
220  get_date_format () const;
221  };
222 
223 }
224 
225 #endif /* SBUILD_TYPES_H */
226 
227 /*
228  * Local Variables:
229  * mode:C++
230  * End:
231  */
isodate(time_t unix_time)
The constructor.
Definition: sbuild-types.h:210
Debian source builder components.
Definition: sbuild-auth-null.h:24
time_t unix_time
The time.
Definition: sbuild-types.h:154
virtual ~date_base()
The destructor.
Definition: sbuild-types.h:68
std::map< std::string, std::string > string_map
A string map.
Definition: sbuild-types.h:44
std::set< std::string > string_set
A string set.
Definition: sbuild-types.h:41
virtual const char * get_date_format() const
Get the date formatting string.
Definition: sbuild-types.cc:27
virtual ~date()
The destructor.
Definition: sbuild-types.h:195
A date representation.
Definition: sbuild-types.h:49
A date representation in UTC.
Definition: sbuild-types.h:162
std::vector< std::string > string_list
A string vector.
Definition: sbuild-types.h:38
virtual ~gmdate()
The destructor.
Definition: sbuild-types.h:175
virtual ~isodate()
The destructor.
Definition: sbuild-types.h:215
break_time_func break_time
The function to split up the time.
Definition: sbuild-types.h:156
date(time_t unix_time)
The constructor.
Definition: sbuild-types.h:190
date_base(time_t unix_time, break_time_func break_time)
The constructor.
Definition: sbuild-types.h:61
A date representation in ISO-8601 format.
Definition: sbuild-types.h:202
std::tm *(* break_time_func)(const time_t *timep, std::tm *result)
Function pointer to split time into a std::tm.
Definition: sbuild-types.h:53
gmdate(time_t unix_time)
The constructor.
Definition: sbuild-types.h:170
A date representation in local time.
Definition: sbuild-types.h:182