lemon/lgf_reader.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 15 Jun 2008 09:19:53 +0200
changeset 169 5b507a86ad72
parent 163 c82fd9568d75
child 172 c94a80f38d7f
child 173 b026e9779b28
permissions -rw-r--r--
Fix various rename bugs
deba@127
     1
/* -*- C++ -*-
deba@127
     2
 *
deba@127
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@127
     4
 *
deba@127
     5
 * Copyright (C) 2003-2008
deba@127
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@127
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@127
     8
 *
deba@127
     9
 * Permission to use, modify and distribute this software is granted
deba@127
    10
 * provided that this copyright notice appears in all copies. For
deba@127
    11
 * precise terms see the accompanying LICENSE file.
deba@127
    12
 *
deba@127
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@127
    14
 * express or implied, and with no claim as to its suitability for any
deba@127
    15
 * purpose.
deba@127
    16
 *
deba@127
    17
 */
deba@127
    18
deba@127
    19
///\ingroup lemon_io
deba@127
    20
///\file
deba@127
    21
///\brief Lemon Graph Format reader.
deba@127
    22
deba@127
    23
deba@127
    24
#ifndef LEMON_LGF_READER_H
deba@127
    25
#define LEMON_LGF_READER_H
deba@127
    26
deba@127
    27
#include <iostream>
deba@127
    28
#include <fstream>
deba@127
    29
#include <sstream>
deba@127
    30
deba@127
    31
#include <set>
deba@127
    32
#include <map>
deba@127
    33
deba@127
    34
#include <lemon/assert.h>
deba@127
    35
#include <lemon/graph_utils.h>
deba@127
    36
deba@127
    37
#include <lemon/lgf_writer.h>
deba@127
    38
deba@127
    39
#include <lemon/concept_check.h>
deba@127
    40
#include <lemon/concepts/maps.h>
deba@127
    41
deba@127
    42
namespace lemon {
deba@127
    43
deba@127
    44
  namespace _reader_bits {
deba@127
    45
deba@127
    46
    template <typename Value>
deba@127
    47
    struct DefaultConverter {
deba@127
    48
      Value operator()(const std::string& str) {
deba@127
    49
	std::istringstream is(str);
deba@127
    50
	Value value;
deba@127
    51
	is >> value;
deba@127
    52
deba@127
    53
	char c;
deba@127
    54
	if (is >> std::ws >> c) {
deba@127
    55
	  throw DataFormatError("Remaining characters in token");
deba@127
    56
	}
deba@127
    57
	return value;
deba@127
    58
      }
deba@127
    59
    };
deba@127
    60
deba@127
    61
    template <>
deba@127
    62
    struct DefaultConverter<std::string> {
deba@127
    63
      std::string operator()(const std::string& str) {
deba@127
    64
	return str;
deba@127
    65
      }
deba@127
    66
    };
deba@127
    67
deba@127
    68
    template <typename _Item>    
deba@127
    69
    class MapStorageBase {
deba@127
    70
    public:
deba@127
    71
      typedef _Item Item;
deba@127
    72
deba@127
    73
    public:
deba@127
    74
      MapStorageBase() {}
deba@127
    75
      virtual ~MapStorageBase() {}
deba@127
    76
deba@127
    77
      virtual void set(const Item& item, const std::string& value) = 0;
deba@127
    78
deba@127
    79
    };
deba@127
    80
deba@127
    81
    template <typename _Item, typename _Map, 
deba@127
    82
	      typename _Converter = DefaultConverter<typename _Map::Value> >
deba@127
    83
    class MapStorage : public MapStorageBase<_Item> {
deba@127
    84
    public:
deba@127
    85
      typedef _Map Map;
deba@127
    86
      typedef _Converter Converter;
deba@127
    87
      typedef _Item Item;
deba@127
    88
      
deba@127
    89
    private:
deba@127
    90
      Map& _map;
deba@127
    91
      Converter _converter;
deba@127
    92
deba@127
    93
    public:
deba@127
    94
      MapStorage(Map& map, const Converter& converter = Converter()) 
deba@127
    95
	: _map(map), _converter(converter) {}
deba@127
    96
      virtual ~MapStorage() {}
deba@127
    97
deba@127
    98
      virtual void set(const Item& item ,const std::string& value) {
deba@127
    99
	_map.set(item, _converter(value));
deba@127
   100
      }
deba@127
   101
    };
deba@127
   102
deba@165
   103
    template <typename _Graph, bool _dir, typename _Map, 
deba@165
   104
	      typename _Converter = DefaultConverter<typename _Map::Value> >
deba@165
   105
    class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
deba@165
   106
    public:
deba@165
   107
      typedef _Map Map;
deba@165
   108
      typedef _Converter Converter;
deba@165
   109
      typedef _Graph Graph;
deba@165
   110
      typedef typename Graph::Edge Item;
deba@165
   111
      static const bool dir = _dir;
deba@165
   112
      
deba@165
   113
    private:
deba@165
   114
      const Graph& _graph;
deba@165
   115
      Map& _map;
deba@165
   116
      Converter _converter;
deba@165
   117
deba@165
   118
    public:
deba@165
   119
      GraphArcMapStorage(const Graph& graph, Map& map, 
deba@165
   120
			 const Converter& converter = Converter()) 
deba@165
   121
	: _graph(graph), _map(map), _converter(converter) {}
deba@165
   122
      virtual ~GraphArcMapStorage() {}
deba@165
   123
deba@165
   124
      virtual void set(const Item& item ,const std::string& value) {
deba@165
   125
	_map.set(_graph.direct(item, dir), _converter(value));
deba@165
   126
      }
deba@165
   127
    };
deba@165
   128
deba@127
   129
    class ValueStorageBase {
deba@127
   130
    public:
deba@127
   131
      ValueStorageBase() {}
deba@127
   132
      virtual ~ValueStorageBase() {}
deba@127
   133
deba@127
   134
      virtual void set(const std::string&) = 0;
deba@127
   135
    };
deba@127
   136
deba@127
   137
    template <typename _Value, typename _Converter = DefaultConverter<_Value> >
deba@127
   138
    class ValueStorage : public ValueStorageBase {
deba@127
   139
    public:
deba@127
   140
      typedef _Value Value;
deba@127
   141
      typedef _Converter Converter;
deba@127
   142
deba@127
   143
    private:
deba@127
   144
      Value& _value;
deba@127
   145
      Converter _converter;
deba@127
   146
deba@127
   147
    public:
deba@127
   148
      ValueStorage(Value& value, const Converter& converter = Converter())
deba@127
   149
 	: _value(value), _converter(converter) {}
deba@127
   150
deba@127
   151
      virtual void set(const std::string& value) {
deba@127
   152
	_value = _converter(value);
deba@127
   153
      }
deba@127
   154
    };
deba@127
   155
deba@127
   156
    template <typename Value>
deba@127
   157
    struct MapLookUpConverter {
deba@127
   158
      const std::map<std::string, Value>& _map;
deba@127
   159
deba@127
   160
      MapLookUpConverter(const std::map<std::string, Value>& map)
deba@127
   161
        : _map(map) {}
deba@127
   162
deba@127
   163
      Value operator()(const std::string& str) {
deba@127
   164
        typename std::map<std::string, Value>::const_iterator it =
deba@127
   165
          _map.find(str);
deba@127
   166
        if (it == _map.end()) {
deba@127
   167
          std::ostringstream msg;
deba@127
   168
          msg << "Item not found: " << str;
deba@127
   169
          throw DataFormatError(msg.str().c_str());
deba@127
   170
        }
deba@127
   171
        return it->second;
deba@127
   172
      }
deba@127
   173
    };
deba@127
   174
deba@165
   175
    template <typename Graph>
deba@165
   176
    struct GraphArcLookUpConverter {
deba@165
   177
      const Graph& _graph;
deba@165
   178
      const std::map<std::string, typename Graph::Edge>& _map;
deba@165
   179
      
deba@165
   180
      GraphArcLookUpConverter(const Graph& graph, 
deba@165
   181
			      const std::map<std::string,
deba@165
   182
			                     typename Graph::Edge>& map) 
deba@165
   183
	: _graph(graph), _map(map) {}
deba@165
   184
      
deba@165
   185
      typename Graph::Arc operator()(const std::string& str) {
deba@165
   186
	if (str.empty() || (str[0] != '+' && str[0] != '-')) {
deba@165
   187
	  throw DataFormatError("Item must start with '+' or '-'");
deba@165
   188
	}
deba@165
   189
	typename std::map<std::string, typename Graph::Edge>
deba@165
   190
	  ::const_iterator it = _map.find(str.substr(1));
deba@165
   191
	if (it == _map.end()) {
deba@165
   192
	  throw DataFormatError("Item not found");
deba@165
   193
	}
deba@165
   194
	return _graph.direct(it->second, str[0] == '+');
deba@165
   195
      }
deba@165
   196
    };
deba@165
   197
deba@127
   198
    bool isWhiteSpace(char c) {
deba@127
   199
      return c == ' ' || c == '\t' || c == '\v' || 
deba@127
   200
        c == '\n' || c == '\r' || c == '\f'; 
deba@127
   201
    }
deba@127
   202
    
deba@127
   203
    bool isOct(char c) {
deba@127
   204
      return '0' <= c && c <='7'; 
deba@127
   205
    }
deba@127
   206
    
deba@127
   207
    int valueOct(char c) {
deba@127
   208
      LEMON_ASSERT(isOct(c), "The character is not octal.");
deba@127
   209
      return c - '0';
deba@127
   210
    }
deba@127
   211
deba@127
   212
    bool isHex(char c) {
deba@127
   213
      return ('0' <= c && c <= '9') || 
deba@127
   214
	('a' <= c && c <= 'z') || 
deba@127
   215
	('A' <= c && c <= 'Z'); 
deba@127
   216
    }
deba@127
   217
    
deba@127
   218
    int valueHex(char c) {
deba@127
   219
      LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
deba@127
   220
      if ('0' <= c && c <= '9') return c - '0';
deba@127
   221
      if ('a' <= c && c <= 'z') return c - 'a' + 10;
deba@127
   222
      return c - 'A' + 10;
deba@127
   223
    }
deba@127
   224
deba@127
   225
    bool isIdentifierFirstChar(char c) {
deba@127
   226
      return ('a' <= c && c <= 'z') ||
deba@127
   227
	('A' <= c && c <= 'Z') || c == '_';
deba@127
   228
    }
deba@127
   229
deba@127
   230
    bool isIdentifierChar(char c) {
deba@127
   231
      return isIdentifierFirstChar(c) ||
deba@127
   232
	('0' <= c && c <= '9');
deba@127
   233
    }
deba@127
   234
deba@127
   235
    char readEscape(std::istream& is) {
deba@127
   236
      char c;
deba@127
   237
      if (!is.get(c))
deba@127
   238
	throw DataFormatError("Escape format error");
deba@127
   239
deba@127
   240
      switch (c) {
deba@127
   241
      case '\\':
deba@127
   242
	return '\\';
deba@127
   243
      case '\"':
deba@127
   244
	return '\"';
deba@127
   245
      case '\'':
deba@127
   246
	return '\'';
deba@127
   247
      case '\?':
deba@127
   248
	return '\?';
deba@127
   249
      case 'a':
deba@127
   250
	return '\a';
deba@127
   251
      case 'b':
deba@127
   252
	return '\b';
deba@127
   253
      case 'f':
deba@127
   254
	return '\f';
deba@127
   255
      case 'n':
deba@127
   256
	return '\n';
deba@127
   257
      case 'r':
deba@127
   258
	return '\r';
deba@127
   259
      case 't':
deba@127
   260
	return '\t';
deba@127
   261
      case 'v':
deba@127
   262
	return '\v';
deba@127
   263
      case 'x':
deba@127
   264
	{
deba@127
   265
	  int code;
deba@127
   266
	  if (!is.get(c) || !isHex(c)) 
deba@127
   267
	    throw DataFormatError("Escape format error");
deba@127
   268
	  else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
deba@127
   269
	  else code = code * 16 + valueHex(c);
deba@127
   270
	  return code;
deba@127
   271
	}
deba@127
   272
      default:
deba@127
   273
	{
deba@127
   274
	  int code;
deba@127
   275
	  if (!isOct(c)) 
deba@127
   276
	    throw DataFormatError("Escape format error");
deba@127
   277
	  else if (code = valueOct(c), !is.get(c) || !isOct(c)) 
deba@127
   278
	    is.putback(c);
deba@127
   279
	  else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) 
deba@127
   280
	    is.putback(c);
deba@127
   281
	  else code = code * 8 + valueOct(c);
deba@127
   282
	  return code;
deba@127
   283
	}	      
deba@127
   284
      } 
deba@127
   285
    }
