Tag Parser  7.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 ConversionUtilities;
7 
19 namespace TagParser {
20 
26 BasicFileInfo::BasicFileInfo(const std::string &path)
27  : m_path(path)
28  , m_size(0)
29  , m_readOnly(false)
30 {
31  m_file.exceptions(ios_base::failbit | ios_base::badbit);
32 }
33 
40 {
41  close();
42 }
43 
49 void BasicFileInfo::open(bool readOnly)
50 {
51  if (!isOpen()) {
52  reopen(readOnly);
53  }
54 }
55 
62 void BasicFileInfo::reopen(bool readOnly)
63 {
64  invalidated();
65  m_file.open(startsWith(m_path, "file:/") ? m_path.data() + 6 : m_path.data(),
66  (m_readOnly = readOnly) ? ios_base::in | ios_base::binary : ios_base::in | ios_base::out | ios_base::binary);
67  m_file.seekg(0, ios_base::end);
68  m_size = static_cast<uint64>(m_file.tellg());
69  m_file.seekg(0, ios_base::beg);
70 }
71 
76 {
77  if (isOpen()) {
78  m_file.close();
79  }
80  m_file.clear();
81 }
82 
87 {
88  invalidated();
89 }
90 
98 void BasicFileInfo::setPath(const string &path)
99 {
100  if (path != m_path) {
101  invalidated();
102  m_path = path;
103  }
104 }
105 
112 string BasicFileInfo::fileName(const string &path, bool cutExtension)
113 {
114  size_t lastSlash = path.rfind('/');
115  size_t lastBackSlash = path.rfind('\\');
116  size_t lastPoint = cutExtension ? path.rfind('.') : string::npos;
117  size_t lastSeparator;
118  if (lastSlash == string::npos && lastBackSlash == string::npos) {
119  return (lastPoint == string::npos) ? path : path.substr(0, lastPoint);
120  } else if (lastSlash == string::npos) {
121  lastSeparator = lastBackSlash;
122  } else if (lastBackSlash == string::npos) {
123  lastSeparator = lastSlash;
124  } else {
125  lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
126  }
127  return (lastPoint != string::npos) ? path.substr(lastSeparator + 1, lastPoint - lastSeparator - 1) : path.substr(lastSeparator + 1);
128 }
129 
135 string BasicFileInfo::fileName(bool cutExtension) const
136 {
137  return fileName(m_path, cutExtension);
138 }
139 
145 string BasicFileInfo::extension(const string &path)
146 {
147  size_t lastPoint = path.rfind('.');
148  if (lastPoint == string::npos) {
149  return string();
150  } else {
151  return path.substr(lastPoint);
152  }
153 }
154 
159 {
160  return extension(m_path);
161 }
162 
166 string BasicFileInfo::pathWithoutExtension(const string &fullPath)
167 {
168  size_t lastPoint = fullPath.rfind('.');
169  return lastPoint != string::npos ? fullPath.substr(0, lastPoint) : fullPath;
170 }
171 
176 {
177  return pathWithoutExtension(m_path);
178 }
179 
183 string BasicFileInfo::containingDirectory(const string &path)
184 {
185  size_t lastSlash = path.rfind('/');
186  size_t lastBackSlash = path.rfind('\\');
187  size_t lastSeparator;
188  if (lastSlash == string::npos && lastBackSlash == string::npos) {
189  return string();
190  } else if (lastSlash == string::npos) {
191  lastSeparator = lastBackSlash;
192  } else if (lastBackSlash == string::npos) {
193  lastSeparator = lastSlash;
194  } else {
195  lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
196  }
197  if (lastSeparator > 0) {
198  return path.substr(0, lastSeparator);
199  } else {
200  return string();
201  }
202 }
203 
211 {
212  return containingDirectory(m_path);
213 }
214 
223 {
224  m_size = 0;
225  close();
226 }
227 
228 } // namespace TagParser
void setPath(const std::string &path)
Sets the current file.
void open(bool readOnly=false)
Opens a std::fstream for the current file.
std::string extension() const
Returns the extension of the current file.
const std::string & path() const
Returns the path of the current file.
Definition: basicfileinfo.h:97
STL namespace.
virtual void invalidated()
This function is called when the BasicFileInfo gets invalidated.
void invalidate()
Invalidates the file info manually.
std::string containingDirectory() const
Returns the path of the directory containing the current file.
static std::string fileName(const std::string &path, bool cutExtension=false)
Returns the file name of the given file.
virtual ~BasicFileInfo()
Destroys the BasicFileInfo.
void reopen(bool readonly=false)
Opens a std::fstream for the current file.
void close()
A possibly opened std::fstream will be closed.
std::string pathWithoutExtension() const
Returns the path of the current file without the extension/suffix.
bool isOpen() const
Indicates whether a std::fstream is open for the current file.
Definition: basicfileinfo.h:63