Tag Parser: ogg/oggiterator.h 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
oggiterator.h
Go to the documentation of this file.
1#ifndef TAG_PARSER_OGGITERATOR_H
2#define TAG_PARSER_OGGITERATOR_H
3
4#include "./oggpage.h"
5
6#include <iosfwd>
7#include <vector>
8
9namespace TagParser {
10
11 class TAG_PARSER_EXPORT OggIterator {
12public:
13 OggIterator(std::istream &stream, std::uint64_t startOffset, std::uint64_t streamSize);
14
15 void clear(std::istream &stream, std::uint64_t startOffset, std::uint64_t streamSize);
16 std::istream &stream();
17 void setStream(std::istream &stream);
18 std::uint64_t startOffset() const;
19 std::uint64_t streamSize() const;
20 void reset();
21 void nextPage();
22 void nextSegment();
23 void previousPage();
24 void previousSegment();
25 const std::vector<OggPage> &pages() const;
26 std::vector<OggPage> &pages();
27 const OggPage &currentPage() const;
28 std::uint64_t currentPageOffset() const;
29 std::vector<OggPage>::size_type currentPageIndex() const;
30 void setPageIndex(std::vector<OggPage>::size_type index);
31 void setSegmentIndex(std::vector<std::uint32_t>::size_type index);
32 std::vector<std::uint32_t>::size_type currentSegmentIndex() const;
33 std::uint64_t currentSegmentOffset() const;
34 std::uint64_t currentCharacterOffset() const;
35 std::uint64_t tellg() const;
36 std::uint32_t currentSegmentSize() const;
37 std::uint64_t remainingBytesInCurrentSegment() const;
38 std::uint64_t bytesReadFromCurrentSegment() const;
39 void setFilter(std::uint32_t streamSerialId);
40 void removeFilter();
41 bool isLastPageFetched() const;
42 void read(char *buffer, std::size_t count);
43 std::size_t readAll(char *buffer, std::size_t max);
44 void ignore(std::size_t count = 1);
45 bool bytesRemaining(std::size_t atLeast) const;
46 bool resyncAt(std::uint64_t offset);
47
48 operator bool() const;
49 OggIterator &operator++();
50 OggIterator operator++(int);
51 OggIterator &operator--();
52 OggIterator operator--(int);
53
54private:
55 bool fetchNextPage();
56 bool matchesFilter(const OggPage &page);
57
58 std::istream *m_stream;
59 std::uint64_t m_startOffset;
60 std::uint64_t m_streamSize;
61 std::vector<OggPage> m_pages;
62 std::vector<OggPage>::size_type m_page;
63 std::vector<std::uint32_t>::size_type m_segment;
64 std::uint64_t m_offset;
65 std::uint64_t m_bytesRead;
66 bool m_hasIdFilter;
67 std::uint32_t m_idFilter;
68};
69
73 inline OggIterator::OggIterator(std::istream &stream, std::uint64_t startOffset, std::uint64_t streamSize)
74 : m_stream(&stream)
75 , m_startOffset(startOffset)
76 , m_streamSize(streamSize)
77 , m_page(0)
78 , m_segment(0)
79 , m_offset(0)
80 , m_bytesRead(0)
81 , m_hasIdFilter(false)
82 , m_idFilter(0)
83{
84}
85
91 inline std::istream &OggIterator::stream()
92{
93 return *m_stream;
94}
95
101 inline void OggIterator::setStream(std::istream &stream)
102{
103 m_stream = &stream;
104}
105
109 inline std::uint64_t OggIterator::startOffset() const
110{
111 return m_startOffset;
112}
113
117 inline std::uint64_t OggIterator::streamSize() const
118{
119 return m_streamSize;
120}
121
125 inline const std::vector<OggPage> &OggIterator::pages() const
126{
127 return m_pages;
128}
129
133 inline std::vector<OggPage> &OggIterator::pages()
134{
135 return m_pages;
136}
137
142 inline const OggPage &OggIterator::currentPage() const
143{
144 return m_pages[m_page];
145}
146
151 inline std::uint64_t OggIterator::currentPageOffset() const
152{
153 return m_pages[m_page].startOffset();
154}
155
166 inline OggIterator::operator bool() const
167{
168 return m_page < m_pages.size() && m_segment < m_pages[m_page].segmentSizes().size();
169}
170
174 inline std::vector<OggPage>::size_type OggIterator::currentPageIndex() const
175{
176 return m_page;
177}
178
183 inline void OggIterator::setPageIndex(std::vector<OggPage>::size_type index)
184{
185 const OggPage &page = m_pages[m_page = index];
186 m_segment = 0;
187 m_offset = page.startOffset() + page.headerSize();
188}
189
195 inline void OggIterator::setSegmentIndex(std::vector<std::uint32_t>::size_type index)
196{
197 const OggPage &page = m_pages[m_page];
198 m_offset = page.dataOffset(m_segment = index);
199}
200
204 inline std::vector<std::uint32_t>::size_type OggIterator::currentSegmentIndex() const
205{
206 return m_segment;
207}
208
213 inline std::uint64_t OggIterator::currentSegmentOffset() const
214{
215 return m_offset;
216}
217
222 inline std::uint64_t OggIterator::currentCharacterOffset() const
223{
224 return m_offset + m_bytesRead;
225}
226
230 inline std::uint64_t OggIterator::tellg() const
231{
232 return currentCharacterOffset();
233}
234
240 inline std::uint32_t OggIterator::currentSegmentSize() const
241{
242 return m_pages[m_page].segmentSizes()[m_segment];
243}
244
248 inline std::uint64_t OggIterator::remainingBytesInCurrentSegment() const
249{
250 return currentSegmentSize() - m_bytesRead;
251}
252
256 inline uint64_t OggIterator::bytesReadFromCurrentSegment() const
257{
258 return m_bytesRead;
259}
260
269 inline void OggIterator::setFilter(std::uint32_t streamSerialId)
270{
271 m_hasIdFilter = true;
272 m_idFilter = streamSerialId;
273}
274
279 inline void OggIterator::removeFilter()
280{
281 m_hasIdFilter = false;
282}
283
287 inline bool OggIterator::isLastPageFetched() const
288{
289 return (m_pages.empty() ? m_startOffset : m_pages.back().startOffset() + m_pages.back().totalSize()) >= m_streamSize;
290}
291
296 inline bool OggIterator::bytesRemaining(size_t atLeast) const
297{
298 return *this && currentCharacterOffset() + atLeast <= streamSize();
299}
300
304 inline OggIterator &OggIterator::operator++()
305{
306 nextSegment();
307 return *this;
308}
309
313 inline OggIterator OggIterator::operator++(int)
314{
315 OggIterator tmp = *this;
316 nextSegment();
317 return tmp;
318}
319
323 inline OggIterator &OggIterator::operator--()
324{
325 previousSegment();
326 return *this;
327}
328
332 inline OggIterator OggIterator::operator--(int)
333{
334 OggIterator tmp = *this;
335 previousSegment();
336 return tmp;
337}
338
342inline bool OggIterator::matchesFilter(const OggPage &page)
343{
344 return !m_hasIdFilter || m_idFilter == page.streamSerialNumber();
345}
346
347} // namespace TagParser
348
349#endif // TAG_PARSER_OGGITERATOR_H
TagParser::OggIterator::nextPage
void nextPage()
Increases the current position by one page.
Definition oggiterator.cpp:63
TagParser::OggIterator::setSegmentIndex
void setSegmentIndex(std::vector< std::uint32_t >::size_type index)
Sets the current segment index.
Definition oggiterator.h:195
TagParser::OggIterator::currentCharacterOffset
std::uint64_t currentCharacterOffset() const
Returns the offset of the current character in the input stream if the iterator is valid; otherwise a...
Definition oggiterator.h:222
TagParser::OggIterator::bytesRemaining
bool bytesRemaining(std::size_t atLeast) const
Returns whether there are atLeast bytes remaining.
Definition oggiterator.h:296
TagParser::OggIterator::read
void read(char *buffer, std::size_t count)
Reads count bytes from the Ogg stream and writes it to the specified buffer.
Definition oggiterator.cpp:134
TagParser::OggIterator::remainingBytesInCurrentSegment
std::uint64_t remainingBytesInCurrentSegment() const
Returns the number of bytes left to read in the current segment.
Definition oggiterator.h:248
TagParser::OggIterator::removeFilter
void removeFilter()
Removes a previously set filter.
Definition oggiterator.h:279
TagParser::OggIterator::tellg
std::uint64_t tellg() const
Same as currentCharacterOffset(); only provided for compliance with std::istream.
Definition oggiterator.h:230
TagParser::OggIterator::OggIterator
OggIterator(std::istream &stream, std::uint64_t startOffset, std::uint64_t streamSize)
Constructs a new iterator for the specified stream of streamSize bytes at the specified startOffset.
Definition oggiterator.h:73
TagParser::OggIterator::setStream
void setStream(std::istream &stream)
Sets the stream.
Definition oggiterator.h:101
TagParser::OggIterator::operator++
OggIterator & operator++()
Increments the current position by one segment if the iterator is valid; otherwise nothing happens.
Definition oggiterator.h:304
TagParser::OggIterator::currentSegmentIndex
std::vector< std::uint32_t >::size_type currentSegmentIndex() const
Returns the index of the current segment (in the current page) if the iterator is valid; otherwise an...
Definition oggiterator.h:204
TagParser::OggIterator::operator--
OggIterator & operator--()
Decrements the current position by one segment if the iterator is valid; otherwise nothing happens.
Definition oggiterator.h:323
TagParser::OggIterator::readAll
std::size_t readAll(char *buffer, std::size_t max)
Reads all bytes from the Ogg stream and writes it to the specified buffer.
Definition oggiterator.cpp:169
TagParser::OggIterator::clear
void clear(std::istream &stream, std::uint64_t startOffset, std::uint64_t streamSize)
Sets the stream and related parameters and clears all available pages.
Definition oggiterator.cpp:32
TagParser::OggIterator::previousPage
void previousPage()
Decreases the current position by one page.
Definition oggiterator.cpp:98
TagParser::OggIterator::currentSegmentOffset
std::uint64_t currentSegmentOffset() const
Returns the start offset of the current segment in the input stream if the iterator is valid; otherwi...
Definition oggiterator.h:213
TagParser::OggIterator::currentPageOffset
std::uint64_t currentPageOffset() const
Returns the start offset of the current Ogg page.
Definition oggiterator.h:151
TagParser::OggIterator::currentPage
const OggPage & currentPage() const
Returns the current Ogg page.
Definition oggiterator.h:142
TagParser::OggIterator::pages
const std::vector< OggPage > & pages() const
Returns a vector of containing the Ogg pages that have been fetched yet.
Definition oggiterator.h:125
TagParser::OggIterator::resyncAt
bool resyncAt(std::uint64_t offset)
Fetches the next page at the specified offset.
Definition oggiterator.cpp:237
TagParser::OggIterator::stream
std::istream & stream()
Returns the stream.
Definition oggiterator.h:91
TagParser::OggIterator::startOffset
std::uint64_t startOffset() const
Returns the start offset (which has been specified when constructing the iterator).
Definition oggiterator.h:109
TagParser::OggIterator::bytesReadFromCurrentSegment
std::uint64_t bytesReadFromCurrentSegment() const
Returns the number of bytes read from the current segment.
Definition oggiterator.h:256
TagParser::OggIterator::setPageIndex
void setPageIndex(std::vector< OggPage >::size_type index)
Sets the current page index.
Definition oggiterator.h:183
TagParser::OggIterator::currentPageIndex
std::vector< OggPage >::size_type currentPageIndex() const
Returns the index of the current page if the iterator is valid; otherwise an undefined index is retur...
Definition oggiterator.h:174
TagParser::OggIterator::currentSegmentSize
std::uint32_t currentSegmentSize() const
Returns the size of the current segment.
Definition oggiterator.h:240
TagParser::OggIterator::nextSegment
void nextSegment()
Increases the current position by one segment.
Definition oggiterator.cpp:81
TagParser::OggIterator::reset
void reset()
Resets the iterator to point at the first segment of the first page (matching the filter if set).
Definition oggiterator.cpp:46
TagParser::OggIterator::setFilter
void setFilter(std::uint32_t streamSerialId)
Allows to filter pages by the specified streamSerialId.
Definition oggiterator.h:269
TagParser::OggIterator::ignore
void ignore(std::size_t count=1)
Advances the position of the next character to be read from the Ogg stream by count bytes.
Definition oggiterator.cpp:199
TagParser::OggIterator::previousSegment
void previousSegment()
Decreases the current position by one segment.
Definition oggiterator.cpp:113
TagParser::OggIterator::isLastPageFetched
bool isLastPageFetched() const
Returns whether the last page has already been fetched.
Definition oggiterator.h:287
TagParser::OggIterator::streamSize
std::uint64_t streamSize() const
Returns the stream size (which has been specified when constructing the iterator).
Definition oggiterator.h:117
TagParser::OggPage
The OggPage class is used to parse Ogg pages.
Definition oggpage.h:13
TagParser::OggPage::dataOffset
std::uint64_t dataOffset(std::vector< std::uint32_t >::size_type segmentIndex=0) const
Returns the data offset of the segment with the specified segmentIndex.
Definition oggpage.h:259
TagParser::OggPage::streamSerialNumber
std::uint32_t streamSerialNumber() const
Returns the stream serial number.
Definition oggpage.h:166
TagParser::OggPage::startOffset
std::uint64_t startOffset() const
Returns the start offset of the page.
Definition oggpage.h:86
TagParser::OggPage::headerSize
std::uint32_t headerSize() const
Returns the header size in byte.
Definition oggpage.h:230
TAG_PARSER_EXPORT
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
Definition global.h:14
TagParser
Contains all classes and functions of the TagInfo library.
Definition aaccodebook.h:10
oggpage.h