deba@127
   286
    
deba@127
   287
    std::istream& readToken(std::istream& is, std::string& str) {
deba@127
   288
      std::ostringstream os;
deba@127
   289
deba@127
   290
      char c;
deba@127
   291
      is >> std::ws;
deba@127
   292
      
deba@127
   293
      if (!is.get(c)) 
deba@127
   294
	return is;
deba@127
   295
deba@127
   296
      if (c == '\"') {
deba@127
   297
	while (is.get(c) && c != '\"') {
deba@127
   298
	  if (c == '\\') 
deba@127
   299
	    c = readEscape(is);
deba@127
   300
	  os << c;
deba@127
   301
	}
deba@127
   302
	if (!is) 
deba@127
   303
	  throw DataFormatError("Quoted format error");
deba@127
   304
      } else {
deba@127
   305
	is.putback(c);
deba@127
   306
	while (is.get(c) && !isWhiteSpace(c)) {
deba@127
   307
	  if (c == '\\') 
deba@127
   308
	    c = readEscape(is);
deba@127
   309
	  os << c;
deba@127
   310
	}
deba@127
   311
	if (!is) {
deba@127
   312
	  is.clear();
deba@127
   313
	} else {
deba@127
   314
	  is.putback(c);
deba@127
   315
	}
deba@127
   316
      }
deba@127
   317
      str = os.str();
deba@127
   318
      return is;
deba@127
   319
    }
deba@162
   320
deba@162
   321
    class Section {
deba@162
   322
    public:
deba@162
   323
      virtual ~Section() {}
deba@162
   324
      virtual void process(std::istream& is, int& line_num) = 0;
deba@162
   325
    };
deba@162
   326
deba@162
   327
    template <typename Functor>
deba@162
   328
    class LineSection : public Section {
deba@162
   329
    private:
deba@162
   330
deba@162
   331
      Functor _functor;
deba@162
   332
deba@162
   333
    public:
deba@162
   334
      
deba@162
   335
      LineSection(const Functor& functor) : _functor(functor) {}
deba@162
   336
      virtual ~LineSection() {}
deba@162
   337
deba@162
   338
      virtual void process(std::istream& is, int& line_num) {
deba@162
   339
	char c;
deba@162
   340
	std::string line;
deba@162
   341
	while (is.get(c) && c != '@') {
deba@162
   342
	  if (c == '\n') {
deba@162
   343
	    ++line_num;
deba@162
   344
	  } else if (c == '#') {
deba@162
   345
	    getline(is, line);
deba@162
   346
	    ++line_num;
deba@162
   347
	  } else if (!isWhiteSpace(c)) {
deba@162
   348
	    is.putback(c);
deba@162
   349
	    getline(is, line);
deba@162
   350
	    _functor(line);
deba@162
   351
	    ++line_num;
deba@162
   352
	  }
deba@162
   353
	}
deba@162
   354
	if (is) is.putback(c);
deba@162
   355
	else if (is.eof()) is.clear();
deba@162
   356
      }
deba@162
   357
    };
deba@162
   358
deba@162
   359
    template <typename Functor>
deba@162
   360
    class StreamSection : public Section {
deba@162
   361
    private:
deba@162
   362
deba@162
   363
      Functor _functor;
deba@162
   364
deba@162
   365
    public:
deba@162
   366
      
deba@162
   367
      StreamSection(const Functor& functor) : _functor(functor) {}
deba@162
   368
      virtual ~StreamSection() {} 
deba@162
   369
deba@162
   370
      virtual void process(std::istream& is, int& line_num) {
deba@162
   371
	_functor(is, line_num);
deba@162
   372
	char c;
deba@162
   373
	std::string line;
deba@162
   374
	while (is.get(c) && c != '@') {
deba@162
   375
	  if (c == '\n') {
deba@162
   376
	    ++line_num;
deba@162
   377
	  } else if (!isWhiteSpace(c)) {
deba@162
   378
	    getline(is, line);
deba@162
   379
	    ++line_num;
deba@162
   380
	  }
deba@162
   381
	}
deba@162
   382
	if (is) is.putback(c);
deba@162
   383
	else if (is.eof()) is.clear();	
deba@162
   384
      }
deba@162
   385
    };
deba@127
   386
    
deba@127
   387
  }
alpar@156
   388
alpar@156
   389
  /// \ingroup lemon_io
alpar@156
   390
  ///  
alpar@156
   391
  /// \brief LGF reader for directed graphs
alpar@156
   392
  ///
alpar@156
   393
  /// This utility reads an \ref lgf-format "LGF" file.
alpar@156
   394
  ///
alpar@156
   395
  /// The reading method does a batch processing. The user creates a
alpar@156
   396
  /// reader object, then various reading rules can be added to the
alpar@156
   397
  /// reader, and eventually the reading is executed with the \c run()
alpar@156
   398
  /// member function. A map reading rule can be added to the reader
alpar@156
   399
  /// with the \c nodeMap() or \c arcMap() members. An optional
deba@162
   400
  /// converter parameter can also be added as a standard functor
deba@162
   401
  /// converting from std::string to the value type of the map. If it
deba@162
   402
  /// is set, it will determine how the tokens in the file should be
deba@162
   403
  /// is converted to the map's value type. If the functor is not set,
deba@162
   404
  /// then a default conversion will be used. One map can be read into
deba@162
   405
  /// multiple map objects at the same time. The \c attribute(), \c
deba@162
   406
  /// node() and \c arc() functions are used to add attribute reading
deba@162
   407
  /// rules.
alpar@156
   408
  ///
alpar@156
   409
  ///\code
alpar@156
   410
  ///     DigraphReader<Digraph>(std::cin, digraph).
alpar@156
   411
  ///       nodeMap("coordinates", coord_map).
alpar@156
   412
  ///       arcMap("capacity", cap_map).
alpar@156
   413
  ///       node("source", src).
alpar@156
   414
  ///       node("target", trg).
alpar@156
   415
  ///       attribute("caption", caption).
alpar@156
   416
  ///       run();
alpar@156
   417
  ///\endcode
alpar@156
   418
  ///
alpar@156
   419
  /// By default the reader uses the first section in the file of the
alpar@156
   420
  /// proper type. If a section has an optional name, then it can be
deba@162
   421
  /// selected for reading by giving an optional name parameter to the
deba@162
   422
  /// \c nodes(), \c arcs() or \c attributes() functions. The readers
deba@162
   423
  /// also can load extra sections with the \c sectionLines() and
deba@162
   424
  /// sectionStream() functions.
alpar@156
   425
  ///
alpar@156
   426
  /// The \c useNodes() and \c useArcs() functions are used to tell the reader
alpar@156
   427
  /// that the nodes or arcs should not be constructed (added to the
alpar@156
   428
  /// graph) during the reading, but instead the label map of the items
alpar@156
   429
  /// are given as a parameter of these functions. An
alpar@156
   430
  /// application of these function is multipass reading, which is
alpar@156
   431
  /// important if two \e \@arcs sections must be read from the
alpar@156
   432
  /// file. In this example the first phase would read the node set and one
alpar@156
   433
  /// of the arc sets, while the second phase would read the second arc
alpar@156
   434
  /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
alpar@156
   435
  /// The previously read label node map should be passed to the \c
alpar@156
   436
  /// useNodes() functions. Another application of multipass reading when
alpar@156
   437
  /// paths are given as a node map or an arc map. It is impossible read this in
alpar@156
   438
  /// a single pass, because the arcs are not constructed when the node
alpar@156
   439
  /// maps are read.
deba@127
   440
  template <typename _Digraph>
deba@127
   441
  class DigraphReader {
deba@127
   442
  public:
deba@127
   443
deba@127
   444
    typedef _Digraph Digraph;
deba@148
   445
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
deba@127
   446
    
deba@127
   447
  private:
deba@127
   448
deba@127
   449
deba@127
   450
    std::istream* _is;
deba@127
   451
    bool local_is;
deba@127
   452
deba@127
   453
    Digraph& _digraph;
deba@127
   454
deba@127
   455
    std::string _nodes_caption;
deba@127
   456
    std::string _arcs_caption;
deba@127
   457
    std::string _attributes_caption;
deba@127
   458
deba@127
   459
    typedef std::map<std::string, Node> NodeIndex;
deba@127
   460
    NodeIndex _node_index;
deba@127
   461
    typedef std::map<std::string, Arc> ArcIndex;
deba@127
   462
    ArcIndex _arc_index;
deba@127
   463
    
deba@127
   464
    typedef std::vector<std::pair<std::string, 
deba@127
   465
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;    
deba@127
   466
    NodeMaps _node_maps; 
deba@127
   467
deba@127
   468
    typedef std::vector<std::pair<std::string,
deba@127
   469
      _reader_bits::MapStorageBase<Arc>*> >ArcMaps;
deba@127
   470
    ArcMaps _arc_maps;
deba@127
   471
deba@127
   472
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> 
deba@127
   473
      Attributes;
deba@127
   474
    Attributes _attributes;
deba@127
   475
deba@162
   476
    typedef std::map<std::string, _reader_bits::Section*> Sections;
deba@162
   477
    Sections _sections;
deba@162
   478
deba@127
   479
    bool _use_nodes;
deba@127
   480
    bool _use_arcs;
deba@127
   481
deba@127
   482
    int line_num;
deba@127
   483
    std::istringstream line;
deba@127
   484
deba@127
   485
  public:
deba@127
   486
alpar@156
   487
    /// \brief Constructor
alpar@156
   488
    ///
alpar@156
   489
    /// Construct a directed graph reader, which reads from the given
alpar@156
   490
    /// input stream.
deba@127
   491
    DigraphReader(std::istream& is, Digraph& digraph) 
deba@127
   492
      : _is(&is), local_is(false), _digraph(digraph),
deba@127
   493
	_use_nodes(false), _use_arcs(false) {}
deba@127
   494
alpar@156
   495
    /// \brief Constructor
alpar@156
   496
    ///
alpar@156
   497
    /// Construct a directed graph reader, which reads from the given
alpar@156
   498
    /// file.
deba@127
   499
    DigraphReader(const std::string& fn, Digraph& digraph) 
deba@127
   500
      : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph),
deba@127
   501
    	_use_nodes(false), _use_arcs(false) {}
alpar@156
   502
    
alpar@156
   503
    /// \brief Constructor
alpar@156
   504
    ///
alpar@156
   505
    /// Construct a directed graph reader, which reads from the given
alpar@156
   506
    /// file.
deba@127
   507
    DigraphReader(const char* fn, Digraph& digraph) 
deba@127
   508
      : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
deba@127
   509
    	_use_nodes(false), _use_arcs(false) {}
deba@127
   510
alpar@156
   511
    /// \brief Copy constructor
alpar@156
   512
    ///
alpar@156
   513
    /// The copy constructor transfers all data from the other reader,
alpar@156
   514
    /// therefore the copied reader will not be usable more. 
deba@127
   515
    DigraphReader(DigraphReader& other) 
deba@127
   516
      : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
deba@127
   517
	_use_nodes(other._use_nodes), _use_arcs(other._use_arcs) {
deba@127
   518
deba@163
   519
      other._is = 0;
deba@127
   520
      other.local_is = false;
deba@127
   521
      
deba@127
   522
      _node_index.swap(other._node_index);
deba@127
   523
      _arc_index.swap(other._arc_index);
deba@127
   524
deba@127
   525
      _node_maps.swap(other._node_maps);
deba@127
   526
      _arc_maps.swap(other._arc_maps);
deba@127
   527
      _attributes.swap(other._attributes);
deba@127
   528
deba@127
   529
      _nodes_caption = other._nodes_caption;
deba@127
   530
      _arcs_caption = other._arcs_caption;
deba@127
   531
      _attributes_caption = other._attributes_caption;
deba@162
   532
deba@162
   533
      _sections.swap(other._sections);
deba@127
   534
    }
deba@127
   535
alpar@156
   536
    /// \brief Destructor
