5 #include "../exceptions.h"
7 #include <c++utilities/io/binarywriter.h>
24 MatroskaTagField::MatroskaTagField()
31 MatroskaTagField::MatroskaTagField(
const string &
id,
const TagValue &value)
48 string context(
"parsing Matroska tag field");
49 simpleTagElement.
parse(diag);
50 bool tagDefaultFound =
false, tagLanguageFound =
false, tagLanguageIETFFound =
false;
55 diag.emplace_back(
DiagLevel::Critical,
"Unable to parse children of \"SimpleTag\"-element.", context);
58 switch (child->id()) {
61 setId(child->readString());
62 context =
"parsing Matroska tag field " +
id();
65 "\"SimpleTag\"-element contains multiple \"TagName\"-elements. Surplus TagName elements will be ignored.", context);
70 if (
value().isEmpty()) {
71 unique_ptr<char[]> buffer = make_unique<char[]>(child->dataSize());
72 child->stream().seekg(
static_cast<streamoff
>(child->dataOffset()));
73 child->stream().read(buffer.get(),
static_cast<streamoff
>(child->dataSize()));
74 switch (child->id()) {
84 "\"SimpleTag\"-element contains multiple \"TagString\"/\"TagBinary\"-elements. Surplus \"TagName\"/\"TagBinary\"-elements will "
90 if (!tagLanguageFound) {
91 tagLanguageFound =
true;
98 "\"SimpleTag\"-element contains multiple \"TagLanguage\"-elements. Surplus \"TagLanguage\"-elements will be ignored.", context);
102 if (!tagLanguageIETFFound) {
103 tagLanguageIETFFound =
true;
107 "\"SimpleTag\"-element contains multiple \"TagLanguageIETF\"-elements. Surplus \"TagLanguageIETF\"-elements will be ignored.",
112 if (!tagDefaultFound) {
114 tagDefaultFound =
true;
117 "\"SimpleTag\"-element contains multiple \"TagDefault\" elements. Surplus \"TagDefault\"-elements will be ignored.", context);
121 if (parseNestedFields) {
126 "Nested fields are currently not supported. Nested tags can not be displayed and will be discarded when rewriting the file.",
136 "\"SimpleTag\"-element contains unknown element ", child->idToString(),
" at ", child->startOffset(),
". It will be ignored."),
154 static const string context(
"making Matroska \"SimpleTag\" element.");
157 diag.emplace_back(
DiagLevel::Critical,
"Can not make \"SimpleTag\" element with empty \"TagName\".", context);
162 }
catch (
const ConversionException &) {
163 diag.emplace_back(
DiagLevel::Critical,
"The assigned tag value can not be converted to be written appropriately.", context);
197 m_stringValue = m_field.value().toString();
198 }
catch (
const ConversionException &) {
200 "The assigned tag value can not be converted to a string and is treated as binary value (which is likely not what you want since "
201 "official Matroska specifiecation doesn't list any binary fields).",
202 "making Matroska \"SimpleTag\" element.");
207 const auto languageSize = m_language.empty() ? 3 : m_language.size();
210 const auto languageIETFElementSize
217 + m_field.id().size()
219 + languageElementSize
220 + languageIETFElementSize
229 m_nestedMaker.emplace_back(nestedField.prepareMaking(diag));
230 m_simpleTagSize += m_nestedMaker.back().m_totalSize;
244 BinaryWriter writer(&stream);
249 stream.write(buff, sizeDenotationLen);
253 stream.write(buff, sizeDenotationLen);
254 stream.write(m_field.
id().c_str(),
static_cast<std::streamsize
>(m_field.
id().size()));
257 if (m_language.empty()) {
258 stream.put(
static_cast<ostream::char_type
>(0x80 | 3));
259 stream.write(
"und", 3);
262 stream.write(buff, sizeDenotationLen);
263 stream.write(m_language.data(),
static_cast<std::streamsize
>(m_language.size()));
266 if (!m_languageIETF.empty()) {
269 stream.write(buff, sizeDenotationLen);
270 stream.write(m_languageIETF.data(),
static_cast<std::streamsize
>(m_languageIETF.size()));
274 stream.put(
static_cast<ostream::char_type
>(0x80 | 1));
280 stream.write(buff, sizeDenotationLen);
285 stream.write(buff, sizeDenotationLen);
286 stream.write(m_stringValue.data(),
static_cast<std::streamsize
>(m_stringValue.size()));
289 for (
const auto &maker : m_nestedMaker) {
The Diagnostics class is a container for DiagMessage.
The EbmlElement class helps to parse EBML files such as Matroska files.
static std::uint8_t calculateSizeDenotationLength(std::uint64_t size)
Returns the length of the size denotation for the specified size in byte.
static std::uint8_t makeSizeDenotation(std::uint64_t size, char *buff)
Makes the size denotation for the specified size and stores it to buff.
The class inherits from std::exception and serves as base class for exceptions thrown by the elements...
ImplementationType * nextSibling()
Returns the next sibling of the element.
ImplementationType * firstChild()
Returns the first child of the element.
void parse(Diagnostics &diag)
Parses the header information of the element which is read from the related stream at the start offse...
The exception that is thrown when the data to be parsed or to be made seems invalid and therefore can...
The MatroskaTagFieldMaker class helps making tag fields.
void make(std::ostream &stream) const
Saves the field (specified when constructing the object) to the specified stream (makes a "SimpleTag"...
The MatroskaTagField class is used by MatroskaTag to store the fields.
void reparse(EbmlElement &simpleTagElement, Diagnostics &diag, bool parseNestedFields=true)
Parses field information from the specified EbmlElement.
MatroskaTagFieldMaker prepareMaking(Diagnostics &diag)
Prepares making.
void make(std::ostream &stream, Diagnostics &diag)
Saves the field to the specified stream (makes a "SimpleTag" element).
The TagField class is used by FieldMapBasedTag to store the fields.
void setDefault(bool isDefault)
Sets whether the field is labeled as default.
const IdentifierType & id() const
Returns the id of the current TagField.
const std::vector< MatroskaTagField > & nestedFields() const
Returns the nested fields.
void setId(const IdentifierType &id)
Sets the id of the current Tag Field.
bool isDefault() const
Returns an indication whether the field is labeled as default.
TagValue & value()
Returns the value of the current TagField.
The TagValue class wraps values of different types.
void assignData(const char *data, std::size_t length, TagDataType type=TagDataType::Binary, TagTextEncoding encoding=TagTextEncoding::Latin1)
std::size_t dataSize() const
Returns the size of the assigned value in bytes.
const Locale & locale() const
Returns the locale.
char * dataPointer()
Returns a pointer to the raw data assigned to the current instance.
constexpr TAG_PARSER_EXPORT std::string_view language()
Contains all classes and functions of the TagInfo library.
LocaleFormat
The LocaleFormat enum class specifies the format used by a LocaleDetail.