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 */
34 #include <senf/Utils/Range.hh>
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40 prefix_ senf::console::DirectoryNode & senf::console::root()
42 static DirectoryNode::ptr rootNode (new DirectoryNode());
47 void dodump(std::ostream & output, unsigned level, senf::console::DirectoryNode & node)
49 std::string pad (2*level, ' ');
50 senf::console::DirectoryNode::child_iterator i (node.children().begin());
51 senf::console::DirectoryNode::child_iterator const i_end (node.children().end());
52 for (; i != i_end; ++i) {
53 output << pad << i->first;
54 if (i->second->isDirectory()) {
56 dodump(output, level+1,static_cast<senf::console::DirectoryNode&>(*i->second));
58 else if (i->second->isLink())
59 output << "@ -> " << i->second->followLink().path() << '\n';
66 prefix_ void senf::console::dump(std::ostream & os, DirectoryNode & dir)
71 //-/////////////////////////////////////////////////////////////////////////////////////////////////
72 // senf::console::GenericNode
74 prefix_ std::string senf::console::GenericNode::path()
77 std::string path (name());
80 path = node->name() + "/" + path;
81 node = node->parent();
83 return path.empty() ? "/" : path;
86 prefix_ std::string senf::console::GenericNode::path(DirectoryNode const & root)
90 cptr node (thisptr());
91 while (node && node != root.thisptr()) {
93 path = node->name() + "/" + path;
96 node = node->parent();
98 if (path.empty() || path[0] != '/')
103 prefix_ bool senf::console::GenericNode::active()
106 cptr node (thisptr());
107 while (node->parent())
108 node = node->parent();
109 return node == root().thisptr();
112 prefix_ bool senf::console::GenericNode::isChildOf(DirectoryNode & parent)
115 cptr node (thisptr());
116 while (node && node != parent.thisptr())
117 node = node->parent();
121 //-/////////////////////////////////////////////////////////////////////////////////////////////////
122 // senf::console::LinkNode
124 prefix_ void senf::console::LinkNode::v_help(std::ostream & os)
130 prefix_ std::string senf::console::LinkNode::v_shorthelp()
133 return follow().shorthelp();
136 //-/////////////////////////////////////////////////////////////////////////////////////////////////
137 //senf::console::DirectoryNode
139 prefix_ senf::console::DirectoryNode::~DirectoryNode()
141 ChildMap::iterator i (children_.begin());
142 ChildMap::iterator const i_end (children_.end());
143 for (; i != i_end; ++i)
144 i->second->parent_ = 0;
147 prefix_ senf::console::GenericNode::ptr
148 senf::console::DirectoryNode::remove(std::string const & name)
150 ChildMap::iterator i (children_.find(name));
151 if (i == children_.end())
152 throw UnknownNodeNameException() << ": '" << name << "'";
153 GenericNode::ptr node (i->second);
160 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
162 BOOST_ASSERT( ! node->parent() );
163 if (node->name().empty()) {
164 node->name("unnamed");
165 SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
167 if (children_.find(node->name()) != children_.end()) {
172 newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
173 } while (children_.find(newName) != children_.end());
174 SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '"
178 children_.insert(std::make_pair(node->name(),node));
179 node->parent_ = this;
182 prefix_ senf::console::GenericNode &
183 senf::console::DirectoryNode::getLink(std::string const & name)
186 ChildMap::const_iterator i (children_.find(name));
187 if (i == children_.end())
188 throw UnknownNodeNameException() << ": '" << name << "'";
192 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
195 output << doc_ << "\n";
198 prefix_ std::string senf::console::DirectoryNode::v_shorthelp()
201 if (! shortdoc_.empty())
203 return doc_.substr(0,doc_.find('\n'));
206 //-/////////////////////////////////////////////////////////////////////////////////////////////////
207 // senf::console::SimpleCommandNode
209 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
212 output << doc_ << "\n";
215 prefix_ std::string senf::console::SimpleCommandNode::v_shorthelp()
218 if (! shortdoc_.empty())
220 return doc_.substr(0,doc_.find('\n'));
223 prefix_ void senf::console::SimpleCommandNode::v_execute(boost::any & rv, std::ostream & os,
224 ParseCommandInfo const & command)
230 //-/////////////////////////////////////////////////////////////////////////////////////////////////
232 //#include "Node.mpp"
238 // comment-column: 40
239 // c-file-style: "senf"
240 // indent-tabs-mode: nil
241 // ispell-local-dictionary: "american"
242 // compile-command: "scons -u test"