deba@127
   537
    ~DigraphReader() {
deba@127
   538
      for (typename NodeMaps::iterator it = _node_maps.begin(); 
deba@127
   539
	   it != _node_maps.end(); ++it) {
deba@127
   540
	delete it->second;
deba@127
   541
      }
deba@127
   542
deba@127
   543
      for (typename ArcMaps::iterator it = _arc_maps.begin(); 
deba@127
   544
	   it != _arc_maps.end(); ++it) {
deba@127
   545
	delete it->second;
deba@127
   546
      }
deba@127
   547
deba@127
   548
      for (typename Attributes::iterator it = _attributes.begin(); 
deba@127
   549
	   it != _attributes.end(); ++it) {
deba@127
   550
	delete it->second;
deba@127
   551
      }
deba@127
   552
deba@162
   553
      for (typename Sections::iterator it = _sections.begin(); 
deba@162
   554
	   it != _sections.end(); ++it) {
deba@162
   555
	delete it->second;
deba@162
   556
      }
deba@162
   557
deba@127
   558
      if (local_is) {
deba@127
   559
	delete _is;
deba@127
   560
      }
deba@127
   561
deba@127
   562
    }
deba@127
   563
deba@127
   564
  private:
deba@127
   565
    
deba@127
   566
    DigraphReader& operator=(const DigraphReader&);
deba@127
   567
deba@127
   568
  public:
deba@127
   569
alpar@156
   570
    /// \name Reading rules
alpar@156
   571
    /// @{
alpar@156
   572
    
alpar@156
   573
    /// \brief Node map reading rule
alpar@156
   574
    ///
alpar@156
   575
    /// Add a node map reading rule to the reader.
deba@127
   576
    template <typename Map>
deba@127
   577
    DigraphReader& nodeMap(const std::string& caption, Map& map) {
deba@127
   578
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
deba@127
   579
      _reader_bits::MapStorageBase<Node>* storage = 
deba@127
   580
	new _reader_bits::MapStorage<Node, Map>(map);
deba@127
   581
      _node_maps.push_back(std::make_pair(caption, storage));
deba@127
   582
      return *this;
deba@127
   583
    }
deba@127
   584
alpar@156
   585
    /// \brief Node map reading rule
alpar@156
   586
    ///
alpar@156
   587
    /// Add a node map reading rule with specialized converter to the
alpar@156
   588
    /// reader.
deba@127
   589
    template <typename Map, typename Converter>
deba@127
   590
    DigraphReader& nodeMap(const std::string& caption, Map& map, 
deba@127
   591
			   const Converter& converter = Converter()) {
deba@127
   592
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
deba@127
   593
      _reader_bits::MapStorageBase<Node>* storage = 
deba@127
   594
	new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
deba@127
   595
      _node_maps.push_back(std::make_pair(caption, storage));
deba@127
   596
      return *this;
deba@127
   597
    }
deba@127
   598
alpar@156
   599
    /// \brief Arc map reading rule
alpar@156
   600
    ///
alpar@156
   601
    /// Add an arc map reading rule to the reader.
deba@127
   602
    template <typename Map>
deba@127
   603
    DigraphReader& arcMap(const std::string& caption, Map& map) {
deba@127
   604
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
deba@127
   605
      _reader_bits::MapStorageBase<Arc>* storage = 
deba@127
   606
	new _reader_bits::MapStorage<Arc, Map>(map);
deba@127
   607
      _arc_maps.push_back(std::make_pair(caption, storage));
deba@127
   608
      return *this;
deba@127
   609
    }
deba@127
   610
alpar@156
   611
    /// \brief Arc map reading rule
alpar@156
   612
    ///
alpar@156
   613
    /// Add an arc map reading rule with specialized converter to the
alpar@156
   614
    /// reader.
deba@127
   615
    template <typename Map, typename Converter>
deba@127
   616
    DigraphReader& arcMap(const std::string& caption, Map& map, 
deba@127
   617
			  const Converter& converter = Converter()) {
deba@127
   618
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
deba@127
   619
      _reader_bits::MapStorageBase<Arc>* storage = 
deba@127
   620
	new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
deba@127
   621
      _arc_maps.push_back(std::make_pair(caption, storage));
deba@127
   622
      return *this;
deba@127
   623
    }
deba@127
   624
alpar@156
   625
    /// \brief Attribute reading rule
alpar@156
   626
    ///
alpar@156
   627
    /// Add an attribute reading rule to the reader.
deba@127
   628
    template <typename Value>
deba@127
   629
    DigraphReader& attribute(const std::string& caption, Value& value) {
deba@127
   630
      _reader_bits::ValueStorageBase* storage = 
deba@127
   631
	new _reader_bits::ValueStorage<Value>(value);
deba@127
   632
      _attributes.insert(std::make_pair(caption, storage));
deba@127
   633
      return *this;
deba@127
   634
    }
deba@127
   635
alpar@156
   636
    /// \brief Attribute reading rule
alpar@156
   637
    ///
alpar@156
   638
    /// Add an attribute reading rule with specialized converter to the
alpar@156
   639
    /// reader.
deba@127
   640
    template <typename Value, typename Converter>
deba@127
   641
    DigraphReader& attribute(const std::string& caption, Value& value, 
deba@127
   642
			     const Converter& converter = Converter()) {
deba@127
   643
      _reader_bits::ValueStorageBase* storage = 
deba@127
   644
	new _reader_bits::ValueStorage<Value, Converter>(value, converter);
deba@127
   645
      _attributes.insert(std::make_pair(caption, storage));
deba@127
   646
      return *this;
deba@127
   647
    }
deba@127
   648
alpar@156
   649
    /// \brief Node reading rule
alpar@156
   650
    ///
alpar@156
   651
    /// Add a node reading rule to reader.
deba@127
   652
    DigraphReader& node(const std::string& caption, Node& node) {
deba@127
   653
      typedef _reader_bits::MapLookUpConverter<Node> Converter;
deba@127
   654
      Converter converter(_node_index);
deba@127
   655
      _reader_bits::ValueStorageBase* storage = 
deba@127
   656
	new _reader_bits::ValueStorage<Node, Converter>(node, converter);
deba@127
   657
      _attributes.insert(std::make_pair(caption, storage));
deba@127
   658
      return *this;
deba@127
   659
    }
deba@127
   660
alpar@156
   661
    /// \brief Arc reading rule
alpar@156
   662
    ///
alpar@156
   663
    /// Add an arc reading rule to reader.
deba@127
   664
    DigraphReader& arc(const std::string& caption, Arc& arc) {
deba@127
   665
      typedef _reader_bits::MapLookUpConverter<Arc> Converter;
deba@127
   666
      Converter converter(_arc_index);
deba@127
   667
      _reader_bits::ValueStorageBase* storage = 
deba@127
   668
	new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@127
   669
      _attributes.insert(std::make_pair(caption, storage));
deba@127
   670
      return *this;
deba@127
   671
    }
deba@127
   672
alpar@156
   673
    /// @}
alpar@156
   674
alpar@156
   675
    /// \name Select section by name
alpar@156
   676
    /// @{
alpar@156
   677
alpar@156
   678
    /// \brief Set \c \@nodes section to be read
alpar@156
   679
    ///
alpar@156
   680
    /// Set \c \@nodes section to be read
deba@127
   681
    DigraphReader& nodes(const std::string& caption) {
deba@127
   682
      _nodes_caption = caption;
deba@127
   683
      return *this;
deba@127
   684
    }
deba@127
   685
alpar@156
   686
    /// \brief Set \c \@arcs section to be read
alpar@156
   687
    ///
alpar@156
   688
    /// Set \c \@arcs section to be read
deba@127
   689
    DigraphReader& arcs(const std::string& caption) {
deba@127
   690
      _arcs_caption = caption;
deba@127
   691
      return *this;
deba@127
   692
    }
deba@127
   693
alpar@156
   694
    /// \brief Set \c \@attributes section to be read
alpar@156
   695
    ///
alpar@156
   696
    /// Set \c \@attributes section to be read
deba@127
   697
    DigraphReader& attributes(const std::string& caption) {
deba@127
   698
      _attributes_caption = caption;
deba@127
   699
      return *this;
deba@127
   700
    }
deba@127
   701
alpar@156
   702
    /// @}
alpar@156
   703
deba@162
   704
    /// \name Section readers
deba@162
   705
    /// @{
deba@162
   706
deba@162
   707
    /// \brief Add a section processor with line oriented reading
deba@162
   708
    ///
deba@162
   709
    /// In the \e LGF file extra sections can be placed, which contain
deba@162
   710
    /// any data in arbitrary format. These sections can be read with
deba@162
   711
    /// this function line by line. The first parameter is the type
deba@162
   712
    /// descriptor of the section, the second is a functor, which
deba@162
   713
    /// takes just one \c std::string parameter. At the reading
deba@162
   714
    /// process, each line of the section will be given to the functor
deba@162
   715
    /// object. However, the empty lines and the comment lines are
deba@162
   716
    /// filtered out, and the leading whitespaces are stipped from
deba@162
   717
    /// each processed string.
deba@162
   718
    ///
deba@162
   719
    /// For example let's see a section, which contain several
deba@162
   720
    /// integers, which should be inserted into a vector.
deba@162
   721
    ///\code
deba@162
   722
    ///  @numbers
deba@162
   723
    ///  12 45 23
deba@162
   724
    ///  4
deba@162
   725
    ///  23 6
deba@162
   726
    ///\endcode
deba@162
   727
    ///
deba@162
   728
    /// The functor is implemented as an struct:
deba@162
   729
    ///\code
deba@162
   730
    ///  struct NumberSection {
deba@162
   731
    ///    std::vector<int>& _data;
deba@162
   732
    ///    NumberSection(std::vector<int>& data) : _data(data) {}
deba@162
   733
    ///    void operator()(const std::string& line) {
deba@162
   734
    ///      std::istringstream ls(line);
deba@162
   735
    ///      int value;
deba@162
   736
    ///      while (ls >> value) _data.push_back(value);
deba@162
   737
    ///    }
deba@162
   738
    ///  };
deba@162
   739
    ///
deba@162
   740
    ///  // ...
deba@162
   741
    ///
deba@162
   742
    ///  reader.sectionLines("numbers", NumberSection(vec));  
deba@162
   743
    ///\endcode
deba@162
   744
    template <typename Functor>
deba@162
   745
    DigraphReader& sectionLines(const std::string& type, Functor functor) {
deba@162
   746
      LEMON_ASSERT(!type.empty(), "Type is not empty.");
deba@162
   747
      LEMON_ASSERT(_sections.find(type) == _sections.end(), 
deba@162
   748
		   "Multiple reading of section.");
deba@162
   749
      LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
deba@162
   750
		   type != "attributes", "Multiple reading of section.");
deba@162
   751
      _sections.insert(std::make_pair(type, 
deba@162
   752
        new _reader_bits::LineSection<Functor>(functor)));
deba@162
   753
      return *this;
deba@162
   754
    }
deba@162
   755
deba@162
   756
deba@162
   757
    /// \brief Add a section processor with stream oriented reading
deba@162
   758
    ///
deba@162
   759
    /// In the \e LGF file extra sections can be placed, which contain
deba@162
   760
    /// any data in arbitrary format. These sections can be read
deba@162
   761
    /// directly with this function. The first parameter is the type
deba@162
   762
    /// of the section, the second is a functor, which takes an \c
deba@162
   763
    /// std::istream& and an int& parameter, the latter regard to the
deba@162
   764
    /// line number of stream. The functor can read the input while
deba@162
   765
    /// the section go on, and the line number should be modified
deba@162
   766
    /// accordingly.
deba@162
   767
    template <typename Functor>
deba@162
   768
    DigraphReader& sectionStream(const std::string& type, Functor functor) {
deba@162
   769
      LEMON_ASSERT(!type.empty(), "Type is not empty.");
deba@162
   770
      LEMON_ASSERT(_sections.find(type) == _sections.end(), 
deba@162
   771
		   "Multiple reading of section.");
deba@162
   772
      LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
deba@162
   773
		   type != "attributes", "Multiple reading of section.");
deba@162
   774
      _sections.insert(std::make_pair(type, 
deba@162
   775
	 new _reader_bits::StreamSection<Functor>(functor)));
deba@162
   776
      return *this;
deba@162
   777
    }    
deba@162
   778
    
deba@162
   779
    /// @}
deba@162
   780
alpar@156
   781
    /// \name Using previously constructed node or arc set
alpar@156
   782
    /// @{
alpar@156
   783
alpar@156
   784
    /// \brief Use previously constructed node set
alpar@156
   785
    ///
alpar@156
   786
    /// Use previously constructed node set, and specify the node
alpar@156
   787
    /// label map.
deba@127
   788
    template <typename Map>
deba@127
   789
    DigraphReader& useNodes(const Map& map) {
deba@127
   790
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
deba@127
   791
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); 
deba@127
   792
      _use_nodes = true;
