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