lemon/bits/item_reader.h
changeset 1869 52f5a7f9fb48
parent 1744 51d5d41e15b1
child 1875 98698b69a902
equal deleted inserted replaced
1:b1584baeb800 2:fff208e4dd73
   178 
   178 
   179     ItemReader item_reader;
   179     ItemReader item_reader;
   180 
   180 
   181   public:
   181   public:
   182 
   182 
       
   183     /// \brief Constructor for InsertReader
       
   184     ///
       
   185     /// Constructor for InsertReader
       
   186     PushBackReader(const ItemReader& _item_reader = ItemReader())
       
   187       : item_reader(_item_reader) {}
       
   188 
   183     /// \brief Reads the values into the container from the given stream.
   189     /// \brief Reads the values into the container from the given stream.
   184     ///
   190     ///
   185     /// Reads the values into the container from the given stream.
   191     /// Reads the values into the container from the given stream.
   186     void read(std::istream& is, Value& value) const {
   192     void read(std::istream& is, Value& value) const {
   187       char c;
   193       char c;
   192 	typename ItemReader::Value item;
   198 	typename ItemReader::Value item;
   193 	item_reader.read(is, item);
   199 	item_reader.read(is, item);
   194 	value.push_back(item);
   200 	value.push_back(item);
   195       }
   201       }
   196       if (!is) throw DataFormatError("PushBackReader format error");
   202       if (!is) throw DataFormatError("PushBackReader format error");
   197       is.putback(c);
       
   198     }
   203     }
   199 
   204 
   200   };
   205   };
   201 
   206 
   202   /// \ingroup item_io
   207   /// \ingroup item_io
   220   private:
   225   private:
   221 
   226 
   222     ItemReader item_reader;
   227     ItemReader item_reader;
   223 
   228 
   224   public:
   229   public:
       
   230 
       
   231     /// \brief Constructor for InsertReader
       
   232     ///
       
   233     /// Constructor for InsertReader
       
   234     InsertReader(const ItemReader& _item_reader = ItemReader())
       
   235       : item_reader(_item_reader) {}
   225 
   236 
   226     /// \brief Reads the values into the container from the given stream.
   237     /// \brief Reads the values into the container from the given stream.
   227     ///
   238     ///
   228     /// Reads the values into the container from the given stream.
   239     /// Reads the values into the container from the given stream.
   229     void read(std::istream& is, Value& value) const {
   240     void read(std::istream& is, Value& value) const {
   235 	typename ItemReader::Value item;
   246 	typename ItemReader::Value item;
   236 	item_reader.read(is, item);
   247 	item_reader.read(is, item);
   237 	value.insert(item);
   248 	value.insert(item);
   238       }
   249       }
   239       if (!is) throw DataFormatError("PushBackReader format error");
   250       if (!is) throw DataFormatError("PushBackReader format error");
   240       is.putback(c);
       
   241     }
   251     }
   242 
   252 
   243   };
   253   };
   244 
   254 
   245   /// \ingroup item_io
   255   /// \ingroup item_io
   308     LineReader(bool _skipSpaces = true) : skipSpaces(_skipSpaces) {}
   318     LineReader(bool _skipSpaces = true) : skipSpaces(_skipSpaces) {}
   309     
   319     
   310     /// \brief Reads the line from the given stream.
   320     /// \brief Reads the line from the given stream.
   311     ///
   321     ///
   312     /// Reads the line from the given stream.
   322     /// Reads the line from the given stream.
   313     void read(std::istream& is, Value& value) {
   323     void read(std::istream& is, Value& value) const {
   314       if (skipSpaces) is >> std::ws;
   324       if (skipSpaces) is >> std::ws;
   315       if (!getline(is, value)) {
   325       if (!getline(is, value)) {
   316 	throw DataFormatError("LineReader forma error");
   326 	throw DataFormatError("LineReader format error");
   317       }
   327       }
   318     }
   328     }
   319   private:
   329   private:
   320     bool skipSpaces;
   330     bool skipSpaces;
       
   331   };
       
   332 
       
   333   /// \ingroup item_io
       
   334   /// \brief Reader for std::pair.
       
   335   ///
       
   336   /// Reader for std::pair.
       
   337   ///
       
   338   /// \author Balazs Dezso
       
   339   template <typename _Pair, 
       
   340 	    typename _FirstReader = 
       
   341 	    DefaultReader<typename _Pair::first_type>,
       
   342 	    typename _SecondReader = 
       
   343 	    DefaultReader<typename _Pair::second_type> >
       
   344   class PairReader {
       
   345   public:
       
   346     typedef _Pair Value;
       
   347 
       
   348     typedef _FirstReader FirstReader;
       
   349     typedef _SecondReader SecondReader;
       
   350 
       
   351   private:
       
   352 
       
   353     FirstReader first_reader;
       
   354     SecondReader second_reader;
       
   355 
       
   356   public:
       
   357     
       
   358     /// \brief Constructor.
       
   359     ///
       
   360     /// Constructor for the PairReader.
       
   361     PairReader(const FirstReader& _first_reader = FirstReader(), 
       
   362 	       const SecondReader& _second_reader = SecondReader()) 
       
   363       : first_reader(_first_reader), second_reader(_second_reader) {}
       
   364     
       
   365     /// \brief Reads the pair from the given stream.
       
   366     ///
       
   367     /// Reads the pair from the given stream.
       
   368     void read(std::istream& is, Value& value) const {
       
   369       char c;
       
   370       if (!(is >> c) || c != '(') {
       
   371 	throw DataFormatError("PairReader format error");
       
   372       }
       
   373       first_reader.read(is, value.first);
       
   374       if (!(is >> c) || c != '=') {
       
   375 	throw DataFormatError("PairReader format error");
       
   376       }
       
   377       if (!(is >> c) || c != '>') {
       
   378 	throw DataFormatError("PairReader format error");
       
   379       }
       
   380       second_reader.read(is, value.second);
       
   381       if (!(is >> c) || c != ')') {
       
   382 	throw DataFormatError("PairReader format error");
       
   383       }
       
   384     }
   321   };
   385   };
   322 
   386 
   323   /// \ingroup item_io
   387   /// \ingroup item_io
   324   /// 
   388   /// 
   325   /// \brief The default item reader template class.
   389   /// \brief The default item reader template class.
   387 
   451 
   388   template <typename Item>
   452   template <typename Item>
   389   class DefaultReader<std::set<Item> > 
   453   class DefaultReader<std::set<Item> > 
   390     : public InsertReader<std::set<Item> > {};
   454     : public InsertReader<std::set<Item> > {};
   391 
   455 
       
   456   template <typename Key, typename Value>
       
   457   class DefaultReader<std::map<Key, Value> > 
       
   458     : public InsertReader<std::map<Key, Value>,
       
   459 			  DefaultReader<std::pair<Key, Value> > > {};
       
   460 
   392   template <typename Item>
   461   template <typename Item>
   393   class DefaultReader<std::multiset<Item> > 
   462   class DefaultReader<std::multiset<Item> > 
   394     : public InsertReader<std::multiset<Item> > {};
   463     : public InsertReader<std::multiset<Item> > {};
   395 
   464 
       
   465   template <typename Key, typename Value>
       
   466   class DefaultReader<std::multimap<Key, Value> > 
       
   467     : public InsertReader<std::multimap<Key, Value>,
       
   468 			  DefaultReader<std::pair<Key, Value> > > {};
       
   469 
       
   470   template <typename First, typename Second>
       
   471   class DefaultReader<std::pair<First, Second> > 
       
   472     : public PairReader<std::pair<First, Second> > {};
       
   473 
   396   /// \ingroup item_io
   474   /// \ingroup item_io
   397   /// 
   475   /// 
   398   /// \brief The default item reader for skipping a value in the stream.
   476   /// \brief The default item reader for skipping a value in the stream.
   399   ///
   477   ///
   400   /// The default item reader for skipping a value in the stream.
   478   /// The default item reader for skipping a value in the stream.