Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / 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
29 // Custom includes
30 #include <senf/Utils/Range.hh>
31
32 //#include "Node.mpp"
33 #define prefix_
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35
36 prefix_ senf::console::DirectoryNode & senf::console::root()
37 {
38     static DirectoryNode::ptr rootNode(new DirectoryNode());
39     return *rootNode;
40 }
41
42 namespace {
43     void dodump(std::ostream & output, unsigned level, senf::console::DirectoryNode & node)
44     {
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()) {
51                 output << "/\n";
52                 dodump(output, level+1,static_cast<senf::console::DirectoryNode&>(*i->second));
53             }
54             else if (i->second->isLink())
55                 output << "@ -> " << i->second->followLink().path() << '\n';
56             else
57                 output << '\n';
58         }
59     }
60 }
61
62 prefix_ void senf::console::dump(std::ostream & os, DirectoryNode & dir)
63 {
64     dodump(os,0,dir);
65 }
66
67 //-/////////////////////////////////////////////////////////////////////////////////////////////////
68 // senf::console::GenericNode
69
70 prefix_ std::string senf::console::GenericNode::path()
71     const
72 {
73     std::string path (name());
74     ptr node (parent());
75     while (node) {
76         path = node->name() + "/" + path;
77         node = node->parent();
78     }
79     return path.empty() ? "/" : path;
80 }
81
82 prefix_ std::string senf::console::GenericNode::path(DirectoryNode const & root)
83     const
84 {
85     std::string path;
86     cptr node (thisptr());
87     while (node && node != root.thisptr()) {
88         if (! path.empty())
89             path = node->name() + "/" + path;
90         else
91             path = node->name();
92         node = node->parent();
93     }
94     if (path.empty() || path[0] != '/')
95         path = "/" + path;
96     return path;
97 }
98
99 prefix_ bool senf::console::GenericNode::active()
100     const
101 {
102     cptr node (thisptr());
103     while (node->parent())
104         node = node->parent();
105     return node == root().thisptr();
106 }
107
108 prefix_ bool senf::console::GenericNode::isChildOf(DirectoryNode & parent)
109     const
110 {
111     cptr node (thisptr());
112     while (node && node != parent.thisptr())
113         node = node->parent();
114     return node;
115 }
116
117 //-/////////////////////////////////////////////////////////////////////////////////////////////////
118 // senf::console::LinkNode
119
120 prefix_ void senf::console::LinkNode::v_help(std::ostream & os)
121     const
122 {
123     follow().help(os);
124 }
125
126 prefix_ std::string senf::console::LinkNode::v_shorthelp()
127     const
128 {
129     return follow().shorthelp();
130 }
131
132 //-/////////////////////////////////////////////////////////////////////////////////////////////////
133 //senf::console::DirectoryNode
134
135 prefix_ senf::console::DirectoryNode::~DirectoryNode()
136 {
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;
141 }
142
143 prefix_ senf::console::GenericNode::ptr
144 senf::console::DirectoryNode::remove(std::string const & name)
145 {
146     ChildMap::iterator i (children_.find(name));
147     if (i == children_.end())
148         throw UnknownNodeNameException() << ": '" << name << "'";
149     GenericNode::ptr node (i->second);
150     children_.erase(i);
151     node->parent_ = 0;
152     node->name_.clear();
153     return node;
154 }
155
156 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
157 {
158     BOOST_ASSERT( ! node->parent() );
159     if (node->name().empty()) {
160         node->name("unnamed");
161         SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
162     }
163     if (children_.find(node->name()) != children_.end()) {
164         unsigned suffix (0);
165         std::string newName;
166         do {
167             ++suffix;
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 '"
171                                       << newName << "'"));
172         node->name(newName);
173     }
174     children_.insert(std::make_pair(node->name(),node));
175     node->parent_ = this;
176 }
177
178 prefix_ senf::console::GenericNode &
179 senf::console::DirectoryNode::getLink(std::string const & name)
180     const
181 {
182     ChildMap::const_iterator i (children_.find(name));
183     if (i == children_.end())
184         throw UnknownNodeNameException() << ": '" << name << "'";
185     return *(i->second);
186 }
187
188 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
189     const
190 {
191     output << doc_ << "\n";
192 }
193
194 prefix_ std::string senf::console::DirectoryNode::v_shorthelp()
195     const
196 {
197     if (! shortdoc_.empty())
198         return shortdoc_;
199     return doc_.substr(0,doc_.find('\n'));
200 }
201
202 //-/////////////////////////////////////////////////////////////////////////////////////////////////
203 // senf::console::detail::NodeTraverser
204 #ifndef DOXYGEN
205
206 prefix_ void senf::console::detail::NodeTraverser::operator()(std::string const & name)
207 {
208     if (! init_) {
209         init_ = true;
210         if (name == std::string("")) {
211             dir_ = root_.thisptr();
212             return;
213         }
214     }
215     if (! elt_.empty()) {
216         if (elt_ == "..") {
217             dir_ = dir_->parent();
218             if (! dir_ || ! dir_->isChildOf(root_))
219                 dir_ = root_.thisptr();
220         }
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;
226             }
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_) ));
230             dir_ = d.thisptr();
231         }
232     }
233     elt_ = name;
234 }
235
236 prefix_ senf::console::GenericNode & senf::console::detail::NodeTraverser::node()
237 {
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;
243         }
244         return dir_->get(elt_);
245     }
246     else
247         return * dir_;
248 }
249 #endif
250
251 //-/////////////////////////////////////////////////////////////////////////////////////////////////
252 // senf::console::SimpleCommandNode
253
254 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
255     const
256 {
257     output << doc_ << "\n";
258 }
259
260 prefix_ std::string senf::console::SimpleCommandNode::v_shorthelp()
261     const
262 {
263     if (! shortdoc_.empty())
264         return shortdoc_;
265     return doc_.substr(0,doc_.find('\n'));
266 }
267
268 prefix_ void senf::console::SimpleCommandNode::v_execute(boost::any & rv, std::ostream & os,
269                                                          ParseCommandInfo const & command)
270     const
271 {
272     fn_(os, command);
273 }
274
275 //-/////////////////////////////////////////////////////////////////////////////////////////////////
276 #undef prefix_
277 //#include "Node.mpp"
278
279 \f
280 // Local Variables:
281 // mode: c++
282 // fill-column: 100
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"
288 // End: