Tag Parser  7.0.3
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
ebmlelement.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_EBMLELEMENT_H
2 #define TAG_PARSER_EBMLELEMENT_H
3 
4 #include "./ebmlid.h"
5 #include "./matroskaid.h"
6 
7 #include "../genericfileelement.h"
8 
9 #include <c++utilities/conversion/stringbuilder.h>
10 #include <c++utilities/conversion/stringconversion.h>
11 #include <c++utilities/conversion/types.h>
12 
13 #include <iostream>
14 #include <memory>
15 
16 namespace TagParser {
17 
18 class EbmlElement;
19 class MatroskaContainer;
20 
25 public:
27  typedef uint32 IdentifierType;
28  typedef uint64 DataSizeType;
29 };
30 
31 class TAG_PARSER_EXPORT EbmlElement : public GenericFileElement<EbmlElement> {
33 
34 public:
35  EbmlElement(MatroskaContainer &container, uint64 startOffset);
36 
37  std::string idToString() const;
38  bool isParent() const;
39  bool isPadding() const;
40  uint64 firstChildOffset() const;
41  std::string readString();
42  uint64 readUInteger();
43  float64 readFloat();
44 
45  static byte calculateIdLength(IdentifierType id);
46  static byte calculateSizeDenotationLength(uint64 size);
47  static byte makeId(IdentifierType id, char *buff);
48  static byte makeSizeDenotation(uint64 size, char *buff);
49  static byte makeSizeDenotation(uint64 size, char *buff, byte minBytes);
50  static byte calculateUIntegerLength(uint64 integer);
51  static byte makeUInteger(uint64 value, char *buff);
52  static byte makeUInteger(uint64 value, char *buff, byte minBytes);
53  static void makeSimpleElement(std::ostream &stream, IdentifierType id, uint64 content);
54  static void makeSimpleElement(std::ostream &stream, IdentifierType id, const std::string &content);
55  static void makeSimpleElement(std::ostream &stream, IdentifierType id, const char *data, std::size_t dataSize);
56  static uint64 bytesToBeSkipped;
57 
58 protected:
59  EbmlElement(EbmlElement &parent, uint64 startOffset);
60  EbmlElement(MatroskaContainer &container, uint64 startOffset, uint64 maxSize);
61 
62  void internalParse(Diagnostics &diag);
63 
64 private:
65  std::string parsingContext() const;
66 };
67 
71 inline std::string EbmlElement::idToString() const
72 {
73  using namespace ConversionUtilities;
74  const char *const name = matroskaIdName(id());
75  if (*name) {
76  return argsToString('0', 'x', numberToString(id(), 16), ' ', '\"', name, '\"');
77  } else {
78  return "0x" + numberToString(id(), 16);
79  }
80 }
81 
88 inline bool EbmlElement::isParent() const
89 {
90  using namespace EbmlIds;
91  using namespace MatroskaIds;
92  switch (id()) {
93  case Header:
94  case SignatureSlot:
95  case SignatureElements:
97  case Segment:
98  case SeekHead:
99  case Seek:
100  case SegmentInfo:
101  case ChapterTranslate:
102  case Cluster:
103  case SilentTracks:
104  case BlockGroup:
105  case BlockAdditions:
106  case BlockMore:
107  case Slices:
108  case TimeSlice:
109  case ReferenceFrame:
110  case Tracks:
111  case TrackEntry:
112  case TrackTranslate:
113  case TrackVideo:
114  case TrackAudio:
115  case TrackOperation:
116  case TrackCombinePlanes:
117  case TrackPlane:
118  case TrackJoinBlocks:
119  case ContentEncodings:
120  case ContentEncoding:
121  case ContentCompression:
122  case ContentEncryption:
123  case Cues:
124  case CuePoint:
125  case CueTrackPositions:
126  case CueReference:
127  case Attachments:
128  case AttachedFile:
129  case Chapters:
130  case EditionEntry:
131  case ChapterAtom:
132  case ChapterTrack:
133  case ChapterDisplay:
134  case ChapProcess:
135  case ChapProcessCommand:
136  case Tags:
137  case MatroskaIds::Tag:
138  case Targets:
139  case SimpleTag:
140  return true;
141  default:
142  return false;
143  }
144 }
145 
149 inline bool EbmlElement::isPadding() const
150 {
151  return id() == EbmlIds::Void;
152 }
153 
158 inline uint64 EbmlElement::firstChildOffset() const
159 {
160  return isParent() ? (idLength() + sizeLength()) : 0;
161 }
162 
163 } // namespace TagParser
164 
165 #endif // TAG_PARSER_EBMLELEMENT_H
uint32 sizeLength() const
Returns the length of the size denotation of the element in byte.
std::string idToString() const
Converts the specified EBML ID to a printable string.
Definition: ebmlelement.h:71
uint64 firstChildOffset() const
Returns the offset of the first child of the element.
Definition: ebmlelement.h:158
uint32 idLength() const
Returns the length of the id denotation in byte.
bool isPadding() const
Returns an indication whether the element is considered as padding.
Definition: ebmlelement.h:149
static uint64 bytesToBeSkipped
Specifies the number of bytes to be skipped till a valid EBML element is found in the stream...
Definition: ebmlelement.h:56
const IdentifierType & id() const
Returns the element ID.
FileElementTraits< ImplementationType >::IdentifierType IdentifierType
Specifies the type used to store identifiers.
bool isParent() const
Returns an indication whether the element is a parent element.
Definition: ebmlelement.h:88
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
TAG_PARSER_EXPORT const char * matroskaIdName(uint32 matroskaId)
Returns a string for the specified matroskaId if known; otherwise returns an empty string...
Definition: matroskaid.cpp:22