deba@127
   793
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@127
   794
      for (NodeIt n(_digraph); n != INVALID; ++n) {
deba@127
   795
	_node_index.insert(std::make_pair(converter(map[n]), n));
deba@127
   796
      }
deba@127
   797
      return *this;
deba@127
   798
    }
deba@127
   799
alpar@156
   800
    /// \brief Use previously constructed node set
alpar@156
   801
    ///
alpar@156
   802
    /// Use previously constructed node set, and specify the node
alpar@156
   803
    /// label map and a functor which converts the label map values to
alpar@156
   804
    /// std::string.
deba@127
   805
    template <typename Map, typename Converter>
deba@127
   806
    DigraphReader& useNodes(const Map& map, 
deba@127
   807
			    const Converter& converter = Converter()) {
deba@127
   808
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
deba@127
   809
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); 
deba@127
   810
      _use_nodes = true;
deba@127
   811
      for (NodeIt n(_digraph); n != INVALID; ++n) {
deba@127
   812
	_node_index.insert(std::make_pair(converter(map[n]), n));
deba@127
   813
      }
deba@127
   814
      return *this;
deba@127
   815
    }
deba@127
   816
alpar@156
   817
    /// \brief Use previously constructed arc set
alpar@156
   818
    ///
alpar@156
   819
    /// Use previously constructed arc set, and specify the arc
alpar@156
   820
    /// label map.
deba@127
   821
    template <typename Map>
deba@127
   822
    DigraphReader& useArcs(const Map& map) {
deba@127
   823
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
deba@127
   824
      LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
deba@127
   825
      _use_arcs = true;
deba@127
   826
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@127
   827
      for (ArcIt a(_digraph); a != INVALID; ++a) {
deba@127
   828
	_arc_index.insert(std::make_pair(converter(map[a]), a));
deba@127
   829
      }
deba@127
   830
      return *this;
deba@127
   831
    }
deba@127
   832
alpar@156
   833
    /// \brief Use previously constructed arc set
alpar@156
   834
    ///
alpar@156
   835
    /// Use previously constructed arc set, and specify the arc
alpar@156
   836
    /// label map and a functor which converts the label map values to
alpar@156
   837
    /// std::string.
deba@127
   838
    template <typename Map, typename Converter>
deba@127
   839
    DigraphReader& useArcs(const Map& map, 
deba@127
   840
			    const Converter& converter = Converter()) {
deba@127
   841
      checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
deba@127
   842
      LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); 
deba@127
   843
      _use_arcs = true;
deba@127
   844
      for (ArcIt a(_digraph); a != INVALID; ++a) {
deba@127
   845
	_arc_index.insert(std::make_pair(converter(map[a]), a));
deba@127
   846
      }
deba@127
   847
      return *this;
deba@127
   848
    }
deba@127
   849
alpar@156
   850
    /// @}
alpar@156
   851
deba@127
   852
  private:
deba@127
   853
deba@127
   854
    bool readLine() {
deba@127
   855
      std::string str;
deba@127
   856
      while(++line_num, std::getline(*_is, str)) {
deba@127
   857
	line.clear(); line.str(str);
deba@127
   858
	char c;
deba@127
   859
	if (line >> std::ws >> c && c != '#') {
deba@127
   860
	  line.putback(c);
deba@127
   861
	  return true;
deba@127
   862
	}
deba@127
   863
      }
deba@127
   864
      return false;
deba@127
   865
    }
deba@127
   866
deba@127
   867
    bool readSuccess() {
deba@127
   868
      return static_cast<bool>(*_is);
deba@127
   869
    }
deba@127
   870
    
deba@127
   871
    void skipSection() {
deba@127
   872
      char c;
deba@127
   873
      while (readSuccess() && line >> c && c != '@') {
deba@127
   874
	readLine();
deba@127
   875
      }
deba@127
   876
      line.putback(c);
deba@127
   877
    }
deba@127
   878
deba@127
   879
    void readNodes() {
deba@127
   880
deba@127
   881
      std::vector<int> map_index(_node_maps.size());
deba@127
   882
      int map_num, label_index;
deba@127
   883
deba@127
   884
      if (!readLine()) 
deba@127
   885
	throw DataFormatError("Cannot find map captions");
deba@127
   886
      
deba@127
   887
      {
deba@127
   888
	std::map<std::string, int> maps;
deba@127
   889
	
deba@127
   890
	std::string map;
deba@127
   891
	int index = 0;
alpar@156
   892
	while (_reader_bits::readToken(line, map)) {
deba@127
   893
	  if (maps.find(map) != maps.end()) {
deba@127
   894
	    std::ostringstream msg;
deba@127
   895
	    msg << "Multiple occurence of node map: " << map;
deba@127
   896
	    throw DataFormatError(msg.str().c_str());
deba@127
   897
	  }
deba@127
   898
	  maps.insert(std::make_pair(map, index));
deba@127
   899
	  ++index;
deba@127
   900
	}
deba@127
   901
	
deba@127
   902
	for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
deba@127
   903
	  std::map<std::string, int>::iterator jt = 
deba@127
   904
	    maps.find(_node_maps[i].first);
deba@127
   905
	  if (jt == maps.end()) {
deba@127
   906
	    std::ostringstream msg;
deba@127
   907
	    msg << "Map not found in file: " << _node_maps[i].first;
deba@127
   908
	    throw DataFormatError(msg.str().c_str());
deba@127
   909
	  }
deba@127
   910
	  map_index[i] = jt->second;
deba@127
   911
	}
deba@127
   912
deba@127
   913
	{
deba@127
   914
	  std::map<std::string, int>::iterator jt = maps.find("label");
deba@127
   915
	  if (jt == maps.end())
deba@127
   916
	    throw DataFormatError("Label map not found in file");
deba@127
   917
	  label_index = jt->second;
deba@127
   918
	}
deba@127
   919
	map_num = maps.size();
deba@127
   920
      }
deba@127
   921
deba@127
   922
      char c;
deba@127
   923
      while (readLine() && line >> c && c != '@') {
deba@127
   924
	line.putback(c);
deba@127
   925
deba@127
   926
	std::vector<std::string> tokens(map_num);
deba@127
   927
	for (int i = 0; i < map_num; ++i) {
deba@127
   928
	  if (!_reader_bits::readToken(line, tokens[i])) {
deba@127
   929
	    std::ostringstream msg;
deba@127
   930
	    msg << "Column not found (" << i + 1 << ")";
deba@127
   931
	    throw DataFormatError(msg.str().c_str());
deba@127
   932
	  }
deba@127
   933
	}
deba@127
   934
	if (line >> std::ws >> c)
deba@127
   935
	  throw DataFormatError("Extra character on the end of line");
deba@127
   936
	
deba@127
   937
	Node n;
deba@127
   938
	if (!_use_nodes) {
deba@127
   939
	  n = _digraph.addNode();
deba@127
   940
	  _node_index.insert(std::make_pair(tokens[label_index], n));
deba@127
   941
	} else {
deba@127
   942
	  typename std::map<std::string, Node>::iterator it =
deba@127
   943
	    _node_index.find(tokens[label_index]);
deba@127
   944
	  if (it == _node_index.end()) {
deba@127
   945
	    std::ostringstream msg;
deba@127
   946
	    msg << "Node with label not found: " << tokens[label_index];
deba@127
   947
	    throw DataFormatError(msg.str().c_str());	    
deba@127
   948
	  }
deba@127
   949
	  n = it->second;
deba@127
   950
	}
deba@127
   951
deba@127
   952
	for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
deba@127
   953
	  _node_maps[i].second->set(n, tokens[map_index[i]]);
deba@127
   954
	}
deba@127
   955
deba@127
   956
      }
deba@127
   957
      if (readSuccess()) {
deba@127
   958
	line.putback(c);
deba@127
   959
      }
deba@127
   960
    }
deba@127
   961
deba@127
   962
    void readArcs() {
deba@127
   963
deba@127
   964
      std::vector<int> map_index(_arc_maps.size());
deba@127
   965
      int map_num, label_index;
deba@127
   966
deba@127
   967
      if (!readLine()) 
deba@127
   968
	throw DataFormatError("Cannot find map captions");
deba@127
   969
      
deba@127
   970
      {
deba@127
   971
	std::map<std::string, int> maps;
deba@127
   972
	
deba@127
   973
	std::string map;
deba@127
   974
	int index = 0;
alpar@156
   975
	while (_reader_bits::readToken(line, map)) {
deba@127
   976
	  if (maps.find(map) != maps.end()) {
deba@127
   977
	    std::ostringstream msg;
deba@127
   978
	    msg << "Multiple occurence of arc map: " << map;
deba@127
   979
	    throw DataFormatError(msg.str().c_str());
deba@127
   980
	  }
deba@127
   981
	  maps.insert(std::make_pair(map, index));
deba@127
   982
	  ++index;
deba@127
   983
	}
deba@127
   984
	
deba@127
   985
	for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
deba@127
   986
	  std::map<std::string, int>::iterator jt = 
deba@127
   987
	    maps.find(_arc_maps[i].first);
deba@127
   988
	  if (jt == maps.end()) {
deba@127
   989
	    std::ostringstream msg;
deba@127
   990
	    msg << "Map not found in file: " << _arc_maps[i].first;
deba@127
   991
	    throw DataFormatError(msg.str().c_str());
deba@127
   992
	  }
deba@127
   993
	  map_index[i] = jt->second;
deba@127
   994
	}
deba@127
   995
deba@127
   996
	{
deba@127
   997
	  std::map<std::string, int>::iterator jt = maps.find("label");
deba@127
   998
	  if (jt == maps.end())
deba@127
   999
	    throw DataFormatError("Label map not found in file");
deba@127
  1000
	  label_index = jt->second;
deba@127
  1001
	}
deba@127
  1002
	map_num = maps.size();
deba@127
  1003
      }
deba@127
  1004
deba@127
  1005
      char c;
deba@127
  1006
      while (readLine() && line >> c && c != '@') {
deba@127
  1007
	line.putback(c);
deba@127
  1008
deba@127
  1009
	std::string source_token;
deba@127
  1010
	std::string target_token;
deba@127
  1011
deba@127
  1012
	if (!_reader_bits::readToken(line, source_token))
deba@127
  1013
	  throw DataFormatError("Source not found");
deba@127
  1014
deba@127
  1015
	if (!_reader_bits::readToken(line, target_token))
deba@127
  1016
	  throw DataFormatError("Source not found");
deba@127
  1017
	
deba@127
  1018
	std::vector<std::string> tokens(map_num);
deba@127
  1019
	for (int i = 0; i < map_num; ++i) {
deba@127
  1020
	  if (!_reader_bits::readToken(line, tokens[i])) {
deba@127
  1021
	    std::ostringstream msg;
deba@127
  1022
	    msg << "Column not found (" << i + 1 << ")";
deba@127
  1023
	    throw DataFormatError(msg.str().c_str());
deba@127
  1024
	  }
deba@127
  1025
	}
deba@127
  1026
	if (line >> std::ws >> c)
deba@127
  1027
	  throw DataFormatError("Extra character on the end of line");
deba@127
  1028
	
deba@127
  1029
	Arc a;
deba@127
  1030
	if (!_use_arcs) {
deba@127
  1031
deba@127
  1032
          typename NodeIndex::iterator it;
deba@127
  1033
 
deba@127
  1034
          it = _node_index.find(source_token);
deba@127
  1035
          if (it == _node_index.end()) {
deba@127
  1036
            std::ostringstream msg;
deba@127
  1037
            msg << "Item not found: " << source_token;
deba@127
  1038
            throw DataFormatError(msg.str().c_str());
deba@127
  1039
          }
deba@127
  1040
          Node source = it->second;
deba@127
  1041
deba@127
  1042
          it = _node_index.find(target_token);
deba@127
  1043
          if (it == _node_index.end()) {       
deba@127
  1044
            std::ostringstream msg;            
deba@127
  1045
            msg << "Item not found: " << target_token;
deba@127
  1046
            throw DataFormatError(msg.str().c_str());
deba@127
  1047
          }                                          
deba@127
  1048
          Node target = it->second;                            
deba@127
  1049
deba@127
  1050
	  a = _digraph.addArc(source, target);
deba@127
  1051
	  _arc_index.insert(std::make_pair(tokens[label_index], a));
deba@127
  1052
	} else {
deba@127
  1053
	  typename std::map<std::string, Arc>::iterator it =
deba@127
  1054
	    _arc_index.find(tokens[label_index]);
deba@127
  1055
	  if (it == _arc_index.end()) {
deba@127
  1056
	    std::ostringstream msg;
deba@127
  1057
	    msg << "Arc with label not found: " << tokens[label_index];
deba@127
  1058
	    throw DataFormatError(msg.str().c_str());	    
deba@127
  1059
	  }
deba@127
  1060
	  a = it->second;
deba@127
  1061
	}
deba@127
  1062
deba@127
  1063
	for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
deba@127
  1064
	  _arc_maps[i].second->set(a, tokens[map_index[i]]);
deba@127
  1065
	}
