Tag Parser  7.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
abstractattachment.cpp
Go to the documentation of this file.
1 #include "./abstractattachment.h"
2 
3 #include "./exceptions.h"
4 #include "./mediafileinfo.h"
5 
6 #include <c++utilities/io/catchiofailure.h>
7 #include <c++utilities/io/copy.h>
8 
9 #include <memory>
10 #include <sstream>
11 
12 using namespace std;
13 using namespace IoUtilities;
14 
15 namespace TagParser {
16 
27 StreamDataBlock::StreamDataBlock()
28  : m_stream(nullptr)
29  , m_startOffset(0)
30  , m_endOffset(0)
31 {
32 }
33 
44 StreamDataBlock::StreamDataBlock(const std::function<std::istream &()> &stream, std::istream::off_type startOffset, std::ios_base::seekdir startDir,
45  std::istream::off_type endOffset, std::ios_base::seekdir endDir)
46  : m_stream(stream)
47 {
48  auto &s = stream();
49  auto currentPos = s.tellg();
50  s.seekg(startOffset, startDir);
51  m_startOffset = s.tellg();
52  s.seekg(endOffset, endDir);
53  m_endOffset = s.tellg();
54  s.seekg(currentPos);
55  if (m_endOffset < m_startOffset) {
56  IoUtilities::throwIoFailure("End offset is less than start offset.");
57  }
58 }
59 
64 {
65  m_buffer = make_unique<char[]>(size());
66  stream().seekg(startOffset());
67  stream().read(m_buffer.get(), size());
68 }
69 
74 void StreamDataBlock::copyTo(ostream &stream) const
75 {
76  if (buffer()) {
77  stream.write(buffer().get(), size());
78  } else {
79  CopyHelper<0x2000> copyHelper;
80  m_stream().seekg(startOffset());
81  copyHelper.copy(m_stream(), stream, size());
82  }
83 }
84 
97 FileDataBlock::FileDataBlock(const string &path, Diagnostics &diag)
98  : m_fileInfo(new MediaFileInfo)
99 {
100  m_fileInfo->setPath(path);
101  m_fileInfo->open(true);
102  m_fileInfo->parseContainerFormat(diag);
103  m_startOffset = 0;
104  m_endOffset = m_fileInfo->size();
105  m_stream = [this]() -> std::istream & { return this->m_fileInfo->stream(); };
106 }
107 
117 {
118  stringstream ss;
119  ss << "ID: " << id();
120  if (!name().empty()) {
121  ss << ", name: \"" << name() << "\"";
122  }
123  if (!mimeType().empty()) {
124  ss << ", mime-type: \"" << mimeType() << "\"";
125  }
126  return ss.str();
127 }
128 
133 {
134  m_description.clear();
135  m_name.clear();
136  m_mimeType.clear();
137  m_id = 0;
138  m_data.reset();
139 }
140 
154 void AbstractAttachment::setFile(const std::string &path, Diagnostics &diag)
155 {
156  m_data.reset();
157  auto file = make_unique<FileDataBlock>(path, diag);
158  const auto fileName = file->fileInfo()->fileName();
159  if (!fileName.empty()) {
160  m_name = fileName;
161  }
162  const char *mimeType = file->fileInfo()->mimeType();
163  if (*mimeType) {
164  m_mimeType = mimeType;
165  }
166  m_data = move(file);
167  m_isDataFromFile = true;
168 }
169 
170 } // namespace TagParser
FileDataBlock(const std::string &path, Diagnostics &diag)
Constructs a new FileDataBlock with the specified path.
const std::unique_ptr< char[]> & buffer() const
Returns the data buffered via makeBuffer().
void setFile(const std::string &path, Diagnostics &diag)
Sets the data, name and MIME-type for the specified path.
std::unique_ptr< char[]> m_buffer
STL namespace.
void makeBuffer() const
Buffers the data block.
uint64 id() const
Returns the ID of the attachment.
Contains utility classes helping to read and write streams.
const std::string & name() const
Returns the (file) name of the attachment.
void clear()
Resets the object to its initial state.
std::istream::pos_type endOffset() const
Returns the absolute end offset of the data block in the stream.
std::istream::pos_type m_endOffset
StreamDataBlock()
Constructs a new StreamDataBlock.
std::function< std::istream &()> m_stream
std::istream::pos_type startOffset() const
Returns the absolute start offset of the data block in the stream.
std::istream::pos_type size() const
Returns the size of the data block.
std::istream & stream() const
Returns the associated stream.
void copyTo(std::ostream &stream) const
Copies the data to the specified stream.
const std::string & mimeType() const
Returns the MIME-type of the attachment.
std::string label() const
Returns a label for the track.
std::istream::pos_type m_startOffset