src/lemon/graph_reader.h
author athos
Tue, 22 Feb 2005 10:23:08 +0000
changeset 1167 ccbca6ba8b59
parent 1138 f68cb8752d81
child 1208 f486d30e4e7b
permissions -rw-r--r--
Corrected spelling errors.
deba@1137
     1
/* -*- C++ -*-
deba@1137
     2
 * src/lemon/graph_reader.h - Part of LEMON, a generic C++ optimization library
deba@1137
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1137
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
deba@1137
     6
 *
deba@1137
     7
 * Permission to use, modify and distribute this software is granted
deba@1137
     8
 * provided that this copyright notice appears in all copies. For
deba@1137
     9
 * precise terms see the accompanying LICENSE file.
deba@1137
    10
 *
deba@1137
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1137
    12
 * express or implied, and with no claim as to its suitability for any
deba@1137
    13
 * purpose.
deba@1137
    14
 *
deba@1137
    15
 */
deba@1137
    16
deba@1137
    17
///\ingroup gio
deba@1137
    18
///\file
deba@1137
    19
///\brief Graph reader.
deba@1137
    20
deba@1137
    21
#include <iostream>
deba@1137
    22
#include <sstream>
deba@1137
    23
deba@1137
    24
#include <map>
deba@1137
    25
#include <vector>
deba@1137
    26
deba@1137
    27
#include <memory>
deba@1137
    28
deba@1137
    29
#include <lemon/error.h>
deba@1137
    30
deba@1137
    31
/// \todo fix exceptions
deba@1137
    32
deba@1137
    33
deba@1137
    34
