|
| 1 | +/*************************************************************************** |
| 2 | + copyright : (C) 2025 by Urs Fleisch |
| 3 | + email : ufleisch@users.sourceforge.net |
| 4 | + ***************************************************************************/ |
| 5 | + |
| 6 | +/*************************************************************************** |
| 7 | + * This library is free software; you can redistribute it and/or modify * |
| 8 | + * it under the terms of the GNU Lesser General Public License version * |
| 9 | + * 2.1 as published by the Free Software Foundation. * |
| 10 | + * * |
| 11 | + * This library is distributed in the hope that it will be useful, but * |
| 12 | + * WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 | + * Lesser General Public License for more details. * |
| 15 | + * * |
| 16 | + * You should have received a copy of the GNU Lesser General Public * |
| 17 | + * License along with this library; if not, write to the Free Software * |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * |
| 19 | + * 02110-1301 USA * |
| 20 | + * * |
| 21 | + * Alternatively, this file is available under the Mozilla Public * |
| 22 | + * License Version 1.1. You may obtain a copy of the License at * |
| 23 | + * http://www.mozilla.org/MPL/ * |
| 24 | + ***************************************************************************/ |
| 25 | + |
| 26 | +#include "ebmlmkchapters.h" |
| 27 | +#include "ebmlstringelement.h" |
| 28 | +#include "ebmluintelement.h" |
| 29 | +#include "matroskachapters.h" |
| 30 | +#include "matroskachapteredition.h" |
| 31 | + |
| 32 | +using namespace TagLib; |
| 33 | + |
| 34 | +std::unique_ptr<Matroska::Chapters> EBML::MkChapters::parse() |
| 35 | +{ |
| 36 | + auto chapters = std::make_unique<Matroska::Chapters>(); |
| 37 | + chapters->setOffset(offset); |
| 38 | + chapters->setSize(getSize()); |
| 39 | + |
| 40 | + for(const auto &element : elements) { |
| 41 | + if(element->getId() != Id::MkEditionEntry) |
| 42 | + continue; |
| 43 | + |
| 44 | + List<Matroska::Chapter> editionChapters; |
| 45 | + Matroska::ChapterEdition::UID editionUid = 0; |
| 46 | + bool editionIsDefault = false; |
| 47 | + bool editionIsOrdered = false; |
| 48 | + auto edition = element_cast<Id::MkEditionEntry>(element); |
| 49 | + for(const auto &editionChild : *edition) { |
| 50 | + Id id = editionChild->getId(); |
| 51 | + if(id == Id::MkEditionUID) |
| 52 | + editionUid = element_cast<Id::MkEditionUID>(editionChild)->getValue(); |
| 53 | + else if(id == Id::MkEditionFlagDefault) |
| 54 | + editionIsDefault = element_cast<Id::MkEditionFlagDefault>(editionChild)->getValue() != 0; |
| 55 | + else if(id == Id::MkEditionFlagOrdered) |
| 56 | + editionIsOrdered = element_cast<Id::MkEditionFlagOrdered>(editionChild)->getValue() != 0; |
| 57 | + else if(id == Id::MkChapterAtom) { |
| 58 | + Matroska::Chapter::UID chapterUid = 0; |
| 59 | + Matroska::Chapter::Time chapterTimeStart = 0; |
| 60 | + Matroska::Chapter::Time chapterTimeEnd = 0; |
| 61 | + List<Matroska::Chapter::Display> chapterDisplays; |
| 62 | + bool chapterHidden = false; |
| 63 | + auto chapterAtom = element_cast<Id::MkChapterAtom>(editionChild); |
| 64 | + for(const auto &chapterChild : *chapterAtom) { |
| 65 | + Id cid = chapterChild->getId(); |
| 66 | + if(cid == Id::MkChapterUID) |
| 67 | + chapterUid = element_cast<Id::MkChapterUID>(chapterChild)->getValue(); |
| 68 | + else if(cid == Id::MkChapterTimeStart) |
| 69 | + chapterTimeStart = element_cast<Id::MkChapterTimeStart>(chapterChild)->getValue(); |
| 70 | + else if(cid == Id::MkChapterTimeEnd) |
| 71 | + chapterTimeEnd = element_cast<Id::MkChapterTimeEnd>(chapterChild)->getValue(); |
| 72 | + else if(cid == Id::MkChapterFlagHidden) |
| 73 | + chapterHidden = element_cast<Id::MkChapterFlagHidden>(chapterChild)->getValue() != 0; |
| 74 | + else if(cid == Id::MkChapterDisplay) { |
| 75 | + auto display = element_cast<Id::MkChapterDisplay>(chapterChild); |
| 76 | + String displayString; |
| 77 | + String displayLanguage; |
| 78 | + for(const auto &displayChild : *display) { |
| 79 | + Id did = displayChild->getId(); |
| 80 | + if(did == Id::MkChapString) |
| 81 | + displayString = element_cast<Id::MkChapString>(displayChild)->getValue(); |
| 82 | + else if(did == Id::MkChapLanguage) |
| 83 | + displayLanguage = element_cast<Id::MkChapLanguage>(displayChild)->getValue(); |
| 84 | + } |
| 85 | + if(!displayString.isEmpty()) { |
| 86 | + chapterDisplays.append(Matroska::Chapter::Display(displayString, displayLanguage)); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + if(chapterUid) { |
| 91 | + editionChapters.append(Matroska::Chapter( |
| 92 | + chapterTimeStart, chapterTimeEnd, chapterDisplays, chapterUid, chapterHidden)); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + if(!editionChapters.isEmpty()) { |
| 97 | + chapters->addChapterEdition(Matroska::ChapterEdition( |
| 98 | + editionChapters, editionIsDefault, editionIsOrdered, editionUid)); |
| 99 | + } |
| 100 | + } |
| 101 | + return chapters; |
| 102 | +} |
0 commit comments