src/lemon/lemon_reader.h
author deba
Sat, 14 May 2005 21:13:00 +0000
changeset 1424 c3d754f5e631
parent 1423 78502c63f771
child 1427 14c75970840e
permissions -rw-r--r--
Bug fix.

I programmed to much templates.
deba@1408
     1
/* -*- C++ -*-
deba@1408
     2
 * src/lemon/lemon_reader.h - Part of LEMON, a generic C++ optimization library
deba@1408
     3
 *
deba@1408
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1408
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1408
     6
 *
deba@1408
     7
 * Permission to use, modify and distribute this software is granted
deba@1408
     8
 * provided that this copyright notice appears in all copies. For
deba@1408
     9
 * precise terms see the accompanying LICENSE file.
deba@1408
    10
 *
deba@1408
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1408
    12
 * express or implied, and with no claim as to its suitability for any
deba@1408
    13
 * purpose.
deba@1408
    14
 *
deba@1408
    15
 */
deba@1408
    16
deba@1408
    17
///\ingroup io_group
deba@1408
    18
///\file
deba@1408
    19
///\brief Lemon Format reader.
deba@1408
    20
deba@1421
    21
deba@1408
    22
#ifndef LEMON_LEMON_READER_H
deba@1408
    23
#define LEMON_LEMON_READER_H
deba@1408
    24
deba@1421
    25
deba@1408
    26
#include <iostream>
deba@1408
    27
#include <fstream>
deba@1408
    28
#include <string>
deba@1408
    29
#include <vector>
deba@1408
    30
#include <algorithm>
deba@1408
    31
#include <map>
deba@1408
    32
#include <memory>
deba@1408
    33
deba@1408
    34
#include <lemon/error.h>
deba@1421
    35
#include <lemon/graph_utils.h>
deba@1421
    36
#include <lemon/utility.h>
deba@1409
    37
#include <lemon/bits/item_reader.h>
deba@1408
    38
deba@1408
    39
deba@1408
    40