namespace lemon {
deba@1137
    35
deba@1137
    36
  // Exceptions
deba@1137
    37
deba@1137
    38
  class IOException {
deba@1137
    39
  public:
deba@1137
    40
    virtual ~IOException() {}
deba@1137
    41
    virtual string what() const = 0;
deba@1137
    42
  };
deba@1137
    43
deba@1137
    44
  class DataFormatException : public IOException {
deba@1137
    45
    std::string message;
deba@1137
    46
  public:
deba@1137
    47
    DataFormatException(const std::string& _message) 
deba@1137
    48
      : message(_message) {}
deba@1137
    49
    std::string what() const {
deba@1137
    50
      return "DataFormatException: " + message; 
deba@1137
    51
    }
deba@1137
    52
  };
deba@1137
    53
deba@1137
    54
  template <typename _Exception>
deba@1137
    55
  class StreamException : public _Exception {
deba@1137
    56
  public:
deba@1137
    57
    typedef _Exception Exception;
deba@1137
    58
    StreamException(int _line, Exception _exception) 
deba@1137
    59
      : Exception(_exception), line_num(_line) {}
deba@1137
    60
    virtual int line() const {
deba@1137
    61
      return line_num;
deba@1137
    62
    }
deba@1137
    63
deba@1137
    64
    virtual ~StreamException() {}
deba@1137
    65
deba@1137
    66
    virtual std::string what() const {
deba@1137
    67
      ostringstream os;
deba@1137
    68
      os << Exception::what() << " in line " << line();
deba@1137
    69
      return os.str();
deba@1137
    70
    }
deba@1137
    71
  private:
deba@1137
    72
    int line_num;
deba@1137
    73
  };  
deba@1137
    74
deba@1137
    75
deba@1137
    76
  /// \brief Standard ReaderTraits for the GraphReader class.
deba@1137
    77
  ///
deba@1137
    78
  /// Standard ReaderTraits for the GraphReader class.
deba@1137
    79
  /// It defines standard reading method for all type of value. 
deba@1137
    80
  struct DefaultReaderTraits {
deba@1137
    81
deba@1137
    82
    /// \brief Template class for reading an value.
deba@1137
    83
    ///
deba@1137
    84
    /// Template class for reading an value.
deba@1137
    85
    template <typename _Value>
deba@1137
    86
    struct Reader {
deba@1137
    87
      /// The value type.
deba@1137
    88
      typedef _Value Value;
deba@1137
    89
      /// \brief Reads a value from the given stream.
deba@1137
    90
      ///
deba@1137
    91
      /// Reads a value from the given stream.
deba@1137
    92
      void read(std::istream& is, Value& value) {
deba@1137
    93
	if (!(is >> value)) 
deba@1137
    94
	  throw DataFormatException("Default Reader format exception");
deba@1137
    95
      }
deba@1137
    96
    };
deba@1137
    97
deba@1137
    98
    /// The reader class for the not needed maps.
deba@1137
    99
    typedef Reader<std::string> DefaultReader;
deba@1137
   100
deba@1137
   101
  };
deba@1137
   102
deba@1137
   103
  /// \brief Reader class for quoted strings.
deba@1137
   104
  ///
deba@1137
   105
  /// Reader class for quoted strings. It can process the escape
deba@1137
   106
  /// sequences in the string.
deba@1137
   107
  class QuotedStringReader {
deba@1137
   108
  public:
deba@1137
   109
    typedef std::string Value;
deba@1137
   110
    
deba@1137
   111
    /// \brief Constructor for the reader.
deba@1137
   112
    ///
deba@1137
   113
    /// Constructor for the reader. If the given parameter is true
deba@1137
   114
    /// the reader processes the escape sequences.
deba@1137
   115
    QuotedStringReader(bool _escaped = true) : escaped(_escaped) {}
deba@1137
   116
    
deba@1137
   117
    /// \brief Reads a quoted string from the given stream.
deba@1137
   118
    ///
deba@1137
   119
    /// Reads a quoted string from the given stream.
deba@1137
   120
    void read(std::istream& is, std::string& value) {
deba@1137
   121
      char c;
deba@1137
   122
      value.clear();
deba@1137
   123
      is >> ws;
deba@1137
   124
      if (!is.get(c) || c != '\"') 
deba@1137
   125
	throw DataFormatException("Quoted string format");
deba@1137
   126
      while (is.get(c) && c != '\"') {
deba@1137
   127
	if (escaped && c == '\\') {
deba@1137
   128
	  value += readEscape(is);
deba@1137
   129
	} else {
deba@1137
   130
	  value += c;
deba@1137
   131
	}
deba@1137
   132
      }
deba@1137
   133
      if (!is) throw DataFormatException("Quoted string format");
deba@1137
   134
    }
deba@1137
   135
deba@1137
   136
  private:
deba@1137
   137
    
deba@1137
   138
    static char readEscape(std::istream& is) {
deba@1137
   139
      char c;
deba@1137
   140
      switch (is.get(c), c) {
deba@1137
   141
      case '\\':
deba@1137
   142
	return '\\';
deba@1137
   143
      case '\"':
deba@1137
   144
	return '\"';
deba@1137
   145
      case '\'':
deba@1137
   146
	return '\'';
deba@1137
   147
      case '\?':
deba@1137
   148
	return '\?';
deba@1137
   149
      case 'a':
deba@1137
   150
	return '\a';
deba@1137
   151
      case 'b':
deba@1137
   152
	return '\b';
deba@1137
   153
      case 'f':
deba@1137
   154
	return '\f';
deba@1137
   155
      case 'n':
deba@1137
   156
	return '\n';
deba@1137
   157
      case 'r':
deba@1137
   158
	return '\r';
deba@1137
   159
      case 't':
deba@1137
   160
	return '\t';
deba@1137
   161
      case 'v':
deba@1137
   162
	return '\v';
deba@1137
   163
      case 'x':
deba@1137
   164
	{
deba@1137
   165
	  int code;
deba@1137
   166
	  if (!is.get(c) || !isHex(c)) 
deba@1137
   167
	    throw DataFormatException("Escape format exception");
deba@1137
   168
	  else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
deba@1137
   169
	  else code = code * 16 + valueHex(c);
deba@1137
   170
	  return code;
deba@1137
   171
	}
deba@1137
   172
      default:
deba@1137
   173
	{
deba@1137
   174
	  int code;
deba@1137
   175
	  if (!isOct(c)) 
deba@1137
   176
	    throw DataFormatException("Escape format exception");
deba@1137
   177
	  else if (code = valueOct(c), !is.get(c) || !isOct(c)) 
deba@1137
   178
	    is.putback(c);
deba@1137
   179
	  else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) 
deba@1137
   180
	    is.putback(c);
deba@1137
   181
	  else code = code * 8 + valueOct(c);
deba@1137
   182
	  return code;
deba@1137
   183
	}	      
deba@1137
   184
      } 
deba@1137
   185
    }
deba@1137
   186
deba@1137
   187
    static bool isOct(char c) {
deba@1137
   188
      return '0' <= c && c <='7'; 
deba@1137
   189
    }
deba@1137
   190
    
deba@1137
   191
    static int valueOct(char c) {
deba@1137
   192
      return c - '0';
deba@1137
   193
    }
deba@1137
   194
deba@1137
   195
   static bool isHex(char c) {
deba@1137
   196
      return ('0' <= c && c <= '9') || 
deba@1137
   197
	('a' <= c && c <= 'z') || 
deba@1137
   198
	('A' <= c && c <= 'Z'); 
deba@1137
   199
    }
deba@1137
   200
    
deba@1137
   201
    static int valueHex(char c) {
deba@1137
   202
      if ('0' <= c && c <= '9') return c - '0';
deba@1137
   203
      if ('a' <= c && c <= 'z') return c - 'a' + 10;
deba@1137
   204
      return c - 'A' + 10;
deba@1137
   205
    }
deba@1137
   206
deba@1137
   207
    bool escaped;
deba@1137
   208
  };
