Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / starmath / source / mathml / iterator.cxx
blob24de35c183238c5986ea86c1e154b008e831e4aa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <mathml/iterator.hxx>
12 /** The purpose of this iterator is to be able to iterate threw an infinite element tree
13 * infinite -> as much as your memory can hold
14 * No call-backs that will end up in out of stack
17 namespace mathml
19 static inline void deleteElement(SmMlElement* aSmMlElement, void*) { delete aSmMlElement; }
21 static inline void cloneElement(SmMlElement* aSmMlElement, void* aData)
23 // Prepare data
24 SmMlElement* aNewSmMlElement = new SmMlElement(*aSmMlElement);
25 SmMlElement* aCopyTree = *static_cast<SmMlElement**>(aData);
27 // Append data
28 aCopyTree->setSubElement(aCopyTree->getSubElementsCount(), aNewSmMlElement);
30 // Prepare for following
31 // If it has sub elements, then it will be the next
32 if (aSmMlElement->getSubElementsCount() != 0)
33 aCopyTree = aNewSmMlElement;
34 else // Otherwise remounts up to where it should be
36 while (aSmMlElement->getParentElement() != nullptr)
38 // get parent
39 SmMlElement* pParent = aSmMlElement->getParentElement();
40 aCopyTree = aCopyTree->getParentElement();
41 // was this the last branch ?
42 if (aSmMlElement->getSubElementId() + 1 != pParent->getSubElementsCount())
43 break; // no -> stop going up
44 // Prepare for next round
45 aSmMlElement = pParent;
49 // Closing extras
50 *static_cast<SmMlElement**>(aData) = aCopyTree;
53 void SmMlIteratorFree(SmMlElement* pMlElementTree)
55 if (pMlElementTree == nullptr)
56 return;
57 for (size_t i = 0; i < pMlElementTree->getSubElementsCount(); ++i)
59 SmMlIteratorFree(pMlElementTree->getSubElement(i));
61 deleteElement(pMlElementTree, nullptr);
64 SmMlElement* SmMlIteratorCopy(SmMlElement* pMlElementTree)
66 if (pMlElementTree == nullptr)
67 return nullptr;
68 SmMlElement* aDummyElement = new SmMlElement();
69 SmMlIteratorTopToBottom(pMlElementTree, cloneElement, &aDummyElement);
70 SmMlElement* aResultElement = aDummyElement->getSubElement(0);
71 delete aDummyElement;
72 return aResultElement;
75 } // end namespace mathml
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */