Tag Parser  7.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
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 
9 namespace TagParser {
10 
12 public:
13  OggIterator(std::istream &stream, uint64 startOffset, uint64 streamSize);
14 
15  void clear(std::istream &stream, uint64 startOffset, uint64 streamSize);
16  std::istream &stream();
17  void setStream(std::istream &stream);
18  uint64 startOffset() const;
19  uint64 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  const OggPage &currentPage() const;
27  uint64 currentPageOffset() const;
28  std::vector<OggPage>::size_type currentPageIndex() const;
29  void setPageIndex(std::vector<OggPage>::size_type index);
30  void setSegmentIndex(std::vector<uint32>::size_type index);
31  std::vector<uint32>::size_type currentSegmentIndex() const;
32  uint64 currentSegmentOffset() const;
33  uint64 currentCharacterOffset() const;
34  uint64 tellg() const;
35  uint32 currentSegmentSize() const;
36  void setFilter(uint32 streamSerialId);
37  void removeFilter();
38  bool areAllPagesFetched() const;
39  void read(char *buffer, std::size_t count);
40  size_t readAll(char *buffer, std::size_t max);
41  void ignore(std::size_t count = 1);
42  bool bytesRemaining(std::size_t atLeast) const;
43  bool resyncAt(uint64 offset);
44 
45  operator bool() const;
46  OggIterator &operator++();
47  OggIterator operator++(int);
48  OggIterator &operator--();
49  OggIterator operator--(int);
50 
51 private:
52  bool fetchNextPage();
53  bool matchesFilter(const OggPage &page);
54 
55  std::istream *m_stream;
56  uint64 m_startOffset;
57  uint64 m_streamSize;
58  std::vector<OggPage> m_pages;
59  std::vector<OggPage>::size_type m_page;
60  std::vector<uint32>::size_type m_segment;
61  uint64 m_offset;
62  uint32 m_bytesRead;
63  bool m_hasIdFilter;
64  uint32 m_idFilter;
65 };
66 
70 inline OggIterator::OggIterator(std::istream &stream, uint64 startOffset, uint64 streamSize)
71  : m_stream(&stream)
72  , m_startOffset(startOffset)
73  , m_streamSize(streamSize)
74  , m_page(0)
75  , m_segment(0)
76  , m_offset(0)
77  , m_bytesRead(0)
78  , m_hasIdFilter(false)
79  , m_idFilter(0)
80 {
81 }
82 
88 inline std::istream &OggIterator::stream()
89 {
90  return *m_stream;
91 }
92 
98 inline void OggIterator::setStream(std::istream &stream)
99 {
100  m_stream = &stream;
101 }
102 
106 inline uint64 OggIterator::startOffset() const
107 {
108  return m_startOffset;
109 }
110 
114 inline uint64 OggIterator::streamSize() const
115 {
116  return m_streamSize;
117 }
118 
122 inline const std::vector<OggPage> &OggIterator::pages() const
123 {
124  return m_pages;
125 }
126 
131 inline const OggPage &OggIterator::currentPage() const
132 {
133  return m_pages[m_page];
134 }
135 
140 inline uint64 OggIterator::currentPageOffset() const
141 {
142  return m_pages[m_page].startOffset();
143 }
144 
155 inline OggIterator::operator bool() const
156 {
157  return m_page < m_pages.size() && m_segment < m_pages[m_page].segmentSizes().size();
158 }
159 
163 inline std::vector<OggPage>::size_type OggIterator::currentPageIndex() const
164 {
165  return m_page;
166 }
167 
172 inline void OggIterator::setPageIndex(std::vector<OggPage>::size_type index)
173 {
174  const OggPage &page = m_pages[m_page = index];
175  m_segment = 0;
176  m_offset = page.startOffset() + page.headerSize();
177 }
178 
184 inline void OggIterator::setSegmentIndex(std::vector<uint32>::size_type index)
185 {
186  const OggPage &page = m_pages[m_page];
187  m_offset = page.dataOffset(m_segment = index);
188 }
189 
193 inline std::vector<uint32>::size_type OggIterator::currentSegmentIndex() const
194 {
195  return m_segment;
196 }
197 
203 {
204  return m_offset;
205 }
206 
212 {
213  return m_offset + m_bytesRead;
214 }
215 
219 inline uint64 OggIterator::tellg() const
220 {
221  return currentCharacterOffset();
222 }
223 
229 inline uint32 OggIterator::currentSegmentSize() const
230 {
231  return m_pages[m_page].segmentSizes()[m_segment];
232 }
233 
242 inline void OggIterator::setFilter(uint32 streamSerialId)
243 {
244  m_hasIdFilter = true;
245  m_idFilter = streamSerialId;
246 }
247 
253 {
254  m_hasIdFilter = false;
255 }
256 
269 {
270  return (m_pages.empty() ? m_startOffset : m_pages.back().startOffset() + m_pages.back().totalSize()) >= m_streamSize;
271 }
272 
276 inline bool OggIterator::bytesRemaining(size_t atLeast) const
277 {
278  return *this && currentCharacterOffset() + atLeast <= streamSize();
279 }
280 
285 {
286  nextSegment();
287  return *this;
288 }
289 
294 {
295  OggIterator tmp = *this;
296  nextSegment();
297  return tmp;
298 }
299 
304 {
305  previousSegment();
306  return *this;
307 }
308 
313 {
314  OggIterator tmp = *this;
315  previousSegment();
316  return tmp;
317 }
318 
322 inline bool OggIterator::matchesFilter(const OggPage &page)
323 {
324  return !m_hasIdFilter || m_idFilter == page.streamSerialNumber();
325 }
326 
327 } // namespace TagParser
328 
329 #endif // TAG_PARSER_OGGITERATOR_H
uint64 dataOffset(byte segmentIndex=0) const
Returns the data offset of the segment with the specified segmentIndex.
Definition: oggpage.h:249
void previousSegment()
Decreases the current position by one segment.
uint64 streamSize() const
Returns the stream size (which has been specified when constructing the iterator).
Definition: oggiterator.h:114
const OggPage & currentPage() const
Returns the current OGG page.
Definition: oggiterator.h:131
uint64 currentCharacterOffset() const
Returns the offset of the current character in the input stream if the iterator is valid; otherwise a...
Definition: oggiterator.h:211
void removeFilter()
Removes a previously set filter.
Definition: oggiterator.h:252
uint64 currentSegmentOffset() const
Returns the start offset of the current segment in the input stream if the iterator is valid; otherwi...
Definition: oggiterator.h:202
void nextSegment()
Increases the current position by one segment.
Definition: oggiterator.cpp:81
uint64 startOffset() const
Returns the start offset of the page.
Definition: oggpage.h:84
std::vector< uint32 >::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:193
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:163
uint64 tellg() const
Same as currentCharacterOffset(); only provided for compliance with std::istream. ...
Definition: oggiterator.h:219
bool bytesRemaining(std::size_t atLeast) const
Returns whether there are atLeast bytes remaining.
Definition: oggiterator.h:276
const std::vector< OggPage > & pages() const
Returns a vector of containing the OGG pages that have been fetched yet.
Definition: oggiterator.h:122
void setStream(std::istream &stream)
Sets the stream.
Definition: oggiterator.h:98
uint32 streamSerialNumber() const
Returns the stream serial number.
Definition: oggpage.h:156
void setPageIndex(std::vector< OggPage >::size_type index)
Sets the current page index.
Definition: oggiterator.h:172
OggIterator(std::istream &stream, uint64 startOffset, uint64 streamSize)
Constructs a new iterator for the specified stream of streamSize bytes at the specified startOffset...
Definition: oggiterator.h:70
uint64 currentPageOffset() const
Returns the start offset of the current OGG page.
Definition: oggiterator.h:140
void setFilter(uint32 streamSerialId)
Allows to filter pages by the specified streamSerialId.
Definition: oggiterator.h:242
bool areAllPagesFetched() const
Returns an indication whether all pages have been fetched.
Definition: oggiterator.h:268
std::istream & stream()
Returns the stream.
Definition: oggiterator.h:88
OggIterator & operator++()
Increments the current position by one segment if the iterator is valid; otherwise nothing happens...
Definition: oggiterator.h:284
uint32 headerSize() const
Returns the header size in byte.
Definition: oggpage.h:220
OggIterator & operator--()
Decrements the current position by one segment if the iterator is valid; otherwise nothing happens...
Definition: oggiterator.h:303
void setSegmentIndex(std::vector< uint32 >::size_type index)
Sets the current segment index.
Definition: oggiterator.h:184
uint64 startOffset() const
Returns the start offset (which has been specified when constructing the iterator).
Definition: oggiterator.h:106
uint32 currentSegmentSize() const
Returns the size of the current segment.
Definition: oggiterator.h:229
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.