Tag Parser  9.3.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/copy.h>
7 
8 #include <memory>
9 #include <sstream>
10 
11 using namespace std;
12 using namespace CppUtilities;
13 
14 namespace TagParser {
15 
26 StreamDataBlock::StreamDataBlock()
27  : m_stream(nullptr)
28  , m_startOffset(0)
29  , m_endOffset(0)
30 {
31 }
32 
43 StreamDataBlock::StreamDataBlock(const std::function<std::istream &()> &stream, std::istream::off_type startOffset, std::ios_base::seekdir startDir,
44  std::istream::off_type endOffset, std::ios_base::seekdir endDir)
45  : m_stream(stream)
46 {
47  auto &s = stream();
48  auto currentPos = s.tellg();
49  s.seekg(startOffset, startDir);
50  m_startOffset = s.tellg();
51  s.seekg(endOffset, endDir);
52  m_endOffset = s.tellg();
53  s.seekg(currentPos);
54  if (m_endOffset < m_startOffset) {
55  throw std::ios_base::failure("End offset is less than start offset.");
56  }
57 }
58 
63 {
64 }
65 
70 {
71  m_buffer = make_unique<char[]>(size());
72  stream().seekg(startOffset());
73  stream().read(m_buffer.get(), size());
74 }
75 
80 void StreamDataBlock::copyTo(ostream &stream) const
81 {
82  if (buffer()) {
83  stream.write(buffer().get(), size());
84  } else {
85  CopyHelper<0x2000> copyHelper;
86  m_stream().seekg(startOffset());
87  copyHelper.copy(m_stream(), stream, size());
88  }
89 }
90 
103 FileDataBlock::FileDataBlock(const string &path, Diagnostics &diag)
104  : m_fileInfo(make_unique<MediaFileInfo>())
105 {
106  m_fileInfo->setPath(path);
107  m_fileInfo->open(true);
108  m_fileInfo->parseContainerFormat(diag);
109  m_startOffset = 0;
110  m_endOffset = m_fileInfo->size();
111  m_stream = [this]() -> std::istream & { return this->m_fileInfo->stream(); };
112 }
113 
121 {
122 }
123 
133 {
134  stringstream ss;
135  ss << "ID: " << id();
136  if (!name().empty()) {
137  ss << ", name: \"" << name() << "\"";
138  }
139  if (!mimeType().empty()) {
140  ss << ", mime-type: \"" << mimeType() << "\"";
141  }
142  return ss.str();
143 }
144 
149 {
150  m_description.clear();
151  m_name.clear();
152  m_mimeType.clear();
153  m_id = 0;
154  m_data.reset();
155 }
156 
170 void AbstractAttachment::setFile(const std::string &path, Diagnostics &diag)
171 {
172  m_data.reset();
173  auto file = make_unique<FileDataBlock>(path, diag);
174  const auto fileName = file->fileInfo()->fileName();
175  if (!fileName.empty()) {
176  m_name = fileName;
177  }
178  const char *mimeType = file->fileInfo()->mimeType();
179  if (*mimeType) {
180  m_mimeType = mimeType;
181  }
182  m_data = move(file);
183  m_isDataFromFile = true;
184 }
185 
186 } // namespace TagParser
abstractattachment.h
TagParser::StreamDataBlock::makeBuffer
void makeBuffer() const
Buffers the data block.
Definition: abstractattachment.cpp:69
exceptions.h
TagParser::StreamDataBlock::startOffset
std::istream::pos_type startOffset() const
Returns the absolute start offset of the data block in the stream.
Definition: abstractattachment.h:53
TagParser::AbstractAttachment::label
std::string label() const
Returns a label for the track.
Definition: abstractattachment.cpp:132
TagParser::StreamDataBlock::stream
std::istream & stream() const
Returns the associated stream.
Definition: abstractattachment.h:45
TagParser::StreamDataBlock::copyTo
void copyTo(std::ostream &stream) const
Copies the data to the specified stream.
Definition: abstractattachment.cpp:80
TagParser::FileDataBlock::FileDataBlock
FileDataBlock(const std::string &path, Diagnostics &diag)
Constructs a new FileDataBlock with the specified path.
Definition: abstractattachment.cpp:103
TagParser::StreamDataBlock::size
std::istream::pos_type size() const
Returns the size of the data block.
Definition: abstractattachment.h:69
TagParser::AbstractAttachment::name
const std::string & name() const
Returns the (file) name of the attachment.
Definition: abstractattachment.h:167
TagParser::AbstractAttachment::mimeType
const std::string & mimeType() const
Returns the MIME-type of the attachment.
Definition: abstractattachment.h:183
TagParser::Diagnostics
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
TagParser::AbstractAttachment::id
std::uint64_t id() const
Returns the ID of the attachment.
Definition: abstractattachment.h:199
TagParser
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
TagParser::StreamDataBlock::m_endOffset
std::istream::pos_type m_endOffset
Definition: abstractattachment.h:36
TagParser::StreamDataBlock::m_startOffset
std::istream::pos_type m_startOffset
Definition: abstractattachment.h:35
TagParser::StreamDataBlock::buffer
const std::unique_ptr< char[]> & buffer() const
Returns the data buffered via makeBuffer().
Definition: abstractattachment.h:77
TagParser::StreamDataBlock::StreamDataBlock
StreamDataBlock()
Constructs a new StreamDataBlock.
Definition: abstractattachment.cpp:26
TagParser::StreamDataBlock::m_stream
std::function< std::istream &()> m_stream
Definition: abstractattachment.h:34
TagParser::StreamDataBlock::~StreamDataBlock
virtual ~StreamDataBlock()
Discards buffered data.
Definition: abstractattachment.cpp:62
CppUtilities
Definition: abstractcontainer.h:15
TagParser::FileDataBlock::~FileDataBlock
~FileDataBlock()
Destroys the FileDataBlock.
Definition: abstractattachment.cpp:120
CppUtilities::CopyHelper
Definition: oggcontainer.h:16
TagParser::AbstractAttachment::clear
void clear()
Resets the object to its initial state.
Definition: abstractattachment.cpp:148
TagParser::StreamDataBlock::m_buffer
std::unique_ptr< char[]> m_buffer
Definition: abstractattachment.h:37
TagParser::StreamDataBlock::endOffset
std::istream::pos_type endOffset() const
Returns the absolute end offset of the data block in the stream.
Definition: abstractattachment.h:61
TagParser::MediaFileInfo
The MediaFileInfo class allows to read and write tag information providing a container/tag format ind...
Definition: mediafileinfo.h:45
mediafileinfo.h
TagParser::AbstractAttachment::setFile
void setFile(const std::string &path, Diagnostics &diag)
Sets the data, name and MIME-type for the specified path.
Definition: abstractattachment.cpp:170