deba@1137
   209
deba@1137
   210
  /// \brief The graph reader class.
deba@1137
   211
  ///
deba@1137
   212
  /// The reader class for the graph input.
alpar@1138
   213
  /// \see \ref GraphWriter
alpar@1138
   214
  /// \see \ref graph-io-page
deba@1137
   215
  template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
deba@1137
   216
  class GraphReader {
deba@1137
   217
  public:
deba@1137
   218
    
deba@1137
   219
    typedef _Graph Graph;
deba@1137
   220
    typedef typename Graph::Node Node;
deba@1137
   221
    typedef typename Graph::Edge Edge;
deba@1137
   222
deba@1137
   223
    typedef _ReaderTraits ReaderTraits;
deba@1137
   224
    typedef typename ReaderTraits::DefaultReader DefaultReader;
deba@1137
   225
deba@1137
   226
    /// \brief Construct a new GraphReader.
deba@1137
   227
    ///
deba@1137
   228
    /// Construct a new GraphReader. It reads from the given map,
deba@1137
   229
    /// it constructs the given map and it use the given reader as the
deba@1137
   230
    /// default skipper.
deba@1137
   231
    GraphReader(std::istream& _is, Graph& _graph, 
deba@1137
   232
		const DefaultReader& _reader = DefaultReader()) 
deba@1137
   233
      : is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {}
deba@1137
   234
deba@1137
   235
    /// \brief Destruct the graph reader.
deba@1137
   236
    ///
deba@1137
   237
    /// Destruct the graph reader.
deba@1137
   238
    ~GraphReader() {
deba@1137
   239
deba@1137
   240
      for (typename NodeMapReaders::iterator it = node_map_readers.begin(); 
deba@1137
   241
	   it != node_map_readers.end(); ++it) {
deba@1137
   242
	delete it->second;
deba@1137
   243
      }
deba@1137
   244
deba@1137
   245
      for (typename EdgeMapReaders::iterator it = edge_map_readers.begin(); 
deba@1137
   246
	   it != edge_map_readers.end(); ++it) {
deba@1137
   247
	delete it->second;
deba@1137
   248
      }
deba@1137
   249
deba@1137
   250
    }
deba@1137
   251
deba@1137
   252
    /// \brief Add a new node map reader command for the reader.
deba@1137
   253
    ///
deba@1137
   254
    /// Add a new node map reader command for the reader.
deba@1137
   255
    template <typename Map>
deba@1137
   256
    GraphReader& addNodeMap(std::string name, Map& map) {
deba@1137
   257
      return addNodeMap<typename ReaderTraits::template 
deba@1137
   258
	Reader<typename Map::Value>, Map>(name, map);
deba@1137
   259
    }
deba@1137
   260
deba@1137
   261
    /// \brief Add a new node map reader command for the reader.
deba@1137
   262
    ///
deba@1137
   263
    /// Add a new node map reader command for the reader.
deba@1137
   264
    template <typename Reader, typename Map>
deba@1137
   265
    GraphReader& addNodeMap(std::string name, Map& map, 
deba@1137
   266
			     const Reader& reader = Reader()) {
deba@1137
   267
      if (node_map_readers.find(name) != node_map_readers.end()) {
deba@1137
   268
	throw Exception() << "Multiple read rule for node map: " << name;
deba@1137
   269
      }
deba@1137
   270
      node_map_readers.insert(
deba@1137
   271
        make_pair(name, new MapReader<Node, Map, Reader>(map, reader)));
deba@1137
   272
      return *this;
deba@1137
   273
    }
deba@1137
   274
deba@1137
   275
    /// \brief Add a new node map skipper command for the reader.
deba@1137
   276
    ///
deba@1137
   277
    /// Add a new node map skipper command for the reader.
deba@1137
   278
    template <typename Reader>
deba@1137
   279
    GraphReader& skipNodeMap(std::string name, 
deba@1137
   280
			     const Reader& reader = Reader()) {
deba@1137
   281
      if (node_map_readers.find(name) != node_map_readers.end()) {
deba@1137
   282
	throw Exception() << "Multiple read rule for node map: " << name;
deba@1137
   283
      }
deba@1137
   284
      node_map_readers.insert(
deba@1137
   285
        make_pair(name, new SkipReader<Node, Reader>(reader)));
deba@1137
   286
      return *this;
deba@1137
   287
    }
deba@1137
   288
deba@1137
   289
    /// \brief Add a new edge map reader command for the reader.
deba@1137
   290
    ///
deba@1137
   291
    /// Add a new edge map reader command for the reader.
deba@1137
   292
    template <typename Map>
deba@1137
   293
    GraphReader& addEdgeMap(std::string name, Map& map) { 
deba@1137
   294
      return addEdgeMap<typename ReaderTraits::template
deba@1137
   295
	Reader<typename Map::Value>, Map>(name, map);
deba@1137
   296
    }
deba@1137
   297
deba@1137
   298
deba@1137
   299
    /// \brief Add a new edge map reader command for the reader.
deba@1137
   300
    ///
deba@1137
   301
    /// Add a new edge map reader command for the reader.
deba@1137
   302
    template <typename Reader, typename Map>
deba@1137
   303
    GraphReader& addEdgeMap(std::string name, Map& map,
deba@1137
   304
			     const Reader& reader = Reader()) {
deba@1137
   305
      if (edge_map_readers.find(name) != edge_map_readers.end()) {
deba@1137
   306
	throw Exception() << "Multiple read rule for edge map: " << name;
deba@1137
   307
      }
deba@1137
   308
      edge_map_readers.insert(
deba@1137
   309
        make_pair(name, new MapReader<Edge, Map, Reader>(map, reader)));
deba@1137
   310
      return *this;
deba@1137
   311
    }
deba@1137
   312
deba@1137
   313
    /// \brief Add a new edge map skipper command for the reader.
deba@1137
   314
    ///
deba@1137
   315
    /// Add a new edge map skipper command for the reader.
deba@1137
   316
    template <typename Reader>
deba@1137
   317
    GraphReader& skipEdgeMap(std::string name,
deba@1137
   318
			     const Reader& reader = Reader()) {
deba@1137
   319
      if (edge_map_readers.find(name) != edge_map_readers.end()) {
deba@1137
   320
	throw Exception() << "Multiple read rule for edge map: " << name;
deba@1137
   321
      }
deba@1137
   322
      edge_map_readers.insert(
deba@1137
   323
        make_pair(name, new SkipReader<Edge, Reader>(reader)));
deba@1137
   324
      return *this;
deba@1137
   325
    }
deba@1137
   326
deba@1137
   327
    /// \brief Add a new labeled node reader for the reader.
deba@1137
   328
    ///
deba@1137
   329
    /// Add a new labeled node reader for the reader.
deba@1137
   330
    GraphReader& addNode(std::string name, Node& node) {
deba@1137
   331
      if (node_readers.find(name) != node_readers.end()) {
deba@1137
   332
	throw Exception() << "Multiple read rule for node";
deba@1137
   333
      }
deba@1137
   334
      node_readers.insert(make_pair(name, &node));
deba@1137
   335
      return *this;
deba@1137
   336
    }
deba@1137
   337
deba@1137
   338
    /// \brief Add a new labeled edge reader for the reader.
deba@1137
   339
    ///
deba@1137
   340
    /// Add a new labeled edge reader for the reader.
deba@1137
   341
    GraphReader& addEdge(std::string name, Edge& edge) {
deba@1137
   342
      if (edge_readers.find(name) != edge_readers.end()) {
deba@1137
   343
	throw Exception() << "Multiple read rule for edge";
deba@1137
   344
      }
deba@1137
   345
      edge_readers.insert(make_pair(name, &edge));
deba@1137
   346
      return *this;
deba@1137
   347
    }
deba@1137
   348
deba@1137
   349
    /// \brief Executes the reader commands.
deba@1137
   350
    ///
deba@1137
   351
    /// Executes the reader commands.
deba@1137
   352
    void run() {
deba@1137
   353
      int line_num = 0;
deba@1137
   354
      std::auto_ptr<InverterBase<Node> > nodeInverter;
deba@1137
   355
      std::auto_ptr<InverterBase<Edge> > edgeInverter;
deba@1137
   356
      try {
deba@1137
   357
	std::string line = readNotEmptyLine(is, line_num);
deba@1137
   358
	if (line.find("@nodeset") == 0) {
deba@1137
   359
	  line = readNodeSet(line_num, nodeInverter);
deba@1137
   360
	} 
deba@1137
   361
	if (line.find("@edgeset") == 0) {
deba@1137
   362
	  line = readEdgeSet(line_num, edgeInverter, nodeInverter);
deba@1137
   363
	}
deba@1137
   364
	if (line.find("@nodes") == 0) {
deba@1137
   365
	  line = readNodes(line_num, nodeInverter);
deba@1137
   366
	}
deba@1137
   367
	if (line.find("@edges") == 0) {
deba@1137
   368
	  line = readEdges(line_num, edgeInverter);
deba@1137
   369
	}
deba@1137
   370
	if (line.find("@end") != 0) {
deba@1137
   371
	  throw DataFormatException("Invalid control sequence: " + line);
deba@1137
   372
	}
deba@1137
   373
      } catch (DataFormatException e) {
deba@1137
   374
	throw StreamException<DataFormatException>(line_num, e);
deba@1137
   375
      }
deba@1137
   376
    }
deba@1137
   377
deba@1137
   378
  private:
deba@1137
   379
deba@1137
   380
    template <typename Item> class InverterBase;
deba@1137
   381
deba@1137
   382
    std::string readNodeSet(int& line_num, 
deba@1137
   383
			    auto_ptr<InverterBase<Node> > & nodeInverter) {
deba@1137
   384
      std::vector<ReaderBase<Node>* > index;
deba@1137
   385
      {
deba@1137
   386
	std::string line = readNotEmptyLine(is, line_num);    
deba@1137
   387
	std::string id;
deba@1137
   388
	std::istringstream ls(line);	
deba@1137
   389
	while (ls >> id) {
deba@1137
   390
	  if (id[0] == '#') break;
deba@1137
   391
	  typename NodeMapReaders::iterator it = node_map_readers.find(id);
deba@1137
   392
	  if (it != node_map_readers.end()) {
deba@1137
   393
	    index.push_back(it->second);
deba@1137
   394
	    node_map_readers.erase(it);
deba@1137
   395
	  } else {
deba@1137
   396
	    index.push_back(&nodeSkipper);
deba@1137
   397
	  }
deba@1137
   398
	}
deba@1137
   399
      }
deba@1137
   400
deba@1137
   401
      if (index.size() == 0) {
deba@1137
   402
	throw DataFormatException("No node map found");
deba@1137
   403
      }
deba@1137
   404
deba@1137
   405
      nodeInverter = auto_ptr<InverterBase<Node> >(index[0]->getInverter());
deba@1137
   406
      std::string line;
deba@1137
   407
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
deba@1137
   408
	Node node = graph.addNode();
deba@1137
   409
	std::istringstream ls(line);
deba@1137
   410
	nodeInverter->read(ls, node);
deba@1137
   411
	for (int i = 1; i < (int)index.size(); ++i) {
deba@1137
   412
	  index[i]->read(ls, node);
deba@1137
   413
	}
deba@1137
   414
      }
deba@1137
   415
      return line;
deba@1137
   416
    }
