demo/dim_to_lgf.cc
author deba
Mon, 12 Sep 2005 09:19:52 +0000
changeset 1680 4f8b9cee576b
parent 1626 e251336be488
child 1744 51d5d41e15b1
permissions -rw-r--r--
Fixing and improving GridGraph
ladanyi@1626
     1
/* -*- C++ -*-
ladanyi@1626
     2
 * demo/dim_to_lgf.cc - Part of LEMON, a generic C++ optimization library
ladanyi@1626
     3
 *
ladanyi@1626
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
ladanyi@1626
     5
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
ladanyi@1626
     6
 *
ladanyi@1626
     7
 * Permission to use, modify and distribute this software is granted
ladanyi@1626
     8
 * provided that this copyright notice appears in all copies. For
ladanyi@1626
     9
 * precise terms see the accompanying LICENSE file.
ladanyi@1626
    10
 *
ladanyi@1626
    11
 * This software is provided "AS IS" with no warranty of any kind,
ladanyi@1626
    12
 * express or implied, and with no claim as to its suitability for any
ladanyi@1626
    13
 * purpose.
ladanyi@1626
    14
 *
ladanyi@1626
    15
 */
ladanyi@1626
    16
ladanyi@1626
    17
///\ingroup demos
ladanyi@1626
    18
///\file
ladanyi@1626
    19
///\brief DIMACS to LGF converter (demo).
ladanyi@1626
    20
///
ladanyi@1626
    21
/// This program converts various DIMACS formats to the LEMON Graph Format
ladanyi@1626
    22
/// (LGF).
alpar@1641
    23
///
alpar@1641
    24
/// \include dim_to_lgf.cc
ladanyi@1626
    25
deba@1296
    26
#include <iostream>
deba@1296
    27
#include <fstream>
deba@1296
    28
#include <cstring>
deba@1296
    29
deba@1296
    30
#include <lemon/smart_graph.h>
deba@1296
    31
#include <lemon/dimacs.h>
deba@1296
    32
#include <lemon/graph_writer.h>
deba@1296
    33
deba@1296
    34
using namespace std;
deba@1296
    35
using namespace lemon;
deba@1296
    36
deba@1296
    37
const char* versionString =
deba@1296
    38
"dim_to_lgf - part of lemon library\n";
deba@1296
    39
deba@1296
    40
const char* helpString =
ladanyi@1626
    41
"DIMACS to LGF converter\n"
deba@1296
    42
"Usage: dim_to_lgf [OPTIONS]\n"
deba@1296
    43
"\n"
deba@1296
    44
"Examples:\n"
deba@1296
    45
"  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
deba@1296
    46
"\n"
deba@1296
    47
"Options:\n"
deba@1296
    48
"  -i FILE, --input FILE    use FILE as input instead of standard input\n"
deba@1296
    49
"  -o FILE, --output FILE   use FILE as output instead of standard output\n"
deba@1296
    50
"  -t TYPE, --type TYPE     set up the type of the graph\n"
deba@1296
    51
"                             Possible types:\n"
deba@1296
    52
"                               mincostflow\n"
deba@1296
    53
"                               maxflow (default)\n"
deba@1296
    54
"                               shortestpath\n"
deba@1296
    55
"                               capacitated\n"
deba@1296
    56
"                               plain\n"
deba@1296
    57
"  -v, --version            shows the version of the converter\n"
deba@1296
    58
"  -h, --help               shows the help of the converter\n";
deba@1296
    59
deba@1296
    60
deba@1296
    61
