4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief Node non-inline non-template implementation */
35 #include <senf/Utils/Range.hh>
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 prefix_ senf::console::DirectoryNode & senf::console::root()
43 static DirectoryNode::ptr rootNode (new DirectoryNode());
48 void dodump(std::ostream & output, unsigned level, senf::console::DirectoryNode & node)
50 std::string pad (2*level, ' ');
51 senf::console::DirectoryNode::child_iterator i (node.children().begin());
52 senf::console::DirectoryNode::child_iterator const i_end (node.children().end());
53 for (; i != i_end; ++i) {
54 output << pad << i->first;
55 if (i->second->isDirectory()) {
57 dodump(output, level+1,static_cast<senf::console::DirectoryNode&>(*i->second));
59 else if (i->second->isLink())
60 output << "@ -> " << i->second->followLink().path() << '\n';
67 prefix_ void senf::console::dump(std::ostream & os, DirectoryNode & dir)
72 //-/////////////////////////////////////////////////////////////////////////////////////////////////
73 // senf::console::GenericNode
75 prefix_ std::string senf::console::GenericNode::path()
78 std::string path (name());
81 path = node->name() + "/" + path;
82 node = node->parent();
84 return path.empty() ? "/" : path;
87 prefix_ std::string senf::console::GenericNode::path(DirectoryNode const & root)
91 cptr node (thisptr());
92 while (node && node != root.thisptr()) {
94 path = node->name() + "/" + path;
97 node = node->parent();
99 if (path.empty() || path[0] != '/')
104 prefix_ bool senf::console::GenericNode::active()
107 cptr node (thisptr());
108 while (node->parent())
109 node = node->parent();
110 return node == root().thisptr();
113 prefix_ bool senf::console::GenericNode::isChildOf(DirectoryNode & parent)
116 cptr node (thisptr());
117 while (node && node != parent.thisptr())
118 node = node->parent();
122 //-/////////////////////////////////////////////////////////////////////////////////////////////////
123 // senf::console::LinkNode
125 prefix_ void senf::console::LinkNode::v_help(std::ostream & os)
131 prefix_ std::string senf::console::LinkNode::v_shorthelp()
134 return follow().shorthelp();
137 //-/////////////////////////////////////////////////////////////////////////////////////////////////
138 //senf::console::DirectoryNode
140 prefix_ senf::console::DirectoryNode::~DirectoryNode()
142 ChildMap::iterator i (children_.begin());
143 ChildMap::iterator const i_end (children_.end());
144 for (; i != i_end; ++i)
145 i->second->parent_ = 0;
148 prefix_ senf::console::GenericNode::ptr
149 senf::console::DirectoryNode::remove(std::string const & name)
151 ChildMap::iterator i (children_.find(name));
152 if (i == children_.end())
153 throw UnknownNodeNameException() << ": '" << name << "'";
154 GenericNode::ptr node (i->second);
161 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
163 BOOST_ASSERT( ! node->parent() );
164 if (node->name().empty()) {
165 node->name("unnamed");
166 SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
168 if (children_.find(node->name()) != children_.end()) {
173 newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
174 } while (children_.find(newName) != children_.end());
175 SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '"
179 children_.insert(std::make_pair(node->name(),node));
180 node->parent_ = this;
183 prefix_ senf::console::GenericNode &
184 senf::console::DirectoryNode::getLink(std::string const & name)
187 ChildMap::const_iterator i (children_.find(name));
188 if (i == children_.end())
189 throw UnknownNodeNameException() << ": '" << name << "'";
193 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
196 output << doc_ << "\n";
199 prefix_ std::string senf::console::DirectoryNode::v_shorthelp()
202 if (! shortdoc_.empty())
204 return doc_.substr(0,doc_.find('\n'));
207 //-/////////////////////////////////////////////////////////////////////////////////////////////////
208 // senf::console::detail::NodeTraverser
211 prefix_ void senf::console::detail::NodeTraverser::operator()(std::string const & name)
215 if (name == std::string("")) {
216 dir_ = root_.thisptr();
220 if (! elt_.empty()) {
222 dir_ = dir_->parent();
223 if (! dir_ || ! dir_->isChildOf(root_))
224 dir_ = root_.thisptr();
226 else if (elt_ != "" && elt_ != ".") {
227 if (! dir_->hasChild(elt_) && autocomplete_) {
228 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
229 if (has_one_elt(completions))
230 elt_ = completions.begin()->first;
232 // Why does g++ give an error on this line ???? :
233 // dir = dynamic_cast<DirectoryNode&>( dir->get(name) ).thisptr();
234 DirectoryNode & d (dynamic_cast<DirectoryNode&>( dir_->get(elt_) ));
241 prefix_ senf::console::GenericNode & senf::console::detail::NodeTraverser::node()
243 if (elt_ != "" && elt_ != ".") {
244 if (! dir_->hasChild(elt_) && autocomplete_) {
245 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
246 if (has_one_elt(completions))
247 elt_ = completions.begin()->first;
249 return dir_->get(elt_);
256 //-/////////////////////////////////////////////////////////////////////////////////////////////////
257 // senf::console::SimpleCommandNode
259 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
262 output << doc_ << "\n";
265 prefix_ std::string senf::console::SimpleCommandNode::v_shorthelp()
268 if (! shortdoc_.empty())
270 return doc_.substr(0,doc_.find('\n'));
273 prefix_ void senf::console::SimpleCommandNode::v_execute(boost::any & rv, std::ostream & os,
274 ParseCommandInfo const & command)
280 //-/////////////////////////////////////////////////////////////////////////////////////////////////
282 //#include "Node.mpp"
288 // comment-column: 40
289 // c-file-style: "senf"
290 // indent-tabs-mode: nil
291 // ispell-local-dictionary: "american"
292 // compile-command: "scons -u test"