deba@1137
   417
deba@1137
   418
    std::string readEdgeSet(int& line_num, 
deba@1137
   419
			    auto_ptr<InverterBase<Edge> > & edgeInverter, 
deba@1137
   420
			    auto_ptr<InverterBase<Node> > & nodeInverter) {
deba@1137
   421
      std::vector<ReaderBase<Edge>*> index;
deba@1137
   422
      {
deba@1137
   423
	std::string line = readNotEmptyLine(is, line_num);    
deba@1137
   424
	std::string id;
deba@1137
   425
	std::istringstream ls(line);	
deba@1137
   426
	while (ls >> id) {
deba@1137
   427
	  if (id[0] == '#') break;
deba@1137
   428
	  typename EdgeMapReaders::iterator it = edge_map_readers.find(id);
deba@1137
   429
	  if (it != edge_map_readers.end()) {
deba@1137
   430
	    index.push_back(it->second);
deba@1137
   431
	    edge_map_readers.erase(it);
deba@1137
   432
	  } else {
deba@1137
   433
	    index.push_back(&edgeSkipper);
deba@1137
   434
	  }
deba@1137
   435
	}
deba@1137
   436
      }
deba@1137
   437
deba@1137
   438
      if (index.size() == 0) {
deba@1137
   439
	throw DataFormatException("No edge map found");
deba@1137
   440
      }
deba@1137
   441
deba@1137
   442
      edgeInverter = auto_ptr<InverterBase<Edge> >(index[0]->getInverter());
deba@1137
   443
      std::string line;
deba@1137
   444
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {	
deba@1137
   445
	std::istringstream ls(line);
deba@1137
   446
	Node source = nodeInverter->read(ls);
deba@1137
   447
	Node target = nodeInverter->read(ls);
deba@1137
   448
	Edge edge = graph.addEdge(source, target);
deba@1137
   449
	edgeInverter->read(ls, edge);
deba@1137
   450
	for (int i = 1; i < (int)index.size(); ++i) {
deba@1137
   451
	  index[i]->read(ls, edge);
deba@1137
   452
	}
deba@1137
   453
      }      
deba@1137
   454
      return line;
deba@1137
   455
    }
