lemon/bits/item_reader.h
author klao
Wed, 08 Jun 2005 15:40:54 +0000
changeset 1451 d2143018b4e4
parent 1429 4283998fb2be
child 1744 51d5d41e15b1
permissions -rw-r--r--
Bugfix: make doc didn't work in external builddir.
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@1408
   248
  /// Reader for parsed strings. You can give the open and close
deba@1408
   249
  /// parse characters.
deba@1408
   250
  ///
deba@1408
   251
  /// \author Balazs Dezso
deba@1408
   252
  class ParsedStringReader {
deba@1408
   253
  public:
deba@1408
   254
    typedef std::string Value;
deba@1408
   255
deba@1408
   256
    /// \brief Constructor.
deba@1408
   257
    ///
deba@1408
   258
    /// Constructor for ParsedStringReader. You can give as parameter
deba@1408
   259
    /// the open and close parse characters.
deba@1408
   260
    ParsedStringReader(char _open = '(', char _close = ')')
deba@1408
   261
      : open(_open), close(_close) {}
deba@1408
   262
    
deba@1408
   263
    
deba@1408
   264
    /// \brief Reads the parsed string from the given stream.
deba@1408
   265
    ///
deba@1408
   266
    /// Reads the parsed string from the given stream.
deba@1408
   267
    void read(std::istream& is, Value& value) const {
deba@1408
   268
      char c;
deba@1408
   269
      if (!(is >> c) || c != open) {
deba@1408
   270
	throw DataFormatError("ParsedStringReader format error");
deba@1408
   271
      }
deba@1408
   272
      value += c;
deba@1408
   273
      int counter = 1;
deba@1408
   274
      while (counter > 0 && is >> c) {
deba@1408
   275
	if (c == close) {
deba@1408
   276
	  --counter;
deba@1408
   277
	} else if (c == open) {
deba@1408
   278
	  ++counter;
deba@1408
   279
	}
deba@1408
   280
	value += c;
deba@1408
   281
      }
deba@1408
   282
      if (!is) {
deba@1408
   283
	throw DataFormatError("ParsedStrinReader format error");
deba@1408
   284
      }
deba@1408
   285
    }
deba@1408
   286
deba@1408
   287
  private:
deba@1408
   288
    char open, close;
deba@1408
   289
deba@1408
   290
  };
deba@1408
   291
deba@1415
   292
  /// \ingroup item_io
deba@1408
   293
  /// \brief Reader for read the whole line.
deba@1408
   294
  ///
deba@1408
   295
  /// Reader for read the whole line.
deba@1408
   296
  ///
deba@1408
   297
  /// \author Balazs Dezso
deba@1408
   298
  class LineReader {
deba@1408
   299
  public:
deba@1408
   300
    typedef std::string Value;
deba@1408
   301
deba@1408
   302
    /// \brief Constructor.
deba@1408
   303
    ///
deba@1408
   304
    /// Constructor for the LineReader. If the given parameter is
deba@1408
   305
    /// true then the spaces before the first not space character are
deba@1408
   306
    /// skipped.
deba@1408
   307
    LineReader(bool _skipSpaces = true) : skipSpaces(_skipSpaces) {}
deba@1408
   308
    
deba@1408
   309
    /// \brief Reads the line from the given stream.
deba@1408
   310
    ///
deba@1408
   311
    /// Reads the line from the given stream.
deba@1408
   312
    void read(std::istream& is, Value& value) {
deba@1408
   313
      if (skipSpaces) is >> std::ws;
deba@1408
   314
      if (!getline(is, value)) {
deba@1408
   315
	throw DataFormatError("LineReader forma error");
deba@1408
   316
      }
deba@1408
   317
    }
deba@1408
   318
  private:
deba@1408
   319
    bool skipSpaces;
deba@1408
   320
  };
deba@1408
   321
deba@1415
   322
  /// \ingroup item_io
deba@1408
   323
  /// 
deba@1408
   324
  /// \brief The default item reader template class.
deba@1408
   325
  ///
deba@1408
   326
  /// The default item reader template class. If some section reader
deba@1408
   327
  /// needs to read a value from a stream it will give the default way for it.
