gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Fix memory leak hazard If the constructor throws an exception, it should deallocate each dynamically allocated memory.
0 2 0
default
2 files changed with 56 insertions and 14 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -472,109 +472,115 @@
472 472
    std::string _attributes_caption;
473 473

	
474 474
    typedef std::map<std::string, Node> NodeIndex;
475 475
    NodeIndex _node_index;
476 476
    typedef std::map<std::string, Arc> ArcIndex;
477 477
    ArcIndex _arc_index;
478 478

	
479 479
    typedef std::vector<std::pair<std::string,
480 480
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;
481 481
    NodeMaps _node_maps;
482 482

	
483 483
    typedef std::vector<std::pair<std::string,
484 484
      _reader_bits::MapStorageBase<Arc>*> >ArcMaps;
485 485
    ArcMaps _arc_maps;
486 486

	
487 487
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
488 488
      Attributes;
489 489
    Attributes _attributes;
490 490

	
491 491
    bool _use_nodes;
492 492
    bool _use_arcs;
493 493

	
494 494
    bool _skip_nodes;
495 495
    bool _skip_arcs;
496 496

	
497 497
    int line_num;
498 498
    std::istringstream line;
499 499

	
500 500
  public:
501 501

	
502 502
    /// \brief Constructor
503 503
    ///
504 504
    /// Construct a directed graph reader, which reads from the given
505 505
    /// input stream.
506 506
    DigraphReader(Digraph& digraph, std::istream& is = std::cin)
507 507
      : _is(&is), local_is(false), _digraph(digraph),
508 508
        _use_nodes(false), _use_arcs(false),
509 509
        _skip_nodes(false), _skip_arcs(false) {}
510 510

	
511 511
    /// \brief Constructor
512 512
    ///
513 513
    /// Construct a directed graph reader, which reads from the given
514 514
    /// file.
515 515
    DigraphReader(Digraph& digraph, const std::string& fn)
516 516
      : _is(new std::ifstream(fn.c_str())), local_is(true),
517 517
        _filename(fn), _digraph(digraph),
518 518
        _use_nodes(false), _use_arcs(false),
519 519
        _skip_nodes(false), _skip_arcs(false) {
520
      if (!(*_is)) throw IoError("Cannot open file", fn);
520
      if (!(*_is)) {
521
        delete _is;
522
        throw IoError("Cannot open file", fn);
523
      }
521 524
    }
522 525

	
523 526
    /// \brief Constructor
524 527
    ///
525 528
    /// Construct a directed graph reader, which reads from the given
526 529
    /// file.
527 530
    DigraphReader(Digraph& digraph, const char* fn)
528 531
      : _is(new std::ifstream(fn)), local_is(true),
529 532
        _filename(fn), _digraph(digraph),
530 533
        _use_nodes(false), _use_arcs(false),
531 534
        _skip_nodes(false), _skip_arcs(false) {
532
      if (!(*_is)) throw IoError("Cannot open file", fn);
535
      if (!(*_is)) {
536
        delete _is;
537
        throw IoError("Cannot open file", fn);
538
      }
533 539
    }
534 540

	
535 541
    /// \brief Destructor
536 542
    ~DigraphReader() {
537 543
      for (typename NodeMaps::iterator it = _node_maps.begin();
538 544
           it != _node_maps.end(); ++it) {
539 545
        delete it->second;
540 546
      }
541 547

	
542 548
      for (typename ArcMaps::iterator it = _arc_maps.begin();
543 549
           it != _arc_maps.end(); ++it) {
544 550
        delete it->second;
545 551
      }
546 552

	
547 553
      for (typename Attributes::iterator it = _attributes.begin();
548 554
           it != _attributes.end(); ++it) {
549 555
        delete it->second;
550 556
      }
551 557

	
552 558
      if (local_is) {
553 559
        delete _is;
554 560
      }
555 561

	
556 562
    }
557 563

	
558 564
  private:
559 565

	
560 566
    friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph,
561 567
                                                  std::istream& is);
562 568
    friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph,
563 569
                                                  const std::string& fn);
564 570
    friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph,
565 571
                                                  const char *fn);
566 572

	
567 573
    DigraphReader(DigraphReader& other)
568 574
      : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