deba@1137
   456
deba@1137
   457
    std::string readNodes(int& line_num, 
deba@1137
   458
			  auto_ptr<InverterBase<Node> >& nodeInverter) {
deba@1137
   459
      std::string line;
deba@1137
   460
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
deba@1137
   461
	std::istringstream ls(line);
deba@1137
   462
	std::string name;
deba@1137
   463
	ls >> name;
deba@1137
   464
	typename NodeReaders::iterator it = node_readers.find(name);
deba@1137
   465
	if (it != node_readers.end()) {
deba@1137
   466
	  *(it -> second) = nodeInverter->read(ls);
deba@1137
   467
	} 
deba@1137
   468
      }        
deba@1137
   469
      return line;
deba@1137
   470
    }
deba@1137
   471
deba@1137
   472
    std::string readEdges(int& line_num, 
deba@1137
   473
			  auto_ptr<InverterBase<Edge> >& edgeInverter) {
deba@1137
   474
      std::string line;
deba@1137
   475
      while (line = readNotEmptyLine(is, line_num), line[0] != '@') {
deba@1137
   476
	std::istringstream ls(line);
deba@1137
   477
	std::string name;
deba@1137
   478
	ls >> name;
deba@1137
   479
	typename EdgeReaders::iterator it = edge_readers.find(name);
deba@1137
   480
	if (it != edge_readers.end()) {
deba@1137
   481
	  *(it -> second) = edgeInverter->read(ls);
deba@1137
   482
	} 
deba@1137
   483
      }        
deba@1137
   484
      return line;    
deba@1137
   485
    }
