1/*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions, and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions, and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32// NodeSetIOContext.cpp
33
34#include "NodeSetIOContext.h"
35
36#include <algorithm>
37#include <cstring>
38#include <cstdlib>
39
40#include <Debug.h>
41#include <MediaNode.h>
42
43__USE_CORTEX_NAMESPACE
44
45// -------------------------------------------------------- //
46// *** dtor/ctor
47// -------------------------------------------------------- //
48
49NodeSetIOContext::~NodeSetIOContext() {}
50
51NodeSetIOContext::NodeSetIOContext() :
52	m_nodeKeyIndex(1) {}
53
54// -------------------------------------------------------- //
55// *** operations
56// -------------------------------------------------------- //
57
58status_t NodeSetIOContext::addNode(
59	media_node_id									node,
60	const char*										key) {
61
62	if(node == media_node::null.node)
63		return B_BAD_VALUE;
64
65	for(node_set::const_iterator it = m_nodes.begin();
66		it != m_nodes.end(); ++it) {
67
68		if((*it).second == node) {
69			// already in set; does key match?
70			if(key &&
71				(*it).first.Length() &&
72				strcmp(key, (*it).first.String()) != 0) {
73				PRINT((
74					"!!! NodeSetIOContext::addNode(%" B_PRId32 ", '%s'):\n"
75					"    found matching node with key '%s'!\n",
76					node, key, (*it).first.String()));
77			}
78			return B_NOT_ALLOWED;
79		}
80	}
81
82	if(key)
83		m_nodes.push_back(node_entry(key, node));
84	else {
85		char buffer[16];
86		sprintf(buffer, "N_%03d", m_nodeKeyIndex++);
87		m_nodes.push_back(node_entry(buffer, node));
88	}
89
90	return B_OK;
91}
92
93status_t NodeSetIOContext::removeNode(
94	media_node_id									node) {
95
96	for(node_set::iterator it = m_nodes.begin();
97		it != m_nodes.end(); ++it) {
98		if((*it).second == node) {
99			m_nodes.erase(it);
100			return B_OK;
101		}
102	}
103	return B_BAD_VALUE;
104}
105
106status_t NodeSetIOContext::removeNodeAt(
107	uint32												index) {
108
109	if(index < 0 || index >= m_nodes.size())
110		return B_BAD_INDEX;
111
112	m_nodes.erase(m_nodes.begin() + index);
113	return B_OK;
114}
115
116uint32 NodeSetIOContext::countNodes() const {
117	return m_nodes.size();
118}
119
120media_node_id NodeSetIOContext::nodeAt(
121	uint32												index) const {
122
123	if(index < 0 || index >= m_nodes.size())
124		return media_node::null.node;
125
126	return m_nodes[index].second;
127}
128
129const char* NodeSetIOContext::keyAt(
130	uint32												index) const {
131
132	if(index < 0 || index >= m_nodes.size())
133		return 0;
134
135	return m_nodes[index].first.String();
136}
137
138status_t NodeSetIOContext::getKeyFor(
139	media_node_id									node,
140	const char**									outKey) const {
141
142	if(node == media_node::null.node)
143		return B_BAD_VALUE;
144
145	for(node_set::const_iterator it = m_nodes.begin();
146		it != m_nodes.end(); ++it) {
147
148		if((*it).second == node) {
149			*outKey = (*it).first.String();
150			return B_OK;
151		}
152	}
153
154	return B_BAD_VALUE;
155}
156
157status_t NodeSetIOContext::getNodeFor(
158	const char*										key,
159	media_node_id*								outNode) const {
160
161	if(!key || !*key)
162		return B_BAD_VALUE;
163
164	for(node_set::const_iterator it = m_nodes.begin();
165		it != m_nodes.end(); ++it) {
166		if(!strcmp((*it).first.String(), key)) {
167			*outNode = (*it).second;
168			return B_OK;
169		}
170	}
171
172	return B_NAME_NOT_FOUND;
173}
174
175// -------------------------------------------------------- //
176
177// END -- NodeSetIOContext.cpp --
178
179