-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbasicfileinfo.cpp
265 lines (242 loc) · 7.14 KB
/
basicfileinfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "./basicfileinfo.h"
#include <c++utilities/conversion/stringconversion.h>
using namespace std;
using namespace CppUtilities;
/*!
* \brief Contains all classes and functions of the TagInfo library.
*/
namespace TagParser {
/*!
* \class BasicFileInfo
* \brief The BasicFileInfo class provides basic file information such as
* file name, extension, directory and size for a specified file.
*/
/*!
* \brief Constructs a new BasicFileInfo for the specified file.
*
* \param path Specifies the absolute or relative path of the file.
*/
BasicFileInfo::BasicFileInfo()
: m_size(0)
, m_readOnly(false)
{
m_file.exceptions(ios_base::failbit | ios_base::badbit);
}
/*!
* \brief Constructs a new BasicFileInfo for the specified file.
*
* \param path Specifies the absolute or relative path of the file.
*/
BasicFileInfo::BasicFileInfo(std::string &&path)
: m_path(std::move(path))
, m_size(0)
, m_readOnly(false)
{
m_file.exceptions(ios_base::failbit | ios_base::badbit);
}
/*!
* \brief Constructs a new BasicFileInfo for the specified file.
*
* \param path Specifies the absolute or relative path of the file.
*/
BasicFileInfo::BasicFileInfo(std::string_view path)
: m_path(path)
, m_size(0)
, m_readOnly(false)
{
m_file.exceptions(ios_base::failbit | ios_base::badbit);
}
/*!
* \brief Destroys the BasicFileInfo.
*
* A possibly opened std::fstream will be closed.
*/
BasicFileInfo::~BasicFileInfo()
{
close();
}
/*!
* \brief Opens a std::fstream for the current file. Does nothing a stream is already open.
* \param readOnly Indicates whether the stream should be opend as read-only.
* \throws Throws std::ios_base::failure when an IO error occurs.
*/
void BasicFileInfo::open(bool readOnly)
{
if (!isOpen()) {
reopen(readOnly);
}
}
/*!
* \brief Opens a std::fstream for the current file. Closes a possibly already opened stream and
* clears all flags before.
* \param readOnly Indicates whether the stream should be opend as read-only.
* \throws Throws std::ios_base::failure when an IO error occurs.
*/
void BasicFileInfo::reopen(bool readOnly)
{
invalidated();
m_file.open(
pathForOpen(path()).data(), (m_readOnly = readOnly) ? ios_base::in | ios_base::binary : ios_base::in | ios_base::out | ios_base::binary);
m_file.seekg(0, ios_base::end);
m_size = static_cast<std::uint64_t>(m_file.tellg());
m_file.seekg(0, ios_base::beg);
}
/*!
* \brief A possibly opened std::fstream will be closed. All flags of the stream will be cleared.
*/
void BasicFileInfo::close()
{
if (isOpen()) {
m_file.close();
}
m_file.clear();
}
/*!
* \brief Invalidates the file info manually.
*/
void BasicFileInfo::invalidate()
{
invalidated();
}
/*!
* \brief Sets the current file.
*
* A possibly opened std::fstream will be closed and invalidated() will be called.
*
* \param path Specifies the absolute or relative path of the file to be set.
*/
void BasicFileInfo::setPath(std::string_view path)
{
if (path != m_path) {
invalidated();
m_path = path;
}
}
/*!
* \brief Sets the current file.
*
* A possibly opened std::fstream will be closed and invalidated() will be called.
*
* \param path Specifies the absolute or relative path of the file to be set.
*/
void BasicFileInfo::setPath(std::string &&path)
{
if (path != m_path) {
invalidated();
m_path = path;
}
}
/*!
* \brief Returns the file name of the given file.
*
* \param path Specifies the path of the file.
* \param cutExtension Indicates whether the extension/suffix should be cut.
*/
std::string BasicFileInfo::fileName(std::string_view path, bool cutExtension)
{
const auto lastSlash = path.rfind('/');
const auto lastBackSlash = path.rfind('\\');
const auto lastPoint = cutExtension ? path.rfind('.') : string::npos;
auto lastSeparator = decltype(lastSlash)();
if (lastSlash == string::npos && lastBackSlash == string::npos) {
return std::string(lastPoint == string::npos ? path : path.substr(0, lastPoint));
} else if (lastSlash == string::npos) {
lastSeparator = lastBackSlash;
} else if (lastBackSlash == string::npos) {
lastSeparator = lastSlash;
} else {
lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
}
return std::string(lastPoint != string::npos ? path.substr(lastSeparator + 1, lastPoint - lastSeparator - 1) : path.substr(lastSeparator + 1));
}
/*!
* \brief Returns the file name of the current file.
*
* \param cutExtension Indicates whether the extension should be cut.
*/
std::string BasicFileInfo::fileName(bool cutExtension) const
{
return fileName(m_path, cutExtension);
}
/*!
* \brief Returns the extension of the given file.
*
* \param path Specifies the path of the file.
*/
std::string BasicFileInfo::extension(std::string_view path)
{
const auto lastPoint = path.rfind('.');
if (lastPoint == std::string::npos) {
return std::string();
} else {
return std::string(path.data() + lastPoint, path.size() - lastPoint);
}
}
/*!
* \brief Returns the extension of the current file.
*/
std::string BasicFileInfo::extension() const
{
return extension(m_path);
}
/*!
* \brief Returns a copy of the given path without the extension/suffix.
*/
std::string BasicFileInfo::pathWithoutExtension(std::string_view fullPath)
{
const auto lastPoint = fullPath.rfind('.');
return std::string(lastPoint != std::string::npos ? fullPath.substr(0, lastPoint) : fullPath);
}
/*!
* \brief Returns the path of the current file without the extension/suffix.
*/
std::string BasicFileInfo::pathWithoutExtension() const
{
return pathWithoutExtension(m_path);
}
/*!
* \brief Returns the path of the directory containing the given file.
*/
std::string BasicFileInfo::containingDirectory(std::string_view path)
{
const auto lastSlash = path.rfind('/');
const auto lastBackSlash = path.rfind('\\');
auto lastSeparator = decltype(lastSlash)();
if (lastSlash == string::npos && lastBackSlash == std::string::npos) {
return std::string();
} else if (lastSlash == string::npos) {
lastSeparator = lastBackSlash;
} else if (lastBackSlash == string::npos) {
lastSeparator = lastSlash;
} else {
lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
}
if (lastSeparator > 0) {
return std::string(path.substr(0, lastSeparator));
} else {
return std::string();
}
}
/*!
* \brief Returns the path of the directory containing the current file.
*
* The returned path is relative if the path of the file (specified using
* the setPath() method) is relative.
*/
string BasicFileInfo::containingDirectory() const
{
return containingDirectory(m_path);
}
/*!
* \brief This function is called when the BasicFileInfo gets invalidated.
* This is the case when the current file changes or is reopened.
*
* When subclassing and overwriting this virtual method invoke the base
* implementation by calling BasicFileInfo::invalidated() before the reimplemented code.
*/
void BasicFileInfo::invalidated()
{
m_size = 0;
close();
}
} // namespace TagParser