deba@1137
   486
deba@1137
   487
    std::string readNotEmptyLine(std::istream& is, int& line_num) {
deba@1137
   488
      std::string line;
deba@1137
   489
      while (++line_num, getline(is, line)) {	
deba@1137
   490
	int vi = line.find_first_not_of(" \t");
deba@1137
   491
	if (vi != (int)string::npos && line[vi] != '#') {
deba@1137
   492
	  return line.substr(vi);
deba@1137
   493
	}
deba@1137
   494
      }
deba@1137
   495
      throw DataFormatException("End of stream");
deba@1137
   496
    }
deba@1137
   497
    
deba@1137
   498
    // Inverters store and give back the Item from the id,
deba@1137
   499
    // and may put the ids into a map.
deba@1137
   500
    
deba@1137
   501
    template <typename _Item>
deba@1137
   502
    class InverterBase {
deba@1137
   503
    public:
deba@1137
   504
      typedef _Item Item;
deba@1137
   505
      virtual void read(std::istream&, const Item&) = 0;
deba@1137
   506
      virtual Item read(std::istream&) = 0;
deba@1137
   507
    };
deba@1137
   508
deba@1137
   509
    template <typename _Item, typename _Map, typename _Reader>
deba@1137
   510
    class MapReaderInverter : public InverterBase<_Item> {
deba@1137
   511
    public:
deba@1137
   512
      typedef _Item Item;
deba@1137
   513
      typedef _Reader Reader;
deba@1137
   514
      typedef typename Reader::Value Value;
deba@1137
   515
      typedef _Map Map;
deba@1137
   516
      typedef std::map<Value, Item> Inverse;
deba@1137
   517
deba@1137
   518
      Map& map;
deba@1137
   519
      Reader reader;
deba@1137
   520
      Inverse inverse;
deba@1137
   521
deba@1137
   522
      MapReaderInverter(Map& _map, const Reader& _reader) 
deba@1137
   523
	: map(_map), reader(_reader) {}
deba@1137
   524
deba@1137
   525
      virtual ~MapReaderInverter() {}
deba@1137
   526
deba@1137
   527
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   528
	Value value;
deba@1137
   529
	reader.read(is, value);
deba@1137
   530
	map.set(item, value);
deba@1137
   531
	typename Inverse::iterator it = inverse.find(value);
deba@1137
   532
	if (it == inverse.end()) {
deba@1137
   533
	  inverse.insert(make_pair(value, item));
deba@1137
   534
	} else {
deba@1137
   535
	  throw DataFormatException("Multiple ID occurence");
deba@1137
   536
	}
deba@1137
   537
      }
