blob: b73789bd402641a54af0f9ecb33c5f66276091e5 [file] [log] [blame]
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001/*
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 bigbiffc7eee6f2014-09-02 18:59:01 -040017#include <utils/threads.h>
18#include "btree.hpp"
19#include "MtpDebug.h"
20
21// Constructor
that9e0593e2014-10-08 00:01:24 +020022Tree::Tree(MtpObjectHandle handle, MtpObjectHandle parent, const std::string& name)
23 : Node(handle, parent, name), alreadyRead(false) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040024}
25
26// Destructor
27Tree::~Tree() {
that9e0593e2014-10-08 00:01:24 +020028 for (std::map<MtpObjectHandle, Node*>::iterator it = entries.begin(); it != entries.end(); ++it)
29 delete it->second;
Ethan Yonker726a0202014-12-16 20:01:38 -060030 entries.clear();
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040031}
32
33int Tree::getCount(void) {
that9e0593e2014-10-08 00:01:24 +020034 int count = entries.size();
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040035 MTPD("node count: %d\n", count);
36 return count;
37}
38
that9e0593e2014-10-08 00:01:24 +020039void Tree::addEntry(Node* node) {
40 if (node->Mtpid() == 0) {
41 MTPE("Tree::addEntry: not adding node with 0 handle.\n");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040042 return;
43 }
that9e0593e2014-10-08 00:01:24 +020044 if (node->Mtpid() == node->getMtpParentId()) {
45 MTPE("Tree::addEntry: not adding node with handle %u == parent.\n", node->Mtpid());
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040046 return;
47 }
that9e0593e2014-10-08 00:01:24 +020048 entries[node->Mtpid()] = node;
49}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040050
that9e0593e2014-10-08 00:01:24 +020051Node* Tree::findEntryByName(std::string name) {
52 for (std::map<MtpObjectHandle, Node*>::iterator it = entries.begin(); it != entries.end(); ++it)
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040053 {
that9e0593e2014-10-08 00:01:24 +020054 Node* node = it->second;
55 if (node->getName().compare(name) == 0 && node->Mtpid() > 0)
56 return node;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040057 }
that9e0593e2014-10-08 00:01:24 +020058 return NULL;
59}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040060
that9e0593e2014-10-08 00:01:24 +020061Node* Tree::findNode(MtpObjectHandle handle) {
thata1ad19f2014-11-08 01:18:44 +010062 std::map<MtpObjectHandle, Node*>::iterator it = entries.find(handle);
that9e0593e2014-10-08 00:01:24 +020063 if (it != entries.end())
64 return it->second;
65 return NULL;
66}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040067
that9e0593e2014-10-08 00:01:24 +020068void 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 bigbiffc7eee6f2014-09-02 18:59:01 -040072
that9e0593e2014-10-08 00:01:24 +020073void Tree::deleteNode(MtpObjectHandle handle) {
thata1ad19f2014-11-08 01:18:44 +010074 std::map<MtpObjectHandle, Node*>::iterator it = entries.find(handle);
that9e0593e2014-10-08 00:01:24 +020075 if (it != entries.end()) {
76 delete it->second;
77 entries.erase(it);
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040078 }
79}