PPI: Fix senf::ppi::init() event hook scheduling
[senf.git] / Utils / Console / Node.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Node non-inline non-template implementation */
25
26 #include "Node.hh"
27 #include "Node.ih"
28 #include "../../Utils/range.hh"
29
30 // Custom includes
31
32 //#include "Node.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 prefix_ senf::console::DirectoryNode & senf::console::root()
37 {
38     static DirectoryNode::ptr rootNode(new DirectoryNode());
39     return *rootNode;
40 }
41
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::console::GenericNode
44
45 prefix_ std::string senf::console::GenericNode::path()
46     const
47 {
48     std::string path (name());
49     ptr node (parent());
50     while (node) {
51         path = node->name() + "/" + path;
52         node = node->parent();
53     }
54     return path.empty() ? "/" : path;
55 }
56
57 prefix_ std::string senf::console::GenericNode::path(DirectoryNode const & root)
58     const
59 {
60     std::string path;
61     cptr node (thisptr());
62     while (node && node != root.thisptr()) {
63         if (! path.empty())
64             path = node->name() + "/" + path;
65         else
66             path = node->name();
67         node = node->parent();
68     }
69     if (path.empty() || path[0] != '/')
70         path = "/" + path;
71     return path;
72 }
73
74 prefix_ bool senf::console::GenericNode::active()
75     const
76 {
77     cptr node (thisptr());
78     while (node->parent())
79         node = node->parent();
80     return node == root().thisptr();
81 }
82
83 prefix_ bool senf::console::GenericNode::isChildOf(DirectoryNode & parent)
84     const
85 {
86     cptr node (thisptr());
87     while (node && node != parent.thisptr())
88         node = node->parent();
89     return node;
90 }
91
92 ///////////////////////////////////////////////////////////////////////////
93 // senf::console::LinkNode
94
95 prefix_ void senf::console::LinkNode::v_help(std::ostream & os)
96     const
97 {
98     os << "link to ";
99     follow().help(os);
100 }
101
102 ///////////////////////////////////////////////////////////////////////////
103 //senf::console::DirectoryNode
104
105 prefix_ senf::console::DirectoryNode::~DirectoryNode()
106 {
107     ChildMap::iterator i (children_.begin());
108     ChildMap::iterator const i_end (children_.end());
109     for (; i != i_end; ++i)
110         i->second->parent_ = 0;
111 }
112
113 prefix_ senf::console::GenericNode::ptr
114 senf::console::DirectoryNode::remove(std::string const & name)
115 {
116     ChildMap::iterator i (children_.find(name));
117     if (i == children_.end()) 
118         throw UnknownNodeNameException() << ": '" << name << "'";
119     GenericNode::ptr node (i->second);
120     children_.erase(i);
121     node->parent_ = 0;
122     node->name_.clear();
123     return node;
124 }
125
126 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
127 {
128     BOOST_ASSERT( ! node->parent() );
129     if (node->name().empty()) {
130         node->name("unnamed");
131         SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
132     }
133     if (children_.find(node->name()) != children_.end()) {
134         unsigned suffix (0);
135         std::string newName;
136         do {
137             ++suffix;
138             newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
139         } while (children_.find(newName) != children_.end());
140         SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '" 
141                                       << newName << "'"));
142         node->name(newName);
143     }
144     children_.insert(std::make_pair(node->name(),node));
145     node->parent_ = this;
146 }
147
148 prefix_ senf::console::GenericNode &
149 senf::console::DirectoryNode::getLink(std::string const & name)
150     const
151 {
152     ChildMap::const_iterator i (children_.find(name));
153     if (i == children_.end())
154         throw UnknownNodeNameException() << ": '" << name << "'";
155     return *(i->second);
156 }
157
158 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
159     const
160 {
161     output << doc_ << "\n";
162 }
163
164 ///////////////////////////////////////////////////////////////////////////
165 // senf::console::detail::NodeTraverser
166
167 prefix_ void senf::console::detail::NodeTraverser::operator()(std::string const & name)
168 {
169     if (! init_) {
170         init_ = true;
171         if (name == std::string("")) {
172             dir_ = root_.thisptr();
173             return;
174         }
175     }
176     if (! elt_.empty()) {
177         if (elt_ == "..") {
178             dir_ = dir_->parent();
179             if (! dir_ || ! dir_->isChildOf(root_))
180                 dir_ = root_.thisptr();
181         }
182         else if (elt_ != "" && elt_ != ".") {
183             if (! dir_->hasChild(elt_) && autocomplete_) {
184                 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
185                 if (has_one_elt(completions))
186                     elt_ = completions.begin()->first;
187             }
188             // Why does g++ give an error on this line ???? :
189             // dir = dynamic_cast<DirectoryNode&>( dir->get(name) ).thisptr();
190             DirectoryNode & d (dynamic_cast<DirectoryNode&>( dir_->get(elt_) ));
191             dir_ = d.thisptr();
192         }
193     }
194     elt_ = name;
195 }
196
197 prefix_ senf::console::GenericNode & senf::console::detail::NodeTraverser::node()
198 {
199     if (elt_ != "" && elt_ != ".") {
200         if (! dir_->hasChild(elt_) && autocomplete_) {
201             DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
202             if (has_one_elt(completions))
203                 elt_ = completions.begin()->first;
204         }
205         return dir_->get(elt_);
206     }
207     else
208         return * dir_;
209 }
210
211 ///////////////////////////////////////////////////////////////////////////
212 // senf::console::SimpleCommandNode
213
214 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
215     const
216 {
217     output << doc_ << "\n";
218 }
219
220 prefix_ void senf::console::SimpleCommandNode::v_execute(boost::any & rv, std::ostream & os,
221                                                          ParseCommandInfo const & command)
222     const
223 {
224     fn_(os, command);
225 }
226
227 ///////////////////////////////cc.e////////////////////////////////////////
228 #undef prefix_
229 //#include "Node.mpp"
230
231 \f
232 // Local Variables:
233 // mode: c++
234 // fill-column: 100
235 // comment-column: 40
236 // c-file-style: "senf"
237 // indent-tabs-mode: nil
238 // ispell-local-dictionary: "american"
239 // compile-command: "scons -u test"
240 // End: