lemon/bits/item_reader.h
author deba
Wed, 25 Jan 2006 16:44:19 +0000
changeset 1906 7fa90b66ca9e
parent 1852 ffa7c6e96330
child 1910 f95eea8c34b0
permissions -rw-r--r--
Omitting warnings
deba@1408
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/bits/item_reader.h - Part of LEMON, a generic C++ optimization library
deba@1408
     3
 *
alpar@1875
     4
 * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1408
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1408
     6
 *
deba@1408
     7
 * Permission to use, modify and distribute this software is granted
deba@1408
     8
 * provided that this copyright notice appears in all copies. For
deba@1408
     9
 * precise terms see the accompanying LICENSE file.
deba@1408
    10
 *
deba@1408
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1408
    12
 * express or implied, and with no claim as to its suitability for any
deba@1408
    13
 * purpose.
deba@1408
    14
 *
deba@1408
    15
 */
deba@1408
    16
deba@1415
    17
/// @defgroup item_io Item Readers and Writers
deba@1415
    18
/// @ingroup io_group
deba@1415
    19
/// \brief Item Readers and Writers
deba@1415
    20
/// 
deba@1415
    21
/// The Input-Output classes can handle more data type by example
deba@1415
    22
/// as map or attribute value. Each of these should be written and
deba@1415
    23
/// read some way. The module make possible to do this.  
deba@1415
    24
deba@1415
    25
/// \ingroup item_io
deba@1408
    26
/// \file
deba@1408
    27
/// \brief Item reader bits for lemon input.
deba@1408
    28
deba@1408
    29
#ifndef LEMON_BITS_ITEM_READER_H
deba@1408
    30
#define LEMON_BITS_ITEM_READER_H
deba@1408
    31
deba@1408
    32
#include <iostream>
deba@1408
    33
#include <string>
deba@1408
    34
deba@1408
    35
#include <vector>
deba@1408
    36
#include <deque>
deba@1408
    37
#include <list>
deba@1408
    38
#include <set>
deba@1408
    39
deba@1408
    40
