Vaucanson 1.4
dumper.hxx
00001 // dumper.hcc: this file is part of the Vaucanson project.   -*- C++ -*-
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005, 2006, 2007 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 
00017 #ifndef VCSN_TOOLS_DUMPER_HXX
00018 # define VCSN_TOOLS_DUMPER_HXX
00019 
00020 # include <vaucanson/tools/usual_io.hh>
00021 # include <vaucanson/tools/xml_dump.hh>
00022 # include <vaucanson/tools/dot_dump.hh>
00023 # include <vaucanson/tools/fsm_dump.hh>
00024 # include <vaucanson/tools/simple_dump.hh>
00025 # include <cstring>
00026 # include <cstdlib>
00027 # include <cerrno>
00028 # include <string>
00029 
00030 namespace vcsn
00031 {
00032   namespace tools
00033   {
00034     int
00035     string_to_int (const std::string& s)
00036     {
00037       // I don't know how to do that properly in C++.
00038       errno = 0;
00039       const char *ccp = s.c_str ();
00040       char *cp;
00041       long res = std::strtol (ccp, &cp, 10);
00042       if (*cp || INT_MAX < res || errno)
00043       {
00044         std::cerr << "integer out of bounds: " << s;
00045         if (errno)
00046           std::cerr << " (" << std::strerror (errno) << ")";
00047         std::cerr << std::endl;
00048         exit (1);
00049       }
00050       return res;
00051     }
00052 
00053     dumper::dumper (int argc, char **argv, int pos)
00054       : fmt_ (fmt_xml), argc_ (argc), argv_ (argv)
00055     {
00056       if (pos < argc_)
00057         fmt_ = dump_format (argv_[pos]);
00058 
00059       if (fmt_ == fmt_error)
00060       {
00061         std::cerr << "Invalid input: " << pos << " " << argv_[pos] << std::endl;
00062         usage (1);
00063       }
00064     }
00065 
00066     void
00067     dumper::usage (int estatus)
00068     {
00069       std::cerr << "Usage: " << argv_[0] << " ... <fmt>" << std::endl
00070                 << "where fmt is one of:" << std::endl
00071                 << "  dot     graphviz format" << std::endl
00072                 << "  fsm     FSM toolbox format" << std::endl
00073                 << "  simple  internal Vaucanson format" << std::endl
00074                 << "  xml     Vaucanson XML I/O format" << std::endl;
00075       exit (estatus);
00076     }
00077 
00078     enum dumper::dump_format
00079     dumper::dump_format (std::string fmt)
00080     {
00081       if (fmt == "dot")
00082         return fmt_dot;
00083       else if (fmt == "simple")
00084         return fmt_simple;
00085       else if (fmt == "xml")
00086         return fmt_xml;
00087       else if (fmt == "fsm")
00088         return fmt_fsm;
00089       else
00090         return fmt_error;
00091     }
00092 
00093 
00094     const char*
00095     dumper::get_fmt() const
00096     {
00097       switch(fmt_)
00098         {
00099         case fmt_dot: return "dot";
00100         case fmt_xml: return "xml";
00101         case fmt_simple: return "simple";
00102         case fmt_fsm: return "fsm";
00103         case fmt_error: abort ();
00104         }
00105       return "unknown";
00106     }
00107 
00108     void
00109     dumper::operator() (std::ostream& o,
00110                         const automaton_t& automaton,
00111                         const std::string& name)
00112     {
00113       switch(fmt_)
00114         {
00115         case fmt_dot:
00116           vcsn::tools::dot_dump (o, automaton, name);
00117           break;
00118         case fmt_xml:
00119           vcsn::tools::xml_dump (o, automaton, name);
00120           break;
00121         case fmt_simple:
00122           vcsn::tools::simple_dump (o, automaton,
00123                                     vcsn::tools::usual_converter_poly<rat_exp_t>());
00124           break;
00125         case fmt_fsm:
00126           vcsn::tools::fsm_dump (o, automaton);
00127           break;
00128         case fmt_error:
00129           abort ();
00130           break;
00131         }
00132     }
00133 
00134   }
00135 }
00136 
00137 #endif // !VCSN_TOOLS_DUMPER_HXX