mCRL2
Loading...
Searching...
No Matches
file_utility.h
Go to the documentation of this file.
1// Author(s): Sjoerd Cranen
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//
11
12#ifndef MCRL2_UTILITIES_FILE_UTILITY_H
13#define MCRL2_UTILITIES_FILE_UTILITY_H
14
17#include <fstream>
18
19namespace mcrl2
20{
21
22namespace utilities
23{
24
26inline
27bool has_extension(const std::string& filename, const std::string& extension)
28{
29 assert(extension.size()>=1 && extension[0]!='.'); // The extension should not start with a dot and consist out of at least one character.
30 std::string dotted_extension="."+extension;
31 if (filename.size()<dotted_extension.size())
32 {
33 return false;
34 }
35 const std::string filename_extension = filename.substr(filename.size()-dotted_extension.size());
36 return filename_extension==dotted_extension;
37}
38
40{
41 protected:
42 std::string m_shortname;
43 std::string m_description;
45 std::vector<std::string> m_extensions;
46
47 public:
49 : file_format("unknown", "Unknown format", false)
50 {
51 }
52
53 file_format(const std::string& shortname, const std::string& description, bool is_text_format)
55 { }
56
57 void add_extension(const std::string& ext)
58 {
59 m_extensions.push_back(ext);
60 }
61
62 const std::string& shortname() const { return m_shortname; }
63
64 const std::string& description() const { return m_description; }
65
66 bool text_format() const { return m_text_format; }
67
68 bool matches(const std::string& filename) const
69 {
70 for (const std::string& ext: m_extensions)
71 {
72 if (has_extension(filename,ext))
73 {
74 return true;
75 }
76 }
77 return false;
78 }
79
80 bool operator==(const file_format& other) const
81 {
82 return m_shortname == other.m_shortname;
83 }
84
85 bool operator<(const file_format& other) const
86 {
87 return m_shortname < other.m_shortname;
88 }
89};
90
91inline
92std::ostream& operator<<(std::ostream& stream, const file_format& format)
93{
94 return stream << format.shortname();
95}
96
97inline
98bool file_exists(const std::string& filename)
99{
100 if (FILE * file = fopen(filename.c_str(), "r"))
101 {
102 fclose(file);
103 return true;
104 }
105 return false;
106}
107
108} // namespace utilities
109
110} // namespace mcrl2
111
112#endif // MCRL2_UTILITIES_FILE_UTILITY_H
file_format(const std::string &shortname, const std::string &description, bool is_text_format)
std::vector< std::string > m_extensions
const std::string & shortname() const
void add_extension(const std::string &ext)
bool matches(const std::string &filename) const
bool operator==(const file_format &other) const
bool operator<(const file_format &other) const
const std::string & description() const
Exception classes for use in libraries and tools.
bool has_extension(const std::string &filename, const std::string &extension)
Returns true if the given filename has the extension ext. The extension does not start with a dot.
bool file_exists(const std::string &filename)
std::ostream & operator<<(std::ostream &ss, const big_natural_number &l)
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72