namespace lemon {
deba@1408
    41
deba@1421
    42
  namespace _reader_bits {
deba@1421
    43
  
deba@1421
    44
    template <typename T>
deba@1421
    45
    bool operator<(T, T) {
deba@1421
    46
      throw DataFormatError("Id is not comparable");
deba@1421
    47
    }
deba@1421
    48
deba@1421
    49
    template <typename T>
deba@1421
    50
    struct Less {
deba@1421
    51
      bool operator()(const T& p, const T& q) const {
deba@1421
    52
	return p < q;
deba@1421
    53
      }
deba@1421
    54
    };
deba@1421
    55
deba@1421
    56
    template <typename M1, typename M2>
deba@1421
    57
    class WriteComposeMap {
deba@1421
    58
    public:
deba@1421
    59
      typedef True NeedCopy;
deba@1421
    60
      
deba@1421
    61
      typedef typename M2::Key Key;
deba@1421
    62
      typedef typename M1::Value Value;
deba@1421
    63
deba@1421
    64
      WriteComposeMap(typename SmartParameter<M1>::Type _m1, const M2& _m2) 
deba@1421
    65
	: m1(_m1), m2(_m2) {}
deba@1421
    66
      
deba@1421
    67
      void set(const Key& key, const Value& value) {
deba@1421
    68
	m1.set(m2[key], value);
deba@1421
    69
      }
deba@1421
    70
deba@1421
    71
    private:
deba@1421
    72
      
deba@1421
    73
      typename SmartReference<M1>::Type m1;
deba@1421
    74
      typename SmartConstReference<M2>::Type m2;
deba@1421
    75
      
deba@1421
    76
    };
deba@1421
    77
deba@1421
    78
    template <typename M1, typename M2>
deba@1421
    79
    WriteComposeMap<M1, M2> writeComposeMap(M1& m1, const M2& m2) {
deba@1421
    80
      return WriteComposeMap<M1, M2>(m1, m2);
deba@1421
    81
    }
deba@1421
    82
deba@1421
    83
    template <typename M1, typename M2>
deba@1421
    84
    WriteComposeMap<M1, M2> writeComposeMap(const M1& m1, const M2& m2) {
deba@1421
    85
      return WriteComposeMap<M1, M2>(m1, m2);
deba@1421
    86
    }
deba@1421
    87
  
deba@1421
    88
  }
deba@1421
    89
deba@1409
    90
  /// \ingroup io_group
deba@1408
    91
  /// \brief Lemon Format reader class.
deba@1408
    92
  /// 
deba@1409
    93
  /// The Lemon Format contains several sections. We do not want to
deba@1409
    94
  /// determine what sections are in a lemon file we give only a framework
deba@1409
    95
  /// to read a section oriented format.
deba@1409
    96
  ///
deba@1409
    97
  /// In the Lemon Format each section starts with a line contains a \c \@
deba@1409
    98
  /// character on the first not white space position. This line is the
deba@1409
    99
  /// header line of the section. Each next lines belong to this section
deba@1409
   100
  /// while it does not starts with \c \@ character. This line can start a 
deba@1409
   101
  /// new section or if it can close the file with the \c \@end line.
deba@1409
   102
  /// The file format ignore the empty lines and it may contain comments
deba@1409
   103
  /// started with a \c # character to the end of the line. 
deba@1409
   104
  ///
deba@1409
   105
  /// The framework provides an abstract LemonReader::SectionReader class
deba@1409
   106
  /// what defines the interface of a SectionReader. The SectionReader
deba@1409
   107
  /// has the \c header() member function what get a header line string and
deba@1409
   108
  /// decides if it want to process the next section. Several SectionReaders
deba@1409
   109
  /// can be attached to an LemonReader and the first attached what can
deba@1409
   110
  /// process the section will be used. Its \c read() member will called
deba@1409
   111
  /// with a stream contains the section. From this stream the empty lines
deba@1409
   112
  /// and comments are filtered out.
deba@1409
   113
  ///
deba@1409
   114
  /// \relates GraphReader
deba@1409
   115
  /// \relates NodeSetReader
deba@1409
   116
  /// \relates EdgeSetReader
deba@1409
   117
  /// \relates NodesReader
deba@1409
   118
  /// \relates EdgesReader
deba@1409
   119
  /// \relates AttributeReader
deba@1408
   120
  class LemonReader {
deba@1408
   121
  private:
deba@1408
   122
    
deba@1408
   123
    class FilterStreamBuf : public std::streambuf {
deba@1408
   124
    public:
deba@1408
   125
deba@1408
   126
      typedef std::streambuf Parent;
deba@1408
   127
      typedef Parent::char_type char_type;
deba@1408
   128
      FilterStreamBuf(std::istream& is, int& num) 
deba@1408
   129
	: _is(is), _base(0), _eptr(0), 
deba@1408
   130
	  _num(num), skip_state(after_endl) {}
deba@1408
   131
deba@1408
   132
    protected:
deba@1408
   133
deba@1408
   134
      enum skip_state_type {
deba@1408
   135
	no_skip,
deba@1408
   136
	after_comment,
deba@1408
   137
	after_endl,
deba@1408
   138
	empty_line
deba@1408
   139
      };
deba@1408
   140
deba@1408
   141
      char_type small_buf[1];
deba@1408
   142
deba@1408
   143
deba@1408
   144
      std::istream& _is;
deba@1408
   145
deba@1408
   146
      char_type* _base;
deba@1408
   147
      char_type* _eptr;
deba@1408
   148
deba@1408
   149
      int& _num;
deba@1408
   150
deba@1408
   151
      skip_state_type skip_state;
deba@1408
   152
deba@1408
   153
deba@1408
   154
      char_type* base() { return _base; }
deba@1408
   155
deba@1408
   156
      char_type* eptr() { return _eptr; }
deba@1408
   157
deba@1408
   158
      int blen() { return _eptr - _base; }
deba@1408
   159
deba@1408
   160
      void setb(char_type* buf, int len) {
deba@1408
   161
	_base = buf;
deba@1408
   162
	_eptr = buf + len;
deba@1408
   163
      }
deba@1408
   164
  
deba@1408
   165
      virtual std::streambuf* setbuf(char *buf, int len) {
deba@1408
   166
	if (base()) return 0;
deba@1408
   167
	if (buf != 0 && len >= (int)sizeof(small_buf)) {
deba@1408
   168
	  setb(buf, len);
deba@1408
   169
	} else {
deba@1408
   170
	  setb(small_buf, sizeof(small_buf));
deba@1408
   171
	}
deba@1408
   172
	setg(0, 0, 0);
deba@1408
   173
	return this;
deba@1408
   174
      }
deba@1408
   175
deba@1408
   176
      bool put_char(char c) {
deba@1408
   177
	switch (skip_state) {
deba@1408
   178
	case no_skip:
deba@1408
   179
	  switch (c) {
deba@1408
   180
	  case '\n': 
deba@1408
   181
	    skip_state = after_endl;
deba@1408
   182
	    return true;
deba@1408
   183
	  case '#':
deba@1408
   184
	    skip_state = after_comment;
deba@1408
   185
	    return false;
deba@1408
   186
	  default:
deba@1408
   187
	    return true;
deba@1408
   188
	  }
deba@1408
   189
	case after_comment:
deba@1408
   190
	  switch (c) {
deba@1408
   191
	  case '\n': 
deba@1408
   192
	    skip_state = after_endl;
deba@1408
   193
	    return true;
deba@1408
   194
	  default:
deba@1408
   195
	    return false;
deba@1408
   196
	  }        
deba@1408
   197
	case after_endl:
deba@1408
   198
	  switch (c) {
deba@1408
   199
	  case '@':
deba@1408
   200
	    return false;
deba@1408
   201
	  case '\n': 
deba@1408
   202
	    return false;
deba@1408
   203
	  case '#':
deba@1408
   204
	    skip_state = empty_line;
deba@1408
   205
	    return false;
deba@1408
   206
	  default:
deba@1408
   207
	    if (!isspace(c)) {
deba@1408
   208
	      skip_state = no_skip;
deba@1408
   209
	      return true;
deba@1408
   210
	    } else {
deba@1408
   211
	      return false;
deba@1408
   212
	    }
deba@1408
   213
	  }
deba@1408
   214
	  break;
deba@1408
   215
	case empty_line:
deba@1408
   216
	  switch (c) {
deba@1408
   217
	  case '\n': 
deba@1408
   218
	    skip_state = after_endl;
deba@1408
   219
	    return false;
deba@1408
   220
	  default:
deba@1408
   221
	    return false;
deba@1408
   222
	  }
deba@1408
   223
	}
deba@1408
   224
	return false;
deba@1408
   225
      }
deba@1408
   226
deba@1408
   227
      virtual int underflow() {
deba@1408
   228
	char c;
deba@1408
   229
	if (_is.read(&c, 1)) {
deba@1408
   230
	  _is.putback(c);
deba@1408
   231
	  if (c == '@') {
deba@1408
   232
	    return EOF;
deba@1408
   233
	  }
deba@1408
   234
	} else {
deba@1408
   235
	  return EOF;
deba@1408
   236
	}
deba@1408
   237
	char_type *ptr;
deba@1408
   238
	for (ptr = base(); ptr != eptr(); ++ptr) {
deba@1408
   239
	  if (_is.read(&c, 1)) {
deba@1408
   240
	    if (c == '\n') ++_num;
deba@1408
   241
	    if (put_char(c)) {
deba@1408
   242
	      *ptr = c;
deba@1408
   243
	    } else {
deba@1408
   244
	      if (skip_state == after_endl && c == '@') {
deba@1408
   245
		_is.putback('@');
deba@1408
   246
		break;
deba@1408
   247
	      }
deba@1408
   248
	      --ptr;
deba@1408
   249
	    }
deba@1408
   250
	  } else {
deba@1408
   251
	    break;
deba@1408
   252
	  }
deba@1408
   253
	}
deba@1408
   254
	setg(base(), base(), ptr);
deba@1408
   255
	return *base();
deba@1408
   256
      }
deba@1408
   257
deba@1408
   258
      virtual int sync() {
deba@1408
   259
	return EOF;
deba@1408
   260
      }
deba@1408
   261
    };
deba@1408
   262
deba@1408
   263
  public:
deba@1408
   264
deba@1409
   265
    /// \brief Abstract base class for reading a section.
deba@1409
   266
    ///
deba@1409
   267
    /// This class has an \c header() member function what get a 
deba@1409
   268
    /// header line string and decides if it want to process the next 
deba@1409
   269
    /// section. Several SectionReaders can be attached to an LemonReader 
deba@1409
   270
    /// and the first attached what can process the section will be used. 
deba@1409
   271
    /// Its \c read() member will called with a stream contains the section. 
deba@1409
   272
    /// From this stream the empty lines and comments are filtered out.
deba@1408
   273
    class SectionReader {
deba@1409
   274
      friend class LemonReader;
deba@1409
   275
    protected:
deba@1409
   276
      /// \brief Constructor for SectionReader.
deba@1409
   277
      ///
deba@1409
   278
      /// Constructor for SectionReader. It attach this reader to
deba@1409
   279
      /// the given LemonReader.
deba@1409
   280
      SectionReader(LemonReader& reader) {
deba@1409
   281
	reader.attach(*this);
deba@1409
   282
      }
deba@1409
   283
deba@1409
   284
      /// \brief Gives back true when the SectionReader can process 
deba@1409
   285
      /// the section with the given header line.
deba@1409
   286
      ///
deba@1409
   287
      /// It gives back true when the SectionReader can process
deba@1409
   288
      /// the section with the given header line.
deba@1408
   289
      virtual bool header(const std::string& line) = 0;
deba@1409
   290
deba@1409
   291
      /// \brief Reader function of the section.
deba@1409
   292
      ///
deba@1409
   293
      /// It reads the content of the section.
deba@1408
   294
      virtual void read(std::istream& is) = 0;
deba@1408
   295
    };
deba@1408
   296
deba@1409
   297
    /// \brief Constructor for LemonReader.
deba@1409
   298
    ///
deba@1409
   299
    /// Constructor for LemonReader which reads from the given stream.
deba@1408
   300
    LemonReader(std::istream& _is) 
deba@1408
   301
      : is(&_is), own_is(false) {}
deba@1408
   302
deba@1409
   303
    /// \brief Constructor for LemonReader.
deba@1409
   304
    ///
deba@1409
   305
    /// Constructor for LemonReader which reads from the given file.
deba@1408
   306
    LemonReader(const std::string& filename) 
deba@1408
   307
      : is(0), own_is(true) {
deba@1408
   308
      is = new std::ifstream(filename.c_str());
deba@1408
   309
    }
deba@1408
   310
deba@1409
   311
    /// \brief Desctructor for LemonReader.
deba@1409
   312
    ///
deba@1409
   313
    /// Desctructor for LemonReader.
deba@1408
   314
    ~LemonReader() {
deba@1408
   315
      if (own_is) {
deba@1408
   316
	delete is;
deba@1408
   317
      }
deba@1408
   318
    }
deba@1408
   319
deba@1408
   320
  private:
deba@1408
   321
    LemonReader(const LemonReader&);
deba@1408
   322
    void operator=(const LemonReader&);
deba@1408
   323
deba@1408
   324
    void attach(SectionReader& reader) {
deba@1408
   325
      readers.push_back(&reader);
deba@1408
   326
    }
deba@1408
   327
deba@1409
   328
  public:
deba@1409
   329
    /// \brief Executes the LemonReader.
deba@1409
   330
    /// 
deba@1409
   331
    /// It executes the LemonReader.
deba@1408
   332
    void run() {
deba@1408
   333
      int line_num = 0;
deba@1408
   334
      std::string line;
deba@1408
   335
      try {
deba@1408
   336
	while ((++line_num, getline(*is, line)) && line.find("@end") != 0) {
deba@1408
   337
	  SectionReaders::iterator it;
deba@1408
   338
	  for (it = readers.begin(); it != readers.end(); ++it) {
deba@1408
   339
	    if ((*it)->header(line)) {
deba@1408
   340
	      char buf[2048];
deba@1408
   341
	      FilterStreamBuf buffer(*is, line_num);
deba@1408
   342
	      buffer.pubsetbuf(buf, sizeof(buf));
deba@1408
   343
	      std::istream is(&buffer);
deba@1408
   344
	      (*it)->read(is);
deba@1408
   345
	      break;
deba@1408
   346
	    }
deba@1408
   347
	  }
deba@1408
   348
	}
deba@1408
   349
      } catch (DataFormatError& error) {
deba@1408
   350
	error.line(line_num);
deba@1408
   351
	throw error;
deba@1408
   352
      }	
deba@1408
   353
    }
deba@1408
   354
deba@1408
   355
deba@1408
   356
  private:
deba@1408
   357
deba@1408
   358
    std::istream* is;
deba@1408
   359
    bool own_is;
deba@1408
   360
deba@1408
   361
    typedef std::vector<SectionReader*> SectionReaders;
deba@1408
   362
    SectionReaders readers;
deba@1408
   363
deba@1408
   364
  };
deba@1408
   365
deba@1409
   366
  /// \brief Helper class for implementing the common SectionReaders.
deba@1409
   367
  ///
deba@1409
   368
  /// Helper class for implementing the common SectionReaders.
deba@1409
   369
  class CommonSectionReaderBase : public LemonReader::SectionReader {
deba@1409
   370
    typedef LemonReader::SectionReader Parent;
deba@1409
   371
  protected:
deba@1409
   372
    
deba@1409
   373
    /// \brief Constructor for CommonSectionReaderBase.
deba@1409
   374
    ///
deba@1409
   375
    /// Constructor for CommonSectionReaderBase. It attach this reader to
deba@1409
   376
    /// the given LemonReader.
deba@1409
   377
    CommonSectionReaderBase(LemonReader& _reader) 
deba@1409
   378
      : Parent(_reader) {}
deba@1408
   379
deba@1408
   380
    template <typename _Item>
deba@1408
   381
    class ReaderBase;
deba@1408
   382
    
deba@1408
   383
    template <typename _Item>
deba@1408
   384
    class InverterBase : public ReaderBase<_Item> {
deba@1408
   385
    public:
deba@1408
   386
      typedef _Item Item;
deba@1408
   387
      virtual void read(std::istream&, const Item&) = 0;
deba@1408
   388
      virtual Item read(std::istream&) const = 0;
deba@1408
   389
deba@1408
   390
      virtual InverterBase<_Item>* getInverter() {
deba@1408
   391
	return this;
deba@1408
   392
      }
deba@1408
   393
    };
deba@1408
   394
deba@1408
   395
    template <typename _Item, typename _Map, typename _Reader>
deba@1408
   396
    class MapReaderInverter : public InverterBase<_Item> {
deba@1408
   397
    public:
deba@1408
   398
      typedef _Item Item;
deba@1408
   399
      typedef _Reader Reader;
deba@1408
   400
      typedef typename Reader::Value Value;
deba@1408
   401
      typedef _Map Map;
deba@1424
   402
      typedef std::map<Value, Item, _reader_bits::Less<Value> > Inverse;
deba@1408
   403
deba@1421
   404
      typename SmartReference<Map>::Type map;
deba@1408
   405
      Reader reader;
deba@1408
   406
      Inverse inverse;
deba@1408
   407
deba@1421
   408
      MapReaderInverter(typename SmartParameter<Map>::Type _map,
deba@1421
   409
			const Reader& _reader) 
deba@1408
   410
	: map(_map), reader(_reader) {}
deba@1408
   411
deba@1408
   412
      virtual ~MapReaderInverter() {}
deba@1408
   413
deba@1408
   414
      virtual void read(std::istream& is, const Item& item) {
deba@1408
   415
	Value value;
deba@1408
   416
	reader.read(is, value);
deba@1408
   417
	map.set(item, value);
deba@1408
   418
	typename Inverse::iterator it = inverse.find(value);
deba@1408
   419
	if (it == inverse.end()) {
deba@1408
   420
	  inverse.insert(std::make_pair(value, item));
deba@1408
   421
	} else {
deba@1408
   422
	  throw DataFormatError("Multiple ID occurence");
deba@1408
   423
	}
deba@1408
   424
      }
deba@1408
   425
deba@1408
   426
      virtual Item read(std::istream& is) const {
deba@1408
   427
	Value value;
deba@1408
   428
	reader.read(is, value);	
deba@1408
   429
	typename Inverse::const_iterator it = inverse.find(value);
deba@1408
   430
	if (it != inverse.end()) {
deba@1408
   431
	  return it->second;
deba@1408
   432
	} else {
deba@1408
   433
	  throw DataFormatError("Invalid ID error");
deba@1408
   434
	}
deba@1408
   435
      }      
deba@1408
   436
    };
deba@1408
   437
deba@1408
   438
    template <typename _Item, typename _Reader>
deba@1408
   439
    class SkipReaderInverter : public InverterBase<_Item> {
deba@1408
   440
    public:
deba@1408
   441
      typedef _Item Item;
deba@1408
   442
      typedef _Reader Reader;
deba@1408
   443
      typedef typename Reader::Value Value;
deba@1424
   444
      typedef std::map<Value, Item, _reader_bits::Less<Value> > Inverse;
deba@1408
   445
deba@1408
   446
      Reader reader;
deba@1408
   447
deba@1408
   448
      SkipReaderInverter(const Reader& _reader) 
deba@1408
   449
	: reader(_reader) {}
deba@1408
   450
deba@1408
   451
      virtual ~SkipReaderInverter() {}
deba@1408
   452
deba@1408
   453
      virtual void read(std::istream& is, const Item& item) {
deba@1408
   454
	Value value;
deba@1408
   455
	reader.read(is, value);
deba@1408
   456
	typename Inverse::iterator it = inverse.find(value);
deba@1408
   457
	if (it == inverse.end()) {
deba@1408
   458
	  inverse.insert(std::make_pair(value, item));
deba@1408
   459
	} else {
deba@1408
   460
	  throw DataFormatError("Multiple ID occurence error");
deba@1408
   461
	}
deba@1408
   462
      }
deba@1408
   463
deba@1408
   464
      virtual Item read(std::istream& is) const {
deba@1408
   465
	Value value;
deba@1408
   466
	reader.read(is, value);	
deba@1408
   467
	typename Inverse::const_iterator it = inverse.find(value);
deba@1408
   468
	if (it != inverse.end()) {
deba@1408
   469
	  return it->second;
deba@1408
   470
	} else {
deba@1408
   471
	  throw DataFormatError("Invalid ID error");
deba@1408
   472
	}
deba@1408
   473
      }
deba@1408
   474
deba@1408
   475
    private:
deba@1408
   476
      Inverse inverse;
deba@1408
   477
    };
deba@1408
   478
deba@1408
   479
    template <typename _Item>    
deba@1408
   480
    class ReaderBase {
deba@1408
   481
    public:
deba@1408
   482
      typedef _Item Item;
deba@1408
   483
deba@1408
   484
      virtual ~ReaderBase() {}
deba@1408
   485
deba@1408
   486
      virtual void read(std::istream& is, const Item& item) = 0;
deba@1408
   487
      virtual InverterBase<_Item>* getInverter() = 0;
deba@1408
   488
    };
deba@1408
   489
deba@1408
   490
    template <typename _Item, typename _Map, typename _Reader>
deba@1408
   491
    class MapReader : public ReaderBase<_Item> {
deba@1408
   492
    public:
deba@1408
   493
      typedef _Map Map;
deba@1408
   494
      typedef _Reader Reader;
deba@1408
   495
      typedef typename Reader::Value Value;
deba@1408
   496
      typedef _Item Item;
deba@1408
   497
      
deba@1421
   498
      typename SmartReference<Map>::Type map;
deba@1408
   499
      Reader reader;
deba@1408
   500
deba@1421
   501
      MapReader(typename SmartParameter<Map>::Type _map, 
deba@1421
   502
		const Reader& _reader) 
deba@1408
   503
	: map(_map), reader(_reader) {}
deba@1408
   504
deba@1408
   505
      virtual ~MapReader() {}
deba@1408
   506
deba@1408
   507
      virtual void read(std::istream& is, const Item& item) {
deba@1408
   508
	Value value;
deba@1408
   509
	reader.read(is, value);
deba@1408
   510
	map.set(item, value);
deba@1408
   511
      }
deba@1408
   512
deba@1408
   513
      virtual InverterBase<_Item>* getInverter() {
deba@1408
   514
	return new MapReaderInverter<Item, Map, Reader>(map, reader);
deba@1408
   515
      }
deba@1408
   516
    };
deba@1408
   517
deba@1408
   518
deba@1408
   519
    template <typename _Item, typename _Reader>
deba@1408
   520
    class SkipReader : public ReaderBase<_Item> {
deba@1408
   521
    public:
deba@1408
   522
      typedef _Reader Reader;
deba@1408
   523
      typedef typename Reader::Value Value;
deba@1408
   524
      typedef _Item Item;
deba@1408
   525
deba@1408
   526
      Reader reader;
deba@1408
   527
      SkipReader(const Reader& _reader) : reader(_reader) {}
deba@1408
   528
deba@1408
   529
      virtual ~SkipReader() {}
deba@1408
   530
deba@1408
   531
      virtual void read(std::istream& is, const Item&) {
deba@1408
   532
	Value value;
deba@1408
   533
	reader.read(is, value);
deba@1408
   534
      }      
deba@1408
   535
deba@1408
   536
      virtual InverterBase<Item>* getInverter() {
deba@1408
   537
	return new SkipReaderInverter<Item, Reader>(reader);
deba@1408
   538
      }
deba@1408
   539
    };
deba@1408
   540
deba@1408
   541
    template <typename _Item>
deba@1409
   542
    class IdReaderBase {
deba@1408
   543
    public:
deba@1408
   544
      typedef _Item Item;
deba@1409
   545
      virtual Item read(std::istream& is) const = 0;
deba@1408
   546
    };
deba@1408
   547
deba@1409
   548
    template <typename _Item, typename _BoxedIdReader>
deba@1409
   549
    class IdReader : public IdReaderBase<_Item> {
deba@1408
   550
    public:
deba@1408
   551
      typedef _Item Item;
deba@1409
   552
      typedef _BoxedIdReader BoxedIdReader;
deba@1409
   553
      
deba@1409
   554
      const BoxedIdReader& boxedIdReader;
deba@1408
   555
deba@1409
   556
      IdReader(const BoxedIdReader& _boxedIdReader) 
deba@1409
   557
	: boxedIdReader(_boxedIdReader) {}
deba@1408
   558
deba@1409
   559
      virtual Item read(std::istream& is) const {
deba@1409
   560
	return boxedIdReader.readId(is);
deba@1408
   561
      }
deba@1408
   562
    };
deba@1408
   563
deba@1408
   564
    class ValueReaderBase {
deba@1408
   565
    public:
deba@1408
   566
      virtual void read(std::istream&) {};
deba@1408
   567
    };
deba@1408
   568
deba@1408
   569
    template <typename _Value, typename _Reader>
deba@1408
   570
    class ValueReader : public ValueReaderBase {
deba@1408
   571
    public:
deba@1408
   572
      typedef _Value Value;
deba@1408
   573
      typedef _Reader Reader;
deba@1408
   574
deba@1408
   575
      ValueReader(Value& _value, const Reader& _reader)
deba@1408
   576
 	: value(_value), reader(_reader) {}
deba@1408
   577
deba@1408
   578
      virtual void read(std::istream& is) {
deba@1408
   579
	reader.read(is, value);
deba@1408
   580
      }
deba@1408
   581
    private:
deba@1408
   582
      Value& value;
deba@1408
   583
      Reader reader;
deba@1408
   584
    };
deba@1408
   585
    
deba@1408
   586
  };
deba@1408
   587
deba@1409
   588
  /// \ingroup io_group
deba@1409
   589
  /// \brief SectionReader for reading a graph's nodeset.
deba@1409
   590
  ///
deba@1409
   591
  /// The lemon format can store multiple graph nodesets with several maps.
deba@1409
   592
  /// The nodeset section's header line is \c \@nodeset \c nodeset_id, but the
deba@1409
   593
  /// \c nodeset_id may be empty.
deba@1409
   594
  ///
deba@1409
   595
  /// The first line of the section contains the names of the maps separated
deba@1409
   596
  /// with white spaces. Each next lines describes a node in the nodeset, and
deba@1409
   597
  /// contains the mapped values for each map.
deba@1409
   598
  ///
deba@1409
   599
  /// If the nodeset contains an \c "id" named map then it will be regarded
deba@1409
   600
  /// as id map. This map should contain only unique values and when the 
deba@1409
   601
  /// \c readId() member will read a value from the given stream it will
deba@1409
   602
  /// give back that node which is mapped to this value.
deba@1409
   603
  ///
deba@1409
   604
  /// \relates LemonReader
deba@1408
   605
  template <typename _Graph, typename _Traits = DefaultReaderTraits>
deba@1408
   606
  class NodeSetReader : public CommonSectionReaderBase {
deba@1408
   607
    typedef CommonSectionReaderBase Parent;
deba@1408
   608
  public:
deba@1408
   609
deba@1408
   610
    typedef _Graph Graph;
deba@1408
   611
    typedef _Traits Traits;
deba@1408
   612
    typedef typename Graph::Node Item;
deba@1408
   613
    typedef typename Traits::Skipper DefaultSkipper;
deba@1408
   614
deba@1409
   615
    /// \brief Constructor.
deba@1409
   616
    ///
deba@1409
   617
    /// Constructor for NodeSetReader. It creates the NodeSetReader and
deba@1409
   618
    /// attach it into the given LemonReader. The nodeset reader will
deba@1409
   619
    /// add the readed nodes to the given Graph. The reader will read
deba@1409
   620
    /// the section when the \c section_id and the \c _id are the same. 
deba@1421
   621
    NodeSetReader(LemonReader& _reader, 
deba@1421
   622
		  typename SmartParameter<Graph>::Type _graph, 
deba@1408
   623
		  const std::string& _id = std::string(),
deba@1409
   624
		  const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1409
   625
      : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {} 
deba@1408
   626
deba@1409
   627
deba@1409
   628
    /// \brief Destructor.
deba@1409
   629
    ///
deba@1409
   630
    /// Destructor for NodeSetReader.
deba@1408
   631
    virtual ~NodeSetReader() {
deba@1408
   632
      for (typename MapReaders::iterator it = readers.begin(); 
deba@1408
   633
	   it != readers.end(); ++it) {
deba@1408
   634
	delete it->second;
deba@1408
   635
      }
deba@1408
   636
    }
deba@1408
   637
deba@1408
   638
  private:
deba@1408
   639
    NodeSetReader(const NodeSetReader&);
deba@1408
   640
    void operator=(const NodeSetReader&);
deba@1408
   641
  
deba@1408
   642
  public:
deba@1408
   643
deba@1408
   644
    /// \brief Add a new node map reader command for the reader.
deba@1408
   645
    ///
deba@1408
   646
    /// Add a new node map reader command for the reader.
deba@1408
   647
    template <typename Map>
deba@1421
   648
    NodeSetReader& readNodeMap(std::string name, Map& map) {
deba@1421
   649
      return _readMap<
deba@1421
   650
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1421
   651
	typename SmartParameter<Map>::Type>(name, map);
deba@1421
   652
    }
deba@1421
   653
deba@1421
   654
    template <typename Map>
deba@1421
   655
    NodeSetReader& readNodeMap(std::string name, const Map& map) {
deba@1421
   656
      return _readMap<
deba@1421
   657
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1421
   658
	typename SmartParameter<Map>::Type>(name, map);
deba@1408
   659
    }
deba@1408
   660
deba@1408
   661
    /// \brief Add a new node map reader command for the reader.
deba@1408
   662
    ///
deba@1408
   663
    /// Add a new node map reader command for the reader.
deba@1408
   664
    template <typename Reader, typename Map>
deba@1421
   665
    NodeSetReader& readNodeMap(std::string name, Map& map, 
deba@1421
   666
			       const Reader& reader = Reader()) {
deba@1421
   667
      return _readMap<
deba@1421
   668
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1421
   669
	typename SmartParameter<Map>::Type>(name, map, reader);
deba@1421
   670
    }
deba@1421
   671
deba@1421
   672
    template <typename Reader, typename Map>
deba@1421
   673
    NodeSetReader& readNodeMap(std::string name, const Map& map, 
deba@1421
   674
			       const Reader& reader = Reader()) {
deba@1421
   675
      return _readMap<
deba@1421
   676
	typename Traits::template Reader<typename Map::Value>, Map, 
deba@1421
   677
	typename SmartParameter<Map>::Type>(name, map, reader);
deba@1421
   678
    }
deba@1421
   679
deba@1421
   680
  private:
deba@1421
   681
deba@1421
   682
    template <typename Reader, typename Map, typename MapParameter>
deba@1421
   683
    NodeSetReader& _readMap(std::string name, MapParameter map, 
deba@1421
   684
			    const Reader& reader = Reader()) {
deba@1408
   685
      if (readers.find(name) != readers.end()) {
deba@1408
   686
	ErrorMessage msg;
deba@1408
   687
	msg << "Multiple read rule for node map: " << name;
deba@1408
   688
	throw IOParameterError(msg.message());
deba@1408
   689
      }
deba@1408
   690
      readers.insert(
deba@1408
   691
	make_pair(name, new MapReader<Item, Map, Reader>(map, reader)));
deba@1408
   692
      return *this;
deba@1408
   693
    }
deba@1408
   694
deba@1421
   695
  public:
deba@1421
   696
deba@1408
   697
    /// \brief Add a new node map skipper command for the reader.
deba@1408
   698
    ///
deba@1408
   699
    /// Add a new node map skipper command for the reader.
deba@1408
   700
    template <typename Reader>
deba@1421
   701
    NodeSetReader& skipNodeMap(std::string name, 
deba@1408
   702
			   const Reader& reader = Reader()) {
deba@1408
   703
      if (readers.find(name) != readers.end()) {
deba@1408
   704
	ErrorMessage msg;
deba@1408
   705
	msg << "Multiple read rule for node map: " << name;
deba@1408
   706
	throw IOParameterError(msg.message());
deba@1408
   707
      }
deba@1408
   708
      readers.insert(make_pair(name, new SkipReader<Item, Reader>(reader)));
deba@1408
   709
      return *this;
deba@1408
   710
    }
deba@1408
   711
deba@1409
   712
  protected:
deba@1409
   713
deba@1409
   714
    /// \brief Gives back true when the SectionReader can process 
deba@1409
   715
    /// the section with the given header line.
deba@1409
   716
    ///
deba@1421
   717
    /// It gives back true when the header line starts with \c \@nodeset,
deba@1409
   718
    /// and the header line's id and the nodeset's id are the same.
deba@1408
   719
    virtual bool header(const std::string& line) {
deba@1408
   720
      std::istringstream ls(line);
deba@1408
   721
      std::string command;
deba@1408
   722
      std::string name;
deba@1408
   723
      ls >> command >> name;
deba@1408
   724
      return command == "@nodeset" && name == id;
deba@1408
   725
    }
deba@1408
   726
deba@1409
   727
    /// \brief Reader function of the section.
deba@1409
   728
    ///
deba@1409
   729
    /// It reads the content of the section.
deba@1408
   730
    virtual void read(std::istream& is) {
deba@1408
   731
      std::vector<ReaderBase<Item>* > index;
deba@1408
   732
      std::string line;
deba@1408
   733
deba@1408
   734
      getline(is, line);
deba@1408
   735
      std::istringstream ls(line);	
deba@1408
   736
      while (ls >> id) {
deba@1408
   737
	typename MapReaders::iterator it = readers.find(id);
deba@1408
   738
	if (it != readers.end()) {
deba@1408
   739
	  index.push_back(it->second);
deba@1408
   740
	} else {
deba@1408
   741
	  index.push_back(&skipper);
deba@1408
   742
	}
deba@1408
   743
	if (id == "id" && inverter.get() == 0) {
deba@1408
   744
	  inverter.reset(index.back()->getInverter());
deba@1408
   745
	  index.back() = inverter.get();
deba@1408
   746
	}
deba@1408
   747
      }
deba@1408
   748
      while (getline(is, line)) {	
deba@1408
   749
	typename Graph::Node node = graph.addNode();
deba@1408
   750
	std::istringstream ls(line);
deba@1408
   751
	for (int i = 0; i < (int)index.size(); ++i) {
deba@1408
   752
	  index[i]->read(ls, node);
deba@1408
   753
	}
deba@1408
   754
      }
deba@1408
   755
    }
deba@1408
   756
deba@1409
   757
  public:
deba@1409
   758
deba@1409
   759
    /// \brief Returns true if the nodeset can give back the node by its id.
deba@1409
   760
    ///
deba@1409
   761
    /// Returns true if the nodeset can give back the node by its id.
deba@1409
   762
    /// It is possible only if an "id" named map was read.
deba@1409
   763
    bool isIdReader() const {
deba@1408
   764
      return inverter.get() != 0;
deba@1408
   765
    }
deba@1408
   766
deba@1409
   767
    /// \brief Gives back the node by its id.
deba@1409
   768
    ///
deba@1409
   769
    /// It reads an id from the stream and gives back which node belongs to
deba@1409
   770
    /// it. It is possible only if there was read an "id" named map.
deba@1421
   771
    Item readId(std::istream& is) const {
deba@1408
   772
      return inverter->read(is);
deba@1408
   773
    } 
deba@1408
   774
deba@1408
   775
  private:
deba@1408
   776
deba@1408
   777
    typedef std::map<std::string, ReaderBase<Item>*> MapReaders;
deba@1408
   778
    MapReaders readers;
deba@1408
   779
   
deba@1421
   780
    typename SmartReference<Graph>::Type graph;   
deba@1408
   781
    std::string id;
deba@1408
   782
    SkipReader<Item, DefaultSkipper> skipper;
deba@1408
   783
deba@1408
   784
    std::auto_ptr<InverterBase<Item> > inverter;
deba@1408
   785
  };
deba@1408
   786
deba@1409
   787
  /// \ingroup io_group
deba@1409
   788
  /// \brief SectionReader for reading a graph's edgeset.
deba@1409
   789
  ///
deba@1409
   790
  /// The lemon format can store multiple graph edgesets with several maps.
deba@1409
   791
  /// The edgeset section's header line is \c \@edgeset \c edgeset_id, but the
deba@1409
   792
  /// \c edgeset_id may be empty.
deba@1409
   793
  ///
deba@1409
   794
  /// The first line of the section contains the names of the maps separated
deba@1421
   795
  /// with white spaces. Each next lines describes an edge in the edgeset. The
deba@1421
   796
  /// line contains the source and the target nodes' id and the mapped 
deba@1421
   797
  /// values for each map.
deba@1409
   798
  ///
deba@1409
   799
  /// If the edgeset contains an \c "id" named map then it will be regarded
deba@1409
   800
  /// as id map. This map should contain only unique values and when the 
deba@1409
   801
  /// \c readId() member will read a value from the given stream it will
deba@1409
   802
  /// give back that edge which is mapped to this value.
deba@1409
   803
  ///
deba@1409
   804
  /// The edgeset reader needs a node id reader to identify which nodes
deba@1409
   805
  /// have to be connected. If a NodeSetReader reads an "id" named map,
deba@1409
   806
  /// it will be able to resolve the nodes by ids.
deba@1409
   807
  ///
deba@1409
   808
  /// \relates LemonReader
deba@1408
   809
  template <typename _Graph, typename _Traits = DefaultReaderTraits>
deba@1408
   810
  class EdgeSetReader : public CommonSectionReaderBase {
deba@1408
   811
    typedef CommonSectionReaderBase Parent;
deba@1408
   812
  public:
deba@1408
   813
deba@1408
   814
    typedef _Graph Graph;
deba@1408
   815
    typedef _Traits Traits;
deba@1408
   816
    typedef typename Graph::Edge Item;
deba@1408
   817
    typedef typename Traits::Skipper DefaultSkipper;
deba@1408
   818
deba@1409
   819
    /// \brief Constructor.
deba@1409
   820
    ///
deba@1409
   821
    /// Constructor for EdgeSetReader. It creates the EdgeSetReader and
deba@1409
   822
    /// attach it into the given LemonReader. The edgeset reader will
deba@1409
   823
    /// add the readed edges to the given Graph. It will use the given
deba@1409
   824
    /// node id reader to read the source and target nodes of the edges.
deba@1409
   825
    /// The reader will read the section only if the \c _id and the 
deba@1409
   826
    /// \c edgset_id are the same. 
deba@1409
   827
    template <typename NodeIdReader>
deba@1421
   828
    EdgeSetReader(LemonReader& _reader, 
deba@1421
   829
		  typename SmartParameter<Graph>::Type _graph, 
deba@1409
   830
		  const NodeIdReader& _nodeIdReader, 
deba@1408
   831
		  const std::string& _id = std::string(),
deba@1409
   832
		  const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1409
   833
      : Parent(_reader), graph(_graph), id(_id), skipper(_skipper),
deba@1409
   834
	nodeIdReader(new IdReader<typename Graph::Node, NodeIdReader>
deba@1409
   835
		     (_nodeIdReader)) {} 
deba@1408
   836
deba@1409
   837
    /// \brief Destructor.
deba@1409
   838
    ///
deba@1409
   839
    /// Destructor for EdgeSetReader.
deba@1408
   840
    virtual ~EdgeSetReader() {
deba@1408
   841
      for (typename MapReaders::iterator it = readers.begin(); 
deba@1408
   842
	   it != readers.end(); ++it) {
deba@1408
   843
	delete it->second;
deba@1408
   844
      }
deba@1408
   845
    }
deba@1408
   846
deba@1408
   847
  private:
deba@1408
   848
    EdgeSetReader(const EdgeSetReader&);
deba@1408
   849
    void operator=(const EdgeSetReader&);
deba@1408
   850
deba@1408
   851
  public:
deba@1408
   852
deba@1409
   853
    /// \brief Add a new edge map reader command for the reader.
deba@1408
   854
    ///
deba@1409
   855
    /// Add a new edge map reader command for the reader.
deba@1408
   856
    template <typename Map>
deba@1421
   857
    EdgeSetReader& readEdgeMap(std::string name, Map& map) {
deba@1421
   858
      return _readMap<
deba@1421
   859
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1421
   860
	typename SmartParameter<Map>::Type>(name, map);
deba@1421
   861
    }
deba@1421
   862
deba@1421
   863
    template <typename Map>
deba@1421
   864
    EdgeSetReader& readEdgeMap(std::string name, const Map& map) {
deba@1421
   865
      return _readMap<
deba@1421
   866
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1421
   867
	typename SmartParameter<Map>::Type>(name, map);
deba@1408
   868
    }
deba@1408
   869
deba@1409
   870
    /// \brief Add a new edge map reader command for the reader.
deba@1408
   871
    ///
deba@1409
   872
    /// Add a new edge map reader command for the reader.
deba@1408
   873
    template <typename Reader, typename Map>
deba@1421
   874
    EdgeSetReader& readEdgeMap(std::string name, Map& map, 
deba@1421
   875
			   const Reader& reader = Reader()) {
deba@1421
   876
      return _readMap<
deba@1421
   877
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1421
   878
	typename SmartParameter<Map>::Type>(name, map, reader);
deba@1421
   879
    }
deba@1421
   880
deba@1421
   881
    template <typename Reader, typename Map>
deba@1421
   882
    EdgeSetReader& readEdgeMap(std::string name, const Map& map, 
deba@1421
   883
			       const Reader& reader = Reader()) {
deba@1421
   884
      return _readMap<
deba@1421
   885
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1421
   886
	typename SmartParameter<Map>::Type>(name, map, reader);
deba@1421
   887
    }
deba@1421
   888
deba@1421
   889
  private:
deba@1421
   890
deba@1421
   891
    template <typename Reader, typename Map, typename MapParameter>
deba@1421
   892
    EdgeSetReader& _readMap(std::string name, MapParameter map, 
deba@1421
   893
			    const Reader& reader = Reader()) {
deba@1408
   894
      if (readers.find(name) != readers.end()) {
deba@1408
   895
	ErrorMessage msg;
deba@1408
   896
	msg << "Multiple read rule for edge map: " << name;
deba@1408
   897
	throw IOParameterError(msg.message());
deba@1408
   898
      }
deba@1408
   899
      readers.insert(
deba@1408
   900
	make_pair(name, new MapReader<Item, Map, Reader>(map, reader)));
deba@1408
   901
      return *this;
deba@1408
   902
    }
deba@1408
   903
deba@1421
   904
  public:
deba@1421
   905
deba@1409
   906
    /// \brief Add a new edge map skipper command for the reader.
deba@1408
   907
    ///
deba@1409
   908
    /// Add a new edge map skipper command for the reader.
deba@1408
   909
    template <typename Reader>
deba@1421
   910
    EdgeSetReader& skipEdgeMap(std::string name, 
deba@1421
   911
			       const Reader& reader = Reader()) {
deba@1408
   912
      if (readers.find(name) != readers.end()) {
deba@1408
   913
	ErrorMessage msg;
deba@1421
   914
	msg << "Multiple read rule for edge map: " << name;
deba@1408
   915
	throw IOParameterError(msg.message());
deba@1408
   916
      }
deba@1408
   917
      readers.insert(make_pair(name, new SkipReader<Item, Reader>(reader)));
deba@1408
   918
      return *this;
deba@1408
   919
    }
deba@1408
   920
deba@1409
   921
  protected:
deba@1409
   922
deba@1409
   923
    /// \brief Gives back true when the SectionReader can process 
deba@1409
   924
    /// the section with the given header line.
deba@1409
   925
    ///
deba@1421
   926
    /// It gives back true when the header line starts with \c \@edgeset,
deba@1409
   927
    /// and the header line's id and the edgeset's id are the same.
deba@1408
   928
    virtual bool header(const std::string& line) {
deba@1408
   929
      std::istringstream ls(line);
deba@1408
   930
      std::string command;
deba@1408
   931
      std::string name;
deba@1408
   932
      ls >> command >> name;
deba@1408
   933
      return command == "@edgeset" && name == id;
deba@1408
   934
    }
deba@1408
   935
deba@1409
   936
    /// \brief Reader function of the section.
deba@1409
   937
    ///
deba@1409
   938
    /// It reads the content of the section.
deba@1408
   939
    virtual void read(std::istream& is) {
deba@1408
   940
      std::vector<ReaderBase<Item>* > index;
deba@1408
   941
      std::string line;
deba@1408
   942
deba@1408
   943
      getline(is, line);
deba@1408
   944
      std::istringstream ls(line);	
deba@1408
   945
      while (ls >> id) {
deba@1408
   946
	typename MapReaders::iterator it = readers.find(id);
deba@1408
   947
	if (it != readers.end()) {
deba@1408
   948
	  index.push_back(it->second);
deba@1408
   949
	} else {
deba@1408
   950
	  index.push_back(&skipper);
deba@1408
   951
	}
deba@1408
   952
	if (id == "id" && inverter.get() == 0) {
deba@1408
   953
	  inverter.reset(index.back()->getInverter());
deba@1408
   954
	  index.back() = inverter.get();
deba@1408
   955
	}
deba@1408
   956
      }
deba@1408
   957
      while (getline(is, line)) {	
deba@1408
   958
	std::istringstream ls(line);
deba@1409
   959
	typename Graph::Node from = nodeIdReader->read(ls);
deba@1409
   960
	typename Graph::Node to = nodeIdReader->read(ls);
deba@1408
   961
	typename Graph::Edge edge = graph.addEdge(from, to);
deba@1408
   962
	for (int i = 0; i < (int)index.size(); ++i) {
deba@1408
   963
	  index[i]->read(ls, edge);
deba@1408
   964
	}
deba@1408
   965
      }
deba@1408
   966
    }
deba@1408
   967
deba@1409
   968
  public:
deba@1409
   969
deba@1409
   970
    /// \brief Returns true if the edgeset can give back the edge by its id.
deba@1409
   971
    ///
deba@1409
   972
    /// Returns true if the edgeset can give back the edge by its id.
deba@1409
   973
    /// It is possible only if an "id" named map was read.
deba@1409
   974
    bool isIdReader() const {
deba@1408
   975
      return inverter.get() != 0;
deba@1408
   976
    }
deba@1408
   977
deba@1409
   978
    /// \brief Gives back the edge by its id.
deba@1409
   979
    ///
deba@1409
   980
    /// It reads an id from the stream and gives back which edge belongs to
deba@1409
   981
    /// it. It is possible only if there was read an "id" named map.
deba@1421
   982
    Item readId(std::istream& is) const {
deba@1408
   983
      return inverter->read(is);
deba@1408
   984
    } 
deba@1408
   985
deba@1408
   986
  private:
deba@1408
   987
deba@1408
   988
    typedef std::map<std::string, ReaderBase<Item>*> MapReaders;
deba@1408
   989
    MapReaders readers;
deba@1408
   990
   
deba@1421
   991
    typename SmartReference<Graph>::Type graph;   
deba@1421
   992
    std::string id;
deba@1421
   993
    SkipReader<Item, DefaultSkipper> skipper;
deba@1421
   994
deba@1421
   995
    std::auto_ptr<InverterBase<Item> > inverter;
deba@1421
   996
    std::auto_ptr<IdReaderBase<typename Graph::Node> > nodeIdReader;
deba@1421
   997
  };
deba@1421
   998
deba@1421
   999
  /// \ingroup io_group
deba@1421
  1000
  /// \brief SectionReader for reading a undirected graph's edgeset.
deba@1421
  1001
  ///
deba@1421
  1002
  /// The lemon format can store multiple undirected edgesets with several 
deba@1421
  1003
  /// maps. The undirected edgeset section's header line is \c \@undiredgeset 
deba@1421
  1004
  /// \c undiredgeset_id, but the \c undiredgeset_id may be empty.
deba@1421
  1005
  ///
deba@1421
  1006
  /// The first line of the section contains the names of the maps separated
deba@1421
  1007
  /// with white spaces. Each next lines describes an edge in the edgeset. The
deba@1421
  1008
  /// line contains the connected nodes' id and the mapped values for each map.
deba@1421
  1009
  ///
deba@1421
  1010
  /// The section can handle the directed as a syntactical sugar. Two
deba@1421
  1011
  /// undirected edge map describes one directed edge map. This two maps
deba@1421
  1012
  /// are the forward map and the backward map and the names of this map
deba@1421
  1013
  /// is near the same just with a prefix \c '+' or \c '-' character 
deba@1421
  1014
  /// difference.
deba@1421
  1015
  ///
deba@1421
  1016
  /// If the edgeset contains an \c "id" named map then it will be regarded
deba@1421
  1017
  /// as id map. This map should contain only unique values and when the 
deba@1421
  1018
  /// \c readId() member will read a value from the given stream it will
deba@1421
  1019
  /// give back that undiricted edge which is mapped to this value.
deba@1421
  1020
  ///
deba@1421
  1021
  /// The undirected edgeset reader needs a node id reader to identify which 
deba@1421
  1022
  /// nodes have to be connected. If a NodeSetReader reads an "id" named map,
deba@1421
  1023
  /// it will be able to resolve the nodes by ids.
deba@1421
  1024
  ///
deba@1421
  1025
  /// \relates LemonReader
deba@1421
  1026
  template <typename _Graph, typename _Traits = DefaultReaderTraits>
deba@1421
  1027
  class UndirEdgeSetReader : public CommonSectionReaderBase {
deba@1421
  1028
    typedef CommonSectionReaderBase Parent;
deba@1421
  1029
  public:
deba@1421
  1030
deba@1421
  1031
    typedef _Graph Graph;
deba@1421
  1032
    typedef _Traits Traits;
deba@1421
  1033
    typedef typename Graph::UndirEdge Item;
deba@1421
  1034
    typedef typename Traits::Skipper DefaultSkipper;
deba@1421
  1035
deba@1421
  1036
    /// \brief Constructor.
deba@1421
  1037
    ///
deba@1421
  1038
    /// Constructor for UndirEdgeSetReader. It creates the UndirEdgeSetReader 
deba@1421
  1039
    /// and attach it into the given LemonReader. The undirected edgeset 
deba@1421
  1040
    /// reader will add the readed undirected edges to the given Graph. It 
deba@1421
  1041
    /// will use the given node id reader to read the source and target 
deba@1421
  1042
    /// nodes of the edges. The reader will read the section only if the 
deba@1421
  1043
    /// \c _id and the \c undiredgset_id are the same. 
deba@1421
  1044
    template <typename NodeIdReader>
deba@1421
  1045
    UndirEdgeSetReader(LemonReader& _reader, 
deba@1421
  1046
		       typename SmartParameter<Graph>::Type _graph, 
deba@1421
  1047
		       const NodeIdReader& _nodeIdReader, 
deba@1421
  1048
		       const std::string& _id = std::string(),
deba@1421
  1049
		       const DefaultSkipper& _skipper = DefaultSkipper()) 
deba@1421
  1050
      : Parent(_reader), graph(_graph), id(_id), skipper(_skipper),
deba@1421
  1051
	nodeIdReader(new IdReader<typename Graph::Node, NodeIdReader>
deba@1421
  1052
		     (_nodeIdReader)) {} 
deba@1421
  1053
deba@1421
  1054
    /// \brief Destructor.
deba@1421
  1055
    ///
deba@1421
  1056
    /// Destructor for UndirEdgeSetReader.
deba@1421
  1057
    virtual ~UndirEdgeSetReader() {
deba@1421
  1058
      for (typename MapReaders::iterator it = readers.begin(); 
deba@1421
  1059
	   it != readers.end(); ++it) {
deba@1421
  1060
	delete it->second;
deba@1421
  1061
      }
deba@1421
  1062
    }
deba@1421
  1063
deba@1421
  1064
  private:
deba@1421
  1065
    UndirEdgeSetReader(const UndirEdgeSetReader&);
deba@1421
  1066
    void operator=(const UndirEdgeSetReader&);
deba@1421
  1067
deba@1421
  1068
  public:
deba@1421
  1069
deba@1421
  1070
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
  1071
    ///
deba@1421
  1072
    /// Add a new edge undirected map reader command for the reader.
deba@1421
  1073
    template <typename Map>
deba@1421
  1074
    UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map) {
deba@1421
  1075
      return _readMap<
deba@1421
  1076
	typename Traits::template Reader<typename Map::Value>, Map, 
deba@1421
  1077
	typename SmartParameter<Map>::Type>(name, map);
deba@1421
  1078
    }
deba@1421
  1079
deba@1421
  1080
    template <typename Map>
deba@1421
  1081
    UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map) {
deba@1421
  1082
      return _readMap<
deba@1421
  1083
	typename Traits::template Reader<typename Map::Value>, Map, 
deba@1421
  1084
	typename SmartParameter<Map>::Type>(name, map);
deba@1421
  1085
    }
deba@1421
  1086
deba@1421
  1087
    /// \brief Add a new undirected edge map reader command for the reader.
deba@1421
  1088
    ///
deba@1421
  1089
    /// Add a new edge undirected map reader command for the reader.
deba@1421
  1090
    template <typename Reader, typename Map>
deba@1421
  1091
    UndirEdgeSetReader& readUndirEdgeMap(std::string name, Map& map, 
deba@1421
  1092
					 const Reader& reader = Reader()) {
deba@1421
  1093
      return _readMap<Reader, Map, typename SmartParameter<Map>::Type>
deba@1421
  1094
	(name, map, reader);
deba@1421
  1095
    }
deba@1421
  1096
deba@1421
  1097
    template <typename Reader, typename Map>
deba@1421
  1098
    UndirEdgeSetReader& readUndirEdgeMap(std::string name, const Map& map, 
deba@1421
  1099
					 const Reader& reader = Reader()) {
deba@1421
  1100
      return _readMap<Reader, Map, typename SmartParameter<Map>::Type >
deba@1421
  1101
	(name, map, reader);
deba@1421
  1102
    }
deba@1421
  1103
deba@1421
  1104
  private:
deba@1421
  1105
deba@1421
  1106
    template <typename Reader, typename Map, typename MapParameter>
deba@1421
  1107
    UndirEdgeSetReader& _readMap(std::string name, MapParameter map,
deba@1421
  1108
				 const Reader& reader = Reader()) {
deba@1421
  1109
      if (readers.find(name) != readers.end()) {
deba@1421
  1110
	ErrorMessage msg;
deba@1421
  1111
	msg << "Multiple read rule for edge map: " << name;
deba@1421
  1112
	throw IOParameterError(msg.message());
deba@1421
  1113
      }
deba@1421
  1114
      readers.insert(
deba@1421
  1115
	make_pair(name, new MapReader<Item, Map, Reader>(map, reader)));
deba@1421
  1116
      return *this;
deba@1421
  1117
    }
deba@1421
  1118
deba@1421
  1119
  public:
deba@1421
  1120
deba@1421
  1121
    /// \brief Add a new undirected edge map skipper command for the reader.
deba@1421
  1122
    ///
deba@1421
  1123
    /// Add a new undirected edge map skipper command for the reader.
deba@1421
  1124
    template <typename Reader>
deba@1421
  1125
    UndirEdgeSetReader& skipUndirEdgeMap(std::string name, 
deba@1421
  1126
					 const Reader& reader = Reader()) {
deba@1421
  1127
      if (readers.find(name) != readers.end()) {
deba@1421
  1128
	ErrorMessage msg;
deba@1421
  1129
	msg << "Multiple read rule for node map: " << name;
deba@1421
  1130
	throw IOParameterError(msg.message());
deba@1421
  1131
      }
deba@1421
  1132
      readers.insert(make_pair(name, new SkipReader<Item, Reader>(reader)));
deba@1421
  1133
      return *this;
deba@1421
  1134
    }
deba@1421
  1135
deba@1421
  1136
    /// \brief Add a new directed edge map reader command for the reader.
deba@1421
  1137
    ///
deba@1421
  1138
    /// Add a new directed edge map reader command for the reader.
deba@1421
  1139
    template <typename Map>
deba@1421
  1140
    UndirEdgeSetReader& readEdgeMap(std::string name, Map& map) {
deba@1421
  1141
      return _readDirMap<
deba@1421
  1142
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1421
  1143
	typename SmartParameter<Map>::Type>(name, map);
deba@1421
  1144
    }
deba@1421
  1145
deba@1421
  1146
    template <typename Map>
deba@1421
  1147
    UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map) {
deba@1421
  1148
      return _readDirMap<
deba@1421
  1149
	typename Traits::template Reader<typename Map::Value>, Map,
deba@1421
  1150
	typename SmartParameter<Map>::Type>(name, map);
deba@1421
  1151
    }
deba@1421
  1152
deba@1421
  1153
    /// \brief Add a new directed edge map reader command for the reader.
deba@1421
  1154
    ///
deba@1421
  1155
    /// Add a new directed edge map reader command for the reader.
deba@1421
  1156
    template <typename Reader, typename Map>
deba@1421
  1157
    UndirEdgeSetReader& readEdgeMap(std::string name, Map& map, 
deba@1421
  1158
				    const Reader& reader = Reader()) {
deba@1421
  1159
      return _readDirMap<Reader, Map, typename SmartParameter<Map>::Type>
deba@1421
  1160
	(name, map, reader);
deba@1421
  1161
    }
deba@1421
  1162
deba@1421
  1163
    template <typename Reader, typename Map>
deba@1421
  1164
    UndirEdgeSetReader& readEdgeMap(std::string name, const Map& map, 
deba@1421
  1165
				    const Reader& reader = Reader()) {
deba@1421
  1166
      return _readDirMap<Reader, Map, typename SmartParameter<Map>::Type>
deba@1421
  1167
	(name, map, reader);
deba@1421
  1168
    }
deba@1421
  1169
deba@1421
  1170
  private:
deba@1421
  1171
deba@1421
  1172
    template <typename Reader, typename Map, typename MapParameter>
deba@1421
  1173
    UndirEdgeSetReader& _readDirMap(std::string name, MapParameter map,
deba@1421
  1174
				    const Reader& reader = Reader()) {
deba@1421
  1175
      readMap("+" + name, 
deba@1421
  1176
	      _reader_bits::writeComposeMap(map, forwardMap(graph)), reader);
deba@1421
  1177
      readMap("-" + name, 
deba@1421
  1178
	      _reader_bits::writeComposeMap(map, backwardMap(graph)), reader);
deba@1421
  1179
      return *this;      
deba@1421
  1180
    }
deba@1421
  1181
deba@1421
  1182
  public:
deba@1421
  1183
deba@1421
  1184
    /// \brief Add a new directed edge map skipper command for the reader.
deba@1421
  1185
    ///
deba@1421
  1186
    /// Add a new directed edge map skipper command for the reader.
deba@1421
  1187
    template <typename Reader>
deba@1421
  1188
    UndirEdgeSetReader& skipEdgeMap(std::string name, 
deba@1421
  1189
				    const Reader& reader = Reader()) {
deba@1421
  1190
      skipMap("+" + name, reader);
deba@1421
  1191
      skipMap("-" + name, reader);
deba@1421
  1192
      return *this;
deba@1421
  1193
    }
deba@1421
  1194
deba@1421
  1195
  protected:
deba@1421
  1196
deba@1421
  1197
    /// \brief Gives back true when the SectionReader can process 
deba@1421
  1198
    /// the section with the given header line.
deba@1421
  1199
    ///
deba@1421
  1200
    /// It gives back true when the header line starts with \c \@undiredgeset,
deba@1421
  1201
    /// and the header line's id and the edgeset's id are the same.
deba@1421
  1202
    virtual bool header(const std::string& line) {
deba@1421
  1203
      std::istringstream ls(line);
deba@1421
  1204
      std::string command;
deba@1421
  1205
      std::string name;
deba@1421
  1206
      ls >> command >> name;
deba@1421
  1207
      return command == "@undiredgeset" && name == id;
deba@1421
  1208
    }
deba@1421
  1209
deba@1421
  1210
    /// \brief Reader function of the section.
deba@1421
  1211
    ///
deba@1421
  1212
    /// It reads the content of the section.
deba@1421
  1213
    virtual void read(std::istream& is) {
deba@1421
  1214
      std::vector<ReaderBase<Item>* > index;
deba@1421
  1215
      std::string line;
deba@1421
  1216
deba@1421
  1217
      getline(is, line);
deba@1421
  1218
      std::istringstream ls(line);	
deba@1421
  1219
      while (ls >> id) {
deba@1421
  1220
	typename MapReaders::iterator it = readers.find(id);
deba@1421
  1221
	if (it != readers.end()) {
deba@1421
  1222
	  index.push_back(it->second);
deba@1421
  1223
	} else {
deba@1421
  1224
	  index.push_back(&skipper);
deba@1421
  1225
	}
deba@1421
  1226
	if (id == "id" && inverter.get() == 0) {
deba@1421
  1227
	  inverter.reset(index.back()->getInverter());
deba@1421
  1228
	  index.back() = inverter.get();
deba@1421
  1229
	}
deba@1421
  1230
      }
deba@1421
  1231
      while (getline(is, line)) {	
deba@1421
  1232
	std::istringstream ls(line);
deba@1421
  1233
	typename Graph::Node from = nodeIdReader->read(ls);
deba@1421
  1234
	typename Graph::Node to = nodeIdReader->read(ls);
deba@1421
  1235
	typename Graph::UndirEdge edge = graph.addEdge(from, to);
deba@1421
  1236
	for (int i = 0; i < (int)index.size(); ++i) {
deba@1421
  1237
	  index[i]->read(ls, edge);
deba@1421
  1238
	}
deba@1421
  1239
      }
deba@1421
  1240
    }
deba@1421
  1241
deba@1421
  1242
  public:
deba@1421
  1243
deba@1421
  1244
    /// \brief Returns true if the edgeset can give back the edge by its id.
deba@1421
  1245
    ///
deba@1421
  1246
    /// Returns true if the edgeset can give back the undirected edge by its 
deba@1421
  1247
    /// id. It is possible only if an "id" named map was read.
deba@1421
  1248
    bool isIdReader() const {
deba@1421
  1249
      return inverter.get() != 0;
deba@1421
  1250
    }
deba@1421
  1251
deba@1421
  1252
    /// \brief Gives back the undirected edge by its id.
deba@1421
  1253
    ///
deba@1421
  1254
    /// It reads an id from the stream and gives back which undirected edge 
deba@1421
  1255
    /// belongs to it. It is possible only if there was read an "id" named map.
deba@1421
  1256
    Item readId(std::istream& is) const {
deba@1421
  1257
      return inverter->read(is);
deba@1421
  1258
    } 
deba@1421
  1259
deba@1421
  1260
  private:
deba@1421
  1261
deba@1421
  1262
    typedef std::map<std::string, ReaderBase<Item>*> MapReaders;
deba@1421
  1263
    MapReaders readers;
deba@1421
  1264
   
deba@1421
  1265
    typename SmartReference<Graph>::Type graph;   
deba@1408
  1266
    std::string id;
deba@1408
  1267
    SkipReader<Item, DefaultSkipper> skipper;
deba@1408
  1268
deba@1408
  1269
    std::auto_ptr<InverterBase<Item> > inverter;
deba@1409
  1270
    std::auto_ptr<IdReaderBase<typename Graph::Node> > nodeIdReader;
deba@1408
  1271
  };
