Tag Parser: basicfileinfo.cpp Source File

Tag Parser 12.5.3 C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags

Loading...
Searching...
No Matches
basicfileinfo.cpp
Go to the documentation of this file.
1#include "./basicfileinfo.h"
2
3#include <c++utilities/conversion/stringconversion.h>
4
5using namespace std;
6using namespace CppUtilities;
7
11namespace TagParser {
17
23BasicFileInfo::BasicFileInfo()
24 : m_size(0)
25 , m_readOnly(false)
26{
27 m_file.exceptions(ios_base::failbit | ios_base::badbit);
28}
29
35 BasicFileInfo::BasicFileInfo(std::string &&path)
36 : m_path(std::move(path))
37 , m_size(0)
38 , m_readOnly(false)
39{
40 m_file.exceptions(ios_base::failbit | ios_base::badbit);
41}
42
48 BasicFileInfo::BasicFileInfo(std::string_view path)
49 : m_path(path)
50 , m_size(0)
51 , m_readOnly(false)
52{
53 m_file.exceptions(ios_base::failbit | ios_base::badbit);
54}
55
61BasicFileInfo::~BasicFileInfo()
62{
63 close();
64}
65
71 void BasicFileInfo::open(bool readOnly)
72{
73 if (!isOpen()) {
74 reopen(readOnly);
75 }
76}
77
84 void BasicFileInfo::reopen(bool readOnly)
85{
86 invalidated();
87 m_file.open(
88 pathForOpen(path()).data(), (m_readOnly = readOnly) ? ios_base::in | ios_base::binary : ios_base::in | ios_base::out | ios_base::binary);
89 m_file.seekg(0, ios_base::end);
90 m_size = static_cast<std::uint64_t>(m_file.tellg());
91 m_file.seekg(0, ios_base::beg);
92}
93
97 void BasicFileInfo::close()
98{
99 if (isOpen()) {
100 m_file.close();
101 }
102 m_file.clear();
103}
104
108 void BasicFileInfo::invalidate()
109{
110 invalidated();
111}
112
120 void BasicFileInfo::setPath(std::string_view path)
121{
122 if (path != m_path) {
123 invalidated();
124 m_path = path;
125 }
126}
127
135 void BasicFileInfo::setPath(std::string &&path)
136{
137 if (path != m_path) {
138 invalidated();
139 m_path = path;
140 }
141}
142
149std::string BasicFileInfo::fileName(std::string_view path, bool cutExtension)
150{
151 const auto lastSlash = path.rfind('/');
152 const auto lastBackSlash = path.rfind('\\');
153 const auto lastPoint = cutExtension ? path.rfind('.') : string::npos;
154 auto lastSeparator = decltype(lastSlash)();
155 if (lastSlash == string::npos && lastBackSlash == string::npos) {
156 return std::string(lastPoint == string::npos ? path : path.substr(0, lastPoint));
157 } else if (lastSlash == string::npos) {
158 lastSeparator = lastBackSlash;
159 } else if (lastBackSlash == string::npos) {
160 lastSeparator = lastSlash;
161 } else {
162 lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
163 }
164 return std::string(lastPoint != string::npos ? path.substr(lastSeparator + 1, lastPoint - lastSeparator - 1) : path.substr(lastSeparator + 1));
165}
166
172std::string BasicFileInfo::fileName(bool cutExtension) const
173{
174 return fileName(m_path, cutExtension);
175}
176
182std::string BasicFileInfo::extension(std::string_view path)
183{
184 const auto lastPoint = path.rfind('.');
185 if (lastPoint == std::string::npos) {
186 return std::string();
187 } else {
188 return std::string(path.data() + lastPoint, path.size() - lastPoint);
189 }
190}
191
195std::string BasicFileInfo::extension() const
196{
197 return extension(m_path);
198}
199
203std::string BasicFileInfo::pathWithoutExtension(std::string_view fullPath)
204{
205 const auto lastPoint = fullPath.rfind('.');
206 return std::string(lastPoint != std::string::npos ? fullPath.substr(0, lastPoint) : fullPath);
207}
208
212std::string BasicFileInfo::pathWithoutExtension() const
213{
214 return pathWithoutExtension(m_path);
215}
216
220std::string BasicFileInfo::containingDirectory(std::string_view path)
221{
222 const auto lastSlash = path.rfind('/');
223 const auto lastBackSlash = path.rfind('\\');
224 auto lastSeparator = decltype(lastSlash)();
225 if (lastSlash == string::npos && lastBackSlash == std::string::npos) {
226 return std::string();
227 } else if (lastSlash == string::npos) {
228 lastSeparator = lastBackSlash;
229 } else if (lastBackSlash == string::npos) {
230 lastSeparator = lastSlash;
231 } else {
232 lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
233 }
234 if (lastSeparator > 0) {
235 return std::string(path.substr(0, lastSeparator));
236 } else {
237 return std::string();
238 }
239}
240
247 string BasicFileInfo::containingDirectory() const
248{
249 return containingDirectory(m_path);
250}
251
259 void BasicFileInfo::invalidated()
260{
261 m_size = 0;
262 close();
263}
264
265} // namespace TagParser
basicfileinfo.h
TagParser::BasicFileInfo::BasicFileInfo
BasicFileInfo()
Constructs a new BasicFileInfo for the specified file.
Definition basicfileinfo.cpp:23
TagParser::BasicFileInfo::containingDirectory
std::string containingDirectory() const
Returns the path of the directory containing the current file.
Definition basicfileinfo.cpp:247
TagParser::BasicFileInfo::fileName
static std::string fileName(std::string_view path, bool cutExtension=false)
Returns the file name of the given file.
Definition basicfileinfo.cpp:149
TagParser::BasicFileInfo::invalidate
void invalidate()
Invalidates the file info manually.
Definition basicfileinfo.cpp:108
TagParser::BasicFileInfo::path
const std::string & path() const
Returns the path of the current file.
Definition basicfileinfo.h:103
TagParser::BasicFileInfo::reopen
void reopen(bool readOnly=false)
Opens a std::fstream for the current file.
Definition basicfileinfo.cpp:84
TagParser::BasicFileInfo::invalidated
virtual void invalidated()
This function is called when the BasicFileInfo gets invalidated.
Definition basicfileinfo.cpp:259
TagParser::BasicFileInfo::pathWithoutExtension
std::string pathWithoutExtension() const
Returns the path of the current file without the extension/suffix.
Definition basicfileinfo.cpp:212
TagParser::BasicFileInfo::open
void open(bool readOnly=false)
Opens a std::fstream for the current file.
Definition basicfileinfo.cpp:71
TagParser::BasicFileInfo::close
void close()
A possibly opened std::fstream will be closed.
Definition basicfileinfo.cpp:97
TagParser::BasicFileInfo::pathForOpen
static std::string_view pathForOpen(std::string_view url)
Returns removes the "file:/" prefix from url to be able to pass it to functions like open(),...
Definition basicfileinfo.h:153
TagParser::BasicFileInfo::~BasicFileInfo
virtual ~BasicFileInfo()
Destroys the BasicFileInfo.
Definition basicfileinfo.cpp:61
TagParser::BasicFileInfo::extension
std::string extension() const
Returns the extension of the current file.
Definition basicfileinfo.cpp:195
TagParser::BasicFileInfo::isOpen
bool isOpen() const
Indicates whether a std::fstream is open for the current file.
Definition basicfileinfo.h:69
TagParser::BasicFileInfo::setPath
void setPath(std::string_view path)
Sets the current file.
Definition basicfileinfo.cpp:120
CppUtilities
Definition abstractcontainer.h:16
TagParser
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10