569 575
        _use_nodes(other._use_nodes), _use_arcs(other._use_arcs),
570 576
        _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
571 577

	
572 578
      other._is = 0;
573 579
      other.local_is = false;
574 580

	
575 581
      _node_index.swap(other._node_index);
576 582
      _arc_index.swap(other._arc_index);
577 583

	
578 584
      _node_maps.swap(other._node_maps);
579 585
      _arc_maps.swap(other._arc_maps);
580 586
      _attributes.swap(other._attributes);
... ...
@@ -1263,109 +1269,115 @@
1263 1269
    std::string _attributes_caption;
1264 1270

	
1265 1271
    typedef std::map<std::string, Node> NodeIndex;
1266 1272
    NodeIndex _node_index;
1267 1273
    typedef std::map<std::string, Edge> EdgeIndex;
1268 1274
    EdgeIndex _edge_index;
1269 1275

	
1270 1276
    typedef std::vector<std::pair<std::string,
1271 1277
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;
1272 1278
    NodeMaps _node_maps;
1273 1279

	
1274 1280
    typedef std::vector<std::pair<std::string,
1275 1281
      _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
1276 1282
    EdgeMaps _edge_maps;
1277 1283

	
1278 1284
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
1279 1285
      Attributes;
1280 1286
    Attributes _attributes;
1281 1287

	
1282 1288
    bool _use_nodes;
1283 1289
    bool _use_edges;
1284 1290

	
1285 1291
    bool _skip_nodes;
1286 1292
    bool _skip_edges;
1287 1293

	
1288 1294
    int line_num;
1289 1295
    std::istringstream line;
1290 1296

	
1291 1297
  public:
1292 1298

	
1293 1299
    /// \brief Constructor
1294 1300
    ///
1295 1301
    /// Construct an undirected graph reader, which reads from the given
1296 1302
    /// input stream.
1297 1303
    GraphReader(Graph& graph, std::istream& is = std::cin)
1298 1304
      : _is(&is), local_is(false), _graph(graph),
1299 1305
        _use_nodes(false), _use_edges(false),
1300 1306
        _skip_nodes(false), _skip_edges(false) {}
1301 1307

	
1302 1308
    /// \brief Constructor
1303 1309
    ///
1304 1310
    /// Construct an undirected graph reader, which reads from the given
1305 1311
    /// file.
1306 1312
    GraphReader(Graph& graph, const std::string& fn)
1307 1313
      : _is(new std::ifstream(fn.c_str())), local_is(true),
1308 1314
        _filename(fn), _graph(graph),
1309 1315
        _use_nodes(false), _use_edges(false),
1310 1316
        _skip_nodes(false), _skip_edges(false) {
1311
      if (!(*_is)) throw IoError("Cannot open file", fn);
1317
      if (!(*_is)) {
1318
        delete _is;
1319
        throw IoError("Cannot open file", fn);
1320
      }
1312 1321
    }
1313 1322

	
1314 1323
    /// \brief Constructor
1315 1324
    ///
1316 1325
    /// Construct an undirected graph reader, which reads from the given
1317 1326
    /// file.
1318 1327
    GraphReader(Graph& graph, const char* fn)
1319 1328
      : _is(new std::ifstream(fn)), local_is(true),
1320 1329
        _filename(fn), _graph(graph),
1321 1330
        _use_nodes(false), _use_edges(false),
1322 1331
        _skip_nodes(false), _skip_edges(false) {
1323
      if (!(*_is)) throw IoError("Cannot open file", fn);
1332
      if (!(*_is)) {
1333
        delete _is;
1334
        throw IoError("Cannot open file", fn);
1335
      }
1324 1336
    }
1325 1337

	
1326 1338
    /// \brief Destructor
1327 1339
    ~GraphReader() {
1328 1340
      for (typename NodeMaps::iterator it = _node_maps.begin();
1329 1341
           it != _node_maps.end(); ++it) {
1330 1342
        delete it->second;
1331 1343
      }
1332 1344

	
1333 1345
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
1334 1346
           it != _edge_maps.end(); ++it) {
1335 1347
        delete it->second;
1336 1348
      }