namespace lemon {
deba@1408
    41
  
deba@1408
    42
  template <typename Value>
deba@1408
    43
  class DefaultReader;
deba@1408
    44
deba@1415
    45
  /// \ingroup item_io
deba@1408
    46
  ///
deba@1408
    47
  /// \brief Reader class for quoted strings.
deba@1408
    48
  ///
deba@1408
    49
  /// Reader class for quoted strings. It can process the escape
deba@1408
    50
  /// sequences in the string.
deba@1408
    51
  ///
deba@1408
    52
  /// \author Balazs Dezso
deba@1408
    53
  class QuotedStringReader {
deba@1408
    54
  public:
deba@1408
    55
    /// \brief The value type of reader.
deba@1408
    56
    ///
deba@1408
    57
    /// The value type of reader.
deba@1408
    58
    typedef std::string Value;
deba@1408
    59
    
deba@1408
    60
    /// \brief Constructor for the reader.
deba@1408
    61
    ///
deba@1408
    62
    /// Constructor for the reader. If the given parameter is true
deba@1408
    63
    /// the reader processes the escape sequences.
deba@1408
    64
    QuotedStringReader(bool _escaped = true) 
deba@1408
    65
      : escaped(_escaped) {}
deba@1408
    66
    
deba@1408
    67
    /// \brief Reads a quoted string from the given stream.
deba@1408
    68
    ///
deba@1408
    69
    /// Reads a quoted string from the given stream.
deba@1408
    70
    void read(std::istream& is, std::string& value) const {
deba@1408
    71
      char c;
deba@1408
    72
      value.clear();
deba@1408
    73
      is >> std::ws;
deba@1408
    74
      if (!is.get(c) || c != '\"') 
deba@1408
    75
	throw DataFormatError("Quoted string format error");
deba@1408
    76
      while (is.get(c) && c != '\"') {
deba@1408
    77
	if (escaped && c == '\\') {
deba@1408
    78
	  value += readEscape(is);
deba@1408
    79
	} else {
deba@1408
    80
	  value += c;
deba@1408
    81
	}
deba@1408
    82
      }
deba@1408
    83
      if (!is) throw DataFormatError("Quoted string format error");
deba@1408
    84
    }
deba@1408
    85
deba@1408
    86
  private:
deba@1408
    87
    
deba@1408
    88
    static char readEscape(std::istream& is) {
deba@1408
    89
      char c;
deba@1408
    90
      switch (is.get(c), c) {
deba@1408
    91
      case '\\':
deba@1408
    92
	return '\\';
deba@1408
    93
      case '\"':
deba@1408
    94
	return '\"';
deba@1408
    95
      case '\'':
deba@1408
    96
	return '\'';
deba@1408
    97
      case '\?':
deba@1408
    98
	return '\?';
deba@1408
    99
      case 'a':
deba@1408
   100
	return '\a';
deba@1408
   101
      case 'b':
deba@1408
   102
	return '\b';
deba@1408
   103
      case 'f':
deba@1408
   104
	return '\f';
deba@1408
   105
      case 'n':
deba@1408
   106
	return '\n';
deba@1408
   107
      case 'r':
deba@1408
   108
	return '\r';
deba@1408
   109
      case 't':
deba@1408
   110
	return '\t';
deba@1408
   111
      case 'v':
deba@1408
   112
	return '\v';
deba@1408
   113
      case 'x':
deba@1408
   114
	{
deba@1408
   115
	  int code;
deba@1408
   116
	  if (!is.get(c) || !isHex(c)) 
deba@1408
   117
	    throw DataFormatError("Escape format error");
deba@1408
   118
	  else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
deba@1408
   119
	  else code = code * 16 + valueHex(c);
deba@1408
   120
	  return code;
deba@1408
   121
	}
deba@1408
   122
      default:
deba@1408
   123
	{
deba@1408
   124
	  int code;
deba@1408
   125
	  if (!isOct(c)) 
deba@1408
   126
	    throw DataFormatError("Escape format error");
deba@1408
   127
	  else if (code = valueOct(c), !is.get(c) || !isOct(c)) 
deba@1408
   128
	    is.putback(c);
deba@1408
   129
	  else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) 
deba@1408
   130
	    is.putback(c);
deba@1408
   131
	  else code = code * 8 + valueOct(c);
deba@1408
   132
	  return code;
deba@1408
   133
	}	      
deba@1408
   134
      } 
deba@1408
   135
    }
deba@1408
   136
deba@1408
   137
    static bool isOct(char c) {
deba@1408
   138
      return '0' <= c && c <='7'; 
deba@1408
   139
    }
deba@1408
   140
    
deba@1408
   141
    static int valueOct(char c) {
deba@1408
   142
      return c - '0';
deba@1408
   143
    }
deba@1408
   144
deba@1408
   145
   static bool isHex(char c) {
deba@1408
   146
      return ('0' <= c && c <= '9') || 
deba@1408
   147
	('a' <= c && c <= 'z') || 
deba@1408
   148
	('A' <= c && c <= 'Z'); 
deba@1408
   149
    }
deba@1408
   150
    
deba@1408
   151
    static int valueHex(char c) {
deba@1408
   152
      if ('0' <= c && c <= '9') return c - '0';
deba@1408
   153
      if ('a' <= c && c <= 'z') return c - 'a' + 10;
deba@1408
   154
      return c - 'A' + 10;
deba@1408
   155
    }
deba@1408
   156
deba@1408
   157
    bool escaped;
deba@1408
   158
  };
deba@1408
   159
deba@1415
   160
  /// \ingroup item_io
deba@1408
   161
  /// \brief Reader for standard containers.
deba@1408
   162
  ///
deba@1408
   163
  /// Reader for back insertable standard containers. The representation
deba@1408
   164
  /// of the container is the values enumerated between an open and a
deba@1408
   165
  /// close parse. 
deba@1408
   166
  ///
deba@1408
   167
  /// \author Balazs Dezso
deba@1408
   168
  template <
deba@1408
   169
    typename _Container, 
deba@1408
   170
    typename _ItemReader = DefaultReader<typename _Container::value_type> 
