Tag Parser  9.3.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(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(pathForOpen(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<std::uint64_t>(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 
179 string BasicFileInfo::containingDirectory(const string &path)
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 } // namespace TagParser
TagParser::BasicFileInfo::containingDirectory
std::string containingDirectory() const
Returns the path of the directory containing the current file.
Definition: basicfileinfo.cpp:206
TagParser::BasicFileInfo::invalidated
virtual void invalidated()
This function is called when the BasicFileInfo gets invalidated.
Definition: basicfileinfo.cpp:218
TagParser::BasicFileInfo::reopen
void reopen(bool readOnly=false)
Opens a std::fstream for the current file.
Definition: basicfileinfo.cpp:59
TagParser::BasicFileInfo::invalidate
void invalidate()
Invalidates the file info manually.
Definition: basicfileinfo.cpp:82
TagParser::BasicFileInfo::extension
std::string extension() const
Returns the extension of the current file.
Definition: basicfileinfo.cpp:154
TagParser::BasicFileInfo::~BasicFileInfo
virtual ~BasicFileInfo()
Destroys the BasicFileInfo.
Definition: basicfileinfo.cpp:36
TagParser::BasicFileInfo::setPath
void setPath(const std::string &path)
Sets the current file.
Definition: basicfileinfo.cpp:94
TagParser::BasicFileInfo::pathForOpen
static const char * pathForOpen(const std::string &url)
Returns removes the "file:/" prefix from url to be able to pass it to functions like open(),...
Definition: basicfileinfo.h:140
TagParser
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
TagParser::BasicFileInfo::open
void open(bool readOnly=false)
Opens a std::fstream for the current file.
Definition: basicfileinfo.cpp:46
TagParser::BasicFileInfo::pathWithoutExtension
std::string pathWithoutExtension() const
Returns the path of the current file without the extension/suffix.
Definition: basicfileinfo.cpp:171
basicfileinfo.h
CppUtilities
Definition: abstractcontainer.h:15
TagParser::BasicFileInfo::isOpen
bool isOpen() const
Indicates whether a std::fstream is open for the current file.
Definition: basicfileinfo.h:65
TagParser::BasicFileInfo::fileName
static std::string fileName(const std::string &path, bool cutExtension=false)
Returns the file name of the given file.
Definition: basicfileinfo.cpp:108
TagParser::BasicFileInfo::path
const std::string & path() const
Returns the path of the current file.
Definition: basicfileinfo.h:99
TagParser::BasicFileInfo::close
void close()
A possibly opened std::fstream will be closed.
Definition: basicfileinfo.cpp:71