deba@127
  1066
deba@127
  1067
      }
deba@127
  1068
      if (readSuccess()) {
deba@127
  1069
	line.putback(c);
deba@127
  1070
      }
deba@127
  1071
    }
deba@127
  1072
deba@127
  1073
    void readAttributes() {
deba@127
  1074
deba@127
  1075
      std::set<std::string> read_attr;
deba@127
  1076
deba@127
  1077
      char c;
deba@127
  1078
      while (readLine() && line >> c && c != '@') {
deba@127
  1079
	line.putback(c);
deba@127
  1080
	
deba@127
  1081
	std::string attr, token;
alpar@156
  1082
	if (!_reader_bits::readToken(line, attr))
deba@127
  1083
	  throw DataFormatError("Attribute name not found");
deba@127
  1084
	if (!_reader_bits::readToken(line, token))
deba@127
  1085
	  throw DataFormatError("Attribute value not found");
deba@127
  1086
	if (line >> c)
deba@127
  1087
	  throw DataFormatError("Extra character on the end of line");	  
deba@127
  1088
deba@127
  1089
	{
deba@127
  1090
	  std::set<std::string>::iterator it = read_attr.find(attr);
deba@127
  1091
	  if (it != read_attr.end()) {
deba@127
  1092
	    std::ostringstream msg;
deba@127
  1093
	    msg << "Multiple occurence of attribute " << attr;
deba@127
  1094
	    throw DataFormatError(msg.str().c_str());
deba@127
  1095
	  }
deba@127
  1096
	  read_attr.insert(attr);
deba@127
  1097
	}
deba@127
  1098
	
deba@127
  1099
	{
deba@127
  1100
	  typename Attributes::iterator it = _attributes.lower_bound(attr);
deba@127
  1101
	  while (it != _attributes.end() && it->first == attr) {
deba@127
  1102
	    it->second->set(token);
deba@127
  1103
	    ++it;
deba@127
  1104
	  }
deba@127
  1105
	}
deba@127
  1106
deba@127
  1107
      }
deba@127
  1108
      if (readSuccess()) {
deba@127
  1109
	line.putback(c);
deba@127
  1110
      }
deba@127
  1111
      for (typename Attributes::iterator it = _attributes.begin();
deba@127
  1112
	   it != _attributes.end(); ++it) {
deba@127
  1113
	if (read_attr.find(it->first) == read_attr.end()) {
deba@127
  1114
	  std::ostringstream msg;
deba@127
  1115
	  msg << "Attribute not found in file: " << it->first;
deba@127
  1116
	  throw DataFormatError(msg.str().c_str());
deba@127
  1117
	}	
deba@127
  1118
      }
deba@127
  1119
    }
deba@127
  1120
deba@127
  1121
  public:
alpar@156
  1122
alpar@156
  1123
    /// \name Execution of the reader    
alpar@156
  1124
    /// @{
alpar@156
  1125
alpar@156
  1126
    /// \brief Start the batch processing
alpar@156
  1127
    ///
alpar@156
  1128
    /// This function starts the batch processing
deba@127
  1129
    void run() {
deba@127
  1130
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
deba@163
  1131
      if (!*_is) {
deba@163
  1132
	throw DataFormatError("Cannot find file");
deba@163
  1133
      }
deba@127
  1134
      
deba@127
  1135
      bool nodes_done = false;
deba@127
  1136
      bool arcs_done = false;
deba@127
  1137
      bool attributes_done = false;
deba@162
  1138
      std::set<std::string> extra_sections;
deba@127
  1139
deba@127
  1140
      line_num = 0;      
deba@127
  1141
      readLine();
deba@127
  1142
deba@127
  1143
      while (readSuccess()) {
deba@127
  1144
	skipSection();
deba@127
  1145
	try {
deba@127
  1146
	  char c;
deba@127
  1147
	  std::string section, caption;
deba@127
  1148
	  line >> c;
alpar@156
  1149
	  _reader_bits::readToken(line, section);
alpar@156
  1150
	  _reader_bits::readToken(line, caption);
deba@127
  1151
deba@127
  1152
	  if (line >> c) 
deba@127
  1153
	    throw DataFormatError("Extra character on the end of line");
deba@127
  1154
deba@127
  1155
	  if (section == "nodes" && !nodes_done) {
deba@127
  1156
	    if (_nodes_caption.empty() || _nodes_caption == caption) {
deba@127
  1157
	      readNodes();
deba@127
  1158
	      nodes_done = true;
deba@127
  1159
	    }
deba@127
  1160
	  } else if ((section == "arcs" || section == "edges") && 
deba@127
  1161
		     !arcs_done) {
deba@127
  1162
	    if (_arcs_caption.empty() || _arcs_caption == caption) {
deba@127
  1163
	      readArcs();
deba@127
  1164
	      arcs_done = true;
deba@127
  1165
	    }
deba@127
  1166
	  } else if (section == "attributes" && !attributes_done) {
deba@127
  1167
	    if (_attributes_caption.empty() || _attributes_caption == caption) {
deba@127
  1168
	      readAttributes();
deba@127
  1169
	      attributes_done = true;
deba@127
  1170
	    }
deba@127
  1171
	  } else {
deba@162
  1172
	    if (extra_sections.find(section) != extra_sections.end()) {
deba@162
  1173
	      std::ostringstream msg;
deba@162
  1174
	      msg << "Multiple occurence of section " << section;
deba@162
  1175
	      throw DataFormatError(msg.str().c_str());
deba@162
  1176
	    }
deba@162
  1177
	    Sections::iterator it = _sections.find(section);
deba@162
  1178
	    if (it != _sections.end()) {
deba@162
  1179
	      extra_sections.insert(section);
deba@162
  1180
	      it->second->process(*_is, line_num);
deba@162
  1181
	      readLine();
deba@162
  1182
	    } else {
deba@162
  1183
	      readLine();
deba@162
  1184
	      skipSection();
deba@162
  1185
	    }
deba@127
  1186
	  }
deba@127
  1187
	} catch (DataFormatError& error) {
deba@127
  1188
	  error.line(line_num);
deba@127
  1189
	  throw;
deba@127
  1190
	}	
deba@127
  1191
      }
deba@127
  1192
deba@127
  1193
      if (!nodes_done) {
deba@127
  1194
	throw DataFormatError("Section @nodes not found");
deba@127
  1195
      }
deba@127
  1196
deba@127
  1197
      if (!arcs_done) {
deba@127
  1198
	throw DataFormatError("Section @arcs not found");
deba@127
  1199
      }
deba@127
  1200
deba@127
  1201
      if (!attributes_done && !_attributes.empty()) {
deba@127
  1202
	throw DataFormatError("Section @attributes not found");
deba@127
  1203
      }
deba@127
  1204
deba@127
  1205
    }
alpar@156
  1206
alpar@156
  1207
    /// @}
deba@127
  1208
    
deba@127
  1209
  };
deba@127
  1210
alpar@156
  1211
  /// \relates DigraphReader
deba@127
  1212
  template <typename Digraph>
deba@127
  1213
  DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
deba@163
  1214
    DigraphReader<Digraph> tmp(is, digraph);
deba@163
  1215
    return tmp;
deba@127
  1216
  }
deba@127
  1217
alpar@156
  1218
  /// \relates DigraphReader
deba@127
  1219
  template <typename Digraph>
deba@127
  1220
  DigraphReader<Digraph> digraphReader(const std::string& fn, 
deba@127
  1221
				       Digraph& digraph) {
deba@163
  1222
    DigraphReader<Digraph> tmp(fn, digraph);
deba@163
  1223
    return tmp;
deba@127
  1224
  }
deba@127
  1225
alpar@156
  1226
  /// \relates DigraphReader
deba@127
  1227
  template <typename Digraph>
deba@127
  1228
  DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
deba@163
  1229
    DigraphReader<Digraph> tmp(fn, digraph);
deba@163
  1230
    return tmp;
deba@127
  1231
  }
deba@165
  1232
deba@165
  1233
  /// \ingroup lemon_io
deba@165
  1234
  ///  
deba@165
  1235
  /// \brief LGF reader for undirected graphs
deba@165
  1236
  ///
deba@165
  1237
  /// This utility reads an \ref lgf-format "LGF" file.
deba@165
  1238
  template <typename _Graph>
deba@165
  1239
  class GraphReader {
deba@165
  1240
  public:
deba@165
  1241
deba@165
  1242
    typedef _Graph Graph;
deba@165
  1243
    TEMPLATE_GRAPH_TYPEDEFS(Graph);
deba@165
  1244
    
deba@165
  1245
  private:
deba@165
  1246
deba@165
  1247
deba@165
  1248
    std::istream* _is;
deba@165
  1249
    bool local_is;
deba@165
  1250
deba@165
  1251
    Graph& _graph;
deba@165
  1252
deba@165
  1253
    std::string _nodes_caption;
deba@165
  1254
    std::string _edges_caption;
deba@165
  1255
    std::string _attributes_caption;
deba@165
  1256
deba@165
  1257
    typedef std::map<std::string, Node> NodeIndex;
deba@165
  1258
    NodeIndex _node_index;
deba@165
  1259
    typedef std::map<std::string, Edge> EdgeIndex;
deba@165
  1260
    EdgeIndex _edge_index;
deba@165
  1261
    
deba@165
  1262
    typedef std::vector<std::pair<std::string, 
deba@165
  1263
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;    
deba@165
  1264
    NodeMaps _node_maps; 
deba@165
  1265
deba@165
  1266
    typedef std::vector<std::pair<std::string,
deba@165
  1267
      _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
deba@165
  1268
    EdgeMaps _edge_maps;
deba@165
  1269
deba@165
  1270
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> 
deba@165
  1271
      Attributes;
deba@165
  1272
    Attributes _attributes;
deba@165
  1273
deba@165
  1274
    typedef std::map<std::string, _reader_bits::Section*> Sections;
deba@165
  1275
    Sections _sections;
deba@165
  1276
deba@165
  1277
    bool _use_nodes;
deba@165
  1278
    bool _use_edges;
deba@165
  1279
deba@165
  1280
    int line_num;
deba@165
  1281
    std::istringstream line;
deba@165
  1282
deba@165
  1283
  public:
deba@165
  1284
deba@165
  1285
    /// \brief Constructor
deba@165
  1286
    ///
deba@165
  1287
    /// Construct a undirected graph reader, which reads from the given
deba@165
  1288
    /// input stream.
deba@165
  1289
    GraphReader(std::istream& is, Graph& graph) 
deba@165
  1290
      : _is(&is), local_is(false), _graph(graph),
deba@165
  1291
	_use_nodes(false), _use_edges(false) {}
deba@165
  1292
deba@165
  1293
    /// \brief Constructor
deba@165
  1294
    ///
deba@165
  1295
    /// Construct a undirected graph reader, which reads from the given
deba@165
  1296
    /// file.
deba@165
  1297
    GraphReader(const std::string& fn, Graph& graph) 
deba@165
  1298
      : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph),
deba@165
  1299
    	_use_nodes(false), _use_edges(false) {}
deba@165
  1300
    
deba@165
  1301
    /// \brief Constructor
deba@165
  1302
    ///
deba@165
  1303
    /// Construct a undirected graph reader, which reads from the given
deba@165
  1304
    /// file.
deba@165
  1305
    GraphReader(const char* fn, Graph& graph) 
deba@165
  1306
      : _is(new std::ifstream(fn)), local_is(true), _graph(graph),
deba@165
  1307
    	_use_nodes(false), _use_edges(false) {}
deba@165
  1308
deba@165
  1309
    /// \brief Copy constructor
deba@165
  1310
    ///
deba@165
  1311
    /// The copy constructor transfers all data from the other reader,
deba@165
  1312
    /// therefore the copied reader will not be usable more. 
deba@165
  1313
    GraphReader(GraphReader& other) 
deba@165
  1314
      : _is(other._is), local_is(other.local_is), _graph(other._graph),
