demo/dim_to_lgf.cc
author ladanyi
Wed, 17 Aug 2005 20:39:11 +0000
changeset 1639 aec340db8fc2
parent 1435 8e85e6bbefdf
child 1641 77f6ab7ad66f
permissions -rw-r--r--
fixed a typo
     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 <iostream>
    25 #include <fstream>
    26 #include <cstring>
    27 
    28 #include <lemon/smart_graph.h>
    29 #include <lemon/dimacs.h>
    30 #include <lemon/graph_writer.h>
    31 
    32 using namespace std;
    33 using namespace lemon;
    34 
    35 const char* versionString =
    36 "dim_to_lgf - part of lemon library\n";
    37 
    38 const char* helpString =
    39 "DIMACS to LGF converter\n"
    40 "Usage: dim_to_lgf [OPTIONS]\n"
    41 "\n"
    42 "Examples:\n"
    43 "  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
    44 "\n"
    45 "Options:\n"
    46 "  -i FILE, --input FILE    use FILE as input instead of standard input\n"
    47 "  -o FILE, --output FILE   use FILE as output instead of standard output\n"
    48 "  -t TYPE, --type TYPE     set up the type of the graph\n"
    49 "                             Possible types:\n"
    50 "                               mincostflow\n"
    51 "                               maxflow (default)\n"
    52 "                               shortestpath\n"
    53 "                               capacitated\n"
    54 "                               plain\n"
    55 "  -v, --version            shows the version of the converter\n"
    56 "  -h, --help               shows the help of the converter\n";
    57 
    58 
    59 int main(int argc, const char *argv[]) {
    60   typedef SmartGraph Graph;
    61 
    62   typedef Graph::Edge Edge;
    63   typedef Graph::Node Node;
    64   typedef Graph::EdgeIt EdgeIt;
    65   typedef Graph::NodeIt NodeIt;
    66   typedef Graph::EdgeMap<string> StringMap;
    67 
    68   std::string inputName;
    69   std::string outputName;
    70   std::string typeName;
    71 
    72   bool help = false;
    73   bool version = false;
    74 
    75   for (int arg = 1; arg < argc; ++arg) {
    76     if (strcmp(argv[arg], "--type") == 0 || 
    77 	strcmp(argv[arg], "-t") == 0) {
    78       if (!typeName.empty()) {
    79 	cerr << "Multiple type description" << endl;
    80 	return -1;
    81       }
    82       if (arg + 1 == argc) {
    83 	cerr << "Parameter without value" << endl;
    84 	return -1;
    85       }
    86       typeName = argv[++arg];
    87     }
    88     else if (strcmp(argv[arg], "--input") == 0 || 
    89 	     strcmp(argv[arg], "-i") == 0) {
    90       if (!inputName.empty()) {
    91 	cerr << "Multiple input description" << endl;
    92 	return -1;
    93       }
    94       if (arg + 1 == argc) {
    95 	cerr << "Parameter without value" << endl;
    96 	return -1;
    97       }
    98       inputName = argv[++arg];
    99     }
   100     else if (strcmp(argv[arg], "--output") == 0 || 
   101 	     strcmp(argv[arg], "-o") == 0) {
   102       if (!outputName.empty()) {
   103 	cerr << "Multiple input description" << endl;
   104 	return -1;
   105       }
   106       if (arg + 1 == argc) {
   107 	cerr << "Parameter without value" << endl;
   108 	return -1;
   109       }
   110       outputName = argv[++arg];
   111     } else if (strcmp(argv[arg], "--help") == 0 ||
   112 	       strcmp(argv[arg], "-h") == 0) {
   113       help = true;
   114     } else if (strcmp(argv[arg], "--version") == 0 ||
   115 	       strcmp(argv[arg], "-v") == 0) {
   116       version = true;
   117     } else {
   118       cerr << "Invalid option: " << argv[arg] << endl;
   119       return -1;
   120     }
   121   }
   122 
   123   if (version) {
   124     cout << versionString;
   125   }
   126   if (help) {
   127     cout << helpString;
   128   }
   129   if (help || version) {
   130     return 0;
   131   }
   132 
   133   ifstream input;
   134   if (!inputName.empty()) {
   135     input.open(inputName.c_str());
   136     if (!input) {
   137       cerr << "File open error" << endl;
   138       return -1;
   139     }
   140   }
   141   istream& is = (inputName.empty() ? cin : input);
   142 
   143   ofstream output;
   144   if (!outputName.empty()) {
   145     output.open(outputName.c_str());
   146     if (!output) {
   147       cerr << "File open error" << endl;
   148       return -1;
   149     }
   150   }
   151   ostream& os = (outputName.empty() ? cout : output);
   152 
   153   if (typeName.empty()) {
   154     typeName = "maxflow";
   155   }
   156 
   157   if (typeName == "mincostflow") {
   158     Graph graph;
   159     Node s, t;
   160     StringMap cost(graph), capacity(graph);
   161     readDimacs(is, graph, capacity, s, t, cost);
   162     writeGraph(os, graph, capacity, s, t, cost);
   163   } else if (typeName == "maxflow") {
   164     Graph graph;
   165     Node s, t;
   166     StringMap capacity(graph);
   167     readDimacs(is, graph, capacity, s, t);
   168     writeGraph(os, graph, capacity, s, t);
   169   } else if (typeName == "shortestpath") {
   170     Graph graph;
   171     Node s;
   172     StringMap capacity(graph);
   173     readDimacs(is, graph, capacity, s);
   174     writeGraph(os, graph, capacity, s);
   175   } else if (typeName == "capacitated") {
   176     Graph graph;
   177     StringMap capacity(graph);
   178     readDimacs(is, graph, capacity);
   179     writeGraph(os, graph, capacity);
   180   } else if (typeName == "plain") {
   181     Graph graph;
   182     readDimacs(is, graph);
   183     writeGraph(os, graph);
   184   } else {
   185     cerr << "Invalid type error" << endl;
   186     return -1;
   187   }
   188   return 0;
   189 }