Tag Parser  6.4.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 "./mediafileinfo.h"
4 #include "./exceptions.h"
5 
6 #include <c++utilities/io/catchiofailure.h>
7 #include <c++utilities/io/copy.h>
8 
9 #include <sstream>
10 #include <memory>
11 
12 using namespace std;
13 using namespace IoUtilities;
14 
15 namespace Media {
16 
27 StreamDataBlock::StreamDataBlock() :
28  m_stream(nullptr),
29  m_startOffset(0),
30  m_endOffset(0)
31 {}
32 
43 StreamDataBlock::StreamDataBlock(const std::function<std::istream & ()> &stream, std::istream::off_type startOffset, std::ios_base::seekdir startDir, std::istream::off_type endOffset, std::ios_base::seekdir endDir) :
45 {
46  auto &s = stream();
47  auto currentPos = s.tellg();
48  s.seekg(startOffset, startDir);
49  m_startOffset = s.tellg();
50  s.seekg(endOffset, endDir);
51  m_endOffset = s.tellg();
52  s.seekg(currentPos);
54  IoUtilities::throwIoFailure("End offset is less than start offset.");
55  }
56 }
57 
62 {
63  m_buffer = make_unique<char[]>(size());
64  stream().seekg(startOffset());
65  stream().read(m_buffer.get(), size());
66 }
67 
72 void StreamDataBlock::copyTo(ostream &stream) const
73 {
74  if(buffer()) {
75  stream.write(buffer().get(), size());
76  } else {
77  CopyHelper<0x2000> copyHelper;
78  m_stream().seekg(startOffset());
79  copyHelper.copy(m_stream(), stream, size());
80  }
81 }
82 
95 FileDataBlock::FileDataBlock(const string &path) :
96  m_fileInfo(new MediaFileInfo)
97 {
98  m_fileInfo->setPath(path);
99  m_fileInfo->open(true);
100  m_fileInfo->parseContainerFormat();
101  m_startOffset = 0;
102  m_endOffset = m_fileInfo->size();
103  m_stream = [this] () -> std::istream & {
104  return this->m_fileInfo->stream();
105  };
106 }
107 
117 {
118  stringstream ss;
119  ss << "ID: " << id();
120  if(!name().empty()) {
121  ss << ", name: \"" << name() << "\"";
122  }
123  if(!mimeType().empty()) {
124  ss << ", mime-type: \"" << mimeType() << "\"";
125  }
126  return ss.str();
127 }
128 
133 {
134  m_description.clear();
135  m_name.clear();
136  m_mimeType.clear();
137  m_id = 0;
138  m_data.reset();
139 }
140 
154 void AbstractAttachment::setFile(const std::string &path)
155 {
156  m_data.reset();
157  auto file = make_unique<FileDataBlock>(path);
158  const auto fileName = file->fileInfo()->fileName();
159  if(!fileName.empty()) {
160  m_name = fileName;
161  }
162  const char *mimeType = file->fileInfo()->mimeType();
163  if(*mimeType) {
164  m_mimeType = mimeType;
165  }
166  m_data = move(file);
167  m_isDataFromFile = true;
168 }
169 
170 } // namespace Media
171 
std::string label() const
Returns a label for the track.
std::istream::pos_type endOffset() const
Returns the absolute end offset of the data block in the stream.
void setFile(const std::string &path)
Sets the data, name and MIME-type for the specified path.
StreamDataBlock()
Constructs a new StreamDataBlock.
std::istream::pos_type m_startOffset
void makeBuffer() const
Buffers the data block.
std::unique_ptr< char[]> m_buffer
STL namespace.
std::istream::pos_type startOffset() const
Returns the absolute start offset of the data block in the stream.
void clear()
Resets the object to its initial state.
Contains utility classes helping to read and write streams.
The MediaFileInfo class allows to read and write tag information providing a container/tag format ind...
Definition: mediafileinfo.h:53
const std::unique_ptr< char[]> & buffer() const
Returns the data buffered via makeBuffer().
std::istream::pos_type m_endOffset
std::istream & stream() const
Returns the associated stream.
FileDataBlock(const std::string &path)
Constructs a new FileDataBlock with the specified path.
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
std::istream::pos_type size() const
Returns the size of the data block.
void copyTo(std::ostream &stream) const
Copies the data to the specified stream.
std::function< std::istream &()> m_stream