Tag Parser  10.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
basicfileinfo.cpp
Go to the documentation of this file.
1 #include "./basicfileinfo.h"
2 
3 #include <c++utilities/conversion/stringconversion.h>
4 
5 using namespace std;
6 using namespace CppUtilities;
7 
11 namespace TagParser {
23 BasicFileInfo::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 
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 
98 {
99  if (isOpen()) {
100  m_file.close();
101  }
102  m_file.clear();
103 }
104 
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 
149 std::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 
172 std::string BasicFileInfo::fileName(bool cutExtension) const
173 {
174  return fileName(m_path, cutExtension);
175 }
176 
182 std::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 
195 std::string BasicFileInfo::extension() const
196 {
197  return extension(m_path);
198 }
199 
203 std::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 
213 {
214  return pathWithoutExtension(m_path);
215 }
216 
220 std::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 
248 {
249  return containingDirectory(m_path);
250 }
251 
260 {
261  m_size = 0;
262  close();
263 }
264 
265 } // namespace TagParser
BasicFileInfo()
Constructs a new BasicFileInfo for the specified file.
std::string containingDirectory() const
Returns the path of the directory containing the current file.
static std::string fileName(std::string_view path, bool cutExtension=false)
Returns the file name of the given file.
void invalidate()
Invalidates the file info manually.
const std::string & path() const
Returns the path of the current file.
void reopen(bool readOnly=false)
Opens a std::fstream for the current file.
virtual void invalidated()
This function is called when the BasicFileInfo gets invalidated.
std::string pathWithoutExtension() const
Returns the path of the current file without the extension/suffix.
void open(bool readOnly=false)
Opens a std::fstream for the current file.
void close()
A possibly opened std::fstream will be closed.
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(),...
virtual ~BasicFileInfo()
Destroys the BasicFileInfo.
std::string extension() const
Returns the extension of the current file.
bool isOpen() const
Indicates whether a std::fstream is open for the current file.
Definition: basicfileinfo.h:69
void setPath(std::string_view path)
Sets the current file.
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10