deba@165
  1315
	_use_nodes(other._use_nodes), _use_edges(other._use_edges) {
deba@165
  1316
deba@165
  1317
      other._is = 0;
deba@165
  1318
      other.local_is = false;
deba@165
  1319
      
deba@165
  1320
      _node_index.swap(other._node_index);
deba@165
  1321
      _edge_index.swap(other._edge_index);
deba@165
  1322
deba@165
  1323
      _node_maps.swap(other._node_maps);
deba@165
  1324
      _edge_maps.swap(other._edge_maps);
deba@165
  1325
      _attributes.swap(other._attributes);
deba@165
  1326
deba@165
  1327
      _nodes_caption = other._nodes_caption;
deba@165
  1328
      _edges_caption = other._edges_caption;
deba@165
  1329
      _attributes_caption = other._attributes_caption;
deba@165
  1330
deba@165
  1331
      _sections.swap(other._sections);
deba@165
  1332
    }
deba@165
  1333
deba@165
  1334
    /// \brief Destructor
deba@165
  1335
    ~GraphReader() {
deba@165
  1336
      for (typename NodeMaps::iterator it = _node_maps.begin(); 
deba@165
  1337
	   it != _node_maps.end(); ++it) {
deba@165
  1338
	delete it->second;
deba@165
  1339
      }
deba@165
  1340
deba@165
  1341
      for (typename EdgeMaps::iterator it = _edge_maps.begin(); 
deba@165
  1342
	   it != _edge_maps.end(); ++it) {
deba@165
  1343
	delete it->second;
deba@165
  1344
      }
deba@165
  1345
deba@165
  1346
      for (typename Attributes::iterator it = _attributes.begin(); 
deba@165
  1347
	   it != _attributes.end(); ++it) {
deba@165
  1348
	delete it->second;
deba@165
  1349
      }
deba@165
  1350
deba@165
  1351
      for (typename Sections::iterator it = _sections.begin(); 
deba@165
  1352
	   it != _sections.end(); ++it) {
deba@165
  1353
	delete it->second;
deba@165
  1354
      }
deba@165
  1355
deba@165
  1356
      if (local_is) {
deba@165
  1357
	delete _is;
deba@165
  1358
      }
deba@165
  1359
deba@165
  1360
    }
deba@165
  1361
deba@165
  1362
  private:
deba@165
  1363
    
deba@165
  1364
    GraphReader& operator=(const GraphReader&);
deba@165
  1365
deba@165
  1366
  public:
deba@165
  1367
deba@165
  1368
    /// \name Reading rules
deba@165
  1369
    /// @{
deba@165
  1370
    
deba@165
  1371
    /// \brief Node map reading rule
deba@165
  1372
    ///
deba@165
  1373
    /// Add a node map reading rule to the reader.
deba@165
  1374
    template <typename Map>
deba@165
  1375
    GraphReader& nodeMap(const std::string& caption, Map& map) {
deba@165
  1376
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
deba@165
  1377
      _reader_bits::MapStorageBase<Node>* storage = 
deba@165
  1378
	new _reader_bits::MapStorage<Node, Map>(map);
deba@165
  1379
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1380
      return *this;
deba@165
  1381
    }
deba@165
  1382
deba@165
  1383
    /// \brief Node map reading rule
deba@165
  1384
    ///
deba@165
  1385
    /// Add a node map reading rule with specialized converter to the
deba@165
  1386
    /// reader.
deba@165
  1387
    template <typename Map, typename Converter>
deba@165
  1388
    GraphReader& nodeMap(const std::string& caption, Map& map, 
deba@165
  1389
			   const Converter& converter = Converter()) {
deba@165
  1390
      checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
deba@165
  1391
      _reader_bits::MapStorageBase<Node>* storage = 
deba@165
  1392
	new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
deba@165
  1393
      _node_maps.push_back(std::make_pair(caption, storage));
deba@165
  1394
      return *this;
deba@165
  1395
    }
deba@165
  1396
deba@165
  1397
    /// \brief Edge map reading rule
deba@165
  1398
    ///
deba@165
  1399
    /// Add an edge map reading rule to the reader.
deba@165
  1400
    template <typename Map>
deba@165
  1401
    GraphReader& edgeMap(const std::string& caption, Map& map) {
deba@165
  1402
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
deba@165
  1403
      _reader_bits::MapStorageBase<Edge>* storage = 
deba@165
  1404
	new _reader_bits::MapStorage<Edge, Map>(map);
deba@165
  1405
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1406
      return *this;
deba@165
  1407
    }
deba@165
  1408
deba@165
  1409
    /// \brief Edge map reading rule
deba@165
  1410
    ///
deba@165
  1411
    /// Add an edge map reading rule with specialized converter to the
deba@165
  1412
    /// reader.
deba@165
  1413
    template <typename Map, typename Converter>
deba@165
  1414
    GraphReader& edgeMap(const std::string& caption, Map& map, 
deba@165
  1415
			  const Converter& converter = Converter()) {
deba@165
  1416
      checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
deba@165
  1417
      _reader_bits::MapStorageBase<Edge>* storage = 
deba@165
  1418
	new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter);
deba@165
  1419
      _edge_maps.push_back(std::make_pair(caption, storage));
deba@165
  1420
      return *this;
deba@165
  1421
    }
deba@165
  1422
deba@165
  1423
    /// \brief Arc map reading rule
deba@165
  1424
    ///
deba@165
  1425
    /// Add an arc map reading rule to the reader.
deba@165
  1426
    template <typename Map>
deba@165
  1427
    GraphReader& arcMap(const std::string& caption, Map& map) {
deba@165
  1428
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
deba@165
  1429
      _reader_bits::MapStorageBase<Edge>* forward_storage = 
deba@165
  1430
	new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
deba@165
  1431
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
deba@165
  1432
      _reader_bits::MapStorageBase<Edge>* backward_storage = 
deba@165
  1433
	new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
deba@165
  1434
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1435
      return *this;
deba@165
  1436
    }
deba@165
  1437
deba@165
  1438
    /// \brief Arc map reading rule
deba@165
  1439
    ///
deba@165
  1440
    /// Add an arc map reading rule with specialized converter to the
deba@165
  1441
    /// reader.
deba@165
  1442
    template <typename Map, typename Converter>
deba@165
  1443
    GraphReader& arcMap(const std::string& caption, Map& map, 
deba@165
  1444
			  const Converter& converter = Converter()) {
deba@165
  1445
      checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
deba@165
  1446
      _reader_bits::MapStorageBase<Edge>* forward_storage = 
deba@165
  1447
	new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter>
deba@165
  1448
	(_graph, map, converter);
deba@165
  1449
      _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
deba@165
  1450
      _reader_bits::MapStorageBase<Edge>* backward_storage = 
deba@165
  1451
	new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter>
deba@165
  1452
	(_graph, map, converter);
deba@165
  1453
      _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
deba@165
  1454
      return *this;
deba@165
  1455
    }
deba@165
  1456
deba@165
  1457
    /// \brief Attribute reading rule
deba@165
  1458
    ///
deba@165
  1459
    /// Add an attribute reading rule to the reader.
deba@165
  1460
    template <typename Value>
deba@165
  1461
    GraphReader& attribute(const std::string& caption, Value& value) {
deba@165
  1462
      _reader_bits::ValueStorageBase* storage = 
deba@165
  1463
	new _reader_bits::ValueStorage<Value>(value);
deba@165
  1464
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1465
      return *this;
deba@165
  1466
    }
deba@165
  1467
deba@165
  1468
    /// \brief Attribute reading rule
deba@165
  1469
    ///
deba@165
  1470
    /// Add an attribute reading rule with specialized converter to the
deba@165
  1471
    /// reader.
deba@165
  1472
    template <typename Value, typename Converter>
deba@165
  1473
    GraphReader& attribute(const std::string& caption, Value& value, 
deba@165
  1474
			     const Converter& converter = Converter()) {
deba@165
  1475
      _reader_bits::ValueStorageBase* storage = 
deba@165
  1476
	new _reader_bits::ValueStorage<Value, Converter>(value, converter);
deba@165
  1477
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1478
      return *this;
deba@165
  1479
    }
deba@165
  1480
deba@165
  1481
    /// \brief Node reading rule
deba@165
  1482
    ///
deba@165
  1483
    /// Add a node reading rule to reader.
deba@165
  1484
    GraphReader& node(const std::string& caption, Node& node) {
deba@165
  1485
      typedef _reader_bits::MapLookUpConverter<Node> Converter;
deba@165
  1486
      Converter converter(_node_index);
deba@165
  1487
      _reader_bits::ValueStorageBase* storage = 
deba@165
  1488
	new _reader_bits::ValueStorage<Node, Converter>(node, converter);
deba@165
  1489
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1490
      return *this;
deba@165
  1491
    }
deba@165
  1492
deba@165
  1493
    /// \brief Edge reading rule
deba@165
  1494
    ///
deba@165
  1495
    /// Add an edge reading rule to reader.
deba@165
  1496
    GraphReader& edge(const std::string& caption, Edge& edge) {
deba@165
  1497
      typedef _reader_bits::MapLookUpConverter<Edge> Converter;
deba@165
  1498
      Converter converter(_edge_index);
deba@165
  1499
      _reader_bits::ValueStorageBase* storage = 
deba@165
  1500
	new _reader_bits::ValueStorage<Edge, Converter>(edge, converter);
deba@165
  1501
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1502
      return *this;
deba@165
  1503
    }
deba@165
  1504
deba@165
  1505
    /// \brief Arc reading rule
deba@165
  1506
    ///
deba@165
  1507
    /// Add an arc reading rule to reader.
deba@165
  1508
    GraphReader& arc(const std::string& caption, Arc& arc) {
deba@165
  1509
      typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter;
deba@165
  1510
      Converter converter(_graph, _edge_index);
deba@165
  1511
      _reader_bits::ValueStorageBase* storage = 
deba@165
  1512
	new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
deba@165
  1513
      _attributes.insert(std::make_pair(caption, storage));
deba@165
  1514
      return *this;
deba@165
  1515
    }
deba@165
  1516
deba@165
  1517
    /// @}
deba@165
  1518
deba@165
  1519
    /// \name Select section by name
deba@165
  1520
    /// @{
deba@165
  1521
deba@165
  1522
    /// \brief Set \c \@nodes section to be read
deba@165
  1523
    ///
deba@165
  1524
    /// Set \c \@nodes section to be read
deba@165
  1525
    GraphReader& nodes(const std::string& caption) {
deba@165
  1526
      _nodes_caption = caption;
deba@165
  1527
      return *this;
deba@165
  1528
    }
deba@165
  1529
deba@165
  1530
    /// \brief Set \c \@edges section to be read
deba@165
  1531
    ///
deba@165
  1532
    /// Set \c \@edges section to be read
deba@165
  1533
    GraphReader& edges(const std::string& caption) {
deba@165
  1534
      _edges_caption = caption;
deba@165
  1535
      return *this;
deba@165
  1536
    }
deba@165
  1537
deba@165
  1538
    /// \brief Set \c \@attributes section to be read
deba@165
  1539
    ///
deba@165
  1540
    /// Set \c \@attributes section to be read
deba@165
  1541
    GraphReader& attributes(const std::string& caption) {
deba@165
  1542
      _attributes_caption = caption;
deba@165
  1543
      return *this;
deba@165
  1544
    }
deba@165
  1545
deba@165
  1546
    /// @}
deba@165
  1547
deba@165
  1548
    /// \name Section readers
deba@165
  1549
    /// @{
deba@165
  1550
deba@165
  1551
    /// \brief Add a section processor with line oriented reading
deba@165
  1552
    ///
deba@165
  1553
    /// In the \e LGF file extra sections can be placed, which contain
deba@165
  1554
    /// any data in arbitrary format. These sections can be read with
deba@165
  1555
    /// this function line by line. The first parameter is the type
deba@165
  1556
    /// descriptor of the section, the second is a functor, which
deba@165
  1557
    /// takes just one \c std::string parameter. At the reading
deba@165
  1558
    /// process, each line of the section will be given to the functor
deba@165
  1559
    /// object. However, the empty lines and the comment lines are
deba@165
  1560
    /// filtered out, and the leading whitespaces are stipped from
deba@165
  1561
    /// each processed string.
deba@165
  1562
    ///
deba@165
  1563
    /// For example let's see a section, which contain several
deba@165
  1564
    /// integers, which should be inserted into a vector.
deba@165
  1565
    ///\code
deba@165
  1566
    ///  @numbers
deba@165
  1567
    ///  12 45 23
deba@165
  1568
    ///  4
