sbuild
1.4.26
|
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 00033 namespace sbuild 00034 { 00035 00037 typedef std::vector<std::string> string_list; 00038 00040 typedef std::set<std::string> string_set; 00041 00045 class date_base 00046 { 00047 public: 00049 typedef std::tm *(*break_time_func)(const time_t *timep, std:: tm *result); 00050 00057 date_base (time_t unix_time, 00058 break_time_func break_time): 00059 unix_time(unix_time), 00060 break_time(break_time) 00061 {} 00062 00064 virtual ~date_base () 00065 {} 00066 00074 template <class charT, class traits> 00075 friend 00076 std::basic_ostream<charT,traits>& 00077 operator << (std::basic_ostream<charT,traits>& stream, 00078 date_base const& dt) 00079 { 00080 std::ios_base::iostate err = std::ios_base::goodbit; 00081 00082 std::tm dtm; 00083 if ((dt.break_time(&dt.unix_time, &dtm)) == 0) 00084 { 00085 err = std::ios_base::badbit; 00086 } 00087 else 00088 { 00089 try 00090 { 00091 typename std::basic_ostream<charT, traits>::sentry sentry(stream); 00092 if (sentry) 00093 { 00094 const std::basic_string<char> 00095 nfmt(dt.get_date_format()); 00096 std::basic_string<charT> wfmt(nfmt.size(), 0); 00097 assert(nfmt.size() == wfmt.size()); 00098 const char *nptr = nfmt.c_str(); 00099 charT *wptr = const_cast<charT *>(wfmt.c_str()); 00100 00101 std::use_facet<std::ctype<charT> >(stream.getloc()) 00102 .widen(nptr, nptr + nfmt.size(), wptr); 00103 00104 typedef std::time_put<charT,std::ostreambuf_iterator<charT,traits> > 00105 time_type; 00106 if (std::use_facet<time_type>(stream.getloc()) 00107 .put(stream, stream, stream.fill(), 00108 &dtm, 00109 wptr, wptr + wfmt.size()) 00110 .failed()) 00111 { 00112 err = std::ios_base::badbit; 00113 } 00114 stream.width(0); 00115 } 00116 } 00117 catch (...) 00118 { 00119 bool flag = false; 00120 try 00121 { 00122 stream.setstate(std::ios::failbit); 00123 } 00124 catch (std::ios_base::failure const& discard) 00125 { 00126 flag = true; 00127 } 00128 if (flag) 00129 throw; 00130 } 00131 } 00132 00133 if (err) 00134 stream.setstate(err); 00135 00136 return stream; 00137 } 00138 00139 private: 00146 virtual const char * 00147 get_date_format () const; 00148 00150 time_t unix_time; 00152 break_time_func break_time; 00153 }; 00154 00158 class gmdate : public date_base 00159 { 00160 public: 00166 gmdate (time_t unix_time): 00167 date_base(unix_time, gmtime_r) 00168 {} 00169 00171 virtual ~gmdate () 00172 {} 00173 }; 00174 00178 class date : public date_base 00179 { 00180 public: 00186 date (time_t unix_time): 00187 date_base(unix_time, localtime_r) 00188 {} 00189 00191 virtual ~date () 00192 {} 00193 }; 00194 00198 class isodate : public date_base 00199 { 00200 public: 00206 isodate (time_t unix_time): 00207 date_base(unix_time, gmtime_r) 00208 {} 00209 00211 virtual ~isodate () 00212 {} 00213 00214 private: 00215 virtual const char * 00216 get_date_format () const; 00217 }; 00218 00219 } 00220 00221 #endif /* SBUILD_TYPES_H */ 00222 00223 /* 00224 * Local Variables: 00225 * mode:C++ 00226 * End: 00227 */