deba@1408
  1272
deba@1409
  1273
  /// \ingroup io_group
deba@1409
  1274
  /// \brief SectionReader for reading labeled nodes.
deba@1409
  1275
  ///
deba@1409
  1276
  /// The nodes section's header line is \c \@nodes \c nodes_id, but the
deba@1409
  1277
  /// \c nodes_id may be empty.
deba@1409
  1278
  ///
deba@1409
  1279
  /// Each line in the section contains the name of the node 
deba@1409
  1280
  /// and then the node id. 
deba@1409
  1281
  ///
deba@1409
  1282
  /// \relates LemonReader
deba@1409
  1283
  template <typename _Graph>
deba@1409
  1284
  class NodeReader : public CommonSectionReaderBase {
deba@1409
  1285
    typedef CommonSectionReaderBase Parent;
deba@1409
  1286
    typedef _Graph Graph;
deba@1409
  1287
    typedef typename Graph::Node Item;
deba@1409
  1288
  public:
deba@1409
  1289
    
deba@1409
  1290
    /// \brief Constructor.
deba@1409
  1291
    ///
deba@1409
  1292
    /// Constructor for NodeReader. It creates the NodeReader and
deba@1409
  1293
    /// attach it into the given LemonReader. It will use the given
deba@1409
  1294
    /// node id reader to give back the nodes. The reader will read the 
deba@1409
  1295
    /// section only if the \c _id and the \c nodes_id are the same. 
deba@1409
  1296
    template <typename _IdReader>
deba@1409
  1297
    NodeReader(LemonReader& _reader, const _IdReader& _idReader, 
deba@1409
  1298
	       const std::string& _id = std::string()) 
deba@1409
  1299
      : Parent(_reader), id(_id), 
deba@1409
  1300
	idReader(new IdReader<typename Graph::Node, _IdReader>(_idReader)) {} 
deba@1408
  1301
deba@1409
  1302
    /// \brief Destructor.
deba@1409
  1303
    ///
deba@1409
  1304
    /// Destructor for NodeReader.
deba@1409
  1305
    virtual ~NodeReader() {}
deba@1409
  1306
deba@1409
  1307
  private:
deba@1409
  1308
    NodeReader(const NodeReader&);
deba@1409
  1309
    void operator=(const NodeReader&);
deba@1409
  1310
deba@1409
  1311
  public:
deba@1409
  1312
deba@1409
  1313
    /// \brief Add a node reader command for the NodeReader.
deba@1409
  1314
    ///
deba@1409
  1315
    /// Add a node reader command for the NodeReader.
deba@1409
  1316
    void readNode(const std::string& name, Item& item) {
deba@1409
  1317
      if (readers.find(name) != readers.end()) {
deba@1409
  1318
	ErrorMessage msg;
deba@1409
  1319
	msg << "Multiple read rule for node: " << name;
deba@1409
  1320
	throw IOParameterError(msg.message());
deba@1409
  1321
      }
deba@1409
  1322
      readers.insert(make_pair(name, &item));
deba@1409
  1323
    }
deba@1409
  1324
deba@1409
  1325
  protected:
deba@1409
  1326
deba@1409
  1327
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1328
    /// the section with the given header line.
deba@1409
  1329
    ///
deba@1421
  1330
    /// It gives back true when the header line start with \c \@nodes,
deba@1409
  1331
    /// and the header line's id and the reader's id are the same.
deba@1409
  1332
    virtual bool header(const std::string& line) {
deba@1409
  1333
      std::istringstream ls(line);
deba@1409
  1334
      std::string command;
deba@1409
  1335
      std::string name;
deba@1409
  1336
      ls >> command >> name;
deba@1409
  1337
      return command == "@nodes" && name == id;
deba@1409
  1338
    }
deba@1409
  1339
deba@1409
  1340
    /// \brief Reader function of the section.
deba@1409
  1341
    ///
deba@1409
  1342
    /// It reads the content of the section.
deba@1409
  1343
    virtual void read(std::istream& is) {
deba@1409
  1344
      std::string line;
deba@1409
  1345
      while (getline(is, line)) {
deba@1409
  1346
	std::istringstream ls(line);
deba@1409
  1347
	std::string id;
deba@1409
  1348
	ls >> id;
deba@1409
  1349
	typename ItemReaders::iterator it = readers.find(id);
deba@1409
  1350
	if (it != readers.end()) {
deba@1409
  1351
	  *(it->second) = idReader->read(ls); 
deba@1409
  1352
	}	
deba@1409
  1353
      }
deba@1409
  1354
    }
deba@1409
  1355
    
deba@1409
  1356
  private:
deba@1409
  1357
deba@1409
  1358
    std::string id;
deba@1409
  1359
deba@1409
  1360
    typedef std::map<std::string, Item*> ItemReaders;
deba@1409
  1361
    ItemReaders readers;
deba@1409
  1362
    std::auto_ptr<IdReaderBase<Item> > idReader;
deba@1409
  1363
  };