1337 1349

	
1338 1350
      for (typename Attributes::iterator it = _attributes.begin();
1339 1351
           it != _attributes.end(); ++it) {
1340 1352
        delete it->second;
1341 1353
      }
1342 1354

	
1343 1355
      if (local_is) {
1344 1356
        delete _is;
1345 1357
      }
1346 1358

	
1347 1359
    }
1348 1360

	
1349 1361
  private:
1350 1362
    friend GraphReader<Graph> graphReader<>(Graph& graph, std::istream& is);
1351 1363
    friend GraphReader<Graph> graphReader<>(Graph& graph,
1352 1364
                                            const std::string& fn);
1353 1365
    friend GraphReader<Graph> graphReader<>(Graph& graph, const char *fn);
1354 1366

	
1355 1367
    GraphReader(GraphReader& other)
1356 1368
      : _is(other._is), local_is(other.local_is), _graph(other._graph),
1357 1369
        _use_nodes(other._use_nodes), _use_edges(other._use_edges),
1358 1370
        _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
1359 1371

	
1360 1372
      other._is = 0;
1361 1373
      other.local_is = false;
1362 1374

	
1363 1375
      _node_index.swap(other._node_index);
1364 1376
      _edge_index.swap(other._edge_index);
1365 1377

	
1366 1378
      _node_maps.swap(other._node_maps);
1367 1379
      _edge_maps.swap(other._edge_maps);
1368 1380
      _attributes.swap(other._attributes);
1369 1381

	
1370 1382
      _nodes_caption = other._nodes_caption;
1371 1383
      _edges_caption = other._edges_caption;
... ...
@@ -2049,106 +2061,112 @@
2049 2061
    return tmp;
2050 2062
  }
2051 2063

	
2052 2064
  class SectionReader;
2053 2065

	
2054 2066
  SectionReader sectionReader(std::istream& is);
2055 2067
  SectionReader sectionReader(const std::string& fn);
2056 2068
  SectionReader sectionReader(const char* fn);
2057 2069

	
2058 2070
  /// \ingroup lemon_io
2059 2071
  ///
2060 2072
  /// \brief Section reader class
2061 2073
  ///
2062 2074
  /// In the \ref lgf-format "LGF" file extra sections can be placed,
2063 2075
  /// which contain any data in arbitrary format. Such sections can be
2064 2076
  /// read with this class. A reading rule can be added to the class
2065 2077
  /// with two different functions. With the \c sectionLines() function a
2066 2078
  /// functor can process the section line-by-line, while with the \c
2067 2079
  /// sectionStream() member the section can be read from an input
2068 2080
  /// stream.
