Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- iomanip ----------------------------------===// |
| 3 | // |
Howard Hinnant | c566dc3 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | ee11c31 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_IOMANIP |
| 12 | #define _LIBCPP_IOMANIP |
| 13 | |
| 14 | /* |
| 15 | iomanip synopsis |
| 16 | |
Marshall Clow | 096d579 | 2013-11-14 20:01:38 +0000 | [diff] [blame] | 17 | namespace std { |
| 18 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | // types T1, T2, ... are unspecified implementation types |
| 20 | T1 resetiosflags(ios_base::fmtflags mask); |
| 21 | T2 setiosflags (ios_base::fmtflags mask); |
| 22 | T3 setbase(int base); |
| 23 | template<charT> T4 setfill(charT c); |
| 24 | T5 setprecision(int n); |
| 25 | T6 setw(int n); |
| 26 | template <class moneyT> T7 get_money(moneyT& mon, bool intl = false); |
| 27 | template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false); |
| 28 | template <class charT> T9 get_time(struct tm* tmb, const charT* fmt); |
| 29 | template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt); |
| 30 | |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 31 | template <class charT> |
| 32 | T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14 |
| 33 | |
| 34 | template <class charT, class traits, class Allocator> |
| 35 | T12 quoted(const basic_string<charT, traits, Allocator>& s, |
| 36 | charT delim=charT('"'), charT escape=charT('\\')); // C++14 |
| 37 | |
| 38 | template <class charT, class traits, class Allocator> |
| 39 | T13 quoted(basic_string<charT, traits, Allocator>& s, |
| 40 | charT delim=charT('"'), charT escape=charT('\\')); // C++14 |
| 41 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | } // std |
| 43 | |
| 44 | */ |
| 45 | |
| 46 | #include <__config> |
| 47 | #include <istream> |
| 48 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 49 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 50 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 51 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 52 | |
| 53 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 54 | |
| 55 | // resetiosflags |
| 56 | |
| 57 | class __iom_t1 |
| 58 | { |
| 59 | ios_base::fmtflags __mask_; |
| 60 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 61 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 62 | explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {} |
| 63 | |
| 64 | template <class _CharT, class _Traits> |
| 65 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 66 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 67 | basic_istream<_CharT, _Traits>& |
| 68 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x) |
| 69 | { |
| 70 | __is.unsetf(__x.__mask_); |
| 71 | return __is; |
| 72 | } |
| 73 | |
| 74 | template <class _CharT, class _Traits> |
| 75 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 76 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 77 | basic_ostream<_CharT, _Traits>& |
| 78 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x) |
| 79 | { |
| 80 | __os.unsetf(__x.__mask_); |
| 81 | return __os; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | inline _LIBCPP_INLINE_VISIBILITY |
| 86 | __iom_t1 |
| 87 | resetiosflags(ios_base::fmtflags __mask) |
| 88 | { |
| 89 | return __iom_t1(__mask); |
| 90 | } |
| 91 | |
| 92 | // setiosflags |
| 93 | |
| 94 | class __iom_t2 |
| 95 | { |
| 96 | ios_base::fmtflags __mask_; |
| 97 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 98 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 99 | explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {} |
| 100 | |
| 101 | template <class _CharT, class _Traits> |
| 102 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 103 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 104 | basic_istream<_CharT, _Traits>& |
| 105 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x) |
| 106 | { |
| 107 | __is.setf(__x.__mask_); |
| 108 | return __is; |
| 109 | } |
| 110 | |
| 111 | template <class _CharT, class _Traits> |
| 112 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 113 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | basic_ostream<_CharT, _Traits>& |
| 115 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x) |
| 116 | { |
| 117 | __os.setf(__x.__mask_); |
| 118 | return __os; |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | inline _LIBCPP_INLINE_VISIBILITY |
| 123 | __iom_t2 |
| 124 | setiosflags(ios_base::fmtflags __mask) |
| 125 | { |
| 126 | return __iom_t2(__mask); |
| 127 | } |
| 128 | |
| 129 | // setbase |
| 130 | |
| 131 | class __iom_t3 |
| 132 | { |
| 133 | int __base_; |
| 134 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 135 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 136 | explicit __iom_t3(int __b) : __base_(__b) {} |
| 137 | |
| 138 | template <class _CharT, class _Traits> |
| 139 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 140 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 141 | basic_istream<_CharT, _Traits>& |
| 142 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x) |
| 143 | { |
| 144 | __is.setf(__x.__base_ == 8 ? ios_base::oct : |
| 145 | __x.__base_ == 10 ? ios_base::dec : |
| 146 | __x.__base_ == 16 ? ios_base::hex : |
| 147 | ios_base::fmtflags(0), ios_base::basefield); |
| 148 | return __is; |
| 149 | } |
| 150 | |
| 151 | template <class _CharT, class _Traits> |
| 152 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 153 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 154 | basic_ostream<_CharT, _Traits>& |
| 155 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x) |
| 156 | { |
| 157 | __os.setf(__x.__base_ == 8 ? ios_base::oct : |
| 158 | __x.__base_ == 10 ? ios_base::dec : |
| 159 | __x.__base_ == 16 ? ios_base::hex : |
| 160 | ios_base::fmtflags(0), ios_base::basefield); |
| 161 | return __os; |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | inline _LIBCPP_INLINE_VISIBILITY |
| 166 | __iom_t3 |
| 167 | setbase(int __base) |
| 168 | { |
| 169 | return __iom_t3(__base); |
| 170 | } |
| 171 | |
| 172 | // setfill |
| 173 | |
| 174 | template<class _CharT> |
| 175 | class __iom_t4 |
| 176 | { |
| 177 | _CharT __fill_; |
| 178 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 179 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 180 | explicit __iom_t4(_CharT __c) : __fill_(__c) {} |
| 181 | |
| 182 | template <class _Traits> |
| 183 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 184 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 185 | basic_ostream<_CharT, _Traits>& |
| 186 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x) |
| 187 | { |
| 188 | __os.fill(__x.__fill_); |
| 189 | return __os; |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | template<class _CharT> |
| 194 | inline _LIBCPP_INLINE_VISIBILITY |
| 195 | __iom_t4<_CharT> |
| 196 | setfill(_CharT __c) |
| 197 | { |
| 198 | return __iom_t4<_CharT>(__c); |
| 199 | } |
| 200 | |
| 201 | // setprecision |
| 202 | |
| 203 | class __iom_t5 |
| 204 | { |
| 205 | int __n_; |
| 206 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 207 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 208 | explicit __iom_t5(int __n) : __n_(__n) {} |
| 209 | |
| 210 | template <class _CharT, class _Traits> |
| 211 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 212 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 213 | basic_istream<_CharT, _Traits>& |
| 214 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x) |
| 215 | { |
| 216 | __is.precision(__x.__n_); |
| 217 | return __is; |
| 218 | } |
| 219 | |
| 220 | template <class _CharT, class _Traits> |
| 221 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 222 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 223 | basic_ostream<_CharT, _Traits>& |
| 224 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x) |
| 225 | { |
| 226 | __os.precision(__x.__n_); |
| 227 | return __os; |
| 228 | } |
| 229 | }; |
| 230 | |
| 231 | inline _LIBCPP_INLINE_VISIBILITY |
| 232 | __iom_t5 |
| 233 | setprecision(int __n) |
| 234 | { |
| 235 | return __iom_t5(__n); |
| 236 | } |
| 237 | |
| 238 | // setw |
| 239 | |
| 240 | class __iom_t6 |
| 241 | { |
| 242 | int __n_; |
| 243 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 244 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 245 | explicit __iom_t6(int __n) : __n_(__n) {} |
| 246 | |
| 247 | template <class _CharT, class _Traits> |
| 248 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 249 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 250 | basic_istream<_CharT, _Traits>& |
| 251 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x) |
| 252 | { |
| 253 | __is.width(__x.__n_); |
| 254 | return __is; |
| 255 | } |
| 256 | |
| 257 | template <class _CharT, class _Traits> |
| 258 | friend |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 259 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 260 | basic_ostream<_CharT, _Traits>& |
| 261 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x) |
| 262 | { |
| 263 | __os.width(__x.__n_); |
| 264 | return __os; |
| 265 | } |
| 266 | }; |
| 267 | |
| 268 | inline _LIBCPP_INLINE_VISIBILITY |
| 269 | __iom_t6 |
| 270 | setw(int __n) |
| 271 | { |
| 272 | return __iom_t6(__n); |
| 273 | } |
| 274 | |
| 275 | // get_money |
| 276 | |
| 277 | template <class _MoneyT> class __iom_t7; |
| 278 | |
| 279 | template <class _CharT, class _Traits, class _MoneyT> |
| 280 | basic_istream<_CharT, _Traits>& |
| 281 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x); |
| 282 | |
| 283 | template <class _MoneyT> |
| 284 | class __iom_t7 |
| 285 | { |
| 286 | _MoneyT& __mon_; |
| 287 | bool __intl_; |
| 288 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 289 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 290 | __iom_t7(_MoneyT& __mon, bool __intl) |
| 291 | : __mon_(__mon), __intl_(__intl) {} |
| 292 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 293 | template <class _CharT, class _Traits, class _Mp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 294 | friend |
| 295 | basic_istream<_CharT, _Traits>& |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 296 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | template <class _CharT, class _Traits, class _MoneyT> |
| 300 | basic_istream<_CharT, _Traits>& |
| 301 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x) |
| 302 | { |
| 303 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 304 | try |
| 305 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 306 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 307 | typename basic_istream<_CharT, _Traits>::sentry __s(__is); |
| 308 | if (__s) |
| 309 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 310 | typedef istreambuf_iterator<_CharT, _Traits> _Ip; |
| 311 | typedef money_get<_CharT, _Ip> _Fp; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 312 | ios_base::iostate __err = ios_base::goodbit; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 313 | const _Fp& __mf = use_facet<_Fp>(__is.getloc()); |
| 314 | __mf.get(_Ip(__is), _Ip(), __x.__intl_, __is, __err, __x.__mon_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 315 | __is.setstate(__err); |
| 316 | } |
| 317 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 318 | } |
| 319 | catch (...) |
| 320 | { |
| 321 | __is.__set_badbit_and_consider_rethrow(); |
| 322 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 323 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 324 | return __is; |
| 325 | } |
| 326 | |
| 327 | template <class _MoneyT> |
| 328 | inline _LIBCPP_INLINE_VISIBILITY |
| 329 | __iom_t7<_MoneyT> |
| 330 | get_money(_MoneyT& __mon, bool __intl = false) |
| 331 | { |
| 332 | return __iom_t7<_MoneyT>(__mon, __intl); |
| 333 | } |
| 334 | |
| 335 | // put_money |
| 336 | |
| 337 | template <class _MoneyT> class __iom_t8; |
| 338 | |
| 339 | template <class _CharT, class _Traits, class _MoneyT> |
| 340 | basic_ostream<_CharT, _Traits>& |
| 341 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x); |
| 342 | |
| 343 | template <class _MoneyT> |
| 344 | class __iom_t8 |
| 345 | { |
| 346 | const _MoneyT& __mon_; |
| 347 | bool __intl_; |
| 348 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 349 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 350 | __iom_t8(const _MoneyT& __mon, bool __intl) |
| 351 | : __mon_(__mon), __intl_(__intl) {} |
| 352 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 353 | template <class _CharT, class _Traits, class _Mp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 354 | friend |
| 355 | basic_ostream<_CharT, _Traits>& |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 356 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | template <class _CharT, class _Traits, class _MoneyT> |
| 360 | basic_ostream<_CharT, _Traits>& |
| 361 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x) |
| 362 | { |
| 363 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 364 | try |
| 365 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 366 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 367 | typename basic_ostream<_CharT, _Traits>::sentry __s(__os); |
| 368 | if (__s) |
| 369 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 370 | typedef ostreambuf_iterator<_CharT, _Traits> _Op; |
| 371 | typedef money_put<_CharT, _Op> _Fp; |
| 372 | const _Fp& __mf = use_facet<_Fp>(__os.getloc()); |
| 373 | if (__mf.put(_Op(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed()) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 374 | __os.setstate(ios_base::badbit); |
| 375 | } |
| 376 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 377 | } |
| 378 | catch (...) |
| 379 | { |
| 380 | __os.__set_badbit_and_consider_rethrow(); |
| 381 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 382 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 383 | return __os; |
| 384 | } |
| 385 | |
| 386 | template <class _MoneyT> |
| 387 | inline _LIBCPP_INLINE_VISIBILITY |
| 388 | __iom_t8<_MoneyT> |
| 389 | put_money(const _MoneyT& __mon, bool __intl = false) |
| 390 | { |
| 391 | return __iom_t8<_MoneyT>(__mon, __intl); |
| 392 | } |
| 393 | |
| 394 | // get_time |
| 395 | |
| 396 | template <class _CharT> class __iom_t9; |
| 397 | |
| 398 | template <class _CharT, class _Traits> |
| 399 | basic_istream<_CharT, _Traits>& |
| 400 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x); |
| 401 | |
| 402 | template <class _CharT> |
| 403 | class __iom_t9 |
| 404 | { |
| 405 | tm* __tm_; |
| 406 | const _CharT* __fmt_; |
| 407 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 408 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 409 | __iom_t9(tm* __tm, const _CharT* __fmt) |
| 410 | : __tm_(__tm), __fmt_(__fmt) {} |
| 411 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 412 | template <class _Cp, class _Traits> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 413 | friend |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 414 | basic_istream<_Cp, _Traits>& |
| 415 | operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 416 | }; |
| 417 | |
| 418 | template <class _CharT, class _Traits> |
| 419 | basic_istream<_CharT, _Traits>& |
| 420 | operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x) |
| 421 | { |
| 422 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 423 | try |
| 424 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 425 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 426 | typename basic_istream<_CharT, _Traits>::sentry __s(__is); |
| 427 | if (__s) |
| 428 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 429 | typedef istreambuf_iterator<_CharT, _Traits> _Ip; |
| 430 | typedef time_get<_CharT, _Ip> _Fp; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 431 | ios_base::iostate __err = ios_base::goodbit; |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 432 | const _Fp& __tf = use_facet<_Fp>(__is.getloc()); |
| 433 | __tf.get(_Ip(__is), _Ip(), __is, __err, __x.__tm_, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 434 | __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)); |
| 435 | __is.setstate(__err); |
| 436 | } |
| 437 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 438 | } |
| 439 | catch (...) |
| 440 | { |
| 441 | __is.__set_badbit_and_consider_rethrow(); |
| 442 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 443 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 444 | return __is; |
| 445 | } |
| 446 | |
| 447 | template <class _CharT> |
| 448 | inline _LIBCPP_INLINE_VISIBILITY |
| 449 | __iom_t9<_CharT> |
| 450 | get_time(tm* __tm, const _CharT* __fmt) |
| 451 | { |
| 452 | return __iom_t9<_CharT>(__tm, __fmt); |
| 453 | } |
| 454 | |
| 455 | // put_time |
| 456 | |
| 457 | template <class _CharT> class __iom_t10; |
| 458 | |
| 459 | template <class _CharT, class _Traits> |
| 460 | basic_ostream<_CharT, _Traits>& |
| 461 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x); |
| 462 | |
| 463 | template <class _CharT> |
| 464 | class __iom_t10 |
| 465 | { |
| 466 | const tm* __tm_; |
| 467 | const _CharT* __fmt_; |
| 468 | public: |
Howard Hinnant | 64da260 | 2010-09-22 15:29:08 +0000 | [diff] [blame] | 469 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 470 | __iom_t10(const tm* __tm, const _CharT* __fmt) |
| 471 | : __tm_(__tm), __fmt_(__fmt) {} |
| 472 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 473 | template <class _Cp, class _Traits> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 474 | friend |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 475 | basic_ostream<_Cp, _Traits>& |
| 476 | operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | template <class _CharT, class _Traits> |
| 480 | basic_ostream<_CharT, _Traits>& |
| 481 | operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x) |
| 482 | { |
| 483 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 484 | try |
| 485 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 486 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 487 | typename basic_ostream<_CharT, _Traits>::sentry __s(__os); |
| 488 | if (__s) |
| 489 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 490 | typedef ostreambuf_iterator<_CharT, _Traits> _Op; |
| 491 | typedef time_put<_CharT, _Op> _Fp; |
| 492 | const _Fp& __tf = use_facet<_Fp>(__os.getloc()); |
| 493 | if (__tf.put(_Op(__os), __os, __os.fill(), __x.__tm_, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 494 | __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)).failed()) |
| 495 | __os.setstate(ios_base::badbit); |
| 496 | } |
| 497 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 498 | } |
| 499 | catch (...) |
| 500 | { |
| 501 | __os.__set_badbit_and_consider_rethrow(); |
| 502 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 503 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 504 | return __os; |
| 505 | } |
| 506 | |
| 507 | template <class _CharT> |
| 508 | inline _LIBCPP_INLINE_VISIBILITY |
| 509 | __iom_t10<_CharT> |
| 510 | put_time(const tm* __tm, const _CharT* __fmt) |
| 511 | { |
| 512 | return __iom_t10<_CharT>(__tm, __fmt); |
| 513 | } |
| 514 | |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 515 | template <class _CharT, class _Traits, class _ForwardIterator> |
| 516 | std::basic_ostream<_CharT, _Traits> & |
| 517 | __quoted_output ( basic_ostream<_CharT, _Traits> &__os, |
| 518 | _ForwardIterator __first, _ForwardIterator __last, _CharT __delim, _CharT __escape ) |
| 519 | { |
Marshall Clow | 3d03676 | 2014-03-07 21:45:32 +0000 | [diff] [blame] | 520 | _VSTD::basic_string<_CharT, _Traits> __str; |
| 521 | __str.push_back(__delim); |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 522 | for ( ; __first != __last; ++ __first ) |
| 523 | { |
| 524 | if (_Traits::eq (*__first, __escape) || _Traits::eq (*__first, __delim)) |
Marshall Clow | 3d03676 | 2014-03-07 21:45:32 +0000 | [diff] [blame] | 525 | __str.push_back(__escape); |
| 526 | __str.push_back(*__first); |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 527 | } |
Marshall Clow | 3d03676 | 2014-03-07 21:45:32 +0000 | [diff] [blame] | 528 | __str.push_back(__delim); |
| 529 | return __put_character_sequence(__os, __str.data(), __str.size()); |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | template <class _CharT, class _Traits, class _String> |
| 533 | basic_istream<_CharT, _Traits> & |
| 534 | __quoted_input ( basic_istream<_CharT, _Traits> &__is, _String & __string, _CharT __delim, _CharT __escape ) |
| 535 | { |
| 536 | __string.clear (); |
| 537 | _CharT __c; |
| 538 | __is >> __c; |
| 539 | if ( __is.fail ()) |
| 540 | return __is; |
| 541 | |
| 542 | if (!_Traits::eq (__c, __delim)) // no delimiter, read the whole string |
| 543 | { |
| 544 | __is.unget (); |
| 545 | __is >> __string; |
| 546 | return __is; |
| 547 | } |
| 548 | |
| 549 | __save_flags<_CharT, _Traits> sf(__is); |
| 550 | noskipws (__is); |
| 551 | while (true) |
| 552 | { |
| 553 | __is >> __c; |
| 554 | if ( __is.fail ()) |
| 555 | break; |
| 556 | if (_Traits::eq (__c, __escape)) |
| 557 | { |
| 558 | __is >> __c; |
| 559 | if ( __is.fail ()) |
| 560 | break; |
| 561 | } |
| 562 | else if (_Traits::eq (__c, __delim)) |
| 563 | break; |
| 564 | __string.push_back ( __c ); |
| 565 | } |
| 566 | return __is; |
| 567 | } |
| 568 | |
| 569 | |
| 570 | template <class _CharT, class _Iter, class _Traits=char_traits<_CharT>> |
| 571 | struct __quoted_output_proxy |
| 572 | { |
| 573 | _Iter __first; |
| 574 | _Iter __last; |
| 575 | _CharT __delim; |
| 576 | _CharT __escape; |
| 577 | |
| 578 | __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e) |
| 579 | : __first(__f), __last(__l), __delim(__d), __escape(__e) {} |
| 580 | // This would be a nice place for a string_ref |
| 581 | }; |
| 582 | |
| 583 | template <class _CharT, class _Traits, class _Iter> |
| 584 | basic_ostream<_CharT, _Traits>& operator<<( |
| 585 | basic_ostream<_CharT, _Traits>& __os, |
| 586 | const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy) |
| 587 | { |
| 588 | return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape); |
| 589 | } |
| 590 | |
| 591 | template <class _CharT, class _Traits, class _Allocator> |
| 592 | struct __quoted_proxy |
| 593 | { |
| 594 | basic_string<_CharT, _Traits, _Allocator> &__string; |
| 595 | _CharT __delim; |
| 596 | _CharT __escape; |
| 597 | |
| 598 | __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e) |
| 599 | : __string(__s), __delim(__d), __escape(__e) {} |
| 600 | }; |
| 601 | |
| 602 | template <class _CharT, class _Traits, class _Allocator> |
| 603 | _LIBCPP_INLINE_VISIBILITY |
| 604 | basic_ostream<_CharT, _Traits>& operator<<( |
| 605 | basic_ostream<_CharT, _Traits>& __os, |
| 606 | const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy) |
| 607 | { |
Marshall Clow | 096d579 | 2013-11-14 20:01:38 +0000 | [diff] [blame] | 608 | return __quoted_output (__os, __proxy.__string.cbegin (), __proxy.__string.cend (), __proxy.__delim, __proxy.__escape); |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // extractor for non-const basic_string& proxies |
| 612 | template <class _CharT, class _Traits, class _Allocator> |
| 613 | _LIBCPP_INLINE_VISIBILITY |
| 614 | basic_istream<_CharT, _Traits>& operator>>( |
| 615 | basic_istream<_CharT, _Traits>& __is, |
| 616 | const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy) |
| 617 | { |
| 618 | return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape ); |
| 619 | } |
| 620 | |
| 621 | |
| 622 | template <class _CharT> |
| 623 | _LIBCPP_INLINE_VISIBILITY |
| 624 | __quoted_output_proxy<_CharT, const _CharT *> |
| 625 | quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\')) |
| 626 | { |
| 627 | const _CharT *__end = __s; |
| 628 | while ( *__end ) ++__end; |
| 629 | return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape ); |
| 630 | } |
| 631 | |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame^] | 632 | |
| 633 | template <class _CharT, class _Traits, class _Allocator> |
| 634 | _LIBCPP_INLINE_VISIBILITY |
| 635 | __quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator> |
| 636 | __quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\')) |
| 637 | { |
| 638 | return __quoted_output_proxy<_CharT, |
| 639 | typename basic_string <_CharT, _Traits, _Allocator>::const_iterator> |
| 640 | ( __s.cbegin(), __s.cend (), __delim, __escape ); |
| 641 | } |
| 642 | |
| 643 | template <class _CharT, class _Traits, class _Allocator> |
| 644 | _LIBCPP_INLINE_VISIBILITY |
| 645 | __quoted_proxy<_CharT, _Traits, _Allocator> |
| 646 | __quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\')) |
| 647 | { |
| 648 | return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape ); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | #if _LIBCPP_STD_VER > 11 |
| 653 | |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 654 | template <class _CharT, class _Traits, class _Allocator> |
| 655 | _LIBCPP_INLINE_VISIBILITY |
| 656 | __quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator> |
| 657 | quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\')) |
| 658 | { |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame^] | 659 | return __quoted(__s, __delim, __escape); |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | template <class _CharT, class _Traits, class _Allocator> |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame^] | 663 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 664 | __quoted_proxy<_CharT, _Traits, _Allocator> |
| 665 | quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\')) |
| 666 | { |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame^] | 667 | return __quoted(__s, __delim, __escape); |
Marshall Clow | f38a298 | 2013-09-05 04:48:45 +0000 | [diff] [blame] | 668 | } |
| 669 | #endif |
| 670 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 671 | _LIBCPP_END_NAMESPACE_STD |
| 672 | |
| 673 | #endif // _LIBCPP_IOMANIP |