deba@165
  1569
    ///  23 6
deba@165
  1570
    ///\endcode
deba@165
  1571
    ///
deba@165
  1572
    /// The functor is implemented as an struct:
deba@165
  1573
    ///\code
deba@165
  1574
    ///  struct NumberSection {
deba@165
  1575
    ///    std::vector<int>& _data;
deba@165
  1576
    ///    NumberSection(std::vector<int>& data) : _data(data) {}
deba@165
  1577
    ///    void operator()(const std::string& line) {
deba@165
  1578
    ///      std::istringstream ls(line);
deba@165
  1579
    ///      int value;
deba@165
  1580
    ///      while (ls >> value) _data.push_back(value);
deba@165
  1581
    ///    }
deba@165
  1582
    ///  };
deba@165
  1583
    ///
deba@165
  1584
    ///  // ...
deba@165
  1585
    ///
deba@165
  1586
    ///  reader.sectionLines("numbers", NumberSection(vec));  
deba@165
  1587
    ///\endcode
deba@165
  1588
    template <typename Functor>
deba@165
  1589
    GraphReader& sectionLines(const std::string& type, Functor functor) {
deba@165
  1590
      LEMON_ASSERT(!type.empty(), "Type is not empty.");
deba@165
  1591
      LEMON_ASSERT(_sections.find(type) == _sections.end(), 
deba@165
  1592
		   "Multiple reading of section.");
deba@165
  1593
      LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
deba@165
  1594
		   type != "attributes", "Multiple reading of section.");
deba@165
  1595
      _sections.insert(std::make_pair(type, 
deba@165
  1596
        new _reader_bits::LineSection<Functor>(functor)));
deba@165
  1597
      return *this;
deba@165
  1598
    }
deba@165
  1599
deba@165
  1600
deba@165
  1601
    /// \brief Add a section processor with stream oriented reading
deba@165
  1602
    ///
deba@165
  1603
    /// In the \e LGF file extra sections can be placed, which contain
deba@165
  1604
    /// any data in arbitrary format. These sections can be read
deba@165
  1605
    /// directly with this function. The first parameter is the type
deba@165
  1606
    /// of the section, the second is a functor, which takes an \c
deba@165
  1607
    /// std::istream& and an int& parameter, the latter regard to the
deba@165
  1608
    /// line number of stream. The functor can read the input while
deba@165
  1609
    /// the section go on, and the line number should be modified
deba@165
  1610
    /// accordingly.
deba@165
  1611
    template <typename Functor>
deba@165
  1612
    GraphReader& sectionStream(const std::string& type, Functor functor) {
deba@165
  1613
      LEMON_ASSERT(!type.empty(), "Type is not empty.");
deba@165
  1614
      LEMON_ASSERT(_sections.find(type) == _sections.end(), 
deba@165
  1615
		   "Multiple reading of section.");
deba@165
  1616
      LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
deba@165
  1617
		   type != "attributes", "Multiple reading of section.");
deba@165
  1618
      _sections.insert(std::make_pair(type, 
deba@165
  1619
	 new _reader_bits::StreamSection<Functor>(functor)));
deba@165
  1620
      return *this;
deba@165
  1621
    }    
deba@165
  1622
    
deba@165
  1623
    /// @}
deba@165
  1624
deba@165
  1625
    /// \name Using previously constructed node or edge set
deba@165
  1626
    /// @{
deba@165
  1627
deba@165
  1628
    /// \brief Use previously constructed node set
deba@165
  1629
    ///
deba@165
  1630
    /// Use previously constructed node set, and specify the node
deba@165
  1631
    /// label map.
deba@165
  1632
    template <typename Map>
deba@165
  1633
    GraphReader& useNodes(const Map& map) {
deba@165
  1634
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
deba@165
  1635
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); 
deba@165
  1636
      _use_nodes = true;
deba@165
  1637
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@165
  1638
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@165
  1639
	_node_index.insert(std::make_pair(converter(map[n]), n));
deba@165
  1640
      }
deba@165
  1641
      return *this;
deba@165
  1642
    }
deba@165
  1643
deba@165
  1644
    /// \brief Use previously constructed node set
deba@165
  1645
    ///
deba@165
  1646
    /// Use previously constructed node set, and specify the node
deba@165
  1647
    /// label map and a functor which converts the label map values to
deba@165
  1648
    /// std::string.
deba@165
  1649
    template <typename Map, typename Converter>
deba@165
  1650
    GraphReader& useNodes(const Map& map, 
deba@165
  1651
			    const Converter& converter = Converter()) {
deba@165
  1652
      checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
deba@165
  1653
      LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); 
deba@165
  1654
      _use_nodes = true;
deba@165
  1655
      for (NodeIt n(_graph); n != INVALID; ++n) {
deba@165
  1656
	_node_index.insert(std::make_pair(converter(map[n]), n));
deba@165
  1657
      }
deba@165
  1658
      return *this;
deba@165
  1659
    }
deba@165
  1660
deba@165
  1661
    /// \brief Use previously constructed edge set
deba@165
  1662
    ///
deba@165
  1663
    /// Use previously constructed edge set, and specify the edge
deba@165
  1664
    /// label map.
deba@165
  1665
    template <typename Map>
deba@165
  1666
    GraphReader& useEdges(const Map& map) {
deba@165
  1667
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
deba@165
  1668
      LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
deba@165
  1669
      _use_edges = true;
deba@165
  1670
      _writer_bits::DefaultConverter<typename Map::Value> converter;
deba@165
  1671
      for (EdgeIt a(_graph); a != INVALID; ++a) {
deba@165
  1672
	_edge_index.insert(std::make_pair(converter(map[a]), a));
deba@165
  1673
      }
deba@165
  1674
      return *this;
deba@165
  1675
    }
deba@165
  1676
deba@165
  1677
    /// \brief Use previously constructed edge set
deba@165
  1678
    ///
deba@165
  1679
    /// Use previously constructed edge set, and specify the edge
deba@165
  1680
    /// label map and a functor which converts the label map values to
deba@165
  1681
    /// std::string.
deba@165
  1682
    template <typename Map, typename Converter>
deba@165
  1683
    GraphReader& useEdges(const Map& map, 
deba@165
  1684
			    const Converter& converter = Converter()) {
deba@165
  1685
      checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
deba@165
  1686
      LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); 
deba@165
  1687
      _use_edges = true;
deba@165
  1688
      for (EdgeIt a(_graph); a != INVALID; ++a) {
deba@165
  1689
	_edge_index.insert(std::make_pair(converter(map[a]), a));
deba@165
  1690
      }
deba@165
  1691
      return *this;
deba@165
  1692
    }
deba@165
  1693
deba@165
  1694
    /// @}
deba@165
  1695
deba@165
  1696
  private:
deba@165
  1697
deba@165
  1698
    bool readLine() {
deba@165
  1699
      std::string str;
deba@165
  1700
      while(++line_num, std::getline(*_is, str)) {
deba@165
  1701
	line.clear(); line.str(str);
deba@165
  1702
	char c;
deba@165
  1703
	if (line >> std::ws >> c && c != '#') {
deba@165
  1704
	  line.putback(c);
deba@165
  1705
	  return true;
deba@165
  1706
	}
deba@165
  1707
      }
deba@165
  1708
      return false;
deba@165
  1709
    }
deba@165
  1710
deba@165
  1711
    bool readSuccess() {
deba@165
  1712
      return static_cast<bool>(*_is);
deba@165
  1713
    }
deba@165
  1714
    
deba@165
  1715
    void skipSection() {
deba@165
  1716
      char c;
deba@165
  1717
      while (readSuccess() && line >> c && c != '@') {
deba@165
  1718
	readLine();
deba@165
  1719
      }
deba@165
  1720
      line.putback(c);
deba@165
  1721
    }
deba@165
  1722
deba@165
  1723
    void readNodes() {
deba@165
  1724
deba@165
  1725
      std::vector<int> map_index(_node_maps.size());
deba@165
  1726
      int map_num, label_index;
deba@165
  1727
deba@165
  1728
      if (!readLine()) 
deba@165
  1729
	throw DataFormatError("Cannot find map captions");
deba@165
  1730
      
deba@165
  1731
      {
deba@165
  1732
	std::map<std::string, int> maps;
deba@165
  1733
	
deba@165
  1734
	std::string map;
deba@165
  1735
	int index = 0;
deba@165
  1736
	while (_reader_bits::readToken(line, map)) {
deba@165
  1737
	  if (maps.find(map) != maps.end()) {
deba@165
  1738
	    std::ostringstream msg;
deba@165
  1739
	    msg << "Multiple occurence of node map: " << map;
deba@165
  1740
	    throw DataFormatError(msg.str().c_str());
deba@165
  1741
	  }
deba@165
  1742
	  maps.insert(std::make_pair(map, index));
deba@165
  1743
	  ++index;
deba@165
  1744
	}
deba@165
  1745
	
deba@165
  1746
	for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
deba@165
  1747
	  std::map<std::string, int>::iterator jt = 
deba@165
  1748
	    maps.find(_node_maps[i].first);
deba@165
  1749
	  if (jt == maps.end()) {
deba@165
  1750
	    std::ostringstream msg;
deba@165
  1751
	    msg << "Map not found in file: " << _node_maps[i].first;
deba@165
  1752
	    throw DataFormatError(msg.str().c_str());
deba@165
  1753
	  }
deba@165
  1754
	  map_index[i] = jt->second;
deba@165
  1755
	}
deba@165
  1756
deba@165
  1757
	{
deba@165
  1758
	  std::map<std::string, int>::iterator jt = maps.find("label");
deba@165
  1759
	  if (jt == maps.end())
deba@165
  1760
	    throw DataFormatError("Label map not found in file");
deba@165
  1761
	  label_index = jt->second;
deba@165
  1762
	}
deba@165
  1763
	map_num = maps.size();
deba@165
  1764
      }
deba@165
  1765
deba@165
  1766
      char c;
deba@165
  1767
      while (readLine() && line >> c && c != '@') {
deba@165
  1768
	line.putback(c);
deba@165
  1769
deba@165
  1770
	std::vector<std::string> tokens(map_num);
deba@165
  1771
	for (int i = 0; i < map_num; ++i) {
deba@165
  1772
	  if (!_reader_bits::readToken(line, tokens[i])) {
deba@165
  1773
	    std::ostringstream msg;
deba@165
  1774
	    msg << "Column not found (" << i + 1 << ")";
deba@165
  1775
	    throw DataFormatError(msg.str().c_str());
deba@165
  1776
	  }
deba@165
  1777
	}
deba@165
  1778
	if (line >> std::ws >> c)
deba@165
  1779
	  throw DataFormatError("Extra character on the end of line");
deba@165
  1780
	
deba@165
  1781
	Node n;
deba@165
  1782
	if (!_use_nodes) {
deba@165
  1783
	  n = _graph.addNode();
deba@165
  1784
	  _node_index.insert(std::make_pair(tokens[label_index], n));
deba@165
  1785
	} else {
deba@165
  1786
	  typename std::map<std::string, Node>::iterator it =
deba@165
  1787
	    _node_index.find(tokens[label_index]);
deba@165
  1788
	  if (it == _node_index.end()) {
deba@165
  1789
	    std::ostringstream msg;
deba@165
  1790
	    msg << "Node with label not found: " << tokens[label_index];
deba@165
  1791
	    throw DataFormatError(msg.str().c_str());	    
deba@165
  1792
	  }
deba@165
  1793
	  n = it->second;
deba@165
  1794
	}
deba@165
  1795
deba@165
  1796
	for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
deba@165
  1797
	  _node_maps[i].second->set(n, tokens[map_index[i]]);
deba@165
  1798
	}
deba@165
  1799
deba@165
  1800
      }
deba@165
  1801
      if (readSuccess()) {
deba@165
  1802
	line.putback(c);
deba@165
  1803
      }
deba@165
  1804
    }
deba@165
  1805