deba@1409
  1364
deba@1409
  1365
  /// \ingroup io_group
deba@1409
  1366
  /// \brief SectionReader for reading labeled edges.
deba@1409
  1367
  ///
deba@1409
  1368
  /// The edges section's header line is \c \@edges \c edges_id, but the
deba@1409
  1369
  /// \c edges_id may be empty.
deba@1409
  1370
  ///
deba@1409
  1371
  /// Each line in the section contains the name of the edge 
deba@1409
  1372
  /// and then the edge id. 
deba@1409
  1373
  ///
deba@1409
  1374
  /// \relates LemonReader
deba@1409
  1375
  template <typename _Graph>
deba@1409
  1376
  class EdgeReader : public CommonSectionReaderBase {
deba@1409
  1377
    typedef CommonSectionReaderBase Parent;
deba@1409
  1378
    typedef _Graph Graph;
deba@1409
  1379
    typedef typename Graph::Edge Item;
deba@1409
  1380
  public:
deba@1409
  1381
    
deba@1409
  1382
    /// \brief Constructor.
deba@1409
  1383
    ///
deba@1409
  1384
    /// Constructor for EdgeReader. It creates the EdgeReader and
deba@1409
  1385
    /// attach it into the given LemonReader. It will use the given
deba@1409
  1386
    /// edge id reader to give back the edges. The reader will read the 
deba@1421
  1387
    /// section only if the \c _id and the \c edges_id are the same. 
deba@1409
  1388
    template <typename _IdReader>
deba@1409
  1389
    EdgeReader(LemonReader& _reader, const _IdReader& _idReader, 
deba@1409
  1390
	       const std::string& _id = std::string()) 
deba@1409
  1391
      : Parent(_reader), id(_id), 
deba@1409
  1392
	idReader(new IdReader<typename Graph::Edge, _IdReader>(_idReader)) {} 
deba@1409
  1393
deba@1409
  1394
    /// \brief Destructor.
deba@1409
  1395
    ///
deba@1409
  1396
    /// Destructor for EdgeReader.
deba@1409
  1397
    virtual ~EdgeReader() {}
deba@1409
  1398
  private:
deba@1409
  1399
    EdgeReader(const EdgeReader&);
deba@1409
  1400
    void operator=(const EdgeReader&);
deba@1409
  1401
deba@1409
  1402
  public:
deba@1409
  1403
deba@1409
  1404
    /// \brief Add an edge reader command for the EdgeReader.
deba@1409
  1405
    ///
deba@1409
  1406
    /// Add an edge reader command for the EdgeReader.
deba@1409
  1407
    void readEdge(const std::string& name, Item& item) {
deba@1409
  1408
      if (readers.find(name) != readers.end()) {
deba@1409
  1409
	ErrorMessage msg;
deba@1409
  1410
	msg << "Multiple read rule for edge: " << name;
deba@1409
  1411
	throw IOParameterError(msg.message());
deba@1409
  1412
      }
deba@1409
  1413
      readers.insert(make_pair(name, &item));
deba@1409
  1414
    }
deba@1409
  1415
deba@1409
  1416
  protected:
deba@1409
  1417
deba@1409
  1418
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1419
    /// the section with the given header line.
deba@1409
  1420
    ///
deba@1421
  1421
    /// It gives back true when the header line start with \c \@edges,
deba@1421
  1422
    /// and the header line's id and the reader's id are the same.
deba@1421
  1423
    virtual bool header(const std::string& line) {
deba@1421
  1424
      std::istringstream ls(line);
deba@1421
  1425
      std::string command;
deba@1421
  1426
      std::string name;
deba@1421
  1427
      ls >> command >> name;
deba@1421
  1428
      return command == "@edges" && name == id;
deba@1421
  1429
    }
deba@1421
  1430
deba@1421
  1431
    /// \brief Reader function of the section.
deba@1421
  1432
    ///
deba@1421
  1433
    /// It reads the content of the section.
deba@1421
  1434
    virtual void read(std::istream& is) {
deba@1421
  1435
      std::string line;
deba@1421
  1436
      while (getline(is, line)) {
deba@1421
  1437
	std::istringstream ls(line);
deba@1421
  1438
	std::string id;
deba@1421
  1439
	ls >> id;
deba@1421
  1440
	typename ItemReaders::iterator it = readers.find(id);
deba@1421
  1441
	if (it != readers.end()) {
deba@1421
  1442
	  *(it->second) = idReader->read(ls); 
deba@1421
  1443
	}	
deba@1421
  1444
      }
deba@1421
  1445
    }
deba@1421
  1446
    
deba@1421
  1447
  private:
deba@1421
  1448
deba@1421
  1449
    std::string id;
deba@1421
  1450
deba@1421
  1451
    typedef std::map<std::string, Item*> ItemReaders;
deba@1421
  1452
    ItemReaders readers;
deba@1421
  1453
    std::auto_ptr<IdReaderBase<Item> > idReader;
deba@1421
  1454
  };
