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::DirectoryNode
94 prefix_ senf::console::GenericNode::ptr
95 senf::console::DirectoryNode::remove(std::string const & name)
97 ChildMap::iterator i (children_.find(name));
98 if (i == children_.end())
99 throw UnknownNodeNameException() << ": '" << name << "'";
100 GenericNode::ptr node (i->second);
107 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
109 BOOST_ASSERT( ! node->parent() );
110 if (node->name().empty()) {
111 node->name("unnamed");
112 SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
114 if (children_.find(node->name()) != children_.end()) {
119 newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
120 } while (children_.find(newName) != children_.end());
121 SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '"
125 children_.insert(std::make_pair(node->name(),node));
126 node->parent_ = this;
129 prefix_ senf::console::GenericNode &
130 senf::console::DirectoryNode::get(std::string const & name)
133 ChildMap::const_iterator i (children_.find(name));
134 if (i == children_.end())
135 throw UnknownNodeNameException() << ": '" << name << "'";
139 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
142 output << doc_ << "\n";
145 ///////////////////////////////////////////////////////////////////////////
146 // senf::console::detail::NodeTraverser
148 prefix_ void senf::console::detail::NodeTraverser::operator()(std::string const & name)
152 if (name == std::string("")) {
153 dir_ = root_.thisptr();
157 if (! elt_.empty()) {
159 dir_ = dir_->parent();
160 if (! dir_ || ! dir_->isChildOf(root_))
161 dir_ = root_.thisptr();
163 else if (elt_ != "" && elt_ != ".") {
164 if (! dir_->hasChild(elt_) && autocomplete_) {
165 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
166 if (completions.size() == 1)
167 elt_ = completions.begin()->first;
169 // Why does g++ give an error on this line ???? :
170 // dir = dynamic_cast<DirectoryNode&>( dir->get(name) ).thisptr();
171 DirectoryNode & d (dynamic_cast<DirectoryNode&>( dir_->get(elt_) ));
178 prefix_ senf::console::GenericNode & senf::console::detail::NodeTraverser::node()
180 if (elt_ != "" && elt_ != ".") {
181 if (! dir_->hasChild(elt_) && autocomplete_) {
182 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
183 if (completions.size() == 1)
184 elt_ = completions.begin()->first;
186 return dir_->get(elt_);
192 ///////////////////////////////////////////////////////////////////////////
193 // senf::console::SyntaxErrorException
195 prefix_ char const * senf::console::SyntaxErrorException::what()
198 return message().empty() ? "syntax error" : message().c_str();
201 ///////////////////////////////////////////////////////////////////////////
202 // senf::console::SimpleCommandNode
204 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
207 output << doc_ << "\n";
210 prefix_ void senf::console::SimpleCommandNode::v_execute(std::ostream & output,
211 ParseCommandInfo const & command)
214 fn_(output, command);
217 ///////////////////////////////cc.e////////////////////////////////////////
219 //#include "Node.mpp"
225 // comment-column: 40
226 // c-file-style: "senf"
227 // indent-tabs-mode: nil
228 // ispell-local-dictionary: "american"
229 // compile-command: "scons -u test"