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