deba@1408: /* -*- C++ -*- deba@1408: * src/lemon/lemon_reader.h - Part of LEMON, a generic C++ optimization library deba@1408: * deba@1408: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@1408: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@1408: * deba@1408: * Permission to use, modify and distribute this software is granted deba@1408: * provided that this copyright notice appears in all copies. For deba@1408: * precise terms see the accompanying LICENSE file. deba@1408: * deba@1408: * This software is provided "AS IS" with no warranty of any kind, deba@1408: * express or implied, and with no claim as to its suitability for any deba@1408: * purpose. deba@1408: * deba@1408: */ deba@1408: deba@1408: ///\ingroup io_group deba@1408: ///\file deba@1408: ///\brief Lemon Format reader. deba@1408: deba@1408: #ifndef LEMON_LEMON_READER_H deba@1408: #define LEMON_LEMON_READER_H deba@1408: deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: #include deba@1408: deba@1408: #include deba@1409: #include deba@1408: deba@1408: deba@1408: namespace lemon { deba@1408: deba@1409: /// \ingroup io_group deba@1408: /// \brief Lemon Format reader class. deba@1408: /// deba@1409: /// The Lemon Format contains several sections. We do not want to deba@1409: /// determine what sections are in a lemon file we give only a framework deba@1409: /// to read a section oriented format. deba@1409: /// deba@1409: /// In the Lemon Format each section starts with a line contains a \c \@ deba@1409: /// character on the first not white space position. This line is the deba@1409: /// header line of the section. Each next lines belong to this section deba@1409: /// while it does not starts with \c \@ character. This line can start a deba@1409: /// new section or if it can close the file with the \c \@end line. deba@1409: /// The file format ignore the empty lines and it may contain comments deba@1409: /// started with a \c # character to the end of the line. deba@1409: /// deba@1409: /// The framework provides an abstract LemonReader::SectionReader class deba@1409: /// what defines the interface of a SectionReader. The SectionReader deba@1409: /// has the \c header() member function what get a header line string and deba@1409: /// decides if it want to process the next section. Several SectionReaders deba@1409: /// can be attached to an LemonReader and the first attached what can deba@1409: /// process the section will be used. Its \c read() member will called deba@1409: /// with a stream contains the section. From this stream the empty lines deba@1409: /// and comments are filtered out. deba@1409: /// deba@1409: /// \relates GraphReader deba@1409: /// \relates NodeSetReader deba@1409: /// \relates EdgeSetReader deba@1409: /// \relates NodesReader deba@1409: /// \relates EdgesReader deba@1409: /// \relates AttributeReader deba@1408: class LemonReader { deba@1408: private: deba@1408: deba@1408: class FilterStreamBuf : public std::streambuf { deba@1408: public: deba@1408: deba@1408: typedef std::streambuf Parent; deba@1408: typedef Parent::char_type char_type; deba@1408: FilterStreamBuf(std::istream& is, int& num) deba@1408: : _is(is), _base(0), _eptr(0), deba@1408: _num(num), skip_state(after_endl) {} deba@1408: deba@1408: protected: deba@1408: deba@1408: enum skip_state_type { deba@1408: no_skip, deba@1408: after_comment, deba@1408: after_endl, deba@1408: empty_line deba@1408: }; deba@1408: deba@1408: char_type small_buf[1]; deba@1408: deba@1408: deba@1408: std::istream& _is; deba@1408: deba@1408: char_type* _base; deba@1408: char_type* _eptr; deba@1408: deba@1408: int& _num; deba@1408: deba@1408: skip_state_type skip_state; deba@1408: deba@1408: deba@1408: char_type* base() { return _base; } deba@1408: deba@1408: char_type* eptr() { return _eptr; } deba@1408: deba@1408: int blen() { return _eptr - _base; } deba@1408: deba@1408: void setb(char_type* buf, int len) { deba@1408: _base = buf; deba@1408: _eptr = buf + len; deba@1408: } deba@1408: deba@1408: virtual std::streambuf* setbuf(char *buf, int len) { deba@1408: if (base()) return 0; deba@1408: if (buf != 0 && len >= (int)sizeof(small_buf)) { deba@1408: setb(buf, len); deba@1408: } else { deba@1408: setb(small_buf, sizeof(small_buf)); deba@1408: } deba@1408: setg(0, 0, 0); deba@1408: return this; deba@1408: } deba@1408: deba@1408: bool put_char(char c) { deba@1408: switch (skip_state) { deba@1408: case no_skip: deba@1408: switch (c) { deba@1408: case '\n': deba@1408: skip_state = after_endl; deba@1408: return true; deba@1408: case '#': deba@1408: skip_state = after_comment; deba@1408: return false; deba@1408: default: deba@1408: return true; deba@1408: } deba@1408: case after_comment: deba@1408: switch (c) { deba@1408: case '\n': deba@1408: skip_state = after_endl; deba@1408: return true; deba@1408: default: deba@1408: return false; deba@1408: } deba@1408: case after_endl: deba@1408: switch (c) { deba@1408: case '@': deba@1408: return false; deba@1408: case '\n': deba@1408: return false; deba@1408: case '#': deba@1408: skip_state = empty_line; deba@1408: return false; deba@1408: default: deba@1408: if (!isspace(c)) { deba@1408: skip_state = no_skip; deba@1408: return true; deba@1408: } else { deba@1408: return false; deba@1408: } deba@1408: } deba@1408: break; deba@1408: case empty_line: deba@1408: switch (c) { deba@1408: case '\n': deba@1408: skip_state = after_endl; deba@1408: return false; deba@1408: default: deba@1408: return false; deba@1408: } deba@1408: } deba@1408: return false; deba@1408: } deba@1408: deba@1408: virtual int underflow() { deba@1408: char c; deba@1408: if (_is.read(&c, 1)) { deba@1408: _is.putback(c); deba@1408: if (c == '@') { deba@1408: return EOF; deba@1408: } deba@1408: } else { deba@1408: return EOF; deba@1408: } deba@1408: char_type *ptr; deba@1408: for (ptr = base(); ptr != eptr(); ++ptr) { deba@1408: if (_is.read(&c, 1)) { deba@1408: if (c == '\n') ++_num; deba@1408: if (put_char(c)) { deba@1408: *ptr = c; deba@1408: } else { deba@1408: if (skip_state == after_endl && c == '@') { deba@1408: _is.putback('@'); deba@1408: break; deba@1408: } deba@1408: --ptr; deba@1408: } deba@1408: } else { deba@1408: break; deba@1408: } deba@1408: } deba@1408: setg(base(), base(), ptr); deba@1408: return *base(); deba@1408: } deba@1408: deba@1408: virtual int sync() { deba@1408: return EOF; deba@1408: } deba@1408: }; deba@1408: deba@1408: public: deba@1408: deba@1409: /// \brief Abstract base class for reading a section. deba@1409: /// deba@1409: /// This class has an \c header() member function what get a deba@1409: /// header line string and decides if it want to process the next deba@1409: /// section. Several SectionReaders can be attached to an LemonReader deba@1409: /// and the first attached what can process the section will be used. deba@1409: /// Its \c read() member will called with a stream contains the section. deba@1409: /// From this stream the empty lines and comments are filtered out. deba@1408: class SectionReader { deba@1409: friend class LemonReader; deba@1409: protected: deba@1409: /// \brief Constructor for SectionReader. deba@1409: /// deba@1409: /// Constructor for SectionReader. It attach this reader to deba@1409: /// the given LemonReader. deba@1409: SectionReader(LemonReader& reader) { deba@1409: reader.attach(*this); deba@1409: } deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1409: /// It gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1408: virtual bool header(const std::string& line) = 0; deba@1409: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1408: virtual void read(std::istream& is) = 0; deba@1408: }; deba@1408: deba@1409: /// \brief Constructor for LemonReader. deba@1409: /// deba@1409: /// Constructor for LemonReader which reads from the given stream. deba@1408: LemonReader(std::istream& _is) deba@1408: : is(&_is), own_is(false) {} deba@1408: deba@1409: /// \brief Constructor for LemonReader. deba@1409: /// deba@1409: /// Constructor for LemonReader which reads from the given file. deba@1408: LemonReader(const std::string& filename) deba@1408: : is(0), own_is(true) { deba@1408: is = new std::ifstream(filename.c_str()); deba@1408: } deba@1408: deba@1409: /// \brief Desctructor for LemonReader. deba@1409: /// deba@1409: /// Desctructor for LemonReader. deba@1408: ~LemonReader() { deba@1408: if (own_is) { deba@1408: delete is; deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: LemonReader(const LemonReader&); deba@1408: void operator=(const LemonReader&); deba@1408: deba@1408: void attach(SectionReader& reader) { deba@1408: readers.push_back(&reader); deba@1408: } deba@1408: deba@1409: public: deba@1409: /// \brief Executes the LemonReader. deba@1409: /// deba@1409: /// It executes the LemonReader. deba@1408: void run() { deba@1408: int line_num = 0; deba@1408: std::string line; deba@1408: try { deba@1408: while ((++line_num, getline(*is, line)) && line.find("@end") != 0) { deba@1408: SectionReaders::iterator it; deba@1408: for (it = readers.begin(); it != readers.end(); ++it) { deba@1408: if ((*it)->header(line)) { deba@1408: char buf[2048]; deba@1408: FilterStreamBuf buffer(*is, line_num); deba@1408: buffer.pubsetbuf(buf, sizeof(buf)); deba@1408: std::istream is(&buffer); deba@1408: (*it)->read(is); deba@1408: break; deba@1408: } deba@1408: } deba@1408: } deba@1408: } catch (DataFormatError& error) { deba@1408: error.line(line_num); deba@1408: throw error; deba@1408: } deba@1408: } deba@1408: deba@1408: deba@1408: private: deba@1408: deba@1408: std::istream* is; deba@1408: bool own_is; deba@1408: deba@1408: typedef std::vector SectionReaders; deba@1408: SectionReaders readers; deba@1408: deba@1408: }; deba@1408: deba@1409: /// \brief Helper class for implementing the common SectionReaders. deba@1409: /// deba@1409: /// Helper class for implementing the common SectionReaders. deba@1409: class CommonSectionReaderBase : public LemonReader::SectionReader { deba@1409: typedef LemonReader::SectionReader Parent; deba@1409: protected: deba@1409: deba@1409: /// \brief Constructor for CommonSectionReaderBase. deba@1409: /// deba@1409: /// Constructor for CommonSectionReaderBase. It attach this reader to deba@1409: /// the given LemonReader. deba@1409: CommonSectionReaderBase(LemonReader& _reader) deba@1409: : Parent(_reader) {} deba@1408: deba@1408: template deba@1408: class ReaderBase; deba@1408: deba@1408: template deba@1408: class InverterBase : public ReaderBase<_Item> { deba@1408: public: deba@1408: typedef _Item Item; deba@1408: virtual void read(std::istream&, const Item&) = 0; deba@1408: virtual Item read(std::istream&) const = 0; deba@1408: deba@1408: virtual InverterBase<_Item>* getInverter() { deba@1408: return this; deba@1408: } deba@1408: deba@1408: deba@1408: }; deba@1408: deba@1408: template deba@1408: class MapReaderInverter : public InverterBase<_Item> { deba@1408: public: deba@1408: typedef _Item Item; deba@1408: typedef _Reader Reader; deba@1408: typedef typename Reader::Value Value; deba@1408: typedef _Map Map; deba@1408: typedef std::map Inverse; deba@1408: deba@1408: Map& map; deba@1408: Reader reader; deba@1408: Inverse inverse; deba@1408: deba@1408: MapReaderInverter(Map& _map, const Reader& _reader) deba@1408: : map(_map), reader(_reader) {} deba@1408: deba@1408: virtual ~MapReaderInverter() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item& item) { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: map.set(item, value); deba@1408: typename Inverse::iterator it = inverse.find(value); deba@1408: if (it == inverse.end()) { deba@1408: inverse.insert(std::make_pair(value, item)); deba@1408: } else { deba@1408: throw DataFormatError("Multiple ID occurence"); deba@1408: } deba@1408: } deba@1408: deba@1408: virtual Item read(std::istream& is) const { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: typename Inverse::const_iterator it = inverse.find(value); deba@1408: if (it != inverse.end()) { deba@1408: return it->second; deba@1408: } else { deba@1408: throw DataFormatError("Invalid ID error"); deba@1408: } deba@1408: } deba@1408: deba@1408: }; deba@1408: deba@1408: template deba@1408: class SkipReaderInverter : public InverterBase<_Item> { deba@1408: public: deba@1408: typedef _Item Item; deba@1408: typedef _Reader Reader; deba@1408: typedef typename Reader::Value Value; deba@1408: typedef std::map Inverse; deba@1408: deba@1408: Reader reader; deba@1408: deba@1408: SkipReaderInverter(const Reader& _reader) deba@1408: : reader(_reader) {} deba@1408: deba@1408: virtual ~SkipReaderInverter() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item& item) { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: typename Inverse::iterator it = inverse.find(value); deba@1408: if (it == inverse.end()) { deba@1408: inverse.insert(std::make_pair(value, item)); deba@1408: } else { deba@1408: throw DataFormatError("Multiple ID occurence error"); deba@1408: } deba@1408: } deba@1408: deba@1408: virtual Item read(std::istream& is) const { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: typename Inverse::const_iterator it = inverse.find(value); deba@1408: if (it != inverse.end()) { deba@1408: return it->second; deba@1408: } else { deba@1408: throw DataFormatError("Invalid ID error"); deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: Inverse inverse; deba@1408: }; deba@1408: deba@1408: // Readers deba@1408: deba@1408: template deba@1408: class ReaderBase { deba@1408: public: deba@1408: typedef _Item Item; deba@1408: deba@1408: virtual ~ReaderBase() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item& item) = 0; deba@1408: virtual InverterBase<_Item>* getInverter() = 0; deba@1408: }; deba@1408: deba@1408: template deba@1408: class MapReader : public ReaderBase<_Item> { deba@1408: public: deba@1408: typedef _Map Map; deba@1408: typedef _Reader Reader; deba@1408: typedef typename Reader::Value Value; deba@1408: typedef _Item Item; deba@1408: deba@1408: Map& map; deba@1408: Reader reader; deba@1408: deba@1408: MapReader(Map& _map, const Reader& _reader) deba@1408: : map(_map), reader(_reader) {} deba@1408: deba@1408: virtual ~MapReader() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item& item) { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: map.set(item, value); deba@1408: } deba@1408: deba@1408: virtual InverterBase<_Item>* getInverter() { deba@1408: return new MapReaderInverter(map, reader); deba@1408: } deba@1408: }; deba@1408: deba@1408: deba@1408: template deba@1408: class SkipReader : public ReaderBase<_Item> { deba@1408: public: deba@1408: typedef _Reader Reader; deba@1408: typedef typename Reader::Value Value; deba@1408: typedef _Item Item; deba@1408: deba@1408: Reader reader; deba@1408: SkipReader(const Reader& _reader) : reader(_reader) {} deba@1408: deba@1408: virtual ~SkipReader() {} deba@1408: deba@1408: virtual void read(std::istream& is, const Item&) { deba@1408: Value value; deba@1408: reader.read(is, value); deba@1408: } deba@1408: deba@1408: virtual InverterBase* getInverter() { deba@1408: return new SkipReaderInverter(reader); deba@1408: } deba@1408: }; deba@1408: deba@1408: template deba@1409: class IdReaderBase { deba@1408: public: deba@1408: typedef _Item Item; deba@1409: virtual Item read(std::istream& is) const = 0; deba@1408: }; deba@1408: deba@1409: template deba@1409: class IdReader : public IdReaderBase<_Item> { deba@1408: public: deba@1408: typedef _Item Item; deba@1409: typedef _BoxedIdReader BoxedIdReader; deba@1409: deba@1409: const BoxedIdReader& boxedIdReader; deba@1408: deba@1409: IdReader(const BoxedIdReader& _boxedIdReader) deba@1409: : boxedIdReader(_boxedIdReader) {} deba@1408: deba@1409: virtual Item read(std::istream& is) const { deba@1409: return boxedIdReader.readId(is); deba@1408: } deba@1408: }; deba@1408: deba@1408: class ValueReaderBase { deba@1408: public: deba@1408: virtual void read(std::istream&) {}; deba@1408: }; deba@1408: deba@1408: template deba@1408: class ValueReader : public ValueReaderBase { deba@1408: public: deba@1408: typedef _Value Value; deba@1408: typedef _Reader Reader; deba@1408: deba@1408: ValueReader(Value& _value, const Reader& _reader) deba@1408: : value(_value), reader(_reader) {} deba@1408: deba@1408: virtual void read(std::istream& is) { deba@1408: reader.read(is, value); deba@1408: } deba@1408: private: deba@1408: Value& value; deba@1408: Reader reader; deba@1408: }; deba@1408: deba@1408: }; deba@1408: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for reading a graph's nodeset. deba@1409: /// deba@1409: /// The lemon format can store multiple graph nodesets with several maps. deba@1409: /// The nodeset section's header line is \c \@nodeset \c nodeset_id, but the deba@1409: /// \c nodeset_id may be empty. deba@1409: /// deba@1409: /// The first line of the section contains the names of the maps separated deba@1409: /// with white spaces. Each next lines describes a node in the nodeset, and deba@1409: /// contains the mapped values for each map. deba@1409: /// deba@1409: /// If the nodeset contains an \c "id" named map then it will be regarded deba@1409: /// as id map. This map should contain only unique values and when the deba@1409: /// \c readId() member will read a value from the given stream it will deba@1409: /// give back that node which is mapped to this value. deba@1409: /// deba@1409: /// \relates LemonReader deba@1408: template deba@1408: class NodeSetReader : public CommonSectionReaderBase { deba@1408: typedef CommonSectionReaderBase Parent; deba@1408: public: deba@1408: deba@1408: typedef _Graph Graph; deba@1408: typedef _Traits Traits; deba@1408: typedef typename Graph::Node Item; deba@1408: typedef typename Traits::Skipper DefaultSkipper; deba@1408: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for NodeSetReader. It creates the NodeSetReader and deba@1409: /// attach it into the given LemonReader. The nodeset reader will deba@1409: /// add the readed nodes to the given Graph. The reader will read deba@1409: /// the section when the \c section_id and the \c _id are the same. deba@1408: NodeSetReader(LemonReader& _reader, Graph& _graph, deba@1408: const std::string& _id = std::string(), deba@1409: const DefaultSkipper& _skipper = DefaultSkipper()) deba@1409: : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {} deba@1408: deba@1409: deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for NodeSetReader. deba@1408: virtual ~NodeSetReader() { deba@1408: for (typename MapReaders::iterator it = readers.begin(); deba@1408: it != readers.end(); ++it) { deba@1408: delete it->second; deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: NodeSetReader(const NodeSetReader&); deba@1408: void operator=(const NodeSetReader&); deba@1408: deba@1408: public: deba@1408: deba@1408: /// \brief Add a new node map reader command for the reader. deba@1408: /// deba@1408: /// Add a new node map reader command for the reader. deba@1408: template deba@1408: NodeSetReader& readMap(std::string name, Map& map) { deba@1408: return readMap, Map>(name, map); deba@1408: } deba@1408: deba@1408: /// \brief Add a new node map reader command for the reader. deba@1408: /// deba@1408: /// Add a new node map reader command for the reader. deba@1408: template deba@1408: NodeSetReader& readMap(std::string name, Map& map, deba@1408: const Reader& reader = Reader()) { deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1408: msg << "Multiple read rule for node map: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1408: readers.insert( deba@1408: make_pair(name, new MapReader(map, reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1408: /// \brief Add a new node map skipper command for the reader. deba@1408: /// deba@1408: /// Add a new node map skipper command for the reader. deba@1408: template deba@1408: NodeSetReader& skipMap(std::string name, deba@1408: const Reader& reader = Reader()) { deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1408: msg << "Multiple read rule for node map: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1408: readers.insert(make_pair(name, new SkipReader(reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1409: /// It gives back true when the header line starts with \c @nodeset, deba@1409: /// and the header line's id and the nodeset's id are the same. deba@1408: virtual bool header(const std::string& line) { deba@1408: std::istringstream ls(line); deba@1408: std::string command; deba@1408: std::string name; deba@1408: ls >> command >> name; deba@1408: return command == "@nodeset" && name == id; deba@1408: } deba@1408: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1408: virtual void read(std::istream& is) { deba@1408: std::vector* > index; deba@1408: std::string line; deba@1408: deba@1408: getline(is, line); deba@1408: std::istringstream ls(line); deba@1408: while (ls >> id) { deba@1408: typename MapReaders::iterator it = readers.find(id); deba@1408: if (it != readers.end()) { deba@1408: index.push_back(it->second); deba@1408: } else { deba@1408: index.push_back(&skipper); deba@1408: } deba@1408: if (id == "id" && inverter.get() == 0) { deba@1408: inverter.reset(index.back()->getInverter()); deba@1408: index.back() = inverter.get(); deba@1408: } deba@1408: } deba@1408: while (getline(is, line)) { deba@1408: typename Graph::Node node = graph.addNode(); deba@1408: std::istringstream ls(line); deba@1408: for (int i = 0; i < (int)index.size(); ++i) { deba@1408: index[i]->read(ls, node); deba@1408: } deba@1408: } deba@1408: } deba@1408: deba@1409: public: deba@1409: deba@1409: /// \brief Returns true if the nodeset can give back the node by its id. deba@1409: /// deba@1409: /// Returns true if the nodeset can give back the node by its id. deba@1409: /// It is possible only if an "id" named map was read. deba@1409: bool isIdReader() const { deba@1408: return inverter.get() != 0; deba@1408: } deba@1408: deba@1409: /// \brief Gives back the node by its id. deba@1409: /// deba@1409: /// It reads an id from the stream and gives back which node belongs to deba@1409: /// it. It is possible only if there was read an "id" named map. deba@1409: typename Graph::Node readId(std::istream& is) const { deba@1408: return inverter->read(is); deba@1408: } deba@1408: deba@1408: private: deba@1408: deba@1408: typedef std::map*> MapReaders; deba@1408: MapReaders readers; deba@1408: deba@1408: Graph& graph; deba@1408: std::string id; deba@1408: SkipReader skipper; deba@1408: deba@1408: std::auto_ptr > inverter; deba@1408: }; deba@1408: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for reading a graph's edgeset. deba@1409: /// deba@1409: /// The lemon format can store multiple graph edgesets with several maps. deba@1409: /// The edgeset section's header line is \c \@edgeset \c edgeset_id, but the deba@1409: /// \c edgeset_id may be empty. deba@1409: /// deba@1409: /// The first line of the section contains the names of the maps separated deba@1409: /// with white spaces. Each next lines describes a node in the nodeset. The deba@1409: /// line contains the two nodes' id and the mapped values for each map. deba@1409: /// deba@1409: /// If the edgeset contains an \c "id" named map then it will be regarded deba@1409: /// as id map. This map should contain only unique values and when the deba@1409: /// \c readId() member will read a value from the given stream it will deba@1409: /// give back that edge which is mapped to this value. deba@1409: /// deba@1409: /// The edgeset reader needs a node id reader to identify which nodes deba@1409: /// have to be connected. If a NodeSetReader reads an "id" named map, deba@1409: /// it will be able to resolve the nodes by ids. deba@1409: /// deba@1409: /// \relates LemonReader deba@1408: template deba@1408: class EdgeSetReader : public CommonSectionReaderBase { deba@1408: typedef CommonSectionReaderBase Parent; deba@1408: public: deba@1408: deba@1408: typedef _Graph Graph; deba@1408: typedef _Traits Traits; deba@1408: typedef typename Graph::Edge Item; deba@1408: typedef typename Traits::Skipper DefaultSkipper; deba@1408: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for EdgeSetReader. It creates the EdgeSetReader and deba@1409: /// attach it into the given LemonReader. The edgeset reader will deba@1409: /// add the readed edges to the given Graph. It will use the given deba@1409: /// node id reader to read the source and target nodes of the edges. deba@1409: /// The reader will read the section only if the \c _id and the deba@1409: /// \c edgset_id are the same. deba@1409: template deba@1408: EdgeSetReader(LemonReader& _reader, Graph& _graph, deba@1409: const NodeIdReader& _nodeIdReader, deba@1408: const std::string& _id = std::string(), deba@1409: const DefaultSkipper& _skipper = DefaultSkipper()) deba@1409: : Parent(_reader), graph(_graph), id(_id), skipper(_skipper), deba@1409: nodeIdReader(new IdReader deba@1409: (_nodeIdReader)) {} deba@1408: deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for EdgeSetReader. deba@1408: virtual ~EdgeSetReader() { deba@1408: for (typename MapReaders::iterator it = readers.begin(); deba@1408: it != readers.end(); ++it) { deba@1408: delete it->second; deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: EdgeSetReader(const EdgeSetReader&); deba@1408: void operator=(const EdgeSetReader&); deba@1408: deba@1408: public: deba@1408: deba@1409: /// \brief Add a new edge map reader command for the reader. deba@1408: /// deba@1409: /// Add a new edge map reader command for the reader. deba@1408: template deba@1408: EdgeSetReader& readMap(std::string name, Map& map) { deba@1408: return readMap, Map>(name, map); deba@1408: } deba@1408: deba@1409: /// \brief Add a new edge map reader command for the reader. deba@1408: /// deba@1409: /// Add a new edge map reader command for the reader. deba@1408: template deba@1408: EdgeSetReader& readMap(std::string name, Map& map, deba@1408: const Reader& reader = Reader()) { deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1408: msg << "Multiple read rule for edge map: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1408: readers.insert( deba@1408: make_pair(name, new MapReader(map, reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1409: /// \brief Add a new edge map skipper command for the reader. deba@1408: /// deba@1409: /// Add a new edge map skipper command for the reader. deba@1408: template deba@1408: EdgeSetReader& skipMap(std::string name, deba@1408: const Reader& reader = Reader()) { deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1408: msg << "Multiple read rule for node map: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1408: readers.insert(make_pair(name, new SkipReader(reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1409: /// It gives back true when the header line starts with \c @edgeset, deba@1409: /// and the header line's id and the edgeset's id are the same. deba@1408: virtual bool header(const std::string& line) { deba@1408: std::istringstream ls(line); deba@1408: std::string command; deba@1408: std::string name; deba@1408: ls >> command >> name; deba@1408: return command == "@edgeset" && name == id; deba@1408: } deba@1408: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1408: virtual void read(std::istream& is) { deba@1408: std::vector* > index; deba@1408: std::string line; deba@1408: deba@1408: getline(is, line); deba@1408: std::istringstream ls(line); deba@1408: while (ls >> id) { deba@1408: typename MapReaders::iterator it = readers.find(id); deba@1408: if (it != readers.end()) { deba@1408: index.push_back(it->second); deba@1408: } else { deba@1408: index.push_back(&skipper); deba@1408: } deba@1408: if (id == "id" && inverter.get() == 0) { deba@1408: inverter.reset(index.back()->getInverter()); deba@1408: index.back() = inverter.get(); deba@1408: } deba@1408: } deba@1408: while (getline(is, line)) { deba@1408: std::istringstream ls(line); deba@1409: typename Graph::Node from = nodeIdReader->read(ls); deba@1409: typename Graph::Node to = nodeIdReader->read(ls); deba@1408: typename Graph::Edge edge = graph.addEdge(from, to); deba@1408: for (int i = 0; i < (int)index.size(); ++i) { deba@1408: index[i]->read(ls, edge); deba@1408: } deba@1408: } deba@1408: } deba@1408: deba@1409: public: deba@1409: deba@1409: /// \brief Returns true if the edgeset can give back the edge by its id. deba@1409: /// deba@1409: /// Returns true if the edgeset can give back the edge by its id. deba@1409: /// It is possible only if an "id" named map was read. deba@1409: bool isIdReader() const { deba@1408: return inverter.get() != 0; deba@1408: } deba@1408: deba@1409: /// \brief Gives back the edge by its id. deba@1409: /// deba@1409: /// It reads an id from the stream and gives back which edge belongs to deba@1409: /// it. It is possible only if there was read an "id" named map. deba@1409: typename Graph::Edge readId(std::istream& is) const { deba@1408: return inverter->read(is); deba@1408: } deba@1408: deba@1408: private: deba@1408: deba@1408: typedef std::map*> MapReaders; deba@1408: MapReaders readers; deba@1408: deba@1408: Graph& graph; deba@1408: std::string id; deba@1408: SkipReader skipper; deba@1408: deba@1408: std::auto_ptr > inverter; deba@1409: std::auto_ptr > nodeIdReader; deba@1408: }; deba@1408: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for reading labeled nodes. deba@1409: /// deba@1409: /// The nodes section's header line is \c \@nodes \c nodes_id, but the deba@1409: /// \c nodes_id may be empty. deba@1409: /// deba@1409: /// Each line in the section contains the name of the node deba@1409: /// and then the node id. deba@1409: /// deba@1409: /// \relates LemonReader deba@1409: template deba@1409: class NodeReader : public CommonSectionReaderBase { deba@1409: typedef CommonSectionReaderBase Parent; deba@1409: typedef _Graph Graph; deba@1409: typedef typename Graph::Node Item; deba@1409: public: deba@1409: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for NodeReader. It creates the NodeReader and deba@1409: /// attach it into the given LemonReader. It will use the given deba@1409: /// node id reader to give back the nodes. The reader will read the deba@1409: /// section only if the \c _id and the \c nodes_id are the same. deba@1409: template deba@1409: NodeReader(LemonReader& _reader, const _IdReader& _idReader, deba@1409: const std::string& _id = std::string()) deba@1409: : Parent(_reader), id(_id), deba@1409: idReader(new IdReader(_idReader)) {} deba@1408: deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for NodeReader. deba@1409: virtual ~NodeReader() {} deba@1409: deba@1409: private: deba@1409: NodeReader(const NodeReader&); deba@1409: void operator=(const NodeReader&); deba@1409: deba@1409: public: deba@1409: deba@1409: /// \brief Add a node reader command for the NodeReader. deba@1409: /// deba@1409: /// Add a node reader command for the NodeReader. deba@1409: void readNode(const std::string& name, Item& item) { deba@1409: if (readers.find(name) != readers.end()) { deba@1409: ErrorMessage msg; deba@1409: msg << "Multiple read rule for node: " << name; deba@1409: throw IOParameterError(msg.message()); deba@1409: } deba@1409: readers.insert(make_pair(name, &item)); deba@1409: } deba@1409: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1409: /// It gives back true when the header line start with \c @nodes, deba@1409: /// and the header line's id and the reader's id are the same. deba@1409: virtual bool header(const std::string& line) { deba@1409: std::istringstream ls(line); deba@1409: std::string command; deba@1409: std::string name; deba@1409: ls >> command >> name; deba@1409: return command == "@nodes" && name == id; deba@1409: } deba@1409: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1409: virtual void read(std::istream& is) { deba@1409: std::string line; deba@1409: while (getline(is, line)) { deba@1409: std::istringstream ls(line); deba@1409: std::string id; deba@1409: ls >> id; deba@1409: typename ItemReaders::iterator it = readers.find(id); deba@1409: if (it != readers.end()) { deba@1409: *(it->second) = idReader->read(ls); deba@1409: } deba@1409: } deba@1409: } deba@1409: deba@1409: private: deba@1409: deba@1409: std::string id; deba@1409: deba@1409: typedef std::map ItemReaders; deba@1409: ItemReaders readers; deba@1409: std::auto_ptr > idReader; deba@1409: }; deba@1409: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for reading labeled edges. deba@1409: /// deba@1409: /// The edges section's header line is \c \@edges \c edges_id, but the deba@1409: /// \c edges_id may be empty. deba@1409: /// deba@1409: /// Each line in the section contains the name of the edge deba@1409: /// and then the edge id. deba@1409: /// deba@1409: /// \relates LemonReader deba@1409: template deba@1409: class EdgeReader : public CommonSectionReaderBase { deba@1409: typedef CommonSectionReaderBase Parent; deba@1409: typedef _Graph Graph; deba@1409: typedef typename Graph::Edge Item; deba@1409: public: deba@1409: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for EdgeReader. It creates the EdgeReader and deba@1409: /// attach it into the given LemonReader. It will use the given deba@1409: /// edge id reader to give back the edges. The reader will read the deba@1409: /// section only if the \c _id and the \c nodes_id are the same. deba@1409: template deba@1409: EdgeReader(LemonReader& _reader, const _IdReader& _idReader, deba@1409: const std::string& _id = std::string()) deba@1409: : Parent(_reader), id(_id), deba@1409: idReader(new IdReader(_idReader)) {} deba@1409: deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for EdgeReader. deba@1409: virtual ~EdgeReader() {} deba@1409: private: deba@1409: EdgeReader(const EdgeReader&); deba@1409: void operator=(const EdgeReader&); deba@1409: deba@1409: public: deba@1409: deba@1409: /// \brief Add an edge reader command for the EdgeReader. deba@1409: /// deba@1409: /// Add an edge reader command for the EdgeReader. deba@1409: void readEdge(const std::string& name, Item& item) { deba@1409: if (readers.find(name) != readers.end()) { deba@1409: ErrorMessage msg; deba@1409: msg << "Multiple read rule for edge: " << name; deba@1409: throw IOParameterError(msg.message()); deba@1409: } deba@1409: readers.insert(make_pair(name, &item)); deba@1409: } deba@1409: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1409: /// It gives back true when the header line start with \c @edges, deba@1409: /// and the header line's id and the reader's id are the same. deba@1409: virtual bool header(const std::string& line) { deba@1409: std::istringstream ls(line); deba@1409: std::string command; deba@1409: std::string name; deba@1409: ls >> command >> name; deba@1409: return command == "@edges" && name == id; deba@1409: } deba@1409: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1409: virtual void read(std::istream& is) { deba@1409: std::string line; deba@1409: while (getline(is, line)) { deba@1409: std::istringstream ls(line); deba@1409: std::string id; deba@1409: ls >> id; deba@1409: typename ItemReaders::iterator it = readers.find(id); deba@1409: if (it != readers.end()) { deba@1409: *(it->second) = idReader->read(ls); deba@1409: } deba@1409: } deba@1409: } deba@1409: deba@1409: private: deba@1409: deba@1409: std::string id; deba@1409: deba@1409: typedef std::map ItemReaders; deba@1409: ItemReaders readers; deba@1409: std::auto_ptr > idReader; deba@1409: }; deba@1409: deba@1409: /// \ingroup io_group deba@1409: /// \brief SectionReader for attributes. deba@1409: /// deba@1409: /// The lemon format can store multiple attribute set. Each set has deba@1409: /// the header line \c \@attributes \c attributeset_id, but the deba@1409: /// attributeset_id may be empty. deba@1409: /// deba@1409: /// The attributeset section contains several lines. Each of them starts deba@1409: /// with an attribute and then a the value for the id. deba@1409: /// deba@1409: /// \relates LemonReader deba@1408: template deba@1408: class AttributeReader : public CommonSectionReaderBase { deba@1408: typedef CommonSectionReaderBase Parent; deba@1408: typedef _Traits Traits; deba@1408: public: deba@1409: /// \brief Constructor. deba@1409: /// deba@1409: /// Constructor for AttributeReader. It creates the AttributeReader and deba@1409: /// attach it into the given LemonReader. The reader process a section deba@1409: /// only if the \c section_id and the \c _id are the same. deba@1408: AttributeReader(LemonReader& _reader, deba@1409: const std::string& _id = std::string()) deba@1409: : Parent(_reader), id(_id) {} deba@1408: deba@1409: /// \brief Destructor. deba@1409: /// deba@1409: /// Destructor for AttributeReader. deba@1408: virtual ~AttributeReader() { deba@1408: for (typename Readers::iterator it = readers.begin(); deba@1408: it != readers.end(); ++it) { deba@1408: delete it->second; deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: AttributeReader(const AttributeReader&); deba@1408: void operator=(AttributeReader&); deba@1408: deba@1408: public: deba@1409: /// \brief Add an attribute reader command for the reader. deba@1409: /// deba@1409: /// Add an attribute reader command for the reader. deba@1408: template deba@1408: AttributeReader& readAttribute(const std::string& id, Value& value) { deba@1408: return readAttribute > deba@1408: (id, value); deba@1408: } deba@1408: deba@1409: /// \brief Add an attribute reader command for the reader. deba@1409: /// deba@1409: /// Add an attribute reader command for the reader. deba@1408: template deba@1408: AttributeReader& readAttribute(const std::string& name, Value& value, deba@1408: const Reader& reader = Reader()) { deba@1408: if (readers.find(name) != readers.end()) { deba@1408: ErrorMessage msg; deba@1408: msg << "Multiple read rule for attribute: " << name; deba@1408: throw IOParameterError(msg.message()); deba@1408: } deba@1408: readers.insert(make_pair(name, new ValueReader deba@1408: (value, reader))); deba@1408: return *this; deba@1408: } deba@1408: deba@1409: protected: deba@1409: deba@1409: /// \brief Gives back true when the SectionReader can process deba@1409: /// the section with the given header line. deba@1409: /// deba@1409: /// It gives back true when the header line start with \c @attributes, deba@1409: /// and the header line's id and the attributeset's id are the same. deba@1408: bool header(const std::string& line) { deba@1408: std::istringstream ls(line); deba@1408: std::string command; deba@1408: std::string name; deba@1408: ls >> command >> name; deba@1408: return command == "@attributes" && name == id; deba@1408: } deba@1408: deba@1409: /// \brief Reader function of the section. deba@1409: /// deba@1409: /// It reads the content of the section. deba@1408: void read(std::istream& is) { deba@1408: std::string line; deba@1408: while (getline(is, line)) { deba@1408: std::istringstream ls(line); deba@1408: std::string id; deba@1408: ls >> id; deba@1408: typename Readers::iterator it = readers.find(id); deba@1408: if (it != readers.end()) { deba@1408: it->second->read(ls); deba@1408: } deba@1408: } deba@1408: } deba@1408: deba@1408: private: deba@1408: std::string id; deba@1408: deba@1408: typedef std::map Readers; deba@1409: Readers readers; deba@1408: }; deba@1408: deba@1408: deba@1408: } deba@1408: #endif