Tag Parser: abstractattachment.cpp Source File

Tag Parser 12.5.2 C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags

Loading...
Searching...
No Matches
abstractattachment.cpp
Go to the documentation of this file.
1#include "./abstractattachment.h"
2
3#include "./exceptions.h"
4#include "./mediafileinfo.h"
5#include "./progressfeedback.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
18 struct AbstractAttachmentPrivate {};
19
24
30StreamDataBlock::StreamDataBlock()
31 : m_stream(nullptr)
32 , m_startOffset(0)
33 , m_endOffset(0)
34{
35}
36
47 StreamDataBlock::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);
58 if (m_endOffset < m_startOffset) {
59 throw std::ios_base::failure("End offset is less than start offset.");
60 }
61}
62
66StreamDataBlock::~StreamDataBlock()
67{
68}
69
73 void StreamDataBlock::makeBuffer() const
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
84 void 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
99
107FileDataBlock::FileDataBlock(std::string_view path, Diagnostics &diag, AbortableProgressFeedback &progress)
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
124FileDataBlock::~FileDataBlock()
125{
126}
127
132
136AbstractAttachment::AbstractAttachment()
137 : m_id(0)
138 , m_isDataFromFile(false)
139 , m_ignored(false)
140{
141}
142
146AbstractAttachment::~AbstractAttachment()
147{
148}
149
153 string AbstractAttachment::label() const
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
169 void AbstractAttachment::clear()
170{
171 m_description.clear();
172 m_name.clear();
173 m_mimeType.clear();
174 m_id = 0;
175 m_data.reset();
176}
177
191 void AbstractAttachment::setFile(string_view path, Diagnostics &diag, AbortableProgressFeedback &progress)
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
abstractattachment.h
TagParser::AbortableProgressFeedback
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks.
Definition progressfeedback.h:186
TagParser::AbstractAttachment::clear
void clear()
Resets the object to its initial state.
Definition abstractattachment.cpp:169
TagParser::AbstractAttachment::mimeType
const std::string & mimeType() const
Returns the MIME-type of the attachment.
Definition abstractattachment.h:177
TagParser::AbstractAttachment::name
const std::string & name() const
Returns the (file) name of the attachment.
Definition abstractattachment.h:161
TagParser::AbstractAttachment::id
std::uint64_t id() const
Returns the ID of the attachment.
Definition abstractattachment.h:193
TagParser::AbstractAttachment::setFile
void setFile(std::string_view path, Diagnostics &diag, AbortableProgressFeedback &progress)
Sets the data, name and MIME-type for the specified path.
Definition abstractattachment.cpp:191
TagParser::AbstractAttachment::~AbstractAttachment
virtual ~AbstractAttachment()
Destroys the attachment.
Definition abstractattachment.cpp:146
TagParser::AbstractAttachment::label
std::string label() const
Returns a label for the track.
Definition abstractattachment.cpp:153
TagParser::AbstractAttachment::AbstractAttachment
AbstractAttachment()
Constructs a new attachment.
Definition abstractattachment.cpp:136
TagParser::Diagnostics
The Diagnostics class is a container for DiagMessage.
Definition diagnostics.h:156
TagParser::FileDataBlock::FileDataBlock
FileDataBlock(std::string_view path, Diagnostics &diag, AbortableProgressFeedback &progress)
Constructs a new FileDataBlock with the specified path.
Definition abstractattachment.cpp:107
TagParser::FileDataBlock::~FileDataBlock
~FileDataBlock()
Destroys the FileDataBlock.
Definition abstractattachment.cpp:124
TagParser::MediaFileInfo
The MediaFileInfo class allows to read and write tag information providing a container/tag format ind...
Definition mediafileinfo.h:83
TagParser::StreamDataBlock::m_startOffset
std::uint64_t m_startOffset
Definition abstractattachment.h:35
TagParser::StreamDataBlock::m_endOffset
std::uint64_t m_endOffset
Definition abstractattachment.h:36
TagParser::StreamDataBlock::m_buffer
std::unique_ptr< char[]> m_buffer
Definition abstractattachment.h:37
TagParser::StreamDataBlock::makeBuffer
void makeBuffer() const
Buffers the data block.
Definition abstractattachment.cpp:73
TagParser::StreamDataBlock::buffer
const std::unique_ptr< char[]> & buffer() const
Returns the data buffered via makeBuffer().
Definition abstractattachment.h:77
TagParser::StreamDataBlock::startOffset
std::uint64_t startOffset() const
Returns the absolute start offset of the data block in the stream.
Definition abstractattachment.h:53
TagParser::StreamDataBlock::StreamDataBlock
StreamDataBlock()
Constructs a new StreamDataBlock.
Definition abstractattachment.cpp:30
TagParser::StreamDataBlock::size
std::uint64_t size() const
Returns the size of the data block.
Definition abstractattachment.h:69
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:84
TagParser::StreamDataBlock::~StreamDataBlock
virtual ~StreamDataBlock()
Discards buffered data.
Definition abstractattachment.cpp:66
TagParser::StreamDataBlock::endOffset
std::uint64_t endOffset() const
Returns the absolute end offset of the data block in the stream.
Definition abstractattachment.h:61
TagParser::StreamDataBlock::m_stream
std::function< std::istream &()> m_stream
Definition abstractattachment.h:34
exceptions.h
mediafileinfo.h
CppUtilities
Definition abstractcontainer.h:16
TagParser
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
progressfeedback.h
TagParser::AbstractAttachmentPrivate
The AbstractAttachmentPrivate struct contains private fields of the AbstractAttachment class.
Definition abstractattachment.cpp:18