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