2069 2081
  class SectionReader {
2070 2082
  private:
2071 2083

	
2072 2084
    std::istream* _is;
2073 2085
    bool local_is;
2074 2086
    std::string _filename;
2075 2087

	
2076 2088
    typedef std::map<std::string, _reader_bits::Section*> Sections;
2077 2089
    Sections _sections;
2078 2090

	
2079 2091
    int line_num;
2080 2092
    std::istringstream line;
2081 2093

	
2082 2094
  public:
2083 2095

	
2084 2096
    /// \brief Constructor
2085 2097
    ///
2086 2098
    /// Construct a section reader, which reads from the given input
2087 2099
    /// stream.
2088 2100
    SectionReader(std::istream& is)
2089 2101
      : _is(&is), local_is(false) {}
2090 2102

	
2091 2103
    /// \brief Constructor
2092 2104
    ///
2093 2105
    /// Construct a section reader, which reads from the given file.
2094 2106
    SectionReader(const std::string& fn)
2095 2107
      : _is(new std::ifstream(fn.c_str())), local_is(true),
2096 2108
        _filename(fn) {
2097
      if (!(*_is)) throw IoError("Cannot open file", fn);
2109
      if (!(*_is)) {
2110
        delete _is;
2111
        throw IoError("Cannot open file", fn);
2112
      }
2098 2113
    }
2099 2114

	
2100 2115
    /// \brief Constructor
2101 2116
    ///
2102 2117
    /// Construct a section reader, which reads from the given file.
2103 2118
    SectionReader(const char* fn)
2104 2119
      : _is(new std::ifstream(fn)), local_is(true),
2105 2120
        _filename(fn) {
2106
      if (!(*_is)) throw IoError("Cannot open file", fn);
2121
      if (!(*_is)) {
2122
        delete _is;
2123
        throw IoError("Cannot open file", fn);
2124
      }
2107 2125
    }
2108 2126

	
2109 2127
    /// \brief Destructor
2110 2128
    ~SectionReader() {
2111 2129
      for (Sections::iterator it = _sections.begin();
2112 2130
           it != _sections.end(); ++it) {
2113 2131
        delete it->second;
2114 2132
      }
2115 2133

	
2116 2134
      if (local_is) {
2117 2135
        delete _is;
2118 2136
      }
2119 2137

	
2120 2138
    }
2121 2139

	
2122 2140
  private:
2123 2141

	
2124 2142
    friend SectionReader sectionReader(std::istream& is);
2125 2143
    friend SectionReader sectionReader(const std::string& fn);
2126 2144
    friend SectionReader sectionReader(const char* fn);
2127 2145

	
2128 2146
    SectionReader(SectionReader& other)
2129 2147
      : _is(other._is), local_is(other.local_is) {
2130 2148

	
2131 2149
      other._is = 0;
2132 2150
      other.local_is = false;
2133 2151

	
2134 2152
      _sections.swap(other._sections);
2135 2153
    }
2136 2154

	
2137 2155
    SectionReader& operator=(const SectionReader&);
2138 2156

	
2139 2157
  public:
2140 2158

	
2141 2159
    /// \name Section readers
2142 2160
    /// @{
2143 2161

	
2144 2162
    /// \brief Add a section processor with line oriented reading
2145 2163
    ///
2146 2164
    /// The first parameter is the type descriptor of the section, the
2147 2165
    /// second is a functor, which takes just one \c std::string
2148 2166
    /// parameter. At the reading process, each line of the section
2149 2167
    /// will be given to the functor object. However, the empty lines
2150 2168
    /// and the comment lines are filtered out, and the leading
2151 2169
    /// whitespaces are trimmed from each processed string.
2152 2170
    ///
2153 2171
    /// For example let's see a section, which contain several
2154 2172
    /// integers, which should be inserted into a vector.
... ...
@@ -2341,106 +2359,112 @@
2341 2359
  /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
2342 2360
  ///   std::cerr << "Failure, cannot find graph." << std::endl;
2343 2361
  ///   return -1;
2344 2362
  /// }
2345 2363
  /// std::cout << "The name of the default node section: "
2346 2364
  ///           << contents.nodeSection(0) << std::endl;
2347 2365
  /// std::cout << "The number of the arc maps: "
2348 2366
  ///           << contents.arcMaps(0).size() << std::endl;
2349 2367
  /// std::cout << "The name of second arc map: "
2350 2368
  ///           << contents.arcMaps(0)[1] << std::endl;
2351 2369
  ///\endcode
2352 2370
  class LgfContents {
2353 2371
  private:
2354 2372

	
2355 2373
    std::istream* _is;
2356 2374
    bool local_is;
2357 2375

	
2358 2376
    std::vector<std::string> _node_sections;
2359 2377
    std::vector<std::string> _edge_sections;
2360 2378
    std::vector<std::string> _attribute_sections;
2361 2379
    std::vector<std::string> _extra_sections;
2362 2380

	
2363 2381
    std::vector<bool> _arc_sections;
2364 2382

	
2365 2383
    std::vector<std::vector<std::string> > _node_maps;
2366 2384
    std::vector<std::vector<std::string> > _edge_maps;
2367 2385

	
2368 2386
    std::vector<std::vector<std::string> > _attributes;
2369 2387

	
2370 2388

	
2371 2389
    int line_num;
2372 2390
    std::istringstream line;
2373 2391

	
2374 2392
  public:
2375 2393

	
2376 2394
    /// \brief Constructor
2377 2395
    ///
2378 2396
    /// Construct an \e LGF contents reader, which reads from the given
2379 2397
    /// input stream.
2380 2398
    LgfContents(std::istream& is)
2381 2399
      : _is(&is), local_is(false) {}
2382 2400

	
2383 2401
    /// \brief Constructor
2384 2402
    ///
2385 2403
    /// Construct an \e LGF contents reader, which reads from the given
2386 2404
    /// file.
2387 2405
    LgfContents(const std::string& fn)
2388 2406
      : _is(new std::ifstream(fn.c_str())), local_is(true) {
2389
      if (!(*_is)) throw IoError("Cannot open file", fn);
2407
      if (!(*_is)) {
2408
        delete _is;
2409
        throw IoError("Cannot open file", fn);
2410
      }
2390 2411
    }
2391 2412

	
2392 2413
    /// \brief Constructor
2393 2414
    ///
2394 2415
    /// Construct an \e LGF contents reader, which reads from the given
2395 2416
    /// file.
2396 2417
    LgfContents(const char* fn)
2397 2418
      : _is(new std::ifstream(fn)), local_is(true) {
2398
      if (!(*_is)) throw IoError("Cannot open file", fn);
2419
      if (!(*_is)) {
2420
        delete _is;
2421
        throw IoError("Cannot open file", fn);
2422
      }
2399 2423
    }
2400 2424

	
2401 2425
    /// \brief Destructor
2402 2426
    ~LgfContents() {
2403 2427
      if (local_is) delete _is;
2404 2428
    }
2405 2429

	
2406 2430
  private:
2407 2431

	
2408 2432
    LgfContents(const LgfContents&);
2409 2433
    LgfContents& operator=(const LgfContents&);
2410 2434

	
2411 2435
  public:
2412 2436

	
2413 2437

	
2414 2438
    /// \name Node sections
2415 2439
    /// @{
2416 2440

	
2417 2441
    /// \brief Gives back the number of node sections in the file.
2418 2442
    ///
2419 2443
    /// Gives back the number of node sections in the file.
2420 2444
    int nodeSectionNum() const {
2421 2445
      return _node_sections.size();
2422 2446
    }
2423 2447

	
2424 2448
    /// \brief Returns the node section name at the given position.
2425 2449
    ///
2426 2450
    /// Returns the node section name at the given position.
2427 2451
    const std::string& nodeSection(int i) const {
2428 2452
      return _node_sections[i];
2429 2453
    }
2430 2454

	
2431 2455
    /// \brief Gives back the node maps for the given section.
2432 2456
    ///
2433 2457
    /// Gives back the node maps for the given section.
2434 2458
    const std::vector<std::string>& nodeMapNames(int i) const {
2435 2459
      return _node_maps[i];
2436 2460
    }
2437 2461

	
2438 2462
    /// @}
2439 2463

	
2440 2464
    /// \name Arc/Edge sections
2441 2465
    /// @{
2442 2466

	
2443 2467
    /// \brief Gives back the number of arc/edge sections in the file.
2444 2468
    ///
2445 2469
    /// Gives back the number of arc/edge sections in the file.
2446 2470
    /// \note It is synonym of \c edgeSectionNum().
Ignore white space 96 line context
... ...
@@ -418,107 +418,113 @@
418 418

	
419 419

	
420 420
    std::ostream* _os;
421 421
    bool local_os;
422 422

	
423 423
    const Digraph& _digraph;
424 424

	
425 425
    std::string _nodes_caption;
426 426
    std::string _arcs_caption;
427 427
    std::string _attributes_caption;
428 428

	
429 429
    typedef std::map<Node, std::string> NodeIndex;
430 430
    NodeIndex _node_index;
431 431
    typedef std::map<Arc, std::string> ArcIndex;
432 432
    ArcIndex _arc_index;
433 433

	
434 434
    typedef std::vector<std::pair<std::string,
435 435
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;
436 436
    NodeMaps _node_maps;
437 437

	
438 438
    typedef std::vector<std::pair<std::string,
439 439
      _writer_bits::MapStorageBase<Arc>* > >ArcMaps;
440 440
    ArcMaps _arc_maps;
441 441

	
442 442
    typedef std::vector<std::pair<std::string,
443 443
      _writer_bits::ValueStorageBase*> > Attributes;
444 444
    Attributes _attributes;
445 445

	
446 446
    bool _skip_nodes;
447 447
    bool _skip_arcs;
448 448

	
449 449
  public:
450 450

	
451 451
    /// \brief Constructor
452 452
    ///
453 453
    /// Construct a directed graph writer, which writes to the given
454 454
    /// output stream.
455 455
    DigraphWriter(const Digraph& digraph, std::ostream& os = std::cout)
456 456
      : _os(&os), local_os(false), _digraph(digraph),
457 457
        _skip_nodes(false), _skip_arcs(false) {}
458 458

	
459 459
    /// \brief Constructor
460 460
    ///
461 461
    /// Construct a directed graph writer, which writes to the given
462 462
    /// output file.
463 463
    DigraphWriter(const Digraph& digraph, const std::string& fn)
464 464
      : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
465 465
        _skip_nodes(false), _skip_arcs(false) {
466
      if (!(*_os)) throw IoError("Cannot write file", fn);
466
      if (!(*_os)) {
467
        delete _os;
468
        throw IoError("Cannot write file", fn);
469
      }
467 470
    }
468 471

	
469 472
    /// \brief Constructor
470 473
    ///
471 474
    /// Construct a directed graph writer, which writes to the given
472 475
    /// output file.
473 476
    DigraphWriter(const Digraph& digraph, const char* fn)
474 477
      : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
475 478
        _skip_nodes(false), _skip_arcs(false) {
476
      if (!(*_os)) throw IoError("Cannot write file", fn);
479
      if (!(*_os)) {
480
        delete _os;
481
        throw IoError("Cannot write file", fn);
482
      }
477 483
    }
478 484

	
479 485
    /// \brief Destructor
480 486
    ~DigraphWriter() {
481 487
      for (typename NodeMaps::iterator it = _node_maps.begin();
482 488
           it != _node_maps.end(); ++it) {
483 489
        delete it->second;
484 490
      }
485 491

	
486 492
      for (typename ArcMaps::iterator it = _arc_maps.begin();
487 493
           it != _arc_maps.end(); ++it) {
488 494
        delete it->second;
489 495
      }
490 496

	
491 497
      for (typename Attributes::iterator it = _attributes.begin();
492 498
           it != _attributes.end(); ++it) {
493 499
        delete it->second;
494 500
      }
495 501

	
496 502
      if (local_os) {
497 503
        delete _os;
498 504
      }
499 505
    }
500 506

	
501 507
  private:
502 508

	
503 509
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
504 510
                                                  std::ostream& os);
505 511
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
506 512
                                                  const std::string& fn);