deba@1408
   171
  >
deba@1408
   172
  class PushBackReader {
deba@1408
   173
  public:
deba@1408
   174
    typedef _Container Value;
deba@1408
   175
    typedef _ItemReader ItemReader;
deba@1408
   176
deba@1408
   177
  private:
deba@1408
   178
deba@1408
   179
    ItemReader item_reader;
deba@1408
   180
deba@1408
   181
  public:
deba@1408
   182
deba@1852
   183
    /// \brief Constructor for InsertReader
deba@1852
   184
    ///
deba@1852
   185
    /// Constructor for InsertReader
deba@1852
   186
    PushBackReader(const ItemReader& _item_reader = ItemReader())
deba@1852
   187
      : item_reader(_item_reader) {}
deba@1852
   188
deba@1408
   189
    /// \brief Reads the values into the container from the given stream.
deba@1408
   190
    ///
deba@1408
   191
    /// Reads the values into the container from the given stream.
deba@1408
   192
    void read(std::istream& is, Value& value) const {
deba@1408
   193
      char c;
deba@1408
   194
      if (!(is >> c) || c != '(') 
deba@1408
   195
	throw DataFormatError("PushBackReader format error");
deba@1408
   196
      while (is >> c && c != ')') {
deba@1408
   197
	is.putback(c);
deba@1408
   198
	typename ItemReader::Value item;
deba@1408
   199
	item_reader.read(is, item);
deba@1408
   200
	value.push_back(item);
deba@1408
   201
      }
deba@1408
   202
      if (!is) throw DataFormatError("PushBackReader format error");
deba@1408
   203
    }
deba@1408
   204
deba@1408
   205
  };
deba@1408
   206
deba@1415
   207
  /// \ingroup item_io
deba@1408
   208
  ///
deba@1408
   209
  /// \brief Reader for standard containers.
deba@1408
   210
  ///
deba@1408
   211
  /// Reader for insertable standard containers. The representation
deba@1408
   212
  /// of the container is the values enumerated between an open and a
deba@1408
   213
  /// close parse. 
deba@1408
   214
  ///
deba@1408
   215
  /// \author Balazs Dezso
deba@1408
   216
  template <
deba@1408
   217
    typename _Container, 
deba@1408
   218
    typename _ItemReader = DefaultReader<typename _Container::value_type> 
deba@1408
   219
  >
deba@1408
   220
  class InsertReader {
deba@1408
   221
  public:
deba@1408
   222
    typedef _Container Value;
deba@1408
   223
    typedef _ItemReader ItemReader;
deba@1408
   224
deba@1408
   225
  private:
deba@1408
   226
deba@1408
   227
    ItemReader item_reader;
deba@1408
   228
deba@1408
   229
  public:
deba@1408
   230
deba@1852
   231
    /// \brief Constructor for InsertReader
deba@1852
   232
    ///
deba@1852
   233
    /// Constructor for InsertReader
deba@1852
   234
    InsertReader(const ItemReader& _item_reader = ItemReader())
deba@1852
   235
      : item_reader(_item_reader) {}
deba@1852
   236
deba@1408
   237
    /// \brief Reads the values into the container from the given stream.
deba@1408
   238
    ///
deba@1408
   239
    /// Reads the values into the container from the given stream.
deba@1408
   240
    void read(std::istream& is, Value& value) const {
deba@1408
   241
      char c;
deba@1408
   242
      if (!(is >> c) || c != '(') 
deba@1408
   243
	throw DataFormatError("InsertReader format error");
deba@1408
   244
      while (is >> c && c != ')') {
deba@1408
   245
	is.putback(c);
deba@1408
   246
	typename ItemReader::Value item;
deba@1408
   247
	item_reader.read(is, item);
deba@1408
   248
	value.insert(item);
deba@1408
   249
      }
deba@1408
   250
      if (!is) throw DataFormatError("PushBackReader format error");
deba@1408
   251
    }
deba@1408
   252
deba@1408
   253
  };
deba@1408
   254
deba@1415
   255
  /// \ingroup item_io