deba@1421
  1455
deba@1421
  1456
  /// \ingroup io_group
deba@1421
  1457
  /// \brief SectionReader for reading labeled undirected edges.
deba@1421
  1458
  ///
deba@1421
  1459
  /// The undirected edges section's header line is \c \@undiredges 
deba@1421
  1460
  /// \c undiredges_id, but the \c undiredges_id may be empty.
deba@1421
  1461
  ///
deba@1421
  1462
  /// Each line in the section contains the name of the undirected edge 
deba@1421
  1463
  /// and then the undirected edge id. 
deba@1421
  1464
  ///
deba@1421
  1465
  /// \relates LemonReader
deba@1421
  1466
  template <typename _Graph>
deba@1421
  1467
  class UndirEdgeReader : public CommonSectionReaderBase {
deba@1421
  1468
    typedef CommonSectionReaderBase Parent;
deba@1421
  1469
    typedef _Graph Graph;
deba@1421
  1470
    typedef typename Graph::UndirEdge Item;
deba@1421
  1471
  public:
deba@1421
  1472
    
deba@1421
  1473
    /// \brief Constructor.
deba@1421
  1474
    ///
deba@1421
  1475
    /// Constructor for UndirEdgeReader. It creates the UndirEdgeReader and
deba@1421
  1476
    /// attach it into the given LemonReader. It will use the given
deba@1421
  1477
    /// undirected edge id reader to give back the edges. The reader will 
deba@1421
  1478
    /// read the section only if the \c _id and the \c undiredges_id are 
deba@1421
  1479
    /// the same. 
deba@1421
  1480
    template <typename _IdReader>
deba@1421
  1481
    UndirEdgeReader(LemonReader& _reader, const _IdReader& _idReader, 
deba@1421
  1482
	       const std::string& _id = std::string()) 
deba@1421
  1483
      : Parent(_reader), id(_id), 
deba@1421
  1484
	idReader(new IdReader<typename Graph::UndirEdge, _IdReader>(_idReader))
deba@1421
  1485
    {} 
deba@1421
  1486
deba@1421
  1487
    /// \brief Destructor.
deba@1421
  1488
    ///
deba@1421
  1489
    /// Destructor for UndirEdgeReader.
deba@1421
  1490
    virtual ~UndirEdgeReader() {}
deba@1421
  1491
  private:
deba@1421
  1492
    UndirEdgeReader(const UndirEdgeReader&);
deba@1421
  1493
    void operator=(const UndirEdgeReader&);
deba@1421
  1494
deba@1421
  1495
  public:
deba@1421
  1496
deba@1421
  1497
    /// \brief Add an undirected edge reader command for the UndirEdgeReader.
deba@1421
  1498
    ///
deba@1421
  1499
    /// Add an undirected edge reader command for the UndirEdgeReader.
deba@1421
  1500
    void readUndirEdge(const std::string& name, Item& item) {
deba@1421
  1501
      if (readers.find(name) != readers.end()) {
deba@1421
  1502
	ErrorMessage msg;
deba@1421
  1503
	msg << "Multiple read rule for edge: " << name;
deba@1421
  1504
	throw IOParameterError(msg.message());
deba@1421
  1505
      }
deba@1421
  1506
      readers.insert(make_pair(name, &item));
deba@1421
  1507
    }
deba@1421
  1508
deba@1421
  1509
  protected:
deba@1421
  1510
deba@1421
  1511
    /// \brief Gives back true when the SectionReader can process 
deba@1421
  1512
    /// the section with the given header line.
deba@1421
  1513
    ///
deba@1421
  1514
    /// It gives back true when the header line start with \c \@edges,
deba@1409
  1515
    /// and the header line's id and the reader's id are the same.
deba@1409
  1516
    virtual bool header(const std::string& line) {
deba@1409
  1517
      std::istringstream ls(line);
deba@1409
  1518
      std::string command;
deba@1409
  1519
      std::string name;
deba@1409
  1520
      ls >> command >> name;
deba@1409
  1521
      return command == "@edges" && name == id;
deba@1409
  1522
    }
deba@1409
  1523
deba@1409
  1524
    /// \brief Reader function of the section.
deba@1409
  1525
    ///
deba@1409
  1526
    /// It reads the content of the section.
deba@1409
  1527
    virtual void read(std::istream& is) {
deba@1409
  1528
      std::string line;
deba@1409
  1529
      while (getline(is, line)) {
deba@1409
  1530
	std::istringstream ls(line);
deba@1409
  1531
	std::string id;
deba@1409
  1532
	ls >> id;
deba@1409
  1533
	typename ItemReaders::iterator it = readers.find(id);
deba@1409
  1534
	if (it != readers.end()) {
deba@1409
  1535
	  *(it->second) = idReader->read(ls); 
deba@1409
  1536
	}	
deba@1409
  1537
      }
deba@1409
  1538
    }
deba@1409
  1539
    
deba@1409
  1540
  private:
deba@1409
  1541
deba@1409
  1542
    std::string id;
deba@1409
  1543
deba@1409
  1544
    typedef std::map<std::string, Item*> ItemReaders;
deba@1409
  1545
    ItemReaders readers;
deba@1409
  1546
    std::auto_ptr<IdReaderBase<Item> > idReader;
deba@1409
  1547
  };
