mCRL2
Loading...
Searching...
No Matches
liblts.cpp
Go to the documentation of this file.
1// Author(s): Muck van Weerdenburg
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9/// \file liblts.cpp
10
11#include <array>
12
13#include "mcrl2/lts/lts_algorithm.h"
14#include "mcrl2/lts/lts_io.h"
15
16using namespace mcrl2::core;
17using namespace mcrl2::core::detail;
18using namespace mcrl2::log;
19
20
21
22
23namespace mcrl2::lts::detail
24{
25
26lts_type guess_format(std::string const& s, const bool be_verbose/*=true*/)
27{
28 std::string::size_type pos = s.find_last_of('.');
29
30 if (pos != std::string::npos)
31 {
32 std::string ext = s.substr(pos+1);
33
34 if (ext == "aut")
35 {
36 if (be_verbose)
37 {
38 mCRL2log(log_level_t::verbose) << "Detected .aut extension.\n";
39 }
40 return lts_type::lts_aut;
41 }
42 else if (ext == "lts")
43 {
44 if (be_verbose)
45 {
46 mCRL2log(log_level_t::verbose) << "Detected .lts extension.\n";
47 }
48 return lts_type::lts_lts;
49 }
50 else if (ext == "fsm")
51 {
52 if (be_verbose)
53 {
54 mCRL2log(log_level_t::verbose) << "Detected .fsm extension.\n";
55 }
56 return lts_type::lts_fsm;
57 }
58 else if (ext == "dot")
59 {
60 if (be_verbose)
61 {
62 mCRL2log(log_level_t::verbose) << "Detected .dot extension.\n";
63 }
64 return lts_type::lts_dot;
65 }
66 }
67
68 return lts_type::lts_none;
69}
70
71static const std::array<std::string, 5> type_strings = { "unknown", "lts", "aut", "fsm", "dot" };
72
73static const std::array<std::string, 5> extension_strings = { "", "lts", "aut", "fsm", "dot" };
74
75static const std::array<std::string, 5> type_desc_strings = {
76 "unknown LTS format",
77 "mCRL2 LTS format",
78 "Aldebaran format (CADP)",
79 "Finite State Machine format",
80 "GraphViz format (no longer supported as input format)"
81 };
82
83
84static const std::array<std::string, 5> mime_type_strings = { "",
85 "application/lts",
86 "text/aut",
87 "text/fsm",
88 "text/dot"
89 };
90
91lts_type parse_format(std::string const& s)
92{
93 if (s == "lts")
94 {
95 return lts_type::lts_lts;
96 }
97 else if (s == "aut")
98 {
99 return lts_type::lts_aut;
100 }
101 else if (s == "fsm")
102 {
103 return lts_type::lts_fsm;
104 }
105 else if (s == "dot")
106 {
107 return lts_type::lts_dot;
108 }
109 return lts_type::lts_none;
110}
111
113{
114 return (type_strings.at(static_cast<std::size_t>(type)));
115}
116
118{
119 return (extension_strings.at(static_cast<std::size_t>(type)));
120}
121
123{
124 return (mime_type_strings.at(static_cast<std::size_t>(type)));
125}
126
128{
129 static std::set<lts_type> s;
130 for (std::size_t i = static_cast<std::size_t>(lts_type::lts_type_min); i<1+static_cast<std::size_t>(lts_type::lts_type_max); ++i)
131 {
132 if (lts_type::lts_none != (lts_type) i)
133 {
134 s.insert((lts_type) i);
135 }
136 }
137 return s;
138}
140{
141 static const std::set<lts_type>& s = initialise_supported_lts_formats();
142 return s;
143}
144
145/* Auxiliary function, used below */
146template<std::size_t Size, typename T>
147bool lts_named_cmp(const std::array<std::string, Size>& N, T a, T b)
148{
149 return N[static_cast<std::size_t>(a)] < N[static_cast<std::size_t>(b)];
150}
151
153{
154 std::vector<lts_type> types(supported.begin(), supported.end());
155 std::sort(types.begin(),
156 types.end(),
157 [](const lts_type& t1, const lts_type& t2){ return lts_named_cmp(type_strings, t1, t2); });
158
159 std::string r;
160 for (std::vector<lts_type>::iterator i=types.begin(); i!=types.end(); ++i)
161 {
162 r += " '" + type_strings.at(static_cast<std::size_t>(*i)) + "' for the " + type_desc_strings.at(static_cast<std::size_t>(*i));
163
164 if (*i == default_format)
165 {
166 r += " (default)";
167 }
168
169 // Append the separator that fits the position of this entry. The last entry
170 // gets none, the penultimate one gets ", or", the rest get a comma.
171 const std::size_t remaining = static_cast<std::size_t>(std::distance(i, types.end()));
172 if (remaining == 2)
173 {
174 r += ", or\n";
175 }
176 else if (remaining > 2)
177 {
178 r += ",\n";
179 }
180 }
181
182 return r;
183}
184
186{
187 return supported_lts_formats_text(lts_type::lts_none,supported);
188}
189
191{
192 std::vector<lts_type> types(supported.begin(), supported.end());
193 std::sort(types.begin(),
194 types.end(),
195 [](const lts_type& t1, const lts_type& t2){ return lts_named_cmp(extension_strings, t1, t2); });
196
197 std::string r;
198 std::string prev;
199 bool first = true;
200 for (auto & type : types)
201 {
202 if (extension_strings[static_cast<std::size_t>(type)] == prev) // avoid mentioning extensions more than once
203 {
204 continue;
205 }
206 if (first)
207 {
208 first = false;
209 }
210 else
211 {
212 r += sep;
213 }
214 r += "*." + extension_strings[static_cast<std::size_t>(type)];
215 prev = extension_strings[static_cast<std::size_t>(type)];
216 }
217
218 return r;
219}
220
222{
223 return lts_extensions_as_string(",",supported);
224}
225
226} // namespace mcrl2::lts::detail
227//lts
228//data
function object to compare two constln_t pointers based on their contents
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:435
lts_type guess_format(std::string const &s, const bool be_verbose)
Determines the LTS format from a filename by its extension.
Definition liblts.cpp:26
static const std::array< std::string, 5 > extension_strings
Definition liblts.cpp:73
std::string supported_lts_formats_text(const std::set< lts_type > &supported)
Gives a textual list describing supported LTS formats.
Definition liblts.cpp:185
std::string string_for_type(const lts_type type)
Gives a string representation of an LTS format.
Definition liblts.cpp:112
std::string mime_type_for_type(const lts_type type)
Gives the MIME type associated with an LTS format.
Definition liblts.cpp:122
lts_type parse_format(std::string const &s)
Determines the LTS format from a format specification string.
Definition liblts.cpp:91
static const std::array< std::string, 5 > type_strings
Definition liblts.cpp:71
std::string extension_for_type(const lts_type type)
Gives the filename extension associated with an LTS format.
Definition liblts.cpp:117
const std::set< lts_type > & supported_lts_formats()
Gives the set of all supported LTS formats.
Definition liblts.cpp:139
std::string lts_extensions_as_string(const std::set< lts_type > &supported)
Gives a list of extensions for supported LTS formats.
Definition liblts.cpp:221
std::string lts_extensions_as_string(const std::string &sep, const std::set< lts_type > &supported)
Gives a list of extensions for supported LTS formats.
Definition liblts.cpp:190
bool lts_named_cmp(const std::array< std::string, Size > &N, T a, T b)
Definition liblts.cpp:147
std::string supported_lts_formats_text(lts_type default_format, const std::set< lts_type > &supported)
Gives a textual list describing supported LTS formats.
Definition liblts.cpp:152
static const std::array< std::string, 5 > type_desc_strings
Definition liblts.cpp:75
static const std::array< std::string, 5 > mime_type_strings
Definition liblts.cpp:84
static const std::set< lts_type > & initialise_supported_lts_formats()
Definition liblts.cpp:127