Tag Parser  10.0.1
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 #include "./progressfeedback.h"
6 
7 #include <c++utilities/io/copy.h>
8 
9 #include <memory>
10 #include <sstream>
11 
12 using namespace std;
13 using namespace CppUtilities;
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::uint64_t startOffset, std::ios_base::seekdir startDir,
45  std::uint64_t endOffset, std::ios_base::seekdir endDir)
46  : m_stream(stream)
47 {
48  auto &s = stream();
49  auto currentPos = s.tellg();
50  s.seekg(static_cast<std::istream::off_type>(startOffset), startDir);
51  m_startOffset = static_cast<std::uint64_t>(s.tellg());
52  s.seekg(static_cast<std::istream::off_type>(endOffset), endDir);
53  m_endOffset = static_cast<std::uint64_t>(s.tellg());
54  s.seekg(currentPos);
55  if (m_endOffset < m_startOffset) {
56  throw std::ios_base::failure("End offset is less than start offset.");
57  }
58 }
59 
64 {
65 }
66 
71 {
72  m_buffer = make_unique<char[]>(size());
73  stream().seekg(static_cast<std::istream::off_type>(startOffset()));
74  stream().read(m_buffer.get(), static_cast<std::streamsize>(size()));
75 }
76 
81 void StreamDataBlock::copyTo(ostream &stream) const
82 {
83  if (buffer()) {
84  stream.write(buffer().get(), static_cast<std::streamsize>(size()));
85  } else {
86  CopyHelper<0x2000> copyHelper;
87  m_stream().seekg(static_cast<std::streamsize>(startOffset()));
88  copyHelper.copy(m_stream(), stream, size());
89  }
90 }
91 
105  : m_fileInfo(make_unique<MediaFileInfo>())
106 {
107  m_fileInfo->setPath(path);
108  m_fileInfo->open(true);
109  m_fileInfo->parseContainerFormat(diag, progress);
110  m_startOffset = 0;
111  m_endOffset = m_fileInfo->size();
112  m_stream = [this]() -> std::istream & { return this->m_fileInfo->stream(); };
113 }
114 
122 {
123 }
124 
134 {
135  stringstream ss;
136  ss << "ID: " << id();
137  if (!name().empty()) {
138  ss << ", name: \"" << name() << "\"";
139  }
140  if (!mimeType().empty()) {
141  ss << ", mime-type: \"" << mimeType() << "\"";
142  }
143  return ss.str();
144 }
145 
150 {
151  m_description.clear();
152  m_name.clear();
153  m_mimeType.clear();
154  m_id = 0;
155  m_data.reset();
156 }
157 
171 void AbstractAttachment::setFile(string_view path, Diagnostics &diag, AbortableProgressFeedback &progress)
172 {
173  m_data.reset();
174  auto file = make_unique<FileDataBlock>(path, diag, progress);
175  const auto fileName = file->fileInfo()->fileName();
176  if (!fileName.empty()) {
177  m_name = fileName;
178  }
179  const auto mimeType = file->fileInfo()->mimeType();
180  if (!mimeType.empty()) {
181  m_mimeType = mimeType;
182  }
183  m_data = move(file);
184  m_isDataFromFile = true;
185 }
186 
187 } // 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.
std::string label() const
Returns a label for the track.
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
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...
Definition: mediafileinfo.h:74
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