507 513
    friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph,
508 514
                                                  const char *fn);
509 515

	
510 516
    DigraphWriter(DigraphWriter& other)
511 517
      : _os(other._os), local_os(other.local_os), _digraph(other._digraph),
512 518
        _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
513 519

	
514 520
      other._os = 0;
515 521
      other.local_os = false;
516 522

	
517 523
      _node_index.swap(other._node_index);
518 524
      _arc_index.swap(other._arc_index);
519 525

	
520 526
      _node_maps.swap(other._node_maps);
521 527
      _arc_maps.swap(other._arc_maps);
522 528
      _attributes.swap(other._attributes);
523 529

	
524 530
      _nodes_caption = other._nodes_caption;
... ...
@@ -979,107 +985,113 @@
979 985

	
980 986

	
981 987
    std::ostream* _os;
982 988
    bool local_os;
983 989

	
984 990
    const Graph& _graph;
985 991

	
986 992
    std::string _nodes_caption;
987 993
    std::string _edges_caption;
988 994
    std::string _attributes_caption;
989 995

	
990 996
    typedef std::map<Node, std::string> NodeIndex;
991 997
    NodeIndex _node_index;
992 998
    typedef std::map<Edge, std::string> EdgeIndex;
993 999
    EdgeIndex _edge_index;
