lemon/bits/item_reader.h
author hegyi
Tue, 06 Dec 2005 10:53:38 +0000
changeset 1849 a4d1362397fe
parent 1435 8e85e6bbefdf
child 1852 ffa7c6e96330
permissions -rw-r--r--
Notebook style is provided. Without opportunity to close tabs. :-) But with all other necessary things (I think).
deba@1408
     1
/* -*- C++ -*-
ladanyi@1435
     2
 * lemon/bits/item_reader.h - Part of LEMON, a generic C++ optimization library
deba@1408
     3
 *
deba@1408
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1408
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1408
     6
 *
deba@1408
     7
 * Permission to use, modify and distribute this software is granted
deba@1408
     8
 * provided that this copyright notice appears in all copies. For
deba@1408
     9
 * precise terms see the accompanying LICENSE file.
deba@1408
    10
 *
deba@1408
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1408
    12
 * express or implied, and with no claim as to its suitability for any
deba@1408
    13
 * purpose.
deba@1408
    14
 *
deba@1408
    15
 */
deba@1408
    16
deba@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@1408
   183
    /// \brief Reads the values into the container from the given stream.
deba@1408
   184
    ///
deba@1408
   185
    /// Reads the values into the container from the given stream.
deba@1408
   186
    void read(std::istream& is, Value& value) const {
deba@1408
   187
      char c;
deba@1408
   188
      if (!(is >> c) || c != '(') 
deba@1408
   189
	throw DataFormatError("PushBackReader format error");
deba@1408
   190
      while (is >> c && c != ')') {
deba@1408
   191
	is.putback(c);
deba@1408
   192
	typename ItemReader::Value item;
deba@1408
   193
	item_reader.read(is, item);
deba@1408
   194
	value.push_back(item);
deba@1408
   195
      }
deba@1408
   196
      if (!is) throw DataFormatError("PushBackReader format error");
deba@1408
   197
      is.putback(c);
deba@1408
   198
    }
deba@1408
   199
deba@1408
   200
  };
deba@1408
   201
deba@1415
   202
  /// \ingroup item_io
deba@1408
   203
  ///
deba@1408
   204
  /// \brief Reader for standard containers.
deba@1408
   205
  ///
deba@1408
   206
  /// Reader for insertable standard containers. The representation
deba@1408
   207
  /// of the container is the values enumerated between an open and a
deba@1408
   208
  /// close parse. 
deba@1408
   209
  ///
deba@1408
   210
  /// \author Balazs Dezso
deba@1408
   211
  template <
deba@1408
   212
    typename _Container, 
deba@1408
   213
    typename _ItemReader = DefaultReader<typename _Container::value_type> 
deba@1408
   214
  >
deba@1408
   215
  class InsertReader {
deba@1408
   216
  public:
deba@1408
   217
    typedef _Container Value;
deba@1408
   218
    typedef _ItemReader ItemReader;
deba@1408
   219
deba@1408
   220
  private:
deba@1408
   221
deba@1408
   222
    ItemReader item_reader;
deba@1408
   223
deba@1408
   224
  public:
deba@1408
   225
deba@1408
   226
    /// \brief Reads the values into the container from the given stream.
deba@1408
   227
    ///
deba@1408
   228
    /// Reads the values into the container from the given stream.
deba@1408
   229
    void read(std::istream& is, Value& value) const {
deba@1408
   230
      char c;
deba@1408
   231
      if (!(is >> c) || c != '(') 
deba@1408
   232
	throw DataFormatError("InsertReader format error");
deba@1408
   233
      while (is >> c && c != ')') {
deba@1408
   234
	is.putback(c);
deba@1408
   235
	typename ItemReader::Value item;
deba@1408
   236
	item_reader.read(is, item);
deba@1408
   237
	value.insert(item);
deba@1408
   238
      }
deba@1408
   239
      if (!is) throw DataFormatError("PushBackReader format error");
deba@1408
   240
      is.putback(c);
deba@1408
   241
    }
deba@1408
   242
deba@1408
   243
  };
deba@1408
   244
deba@1415
   245
  /// \ingroup item_io
deba@1408
   246
  /// \brief Reader for parsed string.
deba@1408
   247
  ///