deba@1408
   256
  /// \brief Reader for parsed string.
deba@1408
   257
  ///
deba@1744
   258
  /// Reader for parsed strings. You can define the open and close
deba@1744
   259
  /// parse characters. It reads from the input a character sequence
deba@1744
   260
  /// which is right parsed.
deba@1408
   261
  ///
deba@1408
   262
  /// \author Balazs Dezso
deba@1408
   263
  class ParsedStringReader {
deba@1408
   264
  public:
deba@1408
   265
    typedef std::string Value;
deba@1408
   266
deba@1408
   267
    /// \brief Constructor.
deba@1408
   268
    ///
deba@1408
   269
    /// Constructor for ParsedStringReader. You can give as parameter
deba@1408
   270
    /// the open and close parse characters.
deba@1408
   271
    ParsedStringReader(char _open = '(', char _close = ')')
deba@1408
   272
      : open(_open), close(_close) {}
deba@1408
   273
    
deba@1408
   274
    
deba@1408
   275
    /// \brief Reads the parsed string from the given stream.
deba@1408
   276
    ///
deba@1408
   277
    /// Reads the parsed string from the given stream.
deba@1408
   278
    void read(std::istream& is, Value& value) const {
deba@1408
   279
      char c;
deba@1408
   280
      if (!(is >> c) || c != open) {
deba@1408
   281
	throw DataFormatError("ParsedStringReader format error");
deba@1408
   282
      }
deba@1408
   283
      value += c;
deba@1408
   284
      int counter = 1;
deba@1408
   285
      while (counter > 0 && is >> c) {
deba@1408
   286
	if (c == close) {
deba@1408
   287
	  --counter;
deba@1408
   288
	} else if (c == open) {
deba@1408
   289
	  ++counter;
deba@1408
   290
	}
deba@1408
   291
	value += c;
deba@1408
   292
      }
deba@1408
   293
      if (!is) {
deba@1408
   294
	throw DataFormatError("ParsedStrinReader format error");
deba@1408
   295
      }
deba@1408
   296
    }
deba@1408
   297
deba@1408
   298
  private:
deba@1408
   299
    char open, close;
deba@1408
   300
deba@1408
   301
  };
deba@1408
   302
deba@1415
   303
  /// \ingroup item_io
deba@1408
   304
  /// \brief Reader for read the whole line.
deba@1408
   305
  ///
deba@1408
   306
  /// Reader for read the whole line.
deba@1408
   307
  ///
deba@1408
   308
  /// \author Balazs Dezso
deba@1408
   309
  class LineReader {
deba@1408
   310
  public:
deba@1408
   311
    typedef std::string Value;
deba@1408
   312
deba@1408
   313
    /// \brief Constructor.
deba@1408
   314
    ///
deba@1408
   315
    /// Constructor for the LineReader. If the given parameter is
deba@1408
   316
    /// true then the spaces before the first not space character are
deba@1408
   317
    /// skipped.
deba@1408
   318
    LineReader(bool _skipSpaces = true) : skipSpaces(_skipSpaces) {}
deba@1408
   319
    
deba@1408
   320
    /// \brief Reads the line from the given stream.
deba@1408
   321
    ///
deba@1408
   322
    /// Reads the line from the given stream.
deba@1852
   323
    void read(std::istream& is, Value& value) const {
deba@1408
   324
      if (skipSpaces) is >> std::ws;
deba@1408
   325
      if (!getline(is, value)) {
deba@1852
   326
	throw DataFormatError("LineReader format error");
deba@1408
   327
      }
deba@1408
   328
    }
deba@1408
   329
  private:
deba@1408
   330
    bool skipSpaces;
deba@1408
   331
  };
deba@1408
   332
deba@1415
   333
  /// \ingroup item_io
deba@1852
   334
  /// \brief Reader for std::pair.
deba@1852
   335
  ///
deba@1852
   336
  /// Reader for std::pair.
deba@1852
   337
  ///
deba@1852
   338
  /// \author Balazs Dezso
deba@1852
   339
  template <typename _Pair, 