994 1000

	
995 1001
    typedef std::vector<std::pair<std::string,
996 1002
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;
997 1003
    NodeMaps _node_maps;
998 1004

	
999 1005
    typedef std::vector<std::pair<std::string,
1000 1006
      _writer_bits::MapStorageBase<Edge>* > >EdgeMaps;
1001 1007
    EdgeMaps _edge_maps;
1002 1008

	
1003 1009
    typedef std::vector<std::pair<std::string,
1004 1010
      _writer_bits::ValueStorageBase*> > Attributes;
1005 1011
    Attributes _attributes;
1006 1012

	
1007 1013
    bool _skip_nodes;
1008 1014
    bool _skip_edges;
1009 1015

	
1010 1016
  public:
1011 1017

	
1012 1018
    /// \brief Constructor
1013 1019
    ///
1014 1020
    /// Construct a directed graph writer, which writes to the given
1015 1021
    /// output stream.
1016 1022
    GraphWriter(const Graph& graph, std::ostream& os = std::cout)
1017 1023
      : _os(&os), local_os(false), _graph(graph),
1018 1024
        _skip_nodes(false), _skip_edges(false) {}
1019 1025

	
1020 1026
    /// \brief Constructor
1021 1027
    ///
1022 1028
    /// Construct a directed graph writer, which writes to the given
