Utils/Console: Add short help to 'ls' output
[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     follow().help(os);
99 }
100
101 prefix_ std::string senf::console::LinkNode::v_shorthelp()
102     const
103 {
104     return follow().shorthelp();
105 }
106
107 ///////////////////////////////////////////////////////////////////////////
108 //senf::console::DirectoryNode
109
110 prefix_ senf::console::DirectoryNode::~DirectoryNode()
111 {
112     ChildMap::iterator i (children_.begin());
113     ChildMap::iterator const i_end (children_.end());
114     for (; i != i_end; ++i)
115         i->second->parent_ = 0;
116 }
117
118 prefix_ senf::console::GenericNode::ptr
119 senf::console::DirectoryNode::remove(std::string const & name)
120 {
121     ChildMap::iterator i (children_.find(name));
122     if (i == children_.end()) 
123         throw UnknownNodeNameException() << ": '" << name << "'";
124     GenericNode::ptr node (i->second);
125     children_.erase(i);
126     node->parent_ = 0;
127     node->name_.clear();
128     return node;
129 }
130
131 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
132 {
133     BOOST_ASSERT( ! node->parent() );
134     if (node->name().empty()) {
135         node->name("unnamed");
136         SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
137     }
138     if (children_.find(node->name()) != children_.end()) {
139         unsigned suffix (0);
140         std::string newName;
141         do {
142             ++suffix;
143             newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
144         } while (children_.find(newName) != children_.end());
145         SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '" 
146                                       << newName << "'"));
147         node->name(newName);
148     }
149     children_.insert(std::make_pair(node->name(),node));
150     node->parent_ = this;
151 }
152
153 prefix_ senf::console::GenericNode &
154 senf::console::DirectoryNode::getLink(std::string const & name)
155     const
156 {
157     ChildMap::const_iterator i (children_.find(name));
158     if (i == children_.end())
159         throw UnknownNodeNameException() << ": '" << name << "'";
160     return *(i->second);
161 }
162
163 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
164     const
165 {
166     output << doc_ << "\n";
167 }
168
169 prefix_ std::string senf::console::DirectoryNode::v_shorthelp()
170     const
171 {
172     if (! shortdoc_.empty())
173         return shortdoc_;
174     return doc_.substr(0,doc_.find('\n'));
175 }
176
177 ///////////////////////////////////////////////////////////////////////////
178 // senf::console::detail::NodeTraverser
179
180 prefix_ void senf::console::detail::NodeTraverser::operator()(std::string const & name)
181 {
182     if (! init_) {
183         init_ = true;
184         if (name == std::string("")) {
185             dir_ = root_.thisptr();
186             return;
187         }
188     }
189     if (! elt_.empty()) {
190         if (elt_ == "..") {
191             dir_ = dir_->parent();
192             if (! dir_ || ! dir_->isChildOf(root_))
193                 dir_ = root_.thisptr();
194         }
195         else if (elt_ != "" && elt_ != ".") {
196             if (! dir_->hasChild(elt_) && autocomplete_) {
197                 DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
198                 if (has_one_elt(completions))
199                     elt_ = completions.begin()->first;
200             }
201             // Why does g++ give an error on this line ???? :
202             // dir = dynamic_cast<DirectoryNode&>( dir->get(name) ).thisptr();
203             DirectoryNode & d (dynamic_cast<DirectoryNode&>( dir_->get(elt_) ));
204             dir_ = d.thisptr();
205         }
206     }
207     elt_ = name;
208 }
209
210 prefix_ senf::console::GenericNode & senf::console::detail::NodeTraverser::node()
211 {
212     if (elt_ != "" && elt_ != ".") {
213         if (! dir_->hasChild(elt_) && autocomplete_) {
214             DirectoryNode::ChildrenRange completions (dir_->completions(elt_));
215             if (has_one_elt(completions))
216                 elt_ = completions.begin()->first;
217         }
218         return dir_->get(elt_);
219     }
220     else
221         return * dir_;
222 }
223
224 ///////////////////////////////////////////////////////////////////////////
225 // senf::console::SimpleCommandNode
226
227 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
228     const
229 {
230     output << doc_ << "\n";
231 }
232
233 prefix_ std::string senf::console::SimpleCommandNode::v_shorthelp()
234     const
235 {
236     if (! shortdoc_.empty())
237         return shortdoc_;
238     return doc_.substr(0,doc_.find('\n'));
239 }
240
241 prefix_ void senf::console::SimpleCommandNode::v_execute(boost::any & rv, std::ostream & os,
242                                                          ParseCommandInfo const & command)
243     const
244 {
245     fn_(os, command);
246 }
247
248 ///////////////////////////////cc.e////////////////////////////////////////
249 #undef prefix_
250 //#include "Node.mpp"
251
252 \f
253 // Local Variables:
254 // mode: c++
255 // fill-column: 100
256 // comment-column: 40
257 // c-file-style: "senf"
258 // indent-tabs-mode: nil
259 // ispell-local-dictionary: "american"
260 // compile-command: "scons -u test"
261 // End: