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 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | |
| 21 | // Constructor |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 22 | Tree::Tree(MtpObjectHandle handle, MtpObjectHandle parent, const std::string& name) |
| 23 | : Node(handle, parent, name), alreadyRead(false) { |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // Destructor |
| 27 | Tree::~Tree() { |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 28 | for (std::map<MtpObjectHandle, Node*>::iterator it = entries.begin(); it != entries.end(); ++it) |
| 29 | delete it->second; |
Ethan Yonker | 726a020 | 2014-12-16 20:01:38 -0600 | [diff] [blame] | 30 | entries.clear(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | int Tree::getCount(void) { |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 34 | int count = entries.size(); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 35 | MTPD("node count: %d\n", count); |
| 36 | return count; |
| 37 | } |
| 38 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 39 | void Tree::addEntry(Node* node) { |
| 40 | if (node->Mtpid() == 0) { |
| 41 | MTPE("Tree::addEntry: not adding node with 0 handle.\n"); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 42 | return; |
| 43 | } |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 44 | if (node->Mtpid() == node->getMtpParentId()) { |
| 45 | 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] | 46 | return; |
| 47 | } |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 48 | entries[node->Mtpid()] = node; |
| 49 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 50 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 51 | Node* Tree::findEntryByName(std::string name) { |
| 52 | 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] | 53 | { |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 54 | Node* node = it->second; |
| 55 | if (node->getName().compare(name) == 0 && node->Mtpid() > 0) |
| 56 | return node; |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 57 | } |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 58 | return NULL; |
| 59 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 60 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 61 | Node* Tree::findNode(MtpObjectHandle handle) { |
that | a1ad19f | 2014-11-08 01:18:44 +0100 | [diff] [blame] | 62 | std::map<MtpObjectHandle, Node*>::iterator it = entries.find(handle); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 63 | if (it != entries.end()) |
| 64 | return it->second; |
| 65 | return NULL; |
| 66 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 67 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 68 | void Tree::getmtpids(MtpObjectHandleList* mtpids) { |
| 69 | for (std::map<MtpObjectHandle, Node*>::iterator it = entries.begin(); it != entries.end(); ++it) |
| 70 | mtpids->push_back(it->second->Mtpid()); |
| 71 | } |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 72 | |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 73 | void Tree::deleteNode(MtpObjectHandle handle) { |
that | a1ad19f | 2014-11-08 01:18:44 +0100 | [diff] [blame] | 74 | std::map<MtpObjectHandle, Node*>::iterator it = entries.find(handle); |
that | 9e0593e | 2014-10-08 00:01:24 +0200 | [diff] [blame] | 75 | if (it != entries.end()) { |
| 76 | delete it->second; |
| 77 | entries.erase(it); |
bigbiff bigbiff | c7eee6f | 2014-09-02 18:59:01 -0400 | [diff] [blame] | 78 | } |
| 79 | } |