1023 1029
    /// output file.
1024 1030
    GraphWriter(const Graph& graph, const std::string& fn)
1025 1031
      : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph),
1026 1032
        _skip_nodes(false), _skip_edges(false) {
1027
      if (!(*_os)) throw IoError("Cannot write file", fn);
1033
      if (!(*_os)) {
1034
        delete _os;
1035
        throw IoError("Cannot write file", fn);
1036
      }
1028 1037
    }
1029 1038

	
1030 1039
    /// \brief Constructor
1031 1040
    ///
1032 1041
    /// Construct a directed graph writer, which writes to the given
1033 1042
    /// output file.
1034 1043
    GraphWriter(const Graph& graph, const char* fn)
1035 1044
      : _os(new std::ofstream(fn)), local_os(true), _graph(graph),
1036 1045
        _skip_nodes(false), _skip_edges(false) {
1037
      if (!(*_os)) throw IoError("Cannot write file", fn);
1046
      if (!(*_os)) {
1047
        delete _os;
1048
        throw IoError("Cannot write file", fn);
1049
      }
1038 1050
    }
1039 1051

	
1040 1052
    /// \brief Destructor
1041 1053
    ~GraphWriter() {
1042 1054
      for (typename NodeMaps::iterator it = _node_maps.begin();
1043 1055
           it != _node_maps.end(); ++it) {
1044 1056
        delete it->second;
1045 1057
      }
1046 1058

	
1047 1059
      for (typename EdgeMaps::iterator it = _edge_maps.begin();
1048 1060
           it != _edge_maps.end(); ++it) {
1049 1061
        delete it->second;
1050 1062
      }
1051 1063

	
1052 1064
      for (typename Attributes::iterator it = _attributes.begin();
1053 1065
           it != _attributes.end(); ++it) {
1054 1066
        delete it->second;
1055 1067
      }
1056 1068

	
1057 1069
      if (local_os) {
1058 1070
        delete _os;
1059 1071
      }
1060 1072
    }
1061 1073

	
1062 1074
  private:
1063 1075

	
1064 1076
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
1065 1077
                                            std::ostream& os);
1066 1078
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
1067 1079
                                            const std::string& fn);
1068 1080
    friend GraphWriter<Graph> graphWriter<>(const Graph& graph,
1069 1081
                                            const char *fn);
1070 1082

	
1071 1083
    GraphWriter(GraphWriter& other)
1072 1084
      : _os(other._os), local_os(other.local_os), _graph(other._graph),
1073 1085
        _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
1074 1086

	
1075 1087
      other._os = 0;
1076 1088
      other.local_os = false;
1077 1089

	
1078 1090
      _node_index.swap(other._node_index);
1079 1091
      _edge_index.swap(other._edge_index);
1080 1092

	
1081 1093
      _node_maps.swap(other._node_maps);
1082 1094
      _edge_maps.swap(other._edge_maps);
1083 1095
      _attributes.swap(other._attributes);
1084 1096

	
1085 1097
      _nodes_caption = other._nodes_caption;
... ...
@@ -1542,105 +1554,111 @@
1542 1554
  template <typename Graph>
1543 1555
  GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) {
1544 1556
    GraphWriter<Graph> tmp(graph, fn);
1545 1557
    return tmp;
1546 1558
  }
1547 1559

	
1548 1560
  class SectionWriter;
1549 1561

	
1550 1562
  SectionWriter sectionWriter(std::istream& is);
1551 1563
  SectionWriter sectionWriter(const std::string& fn);
1552 1564
  SectionWriter sectionWriter(const char* fn);
1553 1565

	
1554 1566
  /// \ingroup lemon_io
