source: trunk/debathena/third/schroot/sbuild/sbuild-types.h @ 24167

Revision 24167, 4.7 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
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 <set>
27#include <string>
28#include <vector>
29
30namespace sbuild
31{
32
33  /// A string vector.
34  typedef std::vector<std::string> string_list;
35
36  /// A string set.
37  typedef std::set<std::string> string_set;
38
39  /**
40   * A date representation.
41   */
42  class date_base
43  {
44  public:
45    /// Function pointer to split time into a std::tm.
46    typedef std::tm *(*break_time_func)(const time_t *timep, std:: tm *result);
47
48    /**
49     * The constructor.
50     *
51     * @param unix_time the time.
52     * @param break_time the function to split up the time.
53     */
54    date_base (time_t          unix_time,
55               break_time_func break_time):
56      unix_time(unix_time),
57      break_time(break_time)
58    {}
59
60    /// The destructor.
61    virtual ~date_base ()
62    {}
63
64    /**
65     * Output the date to an ostream.
66     *
67     * @param stream the stream to output to.
68     * @param dt the date to output.
69     * @returns the stream.
70     */
71    template <class charT, class traits>
72    friend
73    std::basic_ostream<charT,traits>&
74    operator << (std::basic_ostream<charT,traits>& stream,
75                 date_base const&                  dt)
76    {
77      std::ios_base::iostate err = std::ios_base::goodbit;
78
79      std::tm dtm;
80      if ((dt.break_time(&dt.unix_time, &dtm)) == 0)
81        {
82          err = std::ios_base::badbit;
83        }
84      else
85        {
86          try
87            {
88              typename std::basic_ostream<charT, traits>::sentry sentry(stream);
89              if (sentry)
90                {
91                  const std::basic_string<char>
92                    nfmt(dt.get_date_format());
93                  std::basic_string<charT> wfmt(nfmt.size(), 0);
94                  assert(nfmt.size() == wfmt.size());
95                  const char *nptr = nfmt.c_str();
96                  charT *wptr = const_cast<charT *>(wfmt.c_str());
97
98                  std::use_facet<std::ctype<charT> >(stream.getloc())
99                    .widen(nptr, nptr + nfmt.size(), wptr);
100
101                  typedef std::time_put<charT,std::ostreambuf_iterator<charT,traits> >
102                    time_type;
103                  if (std::use_facet<time_type>(stream.getloc())
104                      .put(stream, stream, stream.fill(),
105                           &dtm,
106                           wptr, wptr + wfmt.size())
107                      .failed())
108                    {
109                      err = std::ios_base::badbit;
110                    }
111                  stream.width(0);
112                }
113            }
114          catch (...)
115            {
116              bool flag = false;
117              try
118                {
119                  stream.setstate(std::ios::failbit);
120                }
121              catch (std::ios_base::failure const& discard)
122                {
123                  flag = true;
124                }
125              if (flag)
126                throw;
127            }
128        }
129
130      if (err)
131        stream.setstate(err);
132
133      return stream;
134    }
135
136  private:
137    /**
138     * Get the date formatting string.  This is used for output with
139     * the locale std::time_put facet.
140     *
141     * @returns a localised format string.
142     */
143    virtual const char *
144    get_date_format () const;
145
146    /// The time.
147    time_t          unix_time;
148    /// The function to split up the time.
149    break_time_func break_time;
150  };
151
152  /**
153   * A date representation in UTC.
154   */
155  class gmdate : public date_base
156  {
157  public:
158    /**
159     * The constructor.
160     *
161     * @param unix_time the time in UTC.
162     */
163    gmdate (time_t          unix_time):
164      date_base(unix_time, gmtime_r)
165    {}
166
167    /// The destructor.
168    virtual ~gmdate ()
169    {}
170  };
171
172  /**
173   * A date representation in local time.
174   */
175  class date : public date_base
176  {
177  public:
178    /**
179     * The constructor.
180     *
181     * @param unix_time the time in the local timezone.
182     */
183    date (time_t           unix_time):
184      date_base(unix_time, localtime_r)
185    {}
186
187    /// The destructor.
188    virtual ~date ()
189    {}
190  };
191
192  /**
193   * A date representation in ISO-8601 format.
194   */
195  class isodate : public date_base
196  {
197  public:
198    /**
199     * The constructor.
200     *
201     * @param unix_time the time in UTC.
202     */
203    isodate (time_t        unix_time):
204      date_base(unix_time, gmtime_r)
205    {}
206
207    /// The destructor.
208    virtual ~isodate ()
209    {}
210
211  private:
212    virtual const char *
213    get_date_format () const;
214  };
215
216}
217
218#endif /* SBUILD_TYPES_H */
219
220/*
221 * Local Variables:
222 * mode:C++
223 * End:
224 */
Note: See TracBrowser for help on using the repository browser.