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());
43 void dodump(std::ostream & output, unsigned level, senf::console::DirectoryNode & node)
45 std::string pad (2*level, ' ');
46 senf::console::DirectoryNode::child_iterator i (node.children().begin());
47 senf::console::DirectoryNode::child_iterator const i_end (node.children().end());
48 for (; i != i_end; ++i) {
49 output << pad << i->first;
50 if (i->second->isDirectory()) {
52 dodump(output, level+1,static_cast<senf::console::DirectoryNode&>(*i->second));
54 else if (i->second->isLink())
55 output << "@ -> " << i->second->followLink().path() << '\n';
62 prefix_ void senf::console::dump(std::ostream & os, DirectoryNode & dir)
67 ///////////////////////////////////////////////////////////////////////////
68 // senf::console::GenericNode
70 prefix_ std::string senf::console::GenericNode::path()
73 std::string path (name());
76 path = node->name() + "/" + path;
77 node = node->parent();
79 return path.empty() ? "/" : path;
82 prefix_ std::string senf::console::GenericNode::path(DirectoryNode const & root)
86 cptr node (thisptr());
87 while (node && node != root.thisptr()) {
89 path = node->name() + "/" + path;
92 node = node->parent();
94 if (path.empty() || path[0] != '/')
99 prefix_ bool senf::console::GenericNode::active()
102 cptr node (thisptr());
103 while (node->parent())
104 node = node->parent();
105 return node == root().thisptr();
108 prefix_ bool senf::console::GenericNode::isChildOf(DirectoryNode & parent)
111 cptr node (thisptr());
112 while (node && node != parent.thisptr())
113 node = node->parent();
117 ///////////////////////////////////////////////////////////////////////////
118 // senf::console::LinkNode
120 prefix_ void senf::console::LinkNode::v_help(std::ostream & os)
126 prefix_ std::string senf::console::LinkNode::v_shorthelp()
129 return follow().shorthelp();
132 ///////////////////////////////////////////////////////////////////////////
133 //senf::console::DirectoryNode
135 prefix_ senf::console::DirectoryNode::~DirectoryNode()
137 ChildMap::iterator i (children_.begin());
138 ChildMap::iterator const i_end (children_.end());
139 for (; i != i_end; ++i)
140 i->second->parent_ = 0;
143 prefix_ senf::console::GenericNode::ptr
144 senf::console::DirectoryNode::remove(std::string const & name)
146 ChildMap::iterator i (children_.find(name));
147 if (i == children_.end())
148 throw UnknownNodeNameException() << ": '" << name << "'";
149 GenericNode::ptr node (i->second);
156 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
158 BOOST_ASSERT( ! node->parent() );
159 if (node->name().empty()) {
160 node->name("unnamed");
161 SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
163 if (children_.find(node->name()) != children_.end()) {
168 newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
169 } while (children_.find(newName) != children_.end());
170 SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '"
174 children_.insert(std::make_pair(node->name(),node));
175 node->parent_ = this;
178 prefix_ senf::console::GenericNode &
179 senf::console::DirectoryNode::getLink(std::string const & name)
182 ChildMap::const_iterator i (children_.find(name));
183 if (i == children_.end())
184 throw UnknownNodeNameException() << ": '" << name << "'";
188 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
191 output << doc_ << "\n";
194 prefix_ std::string senf::console::DirectoryNode::v_shorthelp()
197 if (! shortdoc_.empty())
199 return doc_.substr(0,doc_.find('\n'));
202 ///////////////////////////////////////////////////////////////////////////
203 // senf::console::detail::NodeTraverser
206 prefix_ void senf::console::detail::NodeTraverser::operator()(std::string const & name)
210 if (name == std::string("")) {
211 dir_ = root_.thisptr();
215 if (! elt_.empty()) {
217 dir_ = dir_->parent();
218 if (! dir_ || ! dir_->isChildOf(root_))
219 dir_ = root_.thisptr();
221 else if (elt_ != "" && elt_ != ".") {
222 if (! dir_->hasChild(elt_) && autocomplete_) {
223 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
224 if (has_one_elt(completions))
225 elt_ = completions.begin()->first;
227 // Why does g++ give an error on this line ???? :
228 // dir = dynamic_cast<DirectoryNode&>( dir->get(name) ).thisptr();
229 DirectoryNode & d (dynamic_cast<DirectoryNode&>( dir_->get(elt_) ));
236 prefix_ senf::console::GenericNode & senf::console::detail::NodeTraverser::node()
238 if (elt_ != "" && elt_ != ".") {
239 if (! dir_->hasChild(elt_) && autocomplete_) {
240 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
241 if (has_one_elt(completions))
242 elt_ = completions.begin()->first;
244 return dir_->get(elt_);
251 ///////////////////////////////////////////////////////////////////////////
252 // senf::console::SimpleCommandNode
254 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
257 output << doc_ << "\n";
260 prefix_ std::string senf::console::SimpleCommandNode::v_shorthelp()
263 if (! shortdoc_.empty())
265 return doc_.substr(0,doc_.find('\n'));
268 prefix_ void senf::console::SimpleCommandNode::v_execute(boost::any & rv, std::ostream & os,
269 ParseCommandInfo const & command)
275 ///////////////////////////////cc.e////////////////////////////////////////
277 //#include "Node.mpp"
283 // comment-column: 40
284 // c-file-style: "senf"
285 // indent-tabs-mode: nil
286 // ispell-local-dictionary: "american"
287 // compile-command: "scons -u test"