deba@1852
   340
	    typename _FirstReader = 
deba@1852
   341
	    DefaultReader<typename _Pair::first_type>,
deba@1852
   342
	    typename _SecondReader = 
deba@1852
   343
	    DefaultReader<typename _Pair::second_type> >
deba@1852
   344
  class PairReader {
deba@1852
   345
  public:
deba@1852
   346
    typedef _Pair Value;
deba@1852
   347
deba@1852
   348
    typedef _FirstReader FirstReader;
deba@1852
   349
    typedef _SecondReader SecondReader;
deba@1852
   350
deba@1852
   351
  private:
deba@1852
   352
deba@1852
   353
    FirstReader first_reader;
deba@1852
   354
    SecondReader second_reader;
deba@1852
   355
deba@1852
   356
  public:
deba@1852
   357
    
deba@1852
   358
    /// \brief Constructor.
deba@1852
   359
    ///
deba@1852
   360
    /// Constructor for the PairReader.
deba@1852
   361
    PairReader(const FirstReader& _first_reader = FirstReader(), 
deba@1852
   362
	       const SecondReader& _second_reader = SecondReader()) 
deba@1852
   363
      : first_reader(_first_reader), second_reader(_second_reader) {}
deba@1852
   364
    
deba@1852
   365
    /// \brief Reads the pair from the given stream.
deba@1852
   366
    ///
deba@1852
   367
    /// Reads the pair from the given stream.
deba@1852
   368
    void read(std::istream& is, Value& value) const {
deba@1852
   369
      char c;
deba@1852
   370
      if (!(is >> c) || c != '(') {
deba@1852
   371
	throw DataFormatError("PairReader format error");
deba@1852
   372
      }
deba@1852
   373
      first_reader.read(is, value.first);
deba@1852
   374
      if (!(is >> c) || c != '=') {
deba@1852
   375
	throw DataFormatError("PairReader format error");
deba@1852
   376
      }
deba@1852
   377
      if (!(is >> c) || c != '>') {
deba@1852
   378
	throw DataFormatError("PairReader format error");
deba@1852
   379
      }
deba@1852
   380
      second_reader.read(is, value.second);
deba@1852
   381
      if (!(is >> c) || c != ')') {
deba@1852
   382
	throw DataFormatError("PairReader format error");
deba@1852
   383
      }
deba@1852
   384
    }
deba@1852
   385
  };
deba@1852
   386
deba@1852
   387
  /// \ingroup item_io
deba@1408
   388
  /// 
deba@1408
   389
  /// \brief The default item reader template class.
deba@1408
   390
  ///
deba@1408
   391
  /// The default item reader template class. If some section reader
deba@1408
   392
  /// needs to read a value from a stream it will give the default way for it.
deba@1408
   393
  ///
deba@1408
   394
  /// \author Balazs Dezso
deba@1408
   395
  template <typename _Value>
deba@1408
   396
  class DefaultReader {
deba@1408
   397
  public:
deba@1408
   398
    /// The value type.
deba@1408
   399
    typedef _Value Value;
deba@1408
   400
    /// \brief Reads a value from the given stream.
deba@1408
   401
    ///
deba@1408
   402
    /// Reads a value from the given stream.
deba@1408
   403
    void read(std::istream& is, Value& value) const {
deba@1408
   404
      if (!(is >> value)) 
deba@1408
   405
	throw DataFormatError("DefaultReader format error");
deba@1408
   406
    }
deba@1408
   407
  };
deba@1408
   408
deba@1427
   409
  template <>