deba@1409
  1548
deba@1409
  1549
  /// \ingroup io_group
deba@1409
  1550
  /// \brief SectionReader for attributes.
deba@1409
  1551
  ///
deba@1409
  1552
  /// The lemon format can store multiple attribute set. Each set has
deba@1409
  1553
  /// the header line \c \@attributes \c attributeset_id, but the 
deba@1409
  1554
  /// attributeset_id may be empty.
deba@1409
  1555
  ///
deba@1409
  1556
  /// The attributeset section contains several lines. Each of them starts
deba@1409
  1557
  /// with an attribute and then a the value for the id.
deba@1409
  1558
  ///
deba@1409
  1559
  /// \relates LemonReader
deba@1408
  1560
  template <typename _Traits = DefaultReaderTraits>
deba@1408
  1561
  class AttributeReader : public CommonSectionReaderBase {
deba@1408
  1562
    typedef CommonSectionReaderBase Parent;
deba@1408
  1563
    typedef _Traits Traits; 
deba@1408
  1564
  public:
deba@1409
  1565
    /// \brief Constructor.
deba@1409
  1566
    ///
deba@1409
  1567
    /// Constructor for AttributeReader. It creates the AttributeReader and
deba@1409
  1568
    /// attach it into the given LemonReader. The reader process a section
deba@1409
  1569
    /// only if the \c section_id and the \c _id are the same.
deba@1408
  1570
    AttributeReader(LemonReader& _reader, 
deba@1409
  1571
		    const std::string& _id = std::string()) 
deba@1409
  1572
      : Parent(_reader), id(_id) {}
deba@1408
  1573
deba@1409
  1574
    /// \brief Destructor.
deba@1409
  1575
    ///
deba@1409
  1576
    /// Destructor for AttributeReader.
deba@1408
  1577
    virtual ~AttributeReader() {
deba@1408
  1578
      for (typename Readers::iterator it = readers.begin(); 
deba@1408
  1579
	   it != readers.end(); ++it) {
deba@1408
  1580
	delete it->second;
deba@1408
  1581
      }
deba@1408
  1582
    }
deba@1408
  1583
deba@1408
  1584
  private:
deba@1408
  1585
    AttributeReader(const AttributeReader&);
deba@1408
  1586
    void operator=(AttributeReader&);
deba@1408
  1587
deba@1408
  1588
  public:
deba@1409
  1589
    /// \brief Add an attribute reader command for the reader.
deba@1409
  1590
    ///
deba@1409
  1591
    /// Add an attribute reader command for the reader.
deba@1408
  1592
    template <typename Value>
deba@1408
  1593
    AttributeReader& readAttribute(const std::string& id, Value& value) {
deba@1408
  1594
      return readAttribute<typename Traits::template Reader<Value> >
deba@1408
  1595
	(id, value);
deba@1408
  1596
    }
deba@1408
  1597
deba@1409
  1598
    /// \brief Add an attribute reader command for the reader.
deba@1409
  1599
    ///
deba@1409
  1600
    /// Add an attribute reader command for the reader.
deba@1408
  1601
    template <typename Reader, typename Value>
deba@1408
  1602
    AttributeReader& readAttribute(const std::string& name, Value& value,
deba@1408
  1603
				   const Reader& reader = Reader()) {
deba@1408
  1604
      if (readers.find(name) != readers.end()) {
deba@1408
  1605
	ErrorMessage msg;
deba@1408
  1606
	msg << "Multiple read rule for attribute: " << name;
deba@1408
  1607
	throw IOParameterError(msg.message());
deba@1408
  1608
      }
deba@1408
  1609
      readers.insert(make_pair(name, new ValueReader<Value, Reader>
deba@1408
  1610
      			       (value, reader)));
deba@1408
  1611
      return *this;
deba@1408
  1612
    }
deba@1408
  1613
deba@1409
  1614
  protected:
deba@1409
  1615
deba@1409
  1616
    /// \brief Gives back true when the SectionReader can process 
deba@1409
  1617
    /// the section with the given header line.
deba@1409
  1618
    ///
deba@1421
  1619
    /// It gives back true when the header line start with \c \@attributes,
deba@1409
  1620
    /// and the header line's id and the attributeset's id are the same.
deba@1408
  1621
    bool header(const std::string& line) {
deba@1408
  1622
      std::istringstream ls(line);
deba@1408
  1623
      std::string command;
deba@1408
  1624
      std::string name;
deba@1408
  1625
      ls >> command >> name;
deba@1408
  1626
      return command == "@attributes" && name == id;
deba@1408
  1627
    }
deba@1408
  1628
deba@1409
  1629
    /// \brief Reader function of the section.
deba@1409
  1630
    ///
deba@1409
  1631
    /// It reads the content of the section.
deba@1408
  1632
    void read(std::istream& is) {
deba@1408
  1633
      std::string line;
deba@1408
  1634
      while (getline(is, line)) {
deba@1408
  1635
	std::istringstream ls(line);
deba@1408
  1636
	std::string id;
deba@1408
  1637
	ls >> id;
deba@1408
  1638
	typename Readers::iterator it = readers.find(id);
deba@1408
  1639
	if (it != readers.end()) {
deba@1408
  1640
	  it->second->read(ls);
deba@1408
  1641
	}
deba@1408
  1642
      }
deba@1408
  1643
    }    
deba@1408
  1644
deba@1408
  1645
  private:
deba@1408
  1646
    std::string id;
deba@1408
  1647
deba@1408
  1648
    typedef std::map<std::string, ValueReaderBase*> Readers;
deba@1409
  1649
    Readers readers;  
deba@1408
  1650
  };
