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 */
28 #include "../../Utils/range.hh"
34 ///////////////////////////////cc.p////////////////////////////////////////
36 prefix_ senf::console::DirectoryNode & senf::console::root()
38 static DirectoryNode::ptr rootNode(new DirectoryNode());
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::console::GenericNode
45 prefix_ std::string senf::console::GenericNode::path()
48 std::string path (name());
51 path = node->name() + "/" + path;
52 node = node->parent();
54 return path.empty() ? "/" : path;
57 prefix_ std::string senf::console::GenericNode::path(DirectoryNode const & root)
61 cptr node (thisptr());
62 while (node && node != root.thisptr()) {
64 path = node->name() + "/" + path;
67 node = node->parent();
69 if (path.empty() || path[0] != '/')
74 prefix_ bool senf::console::GenericNode::active()
77 cptr node (thisptr());
78 while (node->parent())
79 node = node->parent();
80 return node == root().thisptr();
83 prefix_ bool senf::console::GenericNode::isChildOf(DirectoryNode & parent)
86 cptr node (thisptr());
87 while (node && node != parent.thisptr())
88 node = node->parent();
92 ///////////////////////////////////////////////////////////////////////////
93 // senf::console::LinkNode
95 prefix_ void senf::console::LinkNode::v_help(std::ostream & os)
102 ///////////////////////////////////////////////////////////////////////////
103 //senf::console::DirectoryNode
105 prefix_ senf::console::GenericNode::ptr
106 senf::console::DirectoryNode::remove(std::string const & name)
108 ChildMap::iterator i (children_.find(name));
109 if (i == children_.end())
110 throw UnknownNodeNameException() << ": '" << name << "'";
111 GenericNode::ptr node (i->second);
118 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
120 BOOST_ASSERT( ! node->parent() );
121 if (node->name().empty()) {
122 node->name("unnamed");
123 SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
125 if (children_.find(node->name()) != children_.end()) {
130 newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
131 } while (children_.find(newName) != children_.end());
132 SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '"
136 children_.insert(std::make_pair(node->name(),node));
137 node->parent_ = this;
140 prefix_ senf::console::GenericNode &
141 senf::console::DirectoryNode::getLink(std::string const & name)
144 ChildMap::const_iterator i (children_.find(name));
145 if (i == children_.end())
146 throw UnknownNodeNameException() << ": '" << name << "'";
150 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
153 output << doc_ << "\n";
156 ///////////////////////////////////////////////////////////////////////////
157 // senf::console::detail::NodeTraverser
159 prefix_ void senf::console::detail::NodeTraverser::operator()(std::string const & name)
163 if (name == std::string("")) {
164 dir_ = root_.thisptr();
168 if (! elt_.empty()) {
170 dir_ = dir_->parent();
171 if (! dir_ || ! dir_->isChildOf(root_))
172 dir_ = root_.thisptr();
174 else if (elt_ != "" && elt_ != ".") {
175 if (! dir_->hasChild(elt_) && autocomplete_) {
176 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
177 if (has_one_elt(completions))
178 elt_ = completions.begin()->first;
180 // Why does g++ give an error on this line ???? :
181 // dir = dynamic_cast<DirectoryNode&>( dir->get(name) ).thisptr();
182 DirectoryNode & d (dynamic_cast<DirectoryNode&>( dir_->get(elt_) ));
189 prefix_ senf::console::GenericNode & senf::console::detail::NodeTraverser::node()
191 if (elt_ != "" && elt_ != ".") {
192 if (! dir_->hasChild(elt_) && autocomplete_) {
193 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
194 if (has_one_elt(completions))
195 elt_ = completions.begin()->first;
197 return dir_->get(elt_);
203 ///////////////////////////////////////////////////////////////////////////
204 // senf::console::SimpleCommandNode
206 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
209 output << doc_ << "\n";
212 prefix_ void senf::console::SimpleCommandNode::v_execute(boost::any & rv, std::ostream & os,
213 ParseCommandInfo const & command)
219 ///////////////////////////////cc.e////////////////////////////////////////
221 //#include "Node.mpp"
227 // comment-column: 40
228 // c-file-style: "senf"
229 // indent-tabs-mode: nil
230 // ispell-local-dictionary: "american"
231 // compile-command: "scons -u test"