1 // NodeSetIOContext.cpp 2 3 #include "NodeSetIOContext.h" 4 5 #include <algorithm> 6 #include <cstring> 7 #include <cstdlib> 8 9 #include <Debug.h> 10 #include <MediaNode.h> 11 12 __USE_CORTEX_NAMESPACE 13 14 // -------------------------------------------------------- // 15 // *** dtor/ctor 16 // -------------------------------------------------------- // 17 18 NodeSetIOContext::~NodeSetIOContext() {} 19 20 NodeSetIOContext::NodeSetIOContext() : 21 m_nodeKeyIndex(1) {} 22 23 // -------------------------------------------------------- // 24 // *** operations 25 // -------------------------------------------------------- // 26 27 status_t NodeSetIOContext::addNode( 28 media_node_id node, 29 const char* key) { 30 31 if(node == media_node::null.node) 32 return B_BAD_VALUE; 33 34 for(node_set::const_iterator it = m_nodes.begin(); 35 it != m_nodes.end(); ++it) { 36 37 if((*it).second == node) { 38 // already in set; does key match? 39 if(key && 40 (*it).first.Length() && 41 strcmp(key, (*it).first.String()) != 0) { 42 PRINT(( 43 "!!! NodeSetIOContext::addNode(%ld, '%s'):\n" 44 " found matching node with key '%s'!\n", 45 node, key, (*it).first.String())); 46 } 47 return B_NOT_ALLOWED; 48 } 49 } 50 51 if(key) 52 m_nodes.push_back(node_entry(key, node)); 53 else { 54 char buffer[16]; 55 sprintf(buffer, "N_%03d", m_nodeKeyIndex++); 56 m_nodes.push_back(node_entry(buffer, node)); 57 } 58 59 return B_OK; 60 } 61 62 status_t NodeSetIOContext::removeNode( 63 media_node_id node) { 64 65 for(node_set::iterator it = m_nodes.begin(); 66 it != m_nodes.end(); ++it) { 67 if((*it).second == node) { 68 m_nodes.erase(it); 69 return B_OK; 70 } 71 } 72 return B_BAD_VALUE; 73 } 74 75 status_t NodeSetIOContext::removeNodeAt( 76 uint32 index) { 77 78 if(index < 0 || index >= m_nodes.size()) 79 return B_BAD_INDEX; 80 81 m_nodes.erase(m_nodes.begin() + index); 82 return B_OK; 83 } 84 85 uint32 NodeSetIOContext::countNodes() const { 86 return m_nodes.size(); 87 } 88 89 media_node_id NodeSetIOContext::nodeAt( 90 uint32 index) const { 91 92 if(index < 0 || index >= m_nodes.size()) 93 return media_node::null.node; 94 95 return m_nodes[index].second; 96 } 97 98 const char* NodeSetIOContext::keyAt( 99 uint32 index) const { 100 101 if(index < 0 || index >= m_nodes.size()) 102 return 0; 103 104 return m_nodes[index].first.String(); 105 } 106 107 status_t NodeSetIOContext::getKeyFor( 108 media_node_id node, 109 const char** outKey) const { 110 111 if(node == media_node::null.node) 112 return B_BAD_VALUE; 113 114 for(node_set::const_iterator it = m_nodes.begin(); 115 it != m_nodes.end(); ++it) { 116 117 if((*it).second == node) { 118 *outKey = (*it).first.String(); 119 return B_OK; 120 } 121 } 122 123 return B_BAD_VALUE; 124 } 125 126 status_t NodeSetIOContext::getNodeFor( 127 const char* key, 128 media_node_id* outNode) const { 129 130 if(!key || !*key) 131 return B_BAD_VALUE; 132 133 for(node_set::const_iterator it = m_nodes.begin(); 134 it != m_nodes.end(); ++it) { 135 if(!strcmp((*it).first.String(), key)) { 136 *outNode = (*it).second; 137 return B_OK; 138 } 139 } 140 141 return B_NAME_NOT_FOUND; 142 } 143 144 // -------------------------------------------------------- // 145 146 // END -- NodeSetIOContext.cpp -- 147 148