demo/dim_to_lgf.cc
changeset 1435 8e85e6bbefdf
parent 1296 12078ddcf065
child 1626 e251336be488
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/dim_to_lgf.cc	Mon May 23 04:48:14 2005 +0000
     1.3 @@ -0,0 +1,166 @@
     1.4 +#include <iostream>
     1.5 +#include <fstream>
     1.6 +#include <cstring>
     1.7 +
     1.8 +#include <lemon/smart_graph.h>
     1.9 +#include <lemon/dimacs.h>
    1.10 +#include <lemon/graph_writer.h>
    1.11 +
    1.12 +using namespace std;
    1.13 +using namespace lemon;
    1.14 +
    1.15 +const char* versionString =
    1.16 +"dim_to_lgf - part of lemon library\n";
    1.17 +
    1.18 +const char* helpString =
    1.19 +"Dimacs to LGF converter\n"
    1.20 +"Usage: dim_to_lgf [OPTIONS]\n"
    1.21 +"\n"
    1.22 +"Examples:\n"
    1.23 +"  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
    1.24 +"\n"
    1.25 +"Options:\n"
    1.26 +"  -i FILE, --input FILE    use FILE as input instead of standard input\n"
    1.27 +"  -o FILE, --output FILE   use FILE as output instead of standard output\n"
    1.28 +"  -t TYPE, --type TYPE     set up the type of the graph\n"
    1.29 +"                             Possible types:\n"
    1.30 +"                               mincostflow\n"
    1.31 +"                               maxflow (default)\n"
    1.32 +"                               shortestpath\n"
    1.33 +"                               capacitated\n"
    1.34 +"                               plain\n"
    1.35 +"  -v, --version            shows the version of the converter\n"
    1.36 +"  -h, --help               shows the help of the converter\n";
    1.37 +
    1.38 +
    1.39 +int main(int argc, const char *argv[]) {
    1.40 +  typedef SmartGraph Graph;
    1.41 +
    1.42 +  typedef Graph::Edge Edge;
    1.43 +  typedef Graph::Node Node;
    1.44 +  typedef Graph::EdgeIt EdgeIt;
    1.45 +  typedef Graph::NodeIt NodeIt;
    1.46 +  typedef Graph::EdgeMap<string> StringMap;
    1.47 +
    1.48 +  std::string inputName;
    1.49 +  std::string outputName;
    1.50 +  std::string typeName;
    1.51 +
    1.52 +  bool help = false;
    1.53 +  bool version = false;
    1.54 +
    1.55 +  for (int arg = 1; arg < argc; ++arg) {
    1.56 +    if (strcmp(argv[arg], "--type") == 0 || 
    1.57 +	strcmp(argv[arg], "-t") == 0) {
    1.58 +      if (!typeName.empty()) {
    1.59 +	cerr << "Multiple type description" << endl;
    1.60 +	return -1;
    1.61 +      }
    1.62 +      if (arg + 1 == argc) {
    1.63 +	cerr << "Parameter without value" << endl;
    1.64 +	return -1;
    1.65 +      }
    1.66 +      typeName = argv[++arg];
    1.67 +    }
    1.68 +    else if (strcmp(argv[arg], "--input") == 0 || 
    1.69 +	     strcmp(argv[arg], "-i") == 0) {
    1.70 +      if (!inputName.empty()) {
    1.71 +	cerr << "Multiple input description" << endl;
    1.72 +	return -1;
    1.73 +      }
    1.74 +      if (arg + 1 == argc) {
    1.75 +	cerr << "Parameter without value" << endl;
    1.76 +	return -1;
    1.77 +      }
    1.78 +      inputName = argv[++arg];
    1.79 +    }
    1.80 +    else if (strcmp(argv[arg], "--output") == 0 || 
    1.81 +	     strcmp(argv[arg], "-o") == 0) {
    1.82 +      if (!outputName.empty()) {
    1.83 +	cerr << "Multiple input description" << endl;
    1.84 +	return -1;
    1.85 +      }
    1.86 +      if (arg + 1 == argc) {
    1.87 +	cerr << "Parameter without value" << endl;
    1.88 +	return -1;
    1.89 +      }
    1.90 +      outputName = argv[++arg];
    1.91 +    } else if (strcmp(argv[arg], "--help") == 0 ||
    1.92 +	       strcmp(argv[arg], "-h") == 0) {
    1.93 +      help = true;
    1.94 +    } else if (strcmp(argv[arg], "--version") == 0 ||
    1.95 +	       strcmp(argv[arg], "-v") == 0) {
    1.96 +      version = true;
    1.97 +    } else {
    1.98 +      cerr << "Invalid option: " << argv[arg] << endl;
    1.99 +      return -1;
   1.100 +    }
   1.101 +  }
   1.102 +
   1.103 +  if (version) {
   1.104 +    cout << versionString;
   1.105 +  }
   1.106 +  if (help) {
   1.107 +    cout << helpString;
   1.108 +  }
   1.109 +  if (help || version) {
   1.110 +    return 0;
   1.111 +  }
   1.112 +
   1.113 +  ifstream input;
   1.114 +  if (!inputName.empty()) {
   1.115 +    input.open(inputName.c_str());
   1.116 +    if (!input) {
   1.117 +      cerr << "File open error" << endl;
   1.118 +      return -1;
   1.119 +    }
   1.120 +  }
   1.121 +  istream& is = (inputName.empty() ? cin : input);
   1.122 +
   1.123 +  ofstream output;
   1.124 +  if (!outputName.empty()) {
   1.125 +    output.open(outputName.c_str());
   1.126 +    if (!output) {
   1.127 +      cerr << "File open error" << endl;
   1.128 +      return -1;
   1.129 +    }
   1.130 +  }
   1.131 +  ostream& os = (outputName.empty() ? cout : output);
   1.132 +
   1.133 +  if (typeName.empty()) {
   1.134 +    typeName = "maxflow";
   1.135 +  }
   1.136 +
   1.137 +  if (typeName == "mincostflow") {
   1.138 +    Graph graph;
   1.139 +    Node s, t;
   1.140 +    StringMap cost(graph), capacity(graph);
   1.141 +    readDimacs(is, graph, capacity, s, t, cost);
   1.142 +    writeGraph(os, graph, capacity, s, t, cost);
   1.143 +  } else if (typeName == "maxflow") {
   1.144 +    Graph graph;
   1.145 +    Node s, t;
   1.146 +    StringMap capacity(graph);
   1.147 +    readDimacs(is, graph, capacity, s, t);
   1.148 +    writeGraph(os, graph, capacity, s, t);
   1.149 +  } else if (typeName == "shortestpath") {
   1.150 +    Graph graph;
   1.151 +    Node s;
   1.152 +    StringMap capacity(graph);
   1.153 +    readDimacs(is, graph, capacity, s);
   1.154 +    writeGraph(os, graph, capacity, s);
   1.155 +  } else if (typeName == "capacitated") {
   1.156 +    Graph graph;
   1.157 +    StringMap capacity(graph);
   1.158 +    readDimacs(is, graph, capacity);
   1.159 +    writeGraph(os, graph, capacity);
   1.160 +  } else if (typeName == "plain") {
   1.161 +    Graph graph;
   1.162 +    readDimacs(is, graph);
   1.163 +    writeGraph(os, graph);
   1.164 +  } else {
   1.165 +    cerr << "Invalid type error" << endl;
   1.166 +    return -1;
   1.167 +  }
   1.168 +  return 0;
   1.169 +}