Tag Parser  6.4.1
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 using namespace std;
4 
16 namespace Media {
17 
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(m_path, (m_readOnly = readOnly) ? ios_base::in | ios_base::binary : ios_base::in | ios_base::out | ios_base::binary);
63  m_file.seekg(0, ios_base::end);
64  m_size = static_cast<uint64>(m_file.tellg());
65  m_file.seekg(0, ios_base::beg);
66 }
67 
72 {
73  if(isOpen()) {
74  m_file.close();
75  }
76  m_file.clear();
77 }
78 
83 {
84  invalidated();
85 }
86 
94 void BasicFileInfo::setPath(const string &path)
95 {
96  if(path != m_path) {
97  invalidated();
98  m_path = path;
99  }
100 }
101 
108 string BasicFileInfo::fileName(const string &path, bool cutExtension)
109 {
110  size_t lastSlash = path.rfind('/');
111  size_t lastBackSlash = path.rfind('\\');
112  size_t lastPoint = cutExtension ? path.rfind('.') : string::npos;
113  size_t lastSeparator;
114  if(lastSlash == string::npos && lastBackSlash == string::npos) {
115  return (lastPoint == string::npos) ? path : path.substr(0, lastPoint);
116  } else if(lastSlash == string::npos) {
117  lastSeparator = lastBackSlash;
118  } else if(lastBackSlash == string::npos) {
119  lastSeparator = lastSlash;
120  } else {
121  lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
122  }
123  return (lastPoint != string::npos) ? path.substr(lastSeparator + 1, lastPoint - lastSeparator - 1) : path.substr(lastSeparator + 1);
124 }
125 
131 string BasicFileInfo::fileName(bool cutExtension) const
132 {
133  return fileName(m_path, cutExtension);
134 }
135 
141 string BasicFileInfo::extension(const string &path)
142 {
143  size_t lastPoint = path.rfind('.');
144  if(lastPoint == string::npos) {
145  return string();
146  } else {
147  return path.substr(lastPoint);
148  }
149 }
150 
155 {
156  return extension(m_path);
157 }
158 
162 string BasicFileInfo::pathWithoutExtension(const string &fullPath)
163 {
164  size_t lastPoint = fullPath.rfind('.');
165  return lastPoint != string::npos ? fullPath.substr(0, lastPoint) : fullPath;
166 }
167 
172 {
173  return pathWithoutExtension(m_path);
174 }
175 
180 {
181  size_t lastSlash = path.rfind('/');
182  size_t lastBackSlash = path.rfind('\\');
183  size_t lastSeparator;
184  if(lastSlash == string::npos && lastBackSlash == string::npos) {
185  return string();
186  } else if(lastSlash == string::npos) {
187  lastSeparator = lastBackSlash;
188  } else if(lastBackSlash == string::npos) {
189  lastSeparator = lastSlash;
190  } else {
191  lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
192  }
193  if(lastSeparator > 0) {
194  return path.substr(0, lastSeparator);
195  } else {
196  return string();
197  }
198 }
199 
207 {
208  return containingDirectory(m_path);
209 }
210 
219 {
220  m_size = 0;
221  close();
222 }
223 
224 }
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:64
virtual ~BasicFileInfo()
Destroys the BasicFileInfo.
STL namespace.
void reopen(bool readonly=false)
Opens a std::fstream for the current file.
void setPath(const std::string &path)
Sets the current file.
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 fileName(const std::string &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.
Definition: basicfileinfo.h:98
std::string containingDirectory() const
Returns the path of the directory containing the current file.
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
virtual void invalidated()
This function is called when the BasicFileInfo gets invalidated.