dimacs_to_lgf demo
authordeba
Mon, 04 Apr 2005 10:11:47 +0000
changeset 129612078ddcf065
parent 1295 02a403c305b9
child 1297 fde0d12545c1
dimacs_to_lgf demo
may we need a tools directory?
src/demo/Makefile.am
src/demo/dim_to_lgf.cc
     1.1 --- a/src/demo/Makefile.am	Mon Apr 04 08:03:43 2005 +0000
     1.2 +++ b/src/demo/Makefile.am	Mon Apr 04 10:11:47 2005 +0000
     1.3 @@ -2,10 +2,15 @@
     1.4  
     1.5  EXTRA_DIST = sub_graph_wrapper_demo.dim
     1.6  
     1.7 -noinst_PROGRAMS = dim_to_dot sub_graph_wrapper_demo graph_to_eps_demo
     1.8 +noinst_PROGRAMS = 	dim_to_dot 					\
     1.9 +			sub_graph_wrapper_demo 				\
    1.10 +			graph_to_eps_demo 				\
    1.11 +			dim_to_lgf
    1.12  
    1.13  dim_to_dot_SOURCES = dim_to_dot.cc
    1.14  
    1.15  sub_graph_wrapper_demo_SOURCES = sub_graph_wrapper_demo.cc tight_edge_filter_map.h
    1.16  
    1.17 -graph_to_eps_demo_SOURCES = graph_to_eps_demo.cc
    1.18 \ No newline at end of file
    1.19 +graph_to_eps_demo_SOURCES = graph_to_eps_demo.cc
    1.20 +
    1.21 +dim_to_lgf_SOURCES = dim_to_lgf.cc
    1.22 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/demo/dim_to_lgf.cc	Mon Apr 04 10:11:47 2005 +0000
     2.3 @@ -0,0 +1,166 @@
     2.4 +#include <iostream>
     2.5 +#include <fstream>
     2.6 +#include <cstring>
     2.7 +
     2.8 +#include <lemon/smart_graph.h>
     2.9 +#include <lemon/dimacs.h>
    2.10 +#include <lemon/graph_writer.h>
    2.11 +
    2.12 +using namespace std;
    2.13 +using namespace lemon;
    2.14 +
    2.15 +const char* versionString =
    2.16 +"dim_to_lgf - part of lemon library\n";
    2.17 +
    2.18 +const char* helpString =
    2.19 +"Dimacs to LGF converter\n"
    2.20 +"Usage: dim_to_lgf [OPTIONS]\n"
    2.21 +"\n"
    2.22 +"Examples:\n"
    2.23 +"  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
    2.24 +"\n"
    2.25 +"Options:\n"
    2.26 +"  -i FILE, --input FILE    use FILE as input instead of standard input\n"
    2.27 +"  -o FILE, --output FILE   use FILE as output instead of standard output\n"
    2.28 +"  -t TYPE, --type TYPE     set up the type of the graph\n"
    2.29 +"                             Possible types:\n"
    2.30 +"                               mincostflow\n"
    2.31 +"                               maxflow (default)\n"
    2.32 +"                               shortestpath\n"
    2.33 +"                               capacitated\n"
    2.34 +"                               plain\n"
    2.35 +"  -v, --version            shows the version of the converter\n"
    2.36 +"  -h, --help               shows the help of the converter\n";
    2.37 +
    2.38 +
    2.39 +int main(int argc, const char *argv[]) {
    2.40 +  typedef SmartGraph Graph;
    2.41 +
    2.42 +  typedef Graph::Edge Edge;
    2.43 +  typedef Graph::Node Node;
    2.44 +  typedef Graph::EdgeIt EdgeIt;
    2.45 +  typedef Graph::NodeIt NodeIt;
    2.46 +  typedef Graph::EdgeMap<string> StringMap;
    2.47 +
    2.48 +  std::string inputName;
    2.49 +  std::string outputName;
    2.50 +  std::string typeName;
    2.51 +
    2.52 +  bool help = false;
    2.53 +  bool version = false;
    2.54 +
    2.55 +  for (int arg = 1; arg < argc; ++arg) {
    2.56 +    if (strcmp(argv[arg], "--type") == 0 || 
    2.57 +	strcmp(argv[arg], "-t") == 0) {
    2.58 +      if (!typeName.empty()) {
    2.59 +	cerr << "Multiple type description" << endl;
    2.60 +	return -1;
    2.61 +      }
    2.62 +      if (arg + 1 == argc) {
    2.63 +	cerr << "Parameter without value" << endl;
    2.64 +	return -1;
    2.65 +      }
    2.66 +      typeName = argv[++arg];
    2.67 +    }
    2.68 +    else if (strcmp(argv[arg], "--input") == 0 || 
    2.69 +	     strcmp(argv[arg], "-i") == 0) {
    2.70 +      if (!inputName.empty()) {
    2.71 +	cerr << "Multiple input description" << endl;
    2.72 +	return -1;
    2.73 +      }
    2.74 +      if (arg + 1 == argc) {
    2.75 +	cerr << "Parameter without value" << endl;
    2.76 +	return -1;
    2.77 +      }
    2.78 +      inputName = argv[++arg];
    2.79 +    }
    2.80 +    else if (strcmp(argv[arg], "--output") == 0 || 
    2.81 +	     strcmp(argv[arg], "-o") == 0) {
    2.82 +      if (!outputName.empty()) {
    2.83 +	cerr << "Multiple input description" << endl;
    2.84 +	return -1;
    2.85 +      }
    2.86 +      if (arg + 1 == argc) {
    2.87 +	cerr << "Parameter without value" << endl;
    2.88 +	return -1;
    2.89 +      }
    2.90 +      outputName = argv[++arg];
    2.91 +    } else if (strcmp(argv[arg], "--help") == 0 ||
    2.92 +	       strcmp(argv[arg], "-h") == 0) {
    2.93 +      help = true;
    2.94 +    } else if (strcmp(argv[arg], "--version") == 0 ||
    2.95 +	       strcmp(argv[arg], "-v") == 0) {
    2.96 +      version = true;
    2.97 +    } else {
    2.98 +      cerr << "Invalid option: " << argv[arg] << endl;
    2.99 +      return -1;
   2.100 +    }
   2.101 +  }
   2.102 +
   2.103 +  if (version) {
   2.104 +    cout << versionString;
   2.105 +  }
   2.106 +  if (help) {
   2.107 +    cout << helpString;
   2.108 +  }
   2.109 +  if (help || version) {
   2.110 +    return 0;
   2.111 +  }
   2.112 +
   2.113 +  ifstream input;
   2.114 +  if (!inputName.empty()) {
   2.115 +    input.open(inputName.c_str());
   2.116 +    if (!input) {
   2.117 +      cerr << "File open error" << endl;
   2.118 +      return -1;
   2.119 +    }
   2.120 +  }
   2.121 +  istream& is = (inputName.empty() ? cin : input);
   2.122 +
   2.123 +  ofstream output;
   2.124 +  if (!outputName.empty()) {
   2.125 +    output.open(outputName.c_str());
   2.126 +    if (!output) {
   2.127 +      cerr << "File open error" << endl;
   2.128 +      return -1;
   2.129 +    }
   2.130 +  }
   2.131 +  ostream& os = (outputName.empty() ? cout : output);
   2.132 +
   2.133 +  if (typeName.empty()) {
   2.134 +    typeName = "maxflow";
   2.135 +  }
   2.136 +
   2.137 +  if (typeName == "mincostflow") {
   2.138 +    Graph graph;
   2.139 +    Node s, t;
   2.140 +    StringMap cost(graph), capacity(graph);
   2.141 +    readDimacs(is, graph, capacity, s, t, cost);
   2.142 +    writeGraph(os, graph, capacity, s, t, cost);
   2.143 +  } else if (typeName == "maxflow") {
   2.144 +    Graph graph;
   2.145 +    Node s, t;
   2.146 +    StringMap capacity(graph);
   2.147 +    readDimacs(is, graph, capacity, s, t);
   2.148 +    writeGraph(os, graph, capacity, s, t);
   2.149 +  } else if (typeName == "shortestpath") {
   2.150 +    Graph graph;
   2.151 +    Node s;
   2.152 +    StringMap capacity(graph);
   2.153 +    readDimacs(is, graph, capacity, s);
   2.154 +    writeGraph(os, graph, capacity, s);
   2.155 +  } else if (typeName == "capacitated") {
   2.156 +    Graph graph;
   2.157 +    StringMap capacity(graph);
   2.158 +    readDimacs(is, graph, capacity);
   2.159 +    writeGraph(os, graph, capacity);
   2.160 +  } else if (typeName == "plain") {
   2.161 +    Graph graph;
   2.162 +    readDimacs(is, graph);
   2.163 +    writeGraph(os, graph);
   2.164 +  } else {
   2.165 +    cerr << "Invalid type error" << endl;
   2.166 +    return -1;
   2.167 +  }
   2.168 +  return 0;
   2.169 +}