-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbasicfileinfo.h
160 lines (138 loc) · 4.39 KB
/
basicfileinfo.h
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
#ifndef TAG_PARSER_BASICFILEINFO_H
#define TAG_PARSER_BASICFILEINFO_H
#include "./global.h"
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/io/nativefilestream.h>
#include <cstdint>
#include <string>
namespace TagParser {
class TAG_PARSER_EXPORT BasicFileInfo {
public:
// constructor, destructor
explicit BasicFileInfo();
explicit BasicFileInfo(std::string &&path);
explicit BasicFileInfo(std::string_view path);
BasicFileInfo(const BasicFileInfo &) = delete;
BasicFileInfo &operator=(const BasicFileInfo &) = delete;
virtual ~BasicFileInfo();
// methods to control associated file stream
void open(bool readOnly = false);
void reopen(bool readOnly = false);
bool isOpen() const;
bool isReadOnly() const;
void close();
void invalidate();
CppUtilities::NativeFileStream &stream();
const CppUtilities::NativeFileStream &stream() const;
// methods to get, set path (components)
const std::string &path() const;
void setPath(std::string_view path);
void setPath(std::string &&path);
static std::string fileName(std::string_view path, bool cutExtension = false);
std::string fileName(bool cutExtension = false) const;
static std::string extension(std::string_view path);
std::string extension() const;
static std::string pathWithoutExtension(std::string_view fullPath);
std::string pathWithoutExtension() const;
static std::string containingDirectory(std::string_view path);
std::string containingDirectory() const;
static std::string_view pathForOpen(std::string_view url);
// methods to get, set the file size
std::uint64_t size() const;
void reportSizeChanged(std::uint64_t newSize);
void reportPathChanged(std::string_view newPath);
void reportPathChanged(std::string &&newPath);
protected:
virtual void invalidated();
private:
std::string m_path;
CppUtilities::NativeFileStream m_file;
std::uint64_t m_size;
bool m_readOnly;
};
/*!
* \brief Indicates whether a std::fstream is open for the current file.
*
* \sa stream()
*/
inline bool BasicFileInfo::isOpen() const
{
return m_file.is_open();
}
/*!
* \brief Indicates whether the last open()/reopen() call was read-only.
*/
inline bool BasicFileInfo::isReadOnly() const
{
return m_readOnly;
}
/*!
* \brief Returns the std::fstream for the current instance.
*/
inline CppUtilities::NativeFileStream &BasicFileInfo::stream()
{
return m_file;
}
/*!
* \brief Returns the std::fstream for the current instance.
*/
inline const CppUtilities::NativeFileStream &BasicFileInfo::stream() const
{
return m_file;
}
/*!
* \brief Returns the path of the current file.
*
* \sa setPath()
*/
inline const std::string &BasicFileInfo::path() const
{
return m_path;
}
/*!
* \brief Returns size of the current file in bytes.
* \remarks The file needs to be opened. Otherwise zero or the size of the
* previously opened file is returned.
* The size is not automatically updated when the file is modified.
* You might update the size using the reportSizeChanged() method.
*/
inline std::uint64_t BasicFileInfo::size() const
{
return m_size;
}
/*!
* \brief Call this function to report that the size changed.
* \remarks Should be called after writing/truncating the stream().
*/
inline void BasicFileInfo::reportSizeChanged(std::uint64_t newSize)
{
m_size = newSize;
}
/*!
* \brief Call this function to report that the path changed.
* \remarks Should be called after associating another file to the stream() manually.
*/
inline void BasicFileInfo::reportPathChanged(std::string_view newPath)
{
m_path = newPath;
}
/*!
* \brief Call this function to report that the path changed.
* \remarks Should be called after associating another file to the stream() manually.
*/
inline void BasicFileInfo::reportPathChanged(std::string &&newPath)
{
m_path = std::move(newPath);
}
/*!
* \brief Returns removes the "file:/" prefix from \a url to be able to pass it to functions
* like open(), stat() and truncate().
* \remarks If \a url is already a plain path it won't changed.
* \returns Returns a pointer the URL data itself. No copy is made.
*/
inline std::string_view BasicFileInfo::pathForOpen(std::string_view url)
{
return CppUtilities::startsWith(url, "file:/") ? url.data() + 6 : url.data();
}
} // namespace TagParser
#endif // TAG_PARSER_BASICFILEINFO_H