deba@1744
   248
  /// Reader for parsed strings. You can define the open and close
deba@1744
   249
  /// parse characters. It reads from the input a character sequence
deba@1744
   250
  /// which is right parsed.
deba@1408
   251
  ///
deba@1408
   252
  /// \author Balazs Dezso
deba@1408
   253
  class ParsedStringReader {
deba@1408
   254
  public:
deba@1408
   255
    typedef std::string Value;
deba@1408
   256
deba@1408
   257
    /// \brief Constructor.
deba@1408
   258
    ///
deba@1408
   259
    /// Constructor for ParsedStringReader. You can give as parameter
deba@1408
   260
    /// the open and close parse characters.
deba@1408
   261
    ParsedStringReader(char _open = '(', char _close = ')')
deba@1408
   262
      : open(_open), close(_close) {}
deba@1408
   263
    
deba@1408
   264
    
deba@1408
   265
    /// \brief Reads the parsed string from the given stream.
deba@1408
   266
    ///
deba@1408
   267
    /// Reads the parsed string from the given stream.
deba@1408
   268
    void read(std::istream& is, Value& value) const {
deba@1408
   269
      char c;
deba@1408
   270
      if (!(is >> c) || c != open) {
deba@1408
   271
	throw DataFormatError("ParsedStringReader format error");
deba@1408
   272
      }
deba@1408
   273
      value += c;
deba@1408
   274
      int counter = 1;
deba@1408
   275
      while (counter > 0 && is >> c) {
deba@1408
   276
	if (c == close) {
deba@1408
   277
	  --counter;
deba@1408
   278
	} else if (c == open) {
deba@1408
   279
	  ++counter;
deba@1408
   280
	}
deba@1408
   281
	value += c;
deba@1408
   282
      }
deba@1408
   283
      if (!is) {
deba@1408
   284
	throw DataFormatError("ParsedStrinReader format error");
deba@1408
   285
      }
deba@1408
   286
    }
deba@1408
   287
deba@1408
   288
  private:
deba@1408
   289
    char open, close;
deba@1408
   290
deba@1408
   291
  };
deba@1408
   292
deba@1415
   293
  /// \ingroup item_io
deba@1408
   294
  /// \brief Reader for read the whole line.
deba@1408
   295
  ///
deba@1408
   296
  /// Reader for read the whole line.
deba@1408
   297
  ///
deba@1408
   298
  /// \author Balazs Dezso
deba@1408
   299
  class LineReader {
deba@1408
   300
  public:
deba@1408
   301
    typedef std::string Value;
deba@1408
   302
deba@1408
   303
    /// \brief Constructor.
deba@1408
   304
    ///
deba@1408
   305
    /// Constructor for the LineReader. If the given parameter is
deba@1408
   306
    /// true then the spaces before the first not space character are
deba@1408
   307
    /// skipped.
deba@1408
   308
    LineReader(bool _skipSpaces = true) : skipSpaces(_skipSpaces) {}
deba@1408
   309
    
deba@1408
   310
    /// \brief Reads the line from the given stream.
deba@1408
   311
    ///
deba@1408
   312
    /// Reads the line from the given stream.
deba@1408
   313
    void read(std::istream& is, Value& value) {
deba@1408
   314
      if (skipSpaces) is >> std::ws;
deba@1408
   315
      if (!getline(is, value)) {
deba@1408
   316
	throw DataFormatError("LineReader forma error");
deba@1408
   317
      }
deba@1408
   318
    }
deba@1408
   319
  private:
deba@1408
   320
    bool skipSpaces;
deba@1408
   321
  };
deba@1408
   322
deba@1415
   323
  /// \ingroup item_io
deba@1408
   324
  /// 
deba@1408
   325
  /// \brief The default item reader template class.
deba@1408
   326
  ///
deba@1408
   327
  /// The default item reader template class. If some section reader
deba@1408
   328
  /// needs to read a value from a stream it will give the default way for it.
deba@1408
   329
  ///
deba@1408
   330
  /// \author Balazs Dezso
deba@1408
   331
  template <typename _Value>
