4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Node non-inline non-template implementation */
33 ///////////////////////////////cc.p////////////////////////////////////////
35 prefix_ senf::console::DirectoryNode & senf::console::root()
37 static DirectoryNode::ptr rootNode(new DirectoryNode());
41 ///////////////////////////////////////////////////////////////////////////
42 // senf::console::GenericNode
44 prefix_ std::string senf::console::GenericNode::path()
47 std::string path (name());
50 path = node->name() + "/" + path;
51 node = node->parent();
53 return path.empty() ? "/" : path;
56 prefix_ std::string senf::console::GenericNode::path(DirectoryNode const & root)
60 cptr node (thisptr());
61 while (node && node != root.thisptr()) {
63 path = node->name() + "/" + path;
66 node = node->parent();
68 if (path.empty() || path[0] != '/')
73 prefix_ bool senf::console::GenericNode::active()
76 cptr node (thisptr());
77 while (node->parent())
78 node = node->parent();
79 return node == root().thisptr();
82 prefix_ bool senf::console::GenericNode::isChildOf(DirectoryNode & parent)
85 cptr node (thisptr());
86 while (node && node != parent.thisptr())
87 node = node->parent();
91 ///////////////////////////////////////////////////////////////////////////
92 // senf::console::LinkNode
94 prefix_ void senf::console::LinkNode::v_help(std::ostream & os)
101 ///////////////////////////////////////////////////////////////////////////
102 //senf::console::DirectoryNode
104 prefix_ senf::console::GenericNode::ptr
105 senf::console::DirectoryNode::remove(std::string const & name)
107 ChildMap::iterator i (children_.find(name));
108 if (i == children_.end())
109 throw UnknownNodeNameException() << ": '" << name << "'";
110 GenericNode::ptr node (i->second);
117 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
119 BOOST_ASSERT( ! node->parent() );
120 if (node->name().empty()) {
121 node->name("unnamed");
122 SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
124 if (children_.find(node->name()) != children_.end()) {
129 newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
130 } while (children_.find(newName) != children_.end());
131 SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '"
135 children_.insert(std::make_pair(node->name(),node));
136 node->parent_ = this;
139 prefix_ senf::console::GenericNode &
140 senf::console::DirectoryNode::getLink(std::string const & name)
143 ChildMap::const_iterator i (children_.find(name));
144 if (i == children_.end())
145 throw UnknownNodeNameException() << ": '" << name << "'";
149 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
152 output << doc_ << "\n";
155 ///////////////////////////////////////////////////////////////////////////
156 // senf::console::detail::NodeTraverser
158 prefix_ void senf::console::detail::NodeTraverser::operator()(std::string const & name)
162 if (name == std::string("")) {
163 dir_ = root_.thisptr();
167 if (! elt_.empty()) {
169 dir_ = dir_->parent();
170 if (! dir_ || ! dir_->isChildOf(root_))
171 dir_ = root_.thisptr();
173 else if (elt_ != "" && elt_ != ".") {
174 if (! dir_->hasChild(elt_) && autocomplete_) {
175 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
176 if (completions.size() == 1)
177 elt_ = completions.begin()->first;
179 // Why does g++ give an error on this line ???? :
180 // dir = dynamic_cast<DirectoryNode&>( dir->get(name) ).thisptr();
181 DirectoryNode & d (dynamic_cast<DirectoryNode&>( dir_->get(elt_) ));
188 prefix_ senf::console::GenericNode & senf::console::detail::NodeTraverser::node()
190 if (elt_ != "" && elt_ != ".") {
191 if (! dir_->hasChild(elt_) && autocomplete_) {
192 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
193 if (completions.size() == 1)
194 elt_ = completions.begin()->first;
196 return dir_->get(elt_);
202 ///////////////////////////////////////////////////////////////////////////
203 // senf::console::SimpleCommandNode
205 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
208 output << doc_ << "\n";
211 prefix_ void senf::console::SimpleCommandNode::v_execute(std::ostream & output,
212 ParseCommandInfo const & command)
215 fn_(output, command);
218 ///////////////////////////////cc.e////////////////////////////////////////
220 //#include "Node.mpp"
226 // comment-column: 40
227 // c-file-style: "senf"
228 // indent-tabs-mode: nil
229 // ispell-local-dictionary: "american"
230 // compile-command: "scons -u test"