/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

#ident "$Project: polymake $$Id: poly2leda_graph.cc,v 1.2 2004/06/22 16:22:36 gawrilow Exp $"

#include <Poly.h>
#include <Graph.h>
#include <argv_options.h>

namespace polymake { namespace graph {

namespace {

template <typename Traits, typename _Graph>
void print_LEDA_graph(std::basic_ostream<char, Traits>& os, const _Graph& G) {
   const int nodes=G.nodes();
   os << "LEDA.GRAPH\n\n\n" << nodes << endl;
   for (int i=0; i<nodes; ++i)
      os << "|{}|\n";
   os << edges(G).size() << endl;
   for (typename Entire< Edges<_Graph> >::const_iterator e=entire(edges(G)); !e.at_end(); ++e)
      os << e.from_node() << " " << e.to_node() << " 0 |{}|\n";
}

}

template <typename Traits>
void poly2leda_graph(Poly& p, const char* section, bool dir, std::basic_ostream<char, Traits>& os)
{
   if (dir) {
      const Graph<nothing,nothing,directed> G=p.give(section);
      print_LEDA_graph(os,G);
   } else {
      const Graph<nothing,nothing,undirected> G=p.give(section);
      print_LEDA_graph(os,G);
   }
}

} }

using namespace polymake;

/** @file poly2leda_graph
 *
 *  Convert a polymake graph into a leda graph, write the latter to standard output.
 *
 *  @synopsis poly2leda_graph <file> <graph_section> { -directed, -undirected }
 *
 *  @index utilities
 */

int main(int argc, const char *argv[]) {
   argv_option options[]={ { "-directed" }, { "-undirected" } };
   enum { dir, undir };
   if (argc != 4 || !extract_options(argc,argv,2,options) || argc != 3) {
      cerr << "usage: " << argv[0] << " <file> <graph_section> { -directed, -undirected } \n";
      return 1;
   }
   try {
      Poly p(argv[1],ios::in);
      graph::poly2leda_graph(p, argv[2], options[dir], std::cout);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
