| 1 | /* boost random/vector_io.hpp header file |
| 2 | * |
| 3 | * Copyright Steven Watanabe 2011 |
| 4 | * Distributed under the Boost Software License, Version 1.0. (See |
| 5 | * accompanying file LICENSE_1_0.txt or copy at |
| 6 | * http://www.boost.org/LICENSE_1_0.txt) |
| 7 | * |
| 8 | * See http://www.boost.org for most recent version including documentation. |
| 9 | * |
| 10 | * $Id$ |
| 11 | */ |
| 12 | |
| 13 | #ifndef BOOST_RANDOM_DETAIL_VECTOR_IO_HPP |
| 14 | #define BOOST_RANDOM_DETAIL_VECTOR_IO_HPP |
| 15 | |
| 16 | #include <vector> |
| 17 | #include <iosfwd> |
| 18 | #include <istream> |
| 19 | |
| 20 | namespace boost { |
| 21 | namespace random { |
| 22 | namespace detail { |
| 23 | |
| 24 | template<class CharT, class Traits, class T> |
| 25 | void print_vector(std::basic_ostream<CharT, Traits>& os, |
| 26 | const std::vector<T>& vec) |
| 27 | { |
| 28 | typename std::vector<T>::const_iterator |
| 29 | iter = vec.begin(), |
| 30 | end = vec.end(); |
| 31 | os << os.widen('['); |
| 32 | if(iter != end) { |
| 33 | os << *iter; |
| 34 | ++iter; |
| 35 | for(; iter != end; ++iter) |
| 36 | { |
| 37 | os << os.widen(' ') << *iter; |
| 38 | } |
| 39 | } |
| 40 | os << os.widen(']'); |
| 41 | } |
| 42 | |
| 43 | template<class CharT, class Traits, class T> |
| 44 | void read_vector(std::basic_istream<CharT, Traits>& is, std::vector<T>& vec) |
| 45 | { |
| 46 | CharT ch; |
| 47 | if(!(is >> ch)) { |
| 48 | return; |
| 49 | } |
| 50 | if(ch != is.widen('[')) { |
| 51 | is.putback(ch); |
| 52 | is.setstate(std::ios_base::failbit); |
| 53 | return; |
| 54 | } |
| 55 | T val; |
| 56 | while(is >> std::ws >> val) { |
| 57 | vec.push_back(val); |
| 58 | } |
| 59 | if(is.fail()) { |
| 60 | is.clear(); |
| 61 | if(!(is >> ch)) { |
| 62 | return; |
| 63 | } |
| 64 | if(ch != is.widen(']')) { |
| 65 | is.putback(ch); |
| 66 | is.setstate(std::ios_base::failbit); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #endif // BOOST_RANDOM_DETAIL_VECTOR_IO_HPP |
| 76 | |