deba@1408
  1651
deba@1423
  1652
  /// \ingroup io_group
deba@1423
  1653
  /// \brief SectionReader for retrieve what is in the file.
deba@1423
  1654
  ///
deba@1423
  1655
  /// SectionReader for retrieve what is in the file. If you want
deba@1423
  1656
  /// to know which sections, maps and items are in the file
deba@1423
  1657
  /// use the next code:
deba@1423
  1658
  /// \code
deba@1423
  1659
  /// LemonReader reader("input.lgf");
deba@1423
  1660
  /// ContentReader content(reader);
deba@1423
  1661
  /// reader.run();
deba@1423
  1662
  /// \endcode
deba@1423
  1663
  class ContentReader : public LemonReader::SectionReader {
deba@1423
  1664
    typedef LemonReader::SectionReader Parent;
deba@1423
  1665
  public:
deba@1423
  1666
    /// \brief Constructor.
deba@1423
  1667
    ///
deba@1423
  1668
    /// Constructor for
deba@1423
  1669
    ContentReader(LemonReader& _reader) : Parent(_reader) {}
deba@1423
  1670
deba@1423
  1671
    /// \brief Desctructor.
deba@1423
  1672
    ///
deba@1423
  1673
    /// Desctructor.
deba@1423
  1674
    virtual ~ContentReader() {}
deba@1423
  1675
deba@1423
  1676
    /// \brief Gives back how many nodesets are in the file.
deba@1423
  1677
    ///
deba@1423
  1678
    /// Gives back how many nodesets are in the file.
deba@1423
  1679
    int nodeSetNum() const {
deba@1423
  1680
      return nodesets.size();
deba@1423
  1681
    }
deba@1423
  1682
deba@1423
  1683
    /// \brief Gives back the name of nodeset on the indiced position.
deba@1423
  1684
    ///
deba@1423
  1685
    /// Gives back the name of nodeset on the indiced position.
deba@1423
  1686
    std::string nodeSetName(int index) const {
deba@1423
  1687
      return nodesets[index].name;
deba@1423
  1688
    }
deba@1423
  1689
deba@1423
  1690
    /// \brief Gives back the map names of nodeset on the indiced position.
deba@1423
  1691
    ///
deba@1423
  1692
    /// Gives back the map names of nodeset on the indiced position.
deba@1423
  1693
    const std::vector<std::string>& nodeSetMaps(int index) const {
deba@1423
  1694
      return nodesets[index].items;
deba@1423
  1695
    }
deba@1423
  1696
deba@1423
  1697
    /// \brief Gives back how many edgesets are in the file.
deba@1423
  1698
    ///
deba@1423
  1699
    /// Gives back how many edgesets are in the file.
deba@1423
  1700
    int edgeSetNum() const {
deba@1423
  1701
      return edgesets.size();
deba@1423
  1702
    }
deba@1423
  1703
deba@1423
  1704
    /// \brief Gives back the name of edgeset on the indiced position.
deba@1423
  1705
    ///
deba@1423
  1706
    /// Gives back the name of edgeset on the indiced position.
deba@1423
  1707
    std::string edgeSetName(int index) const {
deba@1423
  1708
      return edgesets[index].name;
deba@1423
  1709
    }
deba@1423
  1710
deba@1423
  1711
    /// \brief Gives back the map names of edgeset on the indiced position.
deba@1423
  1712
    ///
deba@1423
  1713
    /// Gives back the map names of edgeset on the indiced position.
deba@1423
  1714
    const std::vector<std::string>& edgeSetMaps(int index) const {
deba@1423
  1715
      return edgesets[index].items;
deba@1423
  1716
    }
deba@1423
  1717
deba@1423
  1718
    /// \brief Gives back how many undirected edgesets are in the file.
deba@1423
  1719
    ///
deba@1423
  1720
    /// Gives back how many undirected edgesets are in the file.
deba@1423
  1721
    int undirEdgeSetNum() const {
deba@1423
  1722
      return undiredgesets.size();
deba@1423
  1723
    }
deba@1423
  1724
deba@1423
  1725
    /// \brief Gives back the name of undirected edgeset on the indiced 
deba@1423
  1726
    /// position.
deba@1423
  1727
    ///
deba@1423
  1728
    /// Gives back the name of undirected edgeset on the indiced position.
deba@1423
  1729
    std::string undirEdgeSetName(int index) const {
deba@1423
  1730
      return undiredgesets[index].name;
deba@1423
  1731
    }
deba@1423
  1732
deba@1423
  1733
    /// \brief Gives back the map names of undirected edgeset on the indiced 
deba@1423
  1734
    /// position.
deba@1423
  1735
    ///
deba@1423
  1736
    /// Gives back the map names of undirected edgeset on the indiced position.
deba@1423
  1737
    const std::vector<std::string>& undirEdgeSetMaps(int index) const {
deba@1423
  1738
      return undiredgesets[index].items;
deba@1423
  1739
    }
deba@1423
  1740
deba@1423
  1741
    /// \brief Gives back how many labeled nodes section are in the file.
deba@1423
  1742
    ///
deba@1423
  1743
    /// Gives back how many labeled nodes section are in the file.
deba@1423
  1744
    int nodesNum() const {
deba@1423
  1745
      return nodes.size();
deba@1423
  1746
    }
deba@1423
  1747
deba@1423
  1748
    /// \brief Gives back the name of labeled nodes section on the indiced 
deba@1423
  1749
    /// position.
deba@1423
  1750
    ///
deba@1423
  1751
    /// Gives back the name of labeled nodes section on the indiced position.
deba@1423
  1752
    std::string nodesName(int index) const {
deba@1423
  1753
      return nodes[index].name;
deba@1423
  1754
    }
deba@1423
  1755
deba@1423
  1756
    /// \brief Gives back the names of the labeled nodes in the indiced 
deba@1423
  1757
    /// section.
deba@1423
  1758
    ///
deba@1423
  1759
    /// Gives back the names of the labeled nodes in the indiced section.
deba@1423
  1760
    const std::vector<std::string>& nodesItems(int index) const {
deba@1423
  1761
      return nodes[index].items;
deba@1423
  1762
    }
deba@1423
  1763
deba@1423
  1764
    /// \brief Gives back how many labeled edges section are in the file.
deba@1423
  1765
    ///
deba@1423
  1766
    /// Gives back how many labeled edges section are in the file.
deba@1423
  1767
    int edgesNum() const {
deba@1423
  1768
      return edges.size();
deba@1423
  1769
    }
deba@1423
  1770
deba@1423
  1771
    /// \brief Gives back the name of labeled edges section on the indiced 
deba@1423
  1772
    /// position.
deba@1423
  1773
    ///
deba@1423
  1774
    /// Gives back the name of labeled edges section on the indiced position.
deba@1423
  1775
    std::string edgesName(int index) const {
deba@1423
  1776
      return edges[index].name;
deba@1423
  1777
    }
deba@1423
  1778
deba@1423
  1779
    /// \brief Gives back the names of the labeled edges in the indiced 
deba@1423
  1780
    /// section.
deba@1423
  1781
    ///
deba@1423
  1782
    /// Gives back the names of the labeled edges in the indiced section.
deba@1423
  1783
    const std::vector<std::string>& edgesItems(int index) const {
deba@1423
  1784
      return edges[index].items;
deba@1423
  1785
    }
deba@1423
  1786
 
deba@1423
  1787
    /// \brief Gives back how many labeled undirected edges section are 
deba@1423
  1788
    /// in the file.
deba@1423
  1789
    ///
deba@1423
  1790
    /// Gives back how many labeled undirected edges section are in the file.
deba@1423
  1791
    int undirEdgesNum() const {
deba@1423
  1792
      return undiredges.size();
deba@1423
  1793
    }
deba@1423
  1794
deba@1423
  1795
    /// \brief Gives back the name of labeled undirected edges section 
deba@1423
  1796
    /// on the indiced position.
deba@1423
  1797
    ///
deba@1423
  1798
    /// Gives back the name of labeled undirected edges section on the 
deba@1423
  1799
    /// indiced position.
deba@1423
  1800
    std::string undirEdgesName(int index) const {
deba@1423
  1801
      return undiredges[index].name;
deba@1423
  1802
    }
deba@1423
  1803
deba@1423
  1804
    /// \brief Gives back the names of the labeled undirected edges in 
deba@1423
  1805
    /// the indiced section.
deba@1423
  1806
    ///
deba@1423
  1807
    /// Gives back the names of the labeled undirected edges in the 
deba@1423
  1808
    /// indiced section.
deba@1423
  1809
    const std::vector<std::string>& undirEdgesItems(int index) const {
deba@1423
  1810
      return undiredges[index].items;
deba@1423
  1811
    }
deba@1423
  1812
deba@1423
  1813
 
deba@1423
  1814
    /// \brief Gives back how many attributes section are in the file.
deba@1423
  1815
    ///
deba@1423
  1816
    /// Gives back how many attributes section are in the file.
deba@1423
  1817
    int attributesNum() const {
deba@1423
  1818
      return attributes.size();
deba@1423
  1819
    }
deba@1423
  1820
deba@1423
  1821
    /// \brief Gives back the name of attributes section on the indiced 
deba@1423
  1822
    /// position.
deba@1423
  1823
    ///
deba@1423
  1824
    /// Gives back the name of attributes section on the indiced position.
deba@1423
  1825
    std::string attributesName(int index) const {
deba@1423
  1826
      return attributes[index].name;
deba@1423
  1827
    }
deba@1423
  1828
deba@1423
  1829
    /// \brief Gives back the names of the attributes in the indiced section.
deba@1423
  1830
    ///
deba@1423
  1831
    /// Gives back the names of the attributes in the indiced section.
deba@1423
  1832
    const std::vector<std::string>& attributesItems(int index) const {
deba@1423
  1833
      return attributes[index].items;
deba@1423
  1834
    }
deba@1423
  1835
deba@1423
  1836
    const std::vector<std::string>& otherSections() const {
deba@1423
  1837
      return sections;
deba@1423
  1838
    }
deba@1423
  1839
deba@1423
  1840
  protected:
deba@1423
  1841
    
deba@1423
  1842
    /// \brief Gives back true when the SectionReader can process 
deba@1423
  1843
    /// the section with the given header line.
deba@1423
  1844
    ///
deba@1423
  1845
    /// It gives back true when the section is common section.
deba@1423
  1846
    bool header(const std::string& line) {
deba@1423
  1847
      std::istringstream ls(line);
deba@1423
  1848
      std::string command, name;
deba@1423
  1849
      ls >> command >> name;
deba@1423
  1850
      if (command == "@nodeset") {
deba@1423
  1851
	current = command;
deba@1423
  1852
	nodesets.push_back(SectionInfo(name));
deba@1423
  1853
      } else if (command == "@edgeset") {
deba@1423
  1854
	current = command;
deba@1423
  1855
	edgesets.push_back(SectionInfo(name));
deba@1423
  1856
      } else if (command == "@undiredgeset") {
deba@1423
  1857
	current = command;
deba@1423
  1858
	undiredgesets.push_back(SectionInfo(name));
deba@1423
  1859
      } else if (command == "@nodes") {
deba@1423
  1860
	current = command;
deba@1423
  1861
	nodes.push_back(SectionInfo(name));
deba@1423
  1862
      } else if (command == "@edges") {
deba@1423
  1863
	current = command;
deba@1423
  1864
	edges.push_back(SectionInfo(name));
deba@1423
  1865
      } else if (command == "@undiredges") {
deba@1423
  1866
	current = command;
deba@1423
  1867
	undiredges.push_back(SectionInfo(name));
deba@1423
  1868
      } else if (command == "@attributes") {
deba@1423
  1869
	current = command;
deba@1423
  1870
	attributes.push_back(SectionInfo(name));
deba@1423
  1871
      } else {
deba@1423
  1872
	sections.push_back(line);
deba@1423
  1873
	return false;
deba@1423
  1874
      }
deba@1423
  1875
      return true;
deba@1423
  1876
    }
deba@1423
  1877
deba@1423
  1878
    /// \brief Retrieve the items from various sections.
deba@1423
  1879
    ///
deba@1423
  1880
    /// Retrieve the items from various sections.
deba@1423
  1881
    void read(std::istream& is) {
deba@1423
  1882
      if (current == "@nodeset") {
deba@1423
  1883
	readMapNames(is, nodesets.back().items);
deba@1423
  1884
      } else if (current == "@edgeset") {
deba@1423
  1885
	readMapNames(is, edgesets.back().items);
deba@1423
  1886
      } else if (current == "@undiredgeset") {
deba@1423
  1887
	readMapNames(is, undiredgesets.back().items);
deba@1423
  1888
      } else if (current == "@nodes") {
deba@1423
  1889
	readItemNames(is, nodes.back().items);
deba@1423
  1890
      } else if (current == "@edges") {
deba@1423
  1891
	readItemNames(is, edges.back().items);
deba@1423
  1892
      } else if (current == "@undiredges") {
deba@1423
  1893
	readItemNames(is, undiredges.back().items);
deba@1423
  1894
      } else if (current == "@attributes") {
deba@1423
  1895
	readItemNames(is, attributes.back().items);
deba@1423
  1896
      }
deba@1423
  1897
    }    
deba@1423
  1898
deba@1423
  1899
  private:
deba@1423
  1900
deba@1423
  1901
    void readMapNames(std::istream& is, std::vector<std::string>& maps) {
deba@1423
  1902
      std::string line, id;
deba@1423
  1903
      std::getline(is, line);
deba@1423
  1904
      std::istringstream ls(line);
deba@1423
  1905
      while (ls >> id) {
deba@1423
  1906
	maps.push_back(id);
deba@1423
  1907
      }
deba@1423
  1908
      while (getline(is, line));
deba@1423
  1909
    }
deba@1423
  1910
deba@1423
  1911
    void readItemNames(std::istream& is, std::vector<std::string>& maps) {
deba@1423
  1912
      std::string line, id;
deba@1423
  1913
      while (std::getline(is, line)) {
deba@1423
  1914
	std::istringstream ls(line);
deba@1423
  1915
	ls >> id;
deba@1423
  1916
	maps.push_back(id);
deba@1423
  1917
      }
deba@1423
  1918
    }
deba@1423
  1919
deba@1423
  1920
    struct SectionInfo {
deba@1423
  1921
      std::string name;
deba@1423
  1922
      std::vector<std::string> items;
deba@1423
  1923
deba@1423
  1924
      SectionInfo(const std::string& _name) : name(_name) {}
deba@1423
  1925
    };
deba@1423
  1926
deba@1423
  1927
    std::vector<SectionInfo> nodesets;
deba@1423
  1928
    std::vector<SectionInfo> edgesets;
deba@1423
  1929
    std::vector<SectionInfo> undiredgesets;
deba@1423
  1930
deba@1423
  1931
    std::vector<SectionInfo> nodes;
deba@1423
  1932
    std::vector<SectionInfo> edges;
deba@1423
  1933
    std::vector<SectionInfo> undiredges;
deba@1423
  1934
deba@1423
  1935
    std::vector<SectionInfo> attributes;
deba@1423
  1936
deba@1423
  1937
    std::vector<std::string> sections;
deba@1423
  1938
deba@1423
  1939
    std::string current;
deba@1423
  1940
deba@1423
  1941
  };
deba@1423
  1942
deba@1408
  1943
}
deba@1408
  1944
#endif