deba@1137
   538
deba@1137
   539
      virtual Item read(std::istream& is) {
deba@1137
   540
	Value value;
deba@1137
   541
	reader.read(is, value);	
deba@1137
   542
	typename Inverse::const_iterator it = inverse.find(value);
deba@1137
   543
	if (it != inverse.end()) {
deba@1137
   544
	  return it->second;
deba@1137
   545
	} else {
deba@1137
   546
	  throw DataFormatException("Invalid ID");
deba@1137
   547
	}
deba@1137
   548
      }      
deba@1137
   549
    };
deba@1137
   550
deba@1137
   551
    template <typename _Item, typename _Reader>
deba@1137
   552
    class SkipReaderInverter : public InverterBase<_Item> {
deba@1137
   553
    public:
deba@1137
   554
      typedef _Item Item;
deba@1137
   555
      typedef _Reader Reader;
deba@1137
   556
      typedef typename Reader::Value Value;
deba@1137
   557
      typedef std::map<Value, Item> Inverse;
deba@1137
   558
deba@1137
   559
      Reader reader;
deba@1137
   560
deba@1137
   561
      SkipReaderInverter(const Reader& _reader) 
deba@1137
   562
	: reader(_reader) {}
deba@1137
   563
deba@1137
   564
      virtual ~SkipReaderInverter() {}
deba@1137
   565
deba@1137
   566
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   567
	Value value;
deba@1137
   568
	reader.read(is, value);
deba@1137
   569
	typename Inverse::iterator it = inverse.find(value);
deba@1137
   570
	if (it == inverse.end()) {
deba@1137
   571
	  inverse.insert(make_pair(value, item));
deba@1137
   572
	} else {
deba@1137
   573
	  throw DataFormatException("Multiple ID occurence");
deba@1137
   574
	}
deba@1137
   575
      }
deba@1137
   576
deba@1137
   577
      virtual Item read(std::istream& is) {
deba@1137
   578
	Value value;
deba@1137
   579
	reader.read(is, value);	
deba@1137
   580
	typename Inverse::const_iterator it = inverse.find(value);
deba@1137
   581
	if (it != inverse.end()) {
deba@1137
   582
	  return it->second;
deba@1137
   583
	} else {
deba@1137
   584
	  throw DataFormatException("Invalid ID");
deba@1137
   585
	}
deba@1137
   586
      }      
