Vaucanson  1.4.1
dumper.hxx
1 // dumper.hcc: this file is part of the Vaucanson project. -*- C++ -*-
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2005, 2006, 2007 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 
17 #ifndef VCSN_TOOLS_DUMPER_HXX
18 # define VCSN_TOOLS_DUMPER_HXX
19 
20 # include <vaucanson/tools/usual_io.hh>
22 # include <vaucanson/tools/dot_dump.hh>
24 # include <vaucanson/tools/simple_dump.hh>
25 # include <cstring>
26 # include <cstdlib>
27 # include <cerrno>
28 # include <string>
29 
30 namespace vcsn
31 {
32  namespace tools
33  {
34  int
35  string_to_int (const std::string& s)
36  {
37  // I don't know how to do that properly in C++.
38  errno = 0;
39  const char *ccp = s.c_str ();
40  char *cp;
41  long res = std::strtol (ccp, &cp, 10);
42  if (*cp || INT_MAX < res || errno)
43  {
44  std::cerr << "integer out of bounds: " << s;
45  if (errno)
46  std::cerr << " (" << std::strerror (errno) << ")";
47  std::cerr << std::endl;
48  exit (1);
49  }
50  return res;
51  }
52 
53  dumper::dumper (int argc, char **argv, int pos)
54  : fmt_ (fmt_xml), argc_ (argc), argv_ (argv)
55  {
56  if (pos < argc_)
57  fmt_ = dump_format (argv_[pos]);
58 
59  if (fmt_ == fmt_error)
60  {
61  std::cerr << "Invalid input: " << pos << " " << argv_[pos] << std::endl;
62  usage (1);
63  }
64  }
65 
66  void
67  dumper::usage (int estatus)
68  {
69  std::cerr << "Usage: " << argv_[0] << " ... <fmt>" << std::endl
70  << "where fmt is one of:" << std::endl
71  << " dot graphviz format" << std::endl
72  << " fsm FSM toolbox format" << std::endl
73  << " simple internal Vaucanson format" << std::endl
74  << " xml Vaucanson XML I/O format" << std::endl;
75  exit (estatus);
76  }
77 
78  enum dumper::dump_format
79  dumper::dump_format (std::string fmt)
80  {
81  if (fmt == "dot")
82  return fmt_dot;
83  else if (fmt == "simple")
84  return fmt_simple;
85  else if (fmt == "xml")
86  return fmt_xml;
87  else if (fmt == "fsm")
88  return fmt_fsm;
89  else
90  return fmt_error;
91  }
92 
93 
94  const char*
95  dumper::get_fmt() const
96  {
97  switch(fmt_)
98  {
99  case fmt_dot: return "dot";
100  case fmt_xml: return "xml";
101  case fmt_simple: return "simple";
102  case fmt_fsm: return "fsm";
103  case fmt_error: abort ();
104  }
105  return "unknown";
106  }
107 
108  void
109  dumper::operator() (std::ostream& o,
110  const automaton_t& automaton,
111  const std::string& name)
112  {
113  switch(fmt_)
114  {
115  case fmt_dot:
116  vcsn::tools::dot_dump (o, automaton, name);
117  break;
118  case fmt_xml:
119  vcsn::tools::xml_dump (o, automaton, name);
120  break;
121  case fmt_simple:
122  vcsn::tools::simple_dump (o, automaton,
123  vcsn::tools::usual_converter_poly<rat_exp_t>());
124  break;
125  case fmt_fsm:
126  vcsn::tools::fsm_dump (o, automaton);
127  break;
128  case fmt_error:
129  abort ();
130  break;
131  }
132  }
133 
134  }
135 }
136 
137 #endif // !VCSN_TOOLS_DUMPER_HXX