bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++ |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 17 | #include <utils/threads.h> |
| 18 | #include "btree.hpp" |
| 19 | #include "MtpDebug.h" |
| 20 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 21 | Tree::Tree(MtpObjectHandle handle, MtpObjectHandle parent, const std::string& name) |
| 22 | : Node(handle, parent, name), alreadyRead(false) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 23 | } |
| 24 | |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 25 | Tree::~Tree() { |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 26 | for (std::map<MtpObjectHandle, Node*>::iterator it = entries.begin(); it != entries.end(); ++it) |
| 27 | delete it->second; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 28 | entries.clear(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | int Tree::getCount(void) { |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 32 | int count = entries.size(); |
bigbiff bigbiff | af32bb9 | 2018-12-18 18:39:53 -0500 | [diff] [blame] | 33 | MTPD("Tree::getCount::node count: %d\n", count); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 34 | return count; |
| 35 | } |
| 36 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 37 | void Tree::addEntry(Node* node) { |
| 38 | if (node->Mtpid() == 0) { |
| 39 | MTPE("Tree::addEntry: not adding node with 0 handle.\n"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 40 | return; |
| 41 | } |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 42 | if (node->Mtpid() == node->getMtpParentId()) { |
| 43 | MTPE("Tree::addEntry: not adding node with handle %u == parent.\n", node->Mtpid()); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 44 | return; |
| 45 | } |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 46 | entries[node->Mtpid()] = node; |
| 47 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 48 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 49 | Node* Tree::findEntryByName(std::string name) { |
| 50 | for (std::map<MtpObjectHandle, Node*>::iterator it = entries.begin(); it != entries.end(); ++it) |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 51 | { |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 52 | Node* node = it->second; |
| 53 | if (node->getName().compare(name) == 0 && node->Mtpid() > 0) |
| 54 | return node; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 55 | } |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 56 | return NULL; |
| 57 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 58 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 59 | Node* Tree::findNode(MtpObjectHandle handle) { |
that | a1ad19f | 2014-11-08 01:18:44 +0100 | [diff] [blame] | 60 | std::map<MtpObjectHandle, Node*>::iterator it = entries.find(handle); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 61 | if (it != entries.end()) |
| 62 | return it->second; |
| 63 | return NULL; |
| 64 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 65 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 66 | void Tree::getmtpids(MtpObjectHandleList* mtpids) { |
| 67 | for (std::map<MtpObjectHandle, Node*>::iterator it = entries.begin(); it != entries.end(); ++it) |
| 68 | mtpids->push_back(it->second->Mtpid()); |
| 69 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 70 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 71 | void Tree::deleteNode(MtpObjectHandle handle) { |
that | a1ad19f | 2014-11-08 01:18:44 +0100 | [diff] [blame] | 72 | std::map<MtpObjectHandle, Node*>::iterator it = entries.find(handle); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 73 | if (it != entries.end()) { |
| 74 | delete it->second; |
| 75 | entries.erase(it); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 76 | } |
| 77 | } |