deba@1137
   587
    private:
deba@1137
   588
      Inverse inverse;
deba@1137
   589
    };
deba@1137
   590
deba@1137
   591
    // Readers
deba@1137
   592
deba@1137
   593
    template <typename _Item>    
deba@1137
   594
    class ReaderBase {
deba@1137
   595
    public:
deba@1137
   596
      typedef _Item Item;
deba@1137
   597
deba@1137
   598
      //      virtual ~ReaderBase() {}
deba@1137
   599
deba@1137
   600
      virtual void read(std::istream& is, const Item& item) = 0;
deba@1137
   601
      virtual InverterBase<_Item>* getInverter() = 0;
deba@1137
   602
    };
deba@1137
   603
deba@1137
   604
    template <typename _Item, typename _Map, typename _Reader>
deba@1137
   605
    class MapReader : public ReaderBase<_Item> {
deba@1137
   606
    public:
deba@1137
   607
      typedef _Map Map;
deba@1137
   608
      typedef _Reader Reader;
deba@1137
   609
      typedef typename Reader::Value Value;
deba@1137
   610
      typedef _Item Item;
deba@1137
   611
      
deba@1137
   612
      Map& map;
deba@1137
   613
      Reader reader;
deba@1137
   614
deba@1137
   615
      MapReader(Map& _map, const Reader& _reader) 
deba@1137
   616
	: map(_map), reader(_reader) {}
deba@1137
   617
deba@1137
   618
      virtual ~MapReader() {}
deba@1137
   619
deba@1137
   620
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   621
	Value value;
deba@1137
   622
	reader.read(is, value);
deba@1137
   623
	map.set(item, value);
deba@1137
   624
      }
deba@1137
   625
deba@1137
   626
      virtual InverterBase<_Item>* getInverter() {
deba@1137
   627
	return new MapReaderInverter<Item, Map, Reader>(map, reader);
deba@1137
   628
      }
deba@1137
   629
    };
deba@1137
   630
deba@1137
   631
deba@1137
   632
    template <typename _Item, typename _Reader>
deba@1137
   633
    class SkipReader : public ReaderBase<_Item> {
deba@1137
   634
    public:
deba@1137
   635
      typedef _Reader Reader;
deba@1137
   636
      typedef typename Reader::Value Value;
deba@1137
   637
      typedef _Item Item;
deba@1137
   638
deba@1137
   639
      Reader reader;
deba@1137
   640
      SkipReader(const Reader& _reader) : reader(_reader) {}
deba@1137
   641
deba@1137
   642
      virtual ~SkipReader() {}
deba@1137
   643
deba@1137
   644
      virtual void read(std::istream& is, const Item& item) {
deba@1137
   645
	Value value;
deba@1137
   646
	reader.read(is, value);
deba@1137
   647
      }      
deba@1137
   648
deba@1137
   649
      virtual InverterBase<Item>* getInverter() {
deba@1137
   650
	return new SkipReaderInverter<Item, Reader>(reader);
deba@1137
   651
      }
deba@1137
   652
    };
deba@1137
   653
deba@1137
   654
deba@1137
   655
    typedef std::map<std::string, ReaderBase<Node>*> NodeMapReaders;
deba@1137
   656
    NodeMapReaders node_map_readers;
deba@1137
   657
deba@1137
   658
    typedef std::map<std::string, ReaderBase<Edge>*> EdgeMapReaders;
deba@1137
   659
    EdgeMapReaders edge_map_readers;
deba@1137
   660
deba@1137
   661
    typedef std::map<std::string, Node*> NodeReaders;
deba@1137
   662
    NodeReaders node_readers;
deba@1137
   663
deba@1137
   664
    typedef std::map<std::string, Edge*> EdgeReaders;
deba@1137
   665
    EdgeReaders edge_readers;
deba@1137
   666
deba@1137
   667
    std::istream& is;
deba@1137
   668
    Graph& graph;
deba@1137
   669
deba@1137
   670
    SkipReader<Node, DefaultReader> nodeSkipper;
deba@1137
   671
    SkipReader<Edge, DefaultReader> edgeSkipper;
deba@1137
   672
deba@1137
   673
  };
deba@1137
   674
deba@1137
   675
}