alpar@385: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@385:  *
alpar@385:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@385:  *
alpar@440:  * Copyright (C) 2003-2009
alpar@385:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@385:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@385:  *
alpar@385:  * Permission to use, modify and distribute this software is granted
alpar@385:  * provided that this copyright notice appears in all copies. For
alpar@385:  * precise terms see the accompanying LICENSE file.
alpar@385:  *
alpar@385:  * This software is provided "AS IS" with no warranty of any kind,
alpar@385:  * express or implied, and with no claim as to its suitability for any
alpar@385:  * purpose.
alpar@385:  *
alpar@385:  */
alpar@385: 
alpar@385: ///\ingroup tools
alpar@385: ///\file
alpar@385: ///\brief DIMACS to LGF converter.
alpar@385: ///
alpar@385: /// This program converts various DIMACS formats to the LEMON Digraph Format
alpar@385: /// (LGF).
alpar@385: ///
alpar@385: /// See
kpeter@584: /// \code
kpeter@584: ///   dimacs-to-lgf --help
kpeter@584: /// \endcode
kpeter@584: /// for more info on the usage.
alpar@385: 
alpar@385: #include <iostream>
alpar@385: #include <fstream>
alpar@385: #include <cstring>
alpar@385: 
alpar@385: #include <lemon/smart_graph.h>
alpar@385: #include <lemon/dimacs.h>
alpar@385: #include <lemon/lgf_writer.h>
alpar@385: 
alpar@385: #include <lemon/arg_parser.h>
alpar@387: #include <lemon/error.h>
alpar@385: 
alpar@385: using namespace std;
alpar@385: using namespace lemon;
alpar@385: 
alpar@385: 
alpar@385: int main(int argc, const char *argv[]) {
alpar@385:   typedef SmartDigraph Digraph;
alpar@385: 
alpar@385:   typedef Digraph::Arc Arc;
alpar@385:   typedef Digraph::Node Node;
alpar@385:   typedef Digraph::ArcIt ArcIt;
alpar@385:   typedef Digraph::NodeIt NodeIt;
alpar@385:   typedef Digraph::ArcMap<double> DoubleArcMap;
alpar@385:   typedef Digraph::NodeMap<double> DoubleNodeMap;
alpar@385: 
alpar@385:   std::string inputName;
alpar@385:   std::string outputName;
alpar@385: 
alpar@385:   ArgParser ap(argc, argv);
alpar@387:   ap.other("[INFILE [OUTFILE]]",
alpar@387:            "If either the INFILE or OUTFILE file is missing the standard\n"
alpar@387:            "     input/output will be used instead.")
alpar@385:     .run();
alpar@385: 
alpar@385:   ifstream input;
alpar@387:   ofstream output;
alpar@387: 
alpar@387:   switch(ap.files().size())
alpar@387:     {
alpar@387:     case 2:
alpar@387:       output.open(ap.files()[1].c_str());
alpar@387:       if (!output) {
alpar@387:         throw IoError("Cannot open the file for writing", ap.files()[1]);
alpar@387:       }
alpar@387:     case 1:
alpar@387:       input.open(ap.files()[0].c_str());
alpar@387:       if (!input) {
alpar@387:         throw IoError("File cannot be found", ap.files()[0]);
alpar@387:       }
alpar@387:     case 0:
alpar@387:       break;
alpar@387:     default:
alpar@387:       cerr << ap.commandName() << ": too many arguments\n";
alpar@387:       return 1;
alpar@387:   }
alpar@387:   istream& is = (ap.files().size()<1 ? cin : input);
alpar@387:   ostream& os = (ap.files().size()<2 ? cout : output);
alpar@387: 
alpar@387:   DimacsDescriptor desc = dimacsType(is);
alpar@387:   switch(desc.type)
alpar@387:     {
alpar@387:     case DimacsDescriptor::MIN:
alpar@387:       {
alpar@387:         Digraph digraph;
alpar@387:         DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
alpar@387:         DoubleNodeMap supply(digraph);
alpar@561:         readDimacsMin(is, digraph, lower, capacity, cost, supply, 0, desc);
alpar@387:         DigraphWriter<Digraph>(digraph, os).
alpar@387:           nodeMap("supply", supply).
alpar@387:           arcMap("lower", lower).
alpar@387:           arcMap("capacity", capacity).
alpar@387:           arcMap("cost", cost).
alpar@387:           attribute("problem","min").
alpar@387:           run();
alpar@387:       }
alpar@387:       break;
alpar@387:     case DimacsDescriptor::MAX:
alpar@387:       {
alpar@387:         Digraph digraph;
alpar@387:         Node s, t;
alpar@387:         DoubleArcMap capacity(digraph);
alpar@561:         readDimacsMax(is, digraph, capacity, s, t, 0, desc);
alpar@387:         DigraphWriter<Digraph>(digraph, os).
alpar@387:           arcMap("capacity", capacity).
alpar@387:           node("source", s).
alpar@387:           node("target", t).
alpar@387:           attribute("problem","max").
alpar@387:           run();
alpar@387:       }
alpar@387:       break;
alpar@387:     case DimacsDescriptor::SP:
alpar@387:       {
alpar@387:         Digraph digraph;
alpar@387:         Node s;
alpar@387:         DoubleArcMap capacity(digraph);
alpar@387:         readDimacsSp(is, digraph, capacity, s, desc);
alpar@387:         DigraphWriter<Digraph>(digraph, os).
alpar@387:           arcMap("capacity", capacity).
alpar@387:           node("source", s).
alpar@387:           attribute("problem","sp").
alpar@387:           run();
alpar@387:       }
alpar@387:       break;
alpar@387:     case DimacsDescriptor::MAT:
alpar@387:       {
alpar@387:         Digraph digraph;
alpar@387:         readDimacsMat(is, digraph,desc);
alpar@387:         DigraphWriter<Digraph>(digraph, os).
alpar@387:           attribute("problem","mat").
alpar@387:           run();
alpar@387:       }
alpar@387:       break;
alpar@387:     default:
alpar@387:       break;
alpar@385:     }
alpar@385:   return 0;
alpar@385: }