tools/dimacs-to-lgf.cc
changeset 402 24a2c6ee6cb0
parent 401 9d1faab5e0f1
child 463 88ed40ad0d4f
     1.1 --- a/tools/dimacs-to-lgf.cc	Thu Nov 27 22:05:35 2008 +0000
     1.2 +++ b/tools/dimacs-to-lgf.cc	Fri Nov 28 06:38:20 2008 +0000
     1.3 @@ -39,6 +39,7 @@
     1.4  #include <lemon/lgf_writer.h>
     1.5  
     1.6  #include <lemon/arg_parser.h>
     1.7 +#include <lemon/error.h>
     1.8  
     1.9  using namespace std;
    1.10  using namespace lemon;
    1.11 @@ -56,113 +57,93 @@
    1.12  
    1.13    std::string inputName;
    1.14    std::string outputName;
    1.15 -  std::string typeName;
    1.16 -
    1.17 -  bool mincostflow;
    1.18 -  bool maxflow;
    1.19 -  bool shortestpath;
    1.20 -  bool capacitated;
    1.21 -  bool plain;
    1.22 -
    1.23 -  bool version;
    1.24  
    1.25    ArgParser ap(argc, argv);
    1.26 -  ap.refOption("-input",
    1.27 -               "use FILE as input instead of standard input",
    1.28 -               inputName).synonym("i", "-input")
    1.29 -    .refOption("-output",
    1.30 -               "use FILE as output instead of standard output",
    1.31 -               outputName).synonym("o", "-output")
    1.32 -    .refOption("-mincostflow",
    1.33 -               "set the type of the digraph to \"mincostflow\" digraph",
    1.34 -               mincostflow)
    1.35 -    .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
    1.36 -    .refOption("-maxflow",
    1.37 -               "set the type of the digraph to \"maxflow\" digraph",
    1.38 -               maxflow)
    1.39 -    .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
    1.40 -    .refOption("-shortestpath",
    1.41 -               "set the type of the digraph to \"shortestpath\" digraph",
    1.42 -               shortestpath)
    1.43 -    .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
    1.44 -    .refOption("-capacitated",
    1.45 -               "set the type of the digraph to \"capacitated\" digraph",
    1.46 -               capacitated)
    1.47 -    .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
    1.48 -    .refOption("-plain",
    1.49 -               "set the type of the digraph to \"plain\" digraph",
    1.50 -               plain)
    1.51 -    .optionGroup("type", "-plain").synonym("pl", "-plain")
    1.52 -    .onlyOneGroup("type")
    1.53 -    .mandatoryGroup("type")
    1.54 -    .refOption("-version", "show version information", version)
    1.55 -    .synonym("v", "-version")
    1.56 +  ap.other("[INFILE [OUTFILE]]",
    1.57 +           "If either the INFILE or OUTFILE file is missing the standard\n"
    1.58 +           "     input/output will be used instead.")
    1.59      .run();
    1.60  
    1.61    ifstream input;
    1.62 -  if (!inputName.empty()) {
    1.63 -    input.open(inputName.c_str());
    1.64 -    if (!input) {
    1.65 -      cerr << "File open error" << endl;
    1.66 -      return -1;
    1.67 +  ofstream output;
    1.68 +
    1.69 +  switch(ap.files().size())
    1.70 +    {
    1.71 +    case 2:
    1.72 +      output.open(ap.files()[1].c_str());
    1.73 +      if (!output) {
    1.74 +        throw IoError("Cannot open the file for writing", ap.files()[1]);
    1.75 +      }
    1.76 +    case 1:
    1.77 +      input.open(ap.files()[0].c_str());
    1.78 +      if (!input) {
    1.79 +        throw IoError("File cannot be found", ap.files()[0]);
    1.80 +      }
    1.81 +    case 0:
    1.82 +      break;
    1.83 +    default:
    1.84 +      cerr << ap.commandName() << ": too many arguments\n";
    1.85 +      return 1;
    1.86 +  }
    1.87 +  istream& is = (ap.files().size()<1 ? cin : input);
    1.88 +  ostream& os = (ap.files().size()<2 ? cout : output);
    1.89 +
    1.90 +  DimacsDescriptor desc = dimacsType(is);
    1.91 +  switch(desc.type)
    1.92 +    {
    1.93 +    case DimacsDescriptor::MIN:
    1.94 +      {
    1.95 +        Digraph digraph;
    1.96 +        DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
    1.97 +        DoubleNodeMap supply(digraph);
    1.98 +        readDimacsMin(is, digraph, lower, capacity, cost, supply, desc);
    1.99 +        DigraphWriter<Digraph>(digraph, os).
   1.100 +          nodeMap("supply", supply).
   1.101 +          arcMap("lower", lower).
   1.102 +          arcMap("capacity", capacity).
   1.103 +          arcMap("cost", cost).
   1.104 +          attribute("problem","min").
   1.105 +          run();
   1.106 +      }
   1.107 +      break;
   1.108 +    case DimacsDescriptor::MAX:
   1.109 +      {
   1.110 +        Digraph digraph;
   1.111 +        Node s, t;
   1.112 +        DoubleArcMap capacity(digraph);
   1.113 +        readDimacsMax(is, digraph, capacity, s, t, desc);
   1.114 +        DigraphWriter<Digraph>(digraph, os).
   1.115 +          arcMap("capacity", capacity).
   1.116 +          node("source", s).
   1.117 +          node("target", t).
   1.118 +          attribute("problem","max").
   1.119 +          run();
   1.120 +      }
   1.121 +      break;
   1.122 +    case DimacsDescriptor::SP:
   1.123 +      {
   1.124 +        Digraph digraph;
   1.125 +        Node s;
   1.126 +        DoubleArcMap capacity(digraph);
   1.127 +        readDimacsSp(is, digraph, capacity, s, desc);
   1.128 +        DigraphWriter<Digraph>(digraph, os).
   1.129 +          arcMap("capacity", capacity).
   1.130 +          node("source", s).
   1.131 +          attribute("problem","sp").
   1.132 +          run();
   1.133 +      }
   1.134 +      break;
   1.135 +    case DimacsDescriptor::MAT:
   1.136 +      {
   1.137 +        Digraph digraph;
   1.138 +        readDimacsMat(is, digraph,desc);
   1.139 +        DigraphWriter<Digraph>(digraph, os).
   1.140 +          attribute("problem","mat").
   1.141 +          run();
   1.142 +      }
   1.143 +      break;
   1.144 +    default:
   1.145 +      break;
   1.146      }
   1.147 -  }
   1.148 -  istream& is = (inputName.empty() ? cin : input);
   1.149 -
   1.150 -  ofstream output;
   1.151 -  if (!outputName.empty()) {
   1.152 -    output.open(outputName.c_str());
   1.153 -    if (!output) {
   1.154 -      cerr << "File open error" << endl;
   1.155 -      return -1;
   1.156 -    }
   1.157 -  }
   1.158 -  ostream& os = (outputName.empty() ? cout : output);
   1.159 -
   1.160 -  if (mincostflow) {
   1.161 -    Digraph digraph;
   1.162 -    DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
   1.163 -    DoubleNodeMap supply(digraph);
   1.164 -    readDimacsMin(is, digraph, lower, capacity, cost, supply);
   1.165 -    DigraphWriter<Digraph>(digraph, os).
   1.166 -      nodeMap("supply", supply).
   1.167 -      arcMap("lower", lower).
   1.168 -      arcMap("capacity", capacity).
   1.169 -      arcMap("cost", cost).
   1.170 -      run();
   1.171 -  } else if (maxflow) {
   1.172 -    Digraph digraph;
   1.173 -    Node s, t;
   1.174 -    DoubleArcMap capacity(digraph);
   1.175 -    readDimacsMax(is, digraph, capacity, s, t);
   1.176 -    DigraphWriter<Digraph>(digraph, os).
   1.177 -      arcMap("capacity", capacity).
   1.178 -      node("source", s).
   1.179 -      node("target", t).
   1.180 -      run();
   1.181 -  } else if (shortestpath) {
   1.182 -    Digraph digraph;
   1.183 -    Node s;
   1.184 -    DoubleArcMap capacity(digraph);
   1.185 -    readDimacsSp(is, digraph, capacity, s);
   1.186 -    DigraphWriter<Digraph>(digraph, os).
   1.187 -      arcMap("capacity", capacity).
   1.188 -      node("source", s).
   1.189 -      run();
   1.190 -  } else if (capacitated) {
   1.191 -    Digraph digraph;
   1.192 -    DoubleArcMap capacity(digraph);
   1.193 -    readDimacsMax(is, digraph, capacity);
   1.194 -    DigraphWriter<Digraph>(digraph, os).
   1.195 -      arcMap("capacity", capacity).
   1.196 -      run();
   1.197 -  } else if (plain) {
   1.198 -    Digraph digraph;
   1.199 -    readDimacsMat(is, digraph);
   1.200 -    DigraphWriter<Digraph>(digraph, os).run();
   1.201 -  } else {
   1.202 -    cerr << "Invalid type error" << endl;
   1.203 -    return -1;
   1.204 -  }
   1.205    return 0;
   1.206  }