Tag Parser  8.2.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 
11 namespace TagParser {
23 BasicFileInfo::BasicFileInfo(const std::string &path)
24  : m_path(path)
25  , m_size(0)
26  , m_readOnly(false)
27 {
28  m_file.exceptions(ios_base::failbit | ios_base::badbit);
29 }
30 
37 {
38  close();
39 }
40 
46 void BasicFileInfo::open(bool readOnly)
47 {
48  if (!isOpen()) {
49  reopen(readOnly);
50  }
51 }
52 
59 void BasicFileInfo::reopen(bool readOnly)
60 {
61  invalidated();
62  m_file.open(startsWith(m_path, "file:/") ? m_path.data() + 6 : m_path.data(),
63  (m_readOnly = readOnly) ? ios_base::in | ios_base::binary : ios_base::in | ios_base::out | ios_base::binary);
64  m_file.seekg(0, ios_base::end);
65  m_size = static_cast<uint64>(m_file.tellg());
66  m_file.seekg(0, ios_base::beg);
67 }
68 
73 {
74  if (isOpen()) {
75  m_file.close();
76  }
77  m_file.clear();
78 }
79 
84 {
85  invalidated();
86 }
87 
95 void BasicFileInfo::setPath(const string &path)
96 {
97  if (path != m_path) {
98  invalidated();
99  m_path = path;
100  }
101 }
102 
109 string BasicFileInfo::fileName(const string &path, bool cutExtension)
110 {
111  size_t lastSlash = path.rfind('/');
112  size_t lastBackSlash = path.rfind('\\');
113  size_t lastPoint = cutExtension ? path.rfind('.') : string::npos;
114  size_t lastSeparator;
115  if (lastSlash == string::npos && lastBackSlash == string::npos) {
116  return (lastPoint == string::npos) ? path : path.substr(0, lastPoint);
117  } else if (lastSlash == string::npos) {
118  lastSeparator = lastBackSlash;
119  } else if (lastBackSlash == string::npos) {
120  lastSeparator = lastSlash;
121  } else {
122  lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
123  }
124  return (lastPoint != string::npos) ? path.substr(lastSeparator + 1, lastPoint - lastSeparator - 1) : path.substr(lastSeparator + 1);
125 }
126 
132 string BasicFileInfo::fileName(bool cutExtension) const
133 {
134  return fileName(m_path, cutExtension);
135 }
136 
142 string BasicFileInfo::extension(const string &path)
143 {
144  size_t lastPoint = path.rfind('.');
145  if (lastPoint == string::npos) {
146  return string();
147  } else {
148  return path.substr(lastPoint);
149  }
150 }
151 
156 {
157  return extension(m_path);
158 }
159 
163 string BasicFileInfo::pathWithoutExtension(const string &fullPath)
164 {
165  size_t lastPoint = fullPath.rfind('.');
166  return lastPoint != string::npos ? fullPath.substr(0, lastPoint) : fullPath;
167 }
168 
173 {
174  return pathWithoutExtension(m_path);
175 }
176 
180 string BasicFileInfo::containingDirectory(const string &path)
181 {
182  size_t lastSlash = path.rfind('/');
183  size_t lastBackSlash = path.rfind('\\');
184  size_t lastSeparator;
185  if (lastSlash == string::npos && lastBackSlash == string::npos) {
186  return string();
187  } else if (lastSlash == string::npos) {
188  lastSeparator = lastBackSlash;
189  } else if (lastBackSlash == string::npos) {
190  lastSeparator = lastSlash;
191  } else {
192  lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
193  }
194  if (lastSeparator > 0) {
195  return path.substr(0, lastSeparator);
196  } else {
197  return string();
198  }
199 }
200 
208 {
209  return containingDirectory(m_path);
210 }
211 
220 {
221  m_size = 0;
222  close();
223 }
224 
225 } // 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
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
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9