deba@1408
   332
  class DefaultReader {
deba@1408
   333
  public:
deba@1408
   334
    /// The value type.
deba@1408
   335
    typedef _Value Value;
deba@1408
   336
    /// \brief Reads a value from the given stream.
deba@1408
   337
    ///
deba@1408
   338
    /// Reads a value from the given stream.
deba@1408
   339
    void read(std::istream& is, Value& value) const {
deba@1408
   340
      if (!(is >> value)) 
deba@1408
   341
	throw DataFormatError("DefaultReader format error");
deba@1408
   342
    }
deba@1408
   343
  };
deba@1408
   344
deba@1427
   345
  template <>
deba@1427
   346
  class DefaultReader<std::string> {
deba@1427
   347
  public:
deba@1427
   348
    typedef std::string Value;
deba@1427
   349
    
deba@1427
   350
    void read(std::istream& is, Value& value) const {
deba@1427
   351
      char c;
deba@1429
   352
      if (!(is >> std::ws >> c)) return;
deba@1427
   353
      is.putback(c);
deba@1427
   354
      switch (c) {
deba@1427
   355
      case '\"':
deba@1427
   356
	QuotedStringReader().read(is, value);
deba@1427
   357
	break;
deba@1427
   358
      case '(':
deba@1427
   359
	ParsedStringReader().read(is, value);
deba@1427
   360
	break;
deba@1744
   361
      case '[':
deba@1744
   362
	ParsedStringReader('[', ']').read(is, value);
deba@1744
   363
	break;
deba@1744
   364
      case '/':
deba@1744
   365
	ParsedStringReader('/', '/').read(is, value);
deba@1744
   366
	break;
deba@1427
   367
      default:
deba@1744
   368
	if (!(is >> value)) 
deba@1744
   369
	  throw DataFormatError("DefaultReader format error");
deba@1427
   370
	break;
deba@1427
   371
      }
deba@1427
   372
    }
deba@1427
   373
    
deba@1427
   374
  };
deba@1427
   375
deba@1408
   376
  template <typename Item>
deba@1408
   377
  class DefaultReader<std::vector<Item> > 
deba@1408
   378
    : public PushBackReader<std::vector<Item> > {};
deba@1408
   379
deba@1408
   380
  template <typename Item>
deba@1408
   381
  class DefaultReader<std::deque<Item> > 
deba@1408
   382
    : public PushBackReader<std::deque<Item> > {};
deba@1408
   383
deba@1408
   384
  template <typename Item>
deba@1408
   385
  class DefaultReader<std::list<Item> > 
deba@1408
   386
    : public PushBackReader<std::list<Item> > {};
deba@1408
   387
deba@1408
   388
  template <typename Item>
deba@1408
   389
  class DefaultReader<std::set<Item> > 
deba@1408
   390
    : public InsertReader<std::set<Item> > {};
deba@1408
   391
deba@1408
   392
  template <typename Item>
deba@1408
   393
  class DefaultReader<std::multiset<Item> > 
deba@1408
   394
    : public InsertReader<std::multiset<Item> > {};
deba@1408
   395
deba@1415
   396
  /// \ingroup item_io
deba@1415
   397
  /// 
deba@1415
   398
  /// \brief The default item reader for skipping a value in the stream.
deba@1415
   399
  ///
deba@1415
   400
  /// The default item reader for skipping a value in the stream.
deba@1415
   401
  ///
deba@1415
   402
  /// \author Balazs Dezso
deba@1427
   403
  class DefaultSkipper : public DefaultReader<std::string> {};
deba@1415
   404
deba@1415
   405
  /// \ingroup item_io  
deba@1408
   406
  /// \brief Standard ReaderTraits for the GraphReader class.
deba@1408
   407
  ///
deba@1408
   408
  /// Standard ReaderTraits for the GraphReader class.
deba@1408
   409
  /// It defines standard reading method for all type of value. 
deba@1408
   410
  /// \author Balazs Dezso
deba@1408
   411
  struct DefaultReaderTraits {
deba@1408
   412
deba@1408
   413
    template <typename _Value>
deba@1408
   414
    struct Reader : DefaultReader<_Value> {};
deba@1408
   415
deba@1408
   416
    typedef DefaultSkipper Skipper;
deba@1408
   417
deba@1408
   418
  };
deba@1408
   419
deba@1408
   420
}
deba@1408
   421
deba@1408
   422
#endif