deba@165
  1806
    void readEdges() {
deba@165
  1807
deba@165
  1808
      std::vector<int> map_index(_edge_maps.size());
deba@165
  1809
      int map_num, label_index;
deba@165
  1810
deba@165
  1811
      if (!readLine()) 
deba@165
  1812
	throw DataFormatError("Cannot find map captions");
deba@165
  1813
      
deba@165
  1814
      {
deba@165
  1815
	std::map<std::string, int> maps;
deba@165
  1816
	
deba@165
  1817
	std::string map;
deba@165
  1818
	int index = 0;
deba@165
  1819
	while (_reader_bits::readToken(line, map)) {
deba@165
  1820
	  if (maps.find(map) != maps.end()) {
deba@165
  1821
	    std::ostringstream msg;
deba@165
  1822
	    msg << "Multiple occurence of edge map: " << map;
deba@165
  1823
	    throw DataFormatError(msg.str().c_str());
deba@165
  1824
	  }
deba@165
  1825
	  maps.insert(std::make_pair(map, index));
deba@165
  1826
	  ++index;
deba@165
  1827
	}
deba@165
  1828
	
deba@165
  1829
	for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
deba@165
  1830
	  std::map<std::string, int>::iterator jt = 
deba@165
  1831
	    maps.find(_edge_maps[i].first);
deba@165
  1832
	  if (jt == maps.end()) {
deba@165
  1833
	    std::ostringstream msg;
deba@165
  1834
	    msg << "Map not found in file: " << _edge_maps[i].first;
deba@165
  1835
	    throw DataFormatError(msg.str().c_str());
deba@165
  1836
	  }
deba@165
  1837
	  map_index[i] = jt->second;
deba@165
  1838
	}
deba@165
  1839
deba@165
  1840
	{
deba@165
  1841
	  std::map<std::string, int>::iterator jt = maps.find("label");
deba@165
  1842
	  if (jt == maps.end())
deba@165
  1843
	    throw DataFormatError("Label map not found in file");
deba@165
  1844
	  label_index = jt->second;
deba@165
  1845
	}
deba@165
  1846
	map_num = maps.size();
deba@165
  1847
      }
deba@165
  1848
deba@165
  1849
      char c;
deba@165
  1850
      while (readLine() && line >> c && c != '@') {
deba@165
  1851
	line.putback(c);
deba@165
  1852
deba@165
  1853
	std::string source_token;
deba@165
  1854
	std::string target_token;
deba@165
  1855
deba@165
  1856
	if (!_reader_bits::readToken(line, source_token))
deba@165
  1857
	  throw DataFormatError("Source not found");
deba@165
  1858
deba@165
  1859
	if (!_reader_bits::readToken(line, target_token))
deba@165
  1860
	  throw DataFormatError("Source not found");
deba@165
  1861
	
deba@165
  1862
	std::vector<std::string> tokens(map_num);
deba@165
  1863
	for (int i = 0; i < map_num; ++i) {
deba@165
  1864
	  if (!_reader_bits::readToken(line, tokens[i])) {
deba@165
  1865
	    std::ostringstream msg;
deba@165
  1866
	    msg << "Column not found (" << i + 1 << ")";
deba@165
  1867
	    throw DataFormatError(msg.str().c_str());
deba@165
  1868
	  }
deba@165
  1869
	}
deba@165
  1870
	if (line >> std::ws >> c)
deba@165
  1871
	  throw DataFormatError("Extra character on the end of line");
deba@165
  1872
	
deba@165
  1873
	Edge e;
deba@165
  1874
	if (!_use_edges) {
deba@165
  1875
deba@165
  1876
          typename NodeIndex::iterator it;
deba@165
  1877
 
deba@165
  1878
          it = _node_index.find(source_token);
deba@165
  1879
          if (it == _node_index.end()) {
deba@165
  1880
            std::ostringstream msg;
deba@165
  1881
            msg << "Item not found: " << source_token;
deba@165
  1882
            throw DataFormatError(msg.str().c_str());
deba@165
  1883
          }
deba@165
  1884
          Node source = it->second;
deba@165
  1885
deba@165
  1886
          it = _node_index.find(target_token);
deba@165
  1887
          if (it == _node_index.end()) {       
deba@165
  1888
            std::ostringstream msg;            
deba@165
  1889
            msg << "Item not found: " << target_token;
deba@165
  1890
            throw DataFormatError(msg.str().c_str());
deba@165
  1891
          }                                          
deba@165
  1892
          Node target = it->second;                            
deba@165
  1893
deba@165
  1894
	  e = _graph.addEdge(source, target);
deba@165
  1895
	  _edge_index.insert(std::make_pair(tokens[label_index], e));
deba@165
  1896
	} else {
deba@165
  1897
	  typename std::map<std::string, Edge>::iterator it =
deba@165
  1898
	    _edge_index.find(tokens[label_index]);
deba@165
  1899
	  if (it == _edge_index.end()) {
deba@165
  1900
	    std::ostringstream msg;
deba@165
  1901
	    msg << "Edge with label not found: " << tokens[label_index];
deba@165
  1902
	    throw DataFormatError(msg.str().c_str());	    
deba@165
  1903
	  }
deba@165
  1904
	  e = it->second;
deba@165
  1905
	}
deba@165
  1906
deba@165
  1907
	for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
deba@165
  1908
	  _edge_maps[i].second->set(e, tokens[map_index[i]]);
deba@165
  1909
	}
deba@165
  1910
deba@165
  1911
      }
deba@165
  1912
      if (readSuccess()) {
deba@165
  1913
	line.putback(c);
deba@165
  1914
      }
deba@165
  1915
    }
deba@165
  1916
deba@165
  1917
    void readAttributes() {
deba@165
  1918
deba@165
  1919
      std::set<std::string> read_attr;
deba@165
  1920
deba@165
  1921
      char c;
deba@165
  1922
      while (readLine() && line >> c && c != '@') {
deba@165
  1923
	line.putback(c);
deba@165
  1924
	
deba@165
  1925
	std::string attr, token;
deba@165
  1926
	if (!_reader_bits::readToken(line, attr))
deba@165
  1927
	  throw DataFormatError("Attribute name not found");
deba@165
  1928
	if (!_reader_bits::readToken(line, token))
deba@165
  1929
	  throw DataFormatError("Attribute value not found");
deba@165
  1930
	if (line >> c)
deba@165
  1931
	  throw DataFormatError("Extra character on the end of line");	  
deba@165
  1932
deba@165
  1933
	{
deba@165
  1934
	  std::set<std::string>::iterator it = read_attr.find(attr);
deba@165
  1935
	  if (it != read_attr.end()) {
deba@165
  1936
	    std::ostringstream msg;
deba@165
  1937
	    msg << "Multiple occurence of attribute " << attr;
deba@165
  1938
	    throw DataFormatError(msg.str().c_str());
deba@165
  1939
	  }
deba@165
  1940
	  read_attr.insert(attr);
deba@165
  1941
	}
deba@165
  1942
	
deba@165
  1943
	{
deba@165
  1944
	  typename Attributes::iterator it = _attributes.lower_bound(attr);
deba@165
  1945
	  while (it != _attributes.end() && it->first == attr) {
deba@165
  1946
	    it->second->set(token);
deba@165
  1947
	    ++it;
deba@165
  1948
	  }
deba@165
  1949
	}
deba@165
  1950
deba@165
  1951
      }
deba@165
  1952
      if (readSuccess()) {
deba@165
  1953
	line.putback(c);
deba@165
  1954
      }
deba@165
  1955
      for (typename Attributes::iterator it = _attributes.begin();
deba@165
  1956
	   it != _attributes.end(); ++it) {
deba@165
  1957
	if (read_attr.find(it->first) == read_attr.end()) {
deba@165
  1958
	  std::ostringstream msg;
deba@165
  1959
	  msg << "Attribute not found in file: " << it->first;
deba@165
  1960
	  throw DataFormatError(msg.str().c_str());
deba@165
  1961
	}	
deba@165
  1962
      }
deba@165
  1963
    }
deba@165
  1964
deba@165
  1965
  public:
deba@165
  1966
deba@165
  1967
    /// \name Execution of the reader    
deba@165
  1968
    /// @{
deba@165
  1969
deba@165
  1970
    /// \brief Start the batch processing
deba@165
  1971
    ///
deba@165
  1972
    /// This function starts the batch processing
deba@165
  1973
    void run() {
deba@165
  1974
      
deba@165
  1975
      LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
deba@165
  1976
      
deba@165
  1977
      bool nodes_done = false;
deba@165
  1978
      bool edges_done = false;
deba@165
  1979
      bool attributes_done = false;
deba@165
  1980
      std::set<std::string> extra_sections;
deba@165
  1981
deba@165
  1982
      line_num = 0;      
deba@165
  1983
      readLine();
deba@165
  1984
deba@165
  1985
      while (readSuccess()) {
deba@165
  1986
	skipSection();
deba@165
  1987
	try {
deba@165
  1988
	  char c;
deba@165
  1989
	  std::string section, caption;
deba@165
  1990
	  line >> c;
deba@165
  1991
	  _reader_bits::readToken(line, section);
deba@165
  1992
	  _reader_bits::readToken(line, caption);
deba@165
  1993
deba@165
  1994
	  if (line >> c) 
deba@165
  1995
	    throw DataFormatError("Extra character on the end of line");
deba@165
  1996
deba@165
  1997
	  if (section == "nodes" && !nodes_done) {
deba@165
  1998
	    if (_nodes_caption.empty() || _nodes_caption == caption) {
deba@165
  1999
	      readNodes();
deba@165
  2000
	      nodes_done = true;
deba@165
  2001
	    }
deba@165
  2002
	  } else if ((section == "edges" || section == "arcs") && 
deba@165
  2003
		     !edges_done) {
deba@165
  2004
	    if (_edges_caption.empty() || _edges_caption == caption) {
deba@165
  2005
	      readEdges();
deba@165
  2006
	      edges_done = true;
deba@165
  2007
	    }
deba@165
  2008
	  } else if (section == "attributes" && !attributes_done) {
deba@165
  2009
	    if (_attributes_caption.empty() || _attributes_caption == caption) {
deba@165
  2010
	      readAttributes();
deba@165
  2011
	      attributes_done = true;
deba@165
  2012
	    }
deba@165
  2013
	  } else {
deba@165
  2014
	    if (extra_sections.find(section) != extra_sections.end()) {
deba@165
  2015
	      std::ostringstream msg;
deba@165
  2016
	      msg << "Multiple occurence of section " << section;
deba@165
  2017
	      throw DataFormatError(msg.str().c_str());
deba@165
  2018
	    }
deba@165
  2019
	    Sections::iterator it = _sections.find(section);
deba@165
  2020
	    if (it != _sections.end()) {
deba@165
  2021
	      extra_sections.insert(section);
deba@165
  2022
	      it->second->process(*_is, line_num);
deba@165
  2023
	      readLine();
deba@165
  2024
	    } else {
deba@165
  2025
	      readLine();
deba@165
  2026
	      skipSection();
deba@165
  2027
	    }
deba@165
  2028
	  }
deba@165
  2029
	} catch (DataFormatError& error) {
deba@165
  2030
	  error.line(line_num);
deba@165
  2031
	  throw;
deba@165
  2032
	}	
deba@165
  2033
      }
deba@165
  2034
deba@165
  2035
      if (!nodes_done) {
deba@165
  2036
	throw DataFormatError("Section @nodes not found");
deba@165
  2037
      }
deba@165
  2038
deba@165
  2039
      if (!edges_done) {
deba@165
  2040
	throw DataFormatError("Section @edges not found");
deba@165
  2041
      }
deba@165
  2042
deba@165
  2043
      if (!attributes_done && !_attributes.empty()) {
deba@165
  2044
	throw DataFormatError("Section @attributes not found");
deba@165
  2045
      }
deba@165
  2046
deba@165
  2047
    }
deba@165
  2048
deba@165
  2049
    /// @}
deba@165
  2050
    
deba@165
  2051
  };
deba@165
  2052
deba@165
  2053
  /// \relates GraphReader
deba@165
  2054
  template <typename Graph>
deba@165
  2055
  GraphReader<Graph> graphReader(std::istream& is, Graph& graph) {
deba@165
  2056
    GraphReader<Graph> tmp(is, graph);
deba@165
  2057
    return tmp;
deba@165
  2058
  }
deba@165
  2059
deba@165
  2060
  /// \relates GraphReader
deba@165
  2061
  template <typename Graph>
deba@165
  2062
  GraphReader<Graph> graphReader(const std::string& fn, 
deba@165
  2063
				       Graph& graph) {
deba@165
  2064
    GraphReader<Graph> tmp(fn, graph);
deba@165
  2065
    return tmp;
deba@165
  2066
  }
deba@165
  2067
deba@165
  2068
  /// \relates GraphReader
deba@165
  2069
  template <typename Graph>
deba@165
  2070
  GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
deba@165
  2071
    GraphReader<Graph> tmp(fn, graph);
deba@165
  2072
    return tmp;
deba@165
  2073
  }
deba@127
  2074
}
deba@127
  2075
deba@127
  2076
#endif