00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00036 #ifndef LIBEBML_MASTER_H
00037 #define LIBEBML_MASTER_H
00038
00039 #include <string>
00040 #include <vector>
00041
00042 #include "EbmlTypes.h"
00043 #include "EbmlElement.h"
00044 #include "EbmlCrc32.h"
00045
00046 #define EBML_MASTER_ITERATOR std::vector<EbmlElement *>::iterator
00047 #define EBML_MASTER_CONST_ITERATOR std::vector<EbmlElement *>::const_iterator
00048 #define EBML_MASTER_RITERATOR std::vector<EbmlElement *>::reverse_iterator
00049 #define EBML_MASTER_CONST_RITERATOR std::vector<EbmlElement *>::const_reverse_iterator
00050
00051 START_LIBEBML_NAMESPACE
00052
00053 const bool bChecksumUsedByDefault = false;
00054
00059 class EBML_DLL_API EbmlMaster : public EbmlElement {
00060 public:
00061 EbmlMaster(const EbmlSemanticContext & aContext, bool bSizeIsKnown = true);
00062 EbmlMaster(const EbmlMaster & ElementToClone);
00063 virtual bool ValidateSize() const {return true;}
00067 virtual ~EbmlMaster();
00068
00069 filepos_t RenderData(IOCallback & output, bool bForceRender, bool bWithDefault = false);
00070 filepos_t ReadData(IOCallback & input, ScopeMode ReadFully);
00071 filepos_t UpdateSize(bool bWithDefault = false, bool bForceRender = false);
00072
00076 bool SetSizeInfinite(bool aIsInfinite = true) {SetSizeIsFinite(!aIsInfinite); return true;}
00077
00078 bool PushElement(EbmlElement & element);
00079 uint64 GetSize() const {
00080 if (IsFiniteSize())
00081 return EbmlElement::GetSize();
00082 else
00083 return (0-1);
00084 }
00085
00086 uint64 GetDataStart() const {
00087 return GetElementPosition() + EBML_ID_LENGTH((const EbmlId&)*this) + CodedSizeLength(GetSize(), GetSizeLength(), IsFiniteSize());
00088 }
00089
00093 EbmlElement *FindElt(const EbmlCallbacks & Callbacks) const;
00097 EbmlElement *FindFirstElt(const EbmlCallbacks & Callbacks, bool bCreateIfNull);
00098 EbmlElement *FindFirstElt(const EbmlCallbacks & Callbacks) const;
00099
00103 EbmlElement *FindNextElt(const EbmlElement & PastElt, bool bCreateIfNull);
00104 EbmlElement *FindNextElt(const EbmlElement & PastElt) const;
00105 EbmlElement *AddNewElt(const EbmlCallbacks & Callbacks);
00106
00110 bool InsertElement(EbmlElement & element, size_t position = 0);
00111 bool InsertElement(EbmlElement & element, const EbmlElement & before);
00112
00116 void Read(EbmlStream & inDataStream, const EbmlSemanticContext & Context, int & UpperEltFound, EbmlElement * & FoundElt, bool AllowDummyElt, ScopeMode ReadFully = SCOPE_ALL_DATA);
00117
00121 void Sort();
00122
00123 size_t ListSize() const {return ElementList.size();}
00124
00125 inline EBML_MASTER_ITERATOR begin() {return ElementList.begin();}
00126 inline EBML_MASTER_ITERATOR end() {return ElementList.end();}
00127 inline EBML_MASTER_RITERATOR rbegin() {return ElementList.rbegin();}
00128 inline EBML_MASTER_RITERATOR rend() {return ElementList.rend();}
00129 inline EBML_MASTER_CONST_ITERATOR begin() const {return ElementList.begin();}
00130 inline EBML_MASTER_CONST_ITERATOR end() const {return ElementList.end();}
00131 inline EBML_MASTER_CONST_RITERATOR rbegin() const {return ElementList.rbegin();}
00132 inline EBML_MASTER_CONST_RITERATOR rend() const {return ElementList.rend();}
00133
00134 EbmlElement * operator[](unsigned int position) {return ElementList[position];}
00135 const EbmlElement * operator[](unsigned int position) const {return ElementList[position];}
00136
00137 bool IsDefaultValue() const {
00138 return (ElementList.size() == 0);
00139 }
00140 virtual bool IsMaster() const {return true;}
00141
00146 bool CheckMandatory() const;
00147
00151 void Remove(size_t Index);
00152 void Remove(EBML_MASTER_ITERATOR & Itr);
00153 void Remove(EBML_MASTER_RITERATOR & Itr);
00154
00158 void RemoveAll() {ElementList.clear();}
00159
00164 filepos_t WriteHead(IOCallback & output, int SizeLength, bool bWithDefault = false);
00165
00166 void EnableChecksum(bool bIsEnabled = true) { bChecksumUsed = bIsEnabled; }
00167 bool HasChecksum() const {return bChecksumUsed;}
00168 bool VerifyChecksum() const;
00169 uint32 GetCrc32() const {return Checksum.GetCrc32();}
00170 void ForceChecksum(uint32 NewChecksum) {
00171 Checksum.ForceCrc32(NewChecksum);
00172 bChecksumUsed = true;
00173 }
00174
00178 std::vector<std::string> FindAllMissingElements();
00179
00180 #if defined(EBML_STRICT_API)
00181 private:
00182 #else
00183 protected:
00184 #endif
00185 std::vector<EbmlElement *> ElementList;
00186
00187 const EbmlSemanticContext & Context;
00188
00189 bool bChecksumUsed;
00190 EbmlCrc32 Checksum;
00191
00192 private:
00196 bool ProcessMandatory();
00197 };
00198
00200 template <typename Type>
00201 Type & GetChild(EbmlMaster & Master)
00202 {
00203 return *(static_cast<Type *>(Master.FindFirstElt(EBML_INFO(Type), true)));
00204 }
00205
00206
00207
00208 template <typename Type>
00209 Type * FindChild(EbmlMaster & Master)
00210 {
00211 return static_cast<Type *>(Master.FindFirstElt(EBML_INFO(Type), false));
00212 }
00213
00214 template <typename Type>
00215 Type & GetNextChild(EbmlMaster & Master, const Type & PastElt)
00216 {
00217 return *(static_cast<Type *>(Master.FindNextElt(PastElt, true)));
00218 }
00219
00220 template <typename Type>
00221 Type & AddNewChild(EbmlMaster & Master)
00222 {
00223 return *(static_cast<Type *>(Master.AddNewElt(EBML_INFO(Type))));
00224 }
00225
00226 END_LIBEBML_NAMESPACE
00227
00228 #endif // LIBEBML_MASTER_H