deba@1408
   328
  ///
deba@1408
   329
  /// \author Balazs Dezso
deba@1408
   330
  template <typename _Value>
deba@1408
   331
  class DefaultReader {
deba@1408
   332
  public:
deba@1408
   333
    /// The value type.
deba@1408
   334
    typedef _Value Value;
deba@1408
   335
    /// \brief Reads a value from the given stream.
deba@1408
   336
    ///
deba@1408
   337
    /// Reads a value from the given stream.
deba@1408
   338
    void read(std::istream& is, Value& value) const {
deba@1408
   339
      if (!(is >> value)) 
deba@1408
   340
	throw DataFormatError("DefaultReader format error");
deba@1408
   341
    }
deba@1408
   342
  };
deba@1408
   343
deba@1427
   344
  template <>
deba@1427
   345
  class DefaultReader<std::string> {
deba@1427
   346
  public:
deba@1427
   347
    typedef std::string Value;
deba@1427
   348
    
deba@1427
   349
    void read(std::istream& is, Value& value) const {
deba@1427
   350
      char c;
deba@1429
   351
      if (!(is >> std::ws >> c)) return;
deba@1427
   352
      is.putback(c);
deba@1427
   353
      switch (c) {
deba@1427
   354
      case '\"':
deba@1427
   355
	QuotedStringReader().read(is, value);
deba@1427
   356
	break;
deba@1427
   357
      case '(':
deba@1427
   358
	ParsedStringReader().read(is, value);
deba@1427
   359
	break;
deba@1427
   360
      default:
deba@1427
   361
	is >> value; 
deba@1427
   362
	break;
deba@1427
   363
      }
deba@1427
   364
    }
deba@1427
   365
    
deba@1427
   366
  };
deba@1427
   367
deba@1408
   368
  template <typename Item>
deba@1408
   369
  class DefaultReader<std::vector<Item> > 
deba@1408
   370
    : public PushBackReader<std::vector<Item> > {};
deba@1408
   371
deba@1408
   372
  template <typename Item>
deba@1408
   373
  class DefaultReader<std::deque<Item> > 
deba@1408
   374
    : public PushBackReader<std::deque<Item> > {};
deba@1408
   375
deba@1408
   376
  template <typename Item>
deba@1408
   377
  class DefaultReader<std::list<Item> > 
deba@1408
   378
    : public PushBackReader<std::list<Item> > {};
deba@1408
   379
deba@1408
   380
  template <typename Item>
deba@1408
   381
  class DefaultReader<std::set<Item> > 
deba@1408
   382
    : public InsertReader<std::set<Item> > {};
deba@1408
   383
deba@1408
   384
  template <typename Item>
deba@1408
   385
  class DefaultReader<std::multiset<Item> > 
deba@1408
   386
    : public InsertReader<std::multiset<Item> > {};
deba@1408
   387
deba@1415
   388
  /// \ingroup item_io
deba@1415
   389
  /// 
deba@1415
   390
  /// \brief The default item reader for skipping a value in the stream.
deba@1415
   391
  ///
deba@1415
   392
  /// The default item reader for skipping a value in the stream.
deba@1415
   393
  ///
deba@1415
   394
  /// \author Balazs Dezso
deba@1427
   395
  class DefaultSkipper : public DefaultReader<std::string> {};
deba@1415
   396
deba@1415
   397
  /// \ingroup item_io  
deba@1408
   398
  /// \brief Standard ReaderTraits for the GraphReader class.
deba@1408
   399
  ///
deba@1408
   400
  /// Standard ReaderTraits for the GraphReader class.
deba@1408
   401
  /// It defines standard reading method for all type of value. 
deba@1408
   402
  /// \author Balazs Dezso
deba@1408
   403
  struct DefaultReaderTraits {
deba@1408
   404
deba@1408
   405
    template <typename _Value>
deba@1408
   406
    struct Reader : DefaultReader<_Value> {};
deba@1408
   407
deba@1408
   408
    typedef DefaultSkipper Skipper;
deba@1408
   409
deba@1408
   410
  };
deba@1408
   411
deba@1408
   412
}
deba@1408
   413
deba@1408
   414
#endif