Tag Parser 12.4.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
abstractattachment.cpp
Go to the documentation of this file.
2
3#include "./exceptions.h"
4#include "./mediafileinfo.h"
6
7#include <c++utilities/io/copy.h>
8
9#include <memory>
10#include <sstream>
11
12using namespace std;
13using namespace CppUtilities;
14
15namespace TagParser {
16
19
31 : m_stream(nullptr)
32 , m_startOffset(0)
33 , m_endOffset(0)
34{
35}
36
47StreamDataBlock::StreamDataBlock(const std::function<std::istream &()> &stream, std::uint64_t startOffset, std::ios_base::seekdir startDir,
48 std::uint64_t endOffset, std::ios_base::seekdir endDir)
49 : m_stream(stream)
50{
51 auto &s = stream();
52 auto currentPos = s.tellg();
53 s.seekg(static_cast<std::istream::off_type>(startOffset), startDir);
54 m_startOffset = static_cast<std::uint64_t>(s.tellg());
55 s.seekg(static_cast<std::istream::off_type>(endOffset), endDir);
56 m_endOffset = static_cast<std::uint64_t>(s.tellg());
57 s.seekg(currentPos);
59 throw std::ios_base::failure("End offset is less than start offset.");
60 }
61}
62
69
74{
75 m_buffer = make_unique<char[]>(size());
76 stream().seekg(static_cast<std::istream::off_type>(startOffset()));
77 stream().read(m_buffer.get(), static_cast<std::streamsize>(size()));
78}
79
84void StreamDataBlock::copyTo(ostream &stream) const
85{
86 if (buffer()) {
87 stream.write(buffer().get(), static_cast<std::streamsize>(size()));
88 } else {
89 CopyHelper<0x2000> copyHelper;
90 m_stream().seekg(static_cast<std::streamsize>(startOffset()));
91 copyHelper.copy(m_stream(), stream, size());
92 }
93}
94
108 : m_fileInfo(make_unique<MediaFileInfo>())
109{
110 m_fileInfo->setPath(path);
111 m_fileInfo->open(true);
112 m_fileInfo->parseContainerFormat(diag, progress);
113 m_startOffset = 0;
114 m_endOffset = m_fileInfo->size();
115 m_stream = [this]() -> std::istream & { return this->m_fileInfo->stream(); };
116}
117
127
137 : m_id(0)
138 , m_isDataFromFile(false)
139 , m_ignored(false)
140{
141}
142
149
154{
155 stringstream ss;
156 ss << "ID: " << id();
157 if (!name().empty()) {
158 ss << ", name: \"" << name() << "\"";
159 }
160 if (!mimeType().empty()) {
161 ss << ", mime-type: \"" << mimeType() << "\"";
162 }
163 return ss.str();
164}
165
170{
171 m_description.clear();
172 m_name.clear();
173 m_mimeType.clear();
174 m_id = 0;
175 m_data.reset();
176}
177
192{
193 m_data.reset();
194 auto file = make_unique<FileDataBlock>(path, diag, progress);
195 const auto fileName = file->fileInfo()->fileName();
196 if (!fileName.empty()) {
197 m_name = fileName;
198 }
199 const auto mimeType = file->fileInfo()->mimeType();
200 if (!mimeType.empty()) {
201 m_mimeType = mimeType;
202 }
203 m_data = std::move(file);
204 m_isDataFromFile = true;
205}
206
207} // namespace TagParser
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks.
void clear()
Resets the object to its initial state.
const std::string & mimeType() const
Returns the MIME-type of the attachment.
const std::string & name() const
Returns the (file) name of the attachment.
std::uint64_t id() const
Returns the ID of the attachment.
void setFile(std::string_view path, Diagnostics &diag, AbortableProgressFeedback &progress)
Sets the data, name and MIME-type for the specified path.
virtual ~AbstractAttachment()
Destroys the attachment.
std::string label() const
Returns a label for the track.
AbstractAttachment()
Constructs a new attachment.
The Diagnostics class is a container for DiagMessage.
FileDataBlock(std::string_view path, Diagnostics &diag, AbortableProgressFeedback &progress)
Constructs a new FileDataBlock with the specified path.
~FileDataBlock()
Destroys the FileDataBlock.
The MediaFileInfo class allows to read and write tag information providing a container/tag format ind...
std::unique_ptr< char[]> m_buffer
void makeBuffer() const
Buffers the data block.
const std::unique_ptr< char[]> & buffer() const
Returns the data buffered via makeBuffer().
std::uint64_t startOffset() const
Returns the absolute start offset of the data block in the stream.
StreamDataBlock()
Constructs a new StreamDataBlock.
std::uint64_t 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.
std::uint64_t endOffset() const
Returns the absolute end offset of the data block in the stream.
std::function< std::istream &()> m_stream
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
The AbstractAttachmentPrivate struct contains private fields of the AbstractAttachment class.