1555 1567
  ///
1556 1568
  /// \brief Section writer class
1557 1569
  ///
1558 1570
  /// In the \ref lgf-format "LGF" file extra sections can be placed,
1559 1571
  /// which contain any data in arbitrary format. Such sections can be
1560 1572
  /// written with this class. A writing rule can be added to the
1561 1573
  /// class with two different functions. With the \c sectionLines()
1562 1574
  /// function a generator can write the section line-by-line, while
1563 1575
  /// with the \c sectionStream() member the section can be written to
1564 1576
  /// an output stream.
1565 1577
  class SectionWriter {
1566 1578
  private:
1567 1579

	
1568 1580
    std::ostream* _os;
1569 1581
    bool local_os;
1570 1582

	
1571 1583
    typedef std::vector<std::pair<std::string, _writer_bits::Section*> >
1572 1584
    Sections;
1573 1585

	
1574 1586
    Sections _sections;
1575 1587

	
1576 1588
  public:
1577 1589

	
1578 1590
    /// \brief Constructor
1579 1591
    ///
1580 1592
    /// Construct a section writer, which writes to the given output
1581 1593
    /// stream.
1582 1594
    SectionWriter(std::ostream& os)
1583 1595
      : _os(&os), local_os(false) {}
1584 1596

	
1585 1597
    /// \brief Constructor
1586 1598
    ///
1587 1599
    /// Construct a section writer, which writes into the given file.
1588 1600
    SectionWriter(const std::string& fn)
1589 1601
      : _os(new std::ofstream(fn.c_str())), local_os(true) {
1590
      if (!(*_os)) throw IoError("Cannot write file", fn);
1602
      if (!(*_os)) {
1603
        delete _os;
1604
        throw IoError("Cannot write file", fn);
1605
      }
1591 1606
    }
1592 1607

	
1593 1608
    /// \brief Constructor
1594 1609
    ///
1595 1610
    /// Construct a section writer, which writes into the given file.
1596 1611
    SectionWriter(const char* fn)
1597 1612
      : _os(new std::ofstream(fn)), local_os(true) {
1598
      if (!(*_os)) throw IoError("Cannot write file", fn);
1613
      if (!(*_os)) {
1614
        delete _os;
1615
        throw IoError("Cannot write file", fn);
1616
      }
1599 1617
    }
1600 1618

	
1601 1619
    /// \brief Destructor
1602 1620
    ~SectionWriter() {
1603 1621
      for (Sections::iterator it = _sections.begin();
1604 1622
           it != _sections.end(); ++it) {
1605 1623
        delete it->second;
1606 1624
      }
1607 1625

	
1608 1626
      if (local_os) {
1609 1627
        delete _os;
1610 1628
      }
1611 1629

	
1612 1630
    }
1613 1631

	
1614 1632
  private:
1615 1633

	
1616 1634
    friend SectionWriter sectionWriter(std::ostream& os);
1617 1635
    friend SectionWriter sectionWriter(const std::string& fn);
1618 1636
    friend SectionWriter sectionWriter(const char* fn);
1619 1637

	
1620 1638
    SectionWriter(SectionWriter& other)
1621 1639
      : _os(other._os), local_os(other.local_os) {
1622 1640

	
1623 1641
      other._os = 0;
1624 1642
      other.local_os = false;
1625 1643

	
1626 1644
      _sections.swap(other._sections);
1627 1645
    }
1628 1646

	
1629 1647
    SectionWriter& operator=(const SectionWriter&);
1630 1648

	
1631 1649
  public:
1632 1650

	
1633 1651
    /// \name Section writers
1634 1652
    /// @{
1635 1653

	
1636 1654
    /// \brief Add a section writer with line oriented writing
1637 1655
    ///
1638 1656
    /// The first parameter is the type descriptor of the section, the
1639 1657
    /// second is a generator with std::string values. At the writing
1640 1658
    /// process, the returned \c std::string will be written into the
1641 1659
    /// output file until it is an empty string.
1642 1660
    ///
1643 1661
    /// For example, an integer vector is written into a section.
1644 1662
    ///\code
1645 1663
    ///  @numbers
1646 1664
    ///  12 45 23 78
0 comments (0 inline)