Tag Parser  8.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 }
66 
71 {
72  m_buffer = make_unique<char[]>(size());
73  stream().seekg(startOffset());
74  stream().read(m_buffer.get(), size());
75 }
76 
81 void StreamDataBlock::copyTo(ostream &stream) const
82 {
83  if (buffer()) {
84  stream.write(buffer().get(), size());
85  } else {
86  CopyHelper<0x2000> copyHelper;
87  m_stream().seekg(startOffset());
88  copyHelper.copy(m_stream(), stream, size());
89  }
90 }
91 
104 FileDataBlock::FileDataBlock(const string &path, Diagnostics &diag)
105  : m_fileInfo(make_unique<MediaFileInfo>())
106 {
107  m_fileInfo->setPath(path);
108  m_fileInfo->open(true);
109  m_fileInfo->parseContainerFormat(diag);
110  m_startOffset = 0;
111  m_endOffset = m_fileInfo->size();
112  m_stream = [this]() -> std::istream & { return this->m_fileInfo->stream(); };
113 }
114 
116 {
117 }
118 
128 {
129  stringstream ss;
130  ss << "ID: " << id();
131  if (!name().empty()) {
132  ss << ", name: \"" << name() << "\"";
133  }
134  if (!mimeType().empty()) {
135  ss << ", mime-type: \"" << mimeType() << "\"";
136  }
137  return ss.str();
138 }
139 
144 {
145  m_description.clear();
146  m_name.clear();
147  m_mimeType.clear();
148  m_id = 0;
149  m_data.reset();
150 }
151 
165 void AbstractAttachment::setFile(const std::string &path, Diagnostics &diag)
166 {
167  m_data.reset();
168  auto file = make_unique<FileDataBlock>(path, diag);
169  const auto fileName = file->fileInfo()->fileName();
170  if (!fileName.empty()) {
171  m_name = fileName;
172  }
173  const char *mimeType = file->fileInfo()->mimeType();
174  if (*mimeType) {
175  m_mimeType = mimeType;
176  }
177  m_data = move(file);
178  m_isDataFromFile = true;
179 }
180 
181 } // 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.
The MediaFileInfo class allows to read and write tag information providing a container/tag format ind...
Definition: mediafileinfo.h:44
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.
virtual ~StreamDataBlock()
Discards buffered data.
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9
const std::string & mimeType() const
Returns the MIME-type of the attachment.
std::string label() const
Returns a label for the track.
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
std::istream::pos_type m_startOffset