int main(int argc, const char *argv[]) {
deba@1296
    62
  typedef SmartGraph Graph;
deba@1296
    63
deba@1296
    64
  typedef Graph::Edge Edge;
deba@1296
    65
  typedef Graph::Node Node;
deba@1296
    66
  typedef Graph::EdgeIt EdgeIt;
deba@1296
    67
  typedef Graph::NodeIt NodeIt;
deba@1296
    68
  typedef Graph::EdgeMap<string> StringMap;
deba@1296
    69
deba@1296
    70
  std::string inputName;
deba@1296
    71
  std::string outputName;
deba@1296
    72
  std::string typeName;
deba@1296
    73
deba@1296
    74
  bool help = false;
deba@1296
    75
  bool version = false;
deba@1296
    76
deba@1296
    77
  for (int arg = 1; arg < argc; ++arg) {
deba@1296
    78
    if (strcmp(argv[arg], "--type") == 0 || 
deba@1296
    79
	strcmp(argv[arg], "-t") == 0) {
deba@1296
    80
      if (!typeName.empty()) {
deba@1296
    81
	cerr << "Multiple type description" << endl;
deba@1296
    82
	return -1;
deba@1296
    83
      }
deba@1296
    84
      if (arg + 1 == argc) {
deba@1296
    85
	cerr << "Parameter without value" << endl;
deba@1296
    86
	return -1;
deba@1296
    87
      }
deba@1296
    88
      typeName = argv[++arg];
deba@1296
    89
    }
deba@1296
    90
    else if (strcmp(argv[arg], "--input") == 0 || 
deba@1296
    91
	     strcmp(argv[arg], "-i") == 0) {
deba@1296
    92
      if (!inputName.empty()) {
deba@1296
    93
	cerr << "Multiple input description" << endl;
deba@1296
    94
	return -1;
deba@1296
    95
      }
deba@1296
    96
      if (arg + 1 == argc) {
deba@1296
    97
	cerr << "Parameter without value" << endl;
deba@1296
    98
	return -1;
deba@1296
    99
      }
deba@1296
   100
      inputName = argv[++arg];
deba@1296
   101
    }
deba@1296
   102
    else if (strcmp(argv[arg], "--output") == 0 || 
deba@1296
   103
	     strcmp(argv[arg], "-o") == 0) {
deba@1296
   104
      if (!outputName.empty()) {
deba@1296
   105
	cerr << "Multiple input description" << endl;
deba@1296
   106
	return -1;
deba@1296
   107
      }
deba@1296
   108
      if (arg + 1 == argc) {
deba@1296
   109
	cerr << "Parameter without value" << endl;
deba@1296
   110
	return -1;
deba@1296
   111
      }
deba@1296
   112
      outputName = argv[++arg];
deba@1296
   113
    } else if (strcmp(argv[arg], "--help") == 0 ||
deba@1296
   114
	       strcmp(argv[arg], "-h") == 0) {
deba@1296
   115
      help = true;
deba@1296
   116
    } else if (strcmp(argv[arg], "--version") == 0 ||
deba@1296
   117
	       strcmp(argv[arg], "-v") == 0) {
deba@1296
   118
      version = true;
deba@1296
   119
    } else {
deba@1296
   120
      cerr << "Invalid option: " << argv[arg] << endl;
deba@1296
   121
      return -1;
deba@1296
   122
    }
deba@1296
   123
  }
deba@1296
   124
deba@1296
   125
  if (version) {
deba@1296
   126
    cout << versionString;
deba@1296
   127
  }
deba@1296
   128
  if (help) {
deba@1296
   129
    cout << helpString;
deba@1296
   130
  }
deba@1296
   131
  if (help || version) {
deba@1296
   132
    return 0;
deba@1296
   133
  }
deba@1296
   134
deba@1296
   135
  ifstream input;
deba@1296
   136
  if (!inputName.empty()) {
deba@1296
   137
    input.open(inputName.c_str());
deba@1296
   138
    if (!input) {
deba@1296
   139
      cerr << "File open error" << endl;
deba@1296
   140
      return -1;
deba@1296
   141
    }
deba@1296
   142
  }
deba@1296
   143
  istream& is = (inputName.empty() ? cin : input);
deba@1296
   144
deba@1296
   145
  ofstream output;
deba@1296
   146
  if (!outputName.empty()) {
deba@1296
   147
    output.open(outputName.c_str());
deba@1296
   148
    if (!output) {
deba@1296
   149
      cerr << "File open error" << endl;
deba@1296
   150
      return -1;
deba@1296
   151
    }
deba@1296
   152
  }
deba@1296
   153
  ostream& os = (outputName.empty() ? cout : output);
deba@1296
   154
deba@1296
   155
  if (typeName.empty()) {
deba@1296
   156
    typeName = "maxflow";
deba@1296
   157
  }
deba@1296
   158
deba@1296
   159
  if (typeName == "mincostflow") {
deba@1296
   160
    Graph graph;
deba@1296
   161
    Node s, t;
deba@1296
   162
    StringMap cost(graph), capacity(graph);
deba@1296
   163
    readDimacs(is, graph, capacity, s, t, cost);
deba@1296
   164
    writeGraph(os, graph, capacity, s, t, cost);
deba@1296
   165
  } else if (typeName == "maxflow") {
deba@1296
   166
    Graph graph;
deba@1296
   167
    Node s, t;
deba@1296
   168
    StringMap capacity(graph);
deba@1296
   169
    readDimacs(is, graph, capacity, s, t);
deba@1296
   170
    writeGraph(os, graph, capacity, s, t);
deba@1296
   171
  } else if (typeName == "shortestpath") {
deba@1296
   172
    Graph graph;
deba@1296
   173
    Node s;
deba@1296
   174
    StringMap capacity(graph);
deba@1296
   175
    readDimacs(is, graph, capacity, s);
deba@1296
   176
    writeGraph(os, graph, capacity, s);
deba@1296
   177
  } else if (typeName == "capacitated") {
deba@1296
   178
    Graph graph;
deba@1296
   179
    StringMap capacity(graph);
deba@1296
   180
    readDimacs(is, graph, capacity);
deba@1296
   181
    writeGraph(os, graph, capacity);
deba@1296
   182
  } else if (typeName == "plain") {
deba@1296
   183
    Graph graph;
deba@1296
   184
    readDimacs(is, graph);
deba@1296
   185
    writeGraph(os, graph);
deba@1296
   186
  } else {
deba@1296
   187
    cerr << "Invalid type error" << endl;
deba@1296
   188
    return -1;
deba@1296
   189
  }
deba@1296
   190
  return 0;
deba@1296
   191
}