deba@1427
   410
  class DefaultReader<std::string> {
deba@1427
   411
  public:
deba@1427
   412
    typedef std::string Value;
deba@1427
   413
    
deba@1427
   414
    void read(std::istream& is, Value& value) const {
deba@1427
   415
      char c;
deba@1429
   416
      if (!(is >> std::ws >> c)) return;
deba@1427
   417
      is.putback(c);
deba@1427
   418
      switch (c) {
deba@1427
   419
      case '\"':
deba@1427
   420
	QuotedStringReader().read(is, value);
deba@1427
   421
	break;
deba@1427
   422
      case '(':
deba@1427
   423
	ParsedStringReader().read(is, value);
deba@1427
   424
	break;
deba@1744
   425
      case '[':
deba@1744
   426
	ParsedStringReader('[', ']').read(is, value);
deba@1744
   427
	break;
deba@1744
   428
      case '/':
deba@1744
   429
	ParsedStringReader('/', '/').read(is, value);
deba@1744
   430
	break;
deba@1427
   431
      default:
deba@1744
   432
	if (!(is >> value)) 
deba@1744
   433
	  throw DataFormatError("DefaultReader format error");
deba@1427
   434
	break;
deba@1427
   435
      }
deba@1427
   436
    }
deba@1427
   437
    
deba@1427
   438
  };
deba@1427
   439
deba@1408
   440
  template <typename Item>
deba@1408
   441
  class DefaultReader<std::vector<Item> > 
deba@1408
   442
    : public PushBackReader<std::vector<Item> > {};
deba@1408
   443
deba@1408
   444
  template <typename Item>
deba@1408
   445
  class DefaultReader<std::deque<Item> > 
deba@1408
   446
    : public PushBackReader<std::deque<Item> > {};
deba@1408
   447
deba@1408
   448
  template <typename Item>
deba@1408
   449
  class DefaultReader<std::list<Item> > 
deba@1408
   450
    : public PushBackReader<std::list<Item> > {};
deba@1408
   451
deba@1408
   452
  template <typename Item>
deba@1408
   453
  class DefaultReader<std::set<Item> > 
deba@1408
   454
    : public InsertReader<std::set<Item> > {};
deba@1408
   455
deba@1852
   456
  template <typename Key, typename Value>
deba@1852
   457
  class DefaultReader<std::map<Key, Value> > 
deba@1852
   458
    : public InsertReader<std::map<Key, Value>,
deba@1852
   459
			  DefaultReader<std::pair<Key, Value> > > {};
deba@1852
   460
deba@1408
   461
  template <typename Item>
deba@1408
   462
  class DefaultReader<std::multiset<Item> > 
deba@1408
   463
    : public InsertReader<std::multiset<Item> > {};
deba@1408
   464
deba@1852
   465
  template <typename Key, typename Value>
deba@1852
   466
  class DefaultReader<std::multimap<Key, Value> > 
deba@1852
   467
    : public InsertReader<std::multimap<Key, Value>,
deba@1852
   468
			  DefaultReader<std::pair<Key, Value> > > {};
deba@1852
   469
deba@1852
   470
  template <typename First, typename Second>
deba@1852
   471
  class DefaultReader<std::pair<First, Second> > 
deba@1852
   472
    : public PairReader<std::pair<First, Second> > {};
deba@1852
   473
deba@1415
   474
  /// \ingroup item_io
deba@1415
   475
  /// 
deba@1415
   476
  /// \brief The default item reader for skipping a value in the stream.
deba@1415
   477
  ///
deba@1415
   478
  /// The default item reader for skipping a value in the stream.
deba@1415
   479
  ///
deba@1415
   480
  /// \author Balazs Dezso
deba@1427
   481
  class DefaultSkipper : public DefaultReader<std::string> {};
deba@1415
   482
deba@1415
   483
  /// \ingroup item_io  
deba@1408
   484
  /// \brief Standard ReaderTraits for the GraphReader class.
deba@1408
   485
  ///
deba@1408
   486
  /// Standard ReaderTraits for the GraphReader class.
deba@1408
   487
  /// It defines standard reading method for all type of value. 
deba@1408
   488
  /// \author Balazs Dezso
deba@1408
   489
  struct DefaultReaderTraits {
deba@1408
   490
deba@1408
   491
    template <typename _Value>
deba@1408
   492
    struct Reader : DefaultReader<_Value> {};
deba@1408
   493
deba@1408
   494
    typedef DefaultSkipper Skipper;
deba@1408
   495
deba@1408
   496
  };
deba@1408
   497
deba@1408
   498
}
deba@1408
   499
deba@1408
   500
#endif