f74a37b9741b240bf7e1b2127e97dad827ad3f02
[senf.git] / 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
31 //#include "Node.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 prefix_ senf::console::DirectoryNode & senf::console::root()
36 {
37     static DirectoryNode::ptr rootNode(new DirectoryNode());
38     return *rootNode;
39 }
40
41 ///////////////////////////////////////////////////////////////////////////
42 // senf::console::GenericNode
43
44 prefix_ std::string senf::console::GenericNode::path()
45     const
46 {
47     std::string path (name());
48     ptr node (parent());
49     while (node) {
50         path = node->name() + "/" + path;
51         node = node->parent();
52     }
53     return path.empty() ? "/" : path;
54 }
55
56 prefix_ bool senf::console::GenericNode::active()
57     const
58 {
59     cptr node (thisptr());
60     while (node->parent())
61         node = node->parent();
62     return node == root().thisptr();
63 }
64
65 ///////////////////////////////////////////////////////////////////////////
66 //senf::console::DirectoryNode
67
68 prefix_ senf::console::GenericNode::ptr
69 senf::console::DirectoryNode::remove(std::string const & name)
70 {
71     ChildMap::iterator i (children_.find(name));
72     if (i == children_.end()) 
73         throw UnknownNodeNameException() << ": '" << name << "'";
74     GenericNode::ptr node (i->second);
75     children_.erase(i);
76     node->parent_ = 0;
77     return node;
78 }
79
80 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
81 {
82     BOOST_ASSERT( ! node->parent() );
83     if (node->name().empty()) {
84         node->name("unnamed");
85         SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
86     }
87     if (children_.find(node->name()) != children_.end()) {
88         unsigned suffix (0);
89         std::string newName;
90         do {
91             ++suffix;
92             newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
93         } while (children_.find(newName) != children_.end());
94         SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '" 
95                                       << newName << "'"));
96         node->name(newName);
97     }
98     children_.insert(std::make_pair(node->name(),node));
99     node->parent_ = this;
100 }
101
102 prefix_ senf::console::GenericNode &
103 senf::console::DirectoryNode::get(std::string const & name)
104     const
105 {
106     ChildMap::const_iterator i (children_.find(name));
107     if (i == children_.end())
108         throw UnknownNodeNameException() << ": '" << name << "'";
109     return *(i->second);
110 }
111
112 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
113     const
114 {
115     output << doc_ << "\n";
116 }
117
118 ///////////////////////////////////////////////////////////////////////////
119 // senf::console::SimpleCommandNode
120
121 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
122     const
123 {
124     output << doc_ << "\n";
125 }
126
127 ///////////////////////////////cc.e////////////////////////////////////////
128 #undef prefix_
129 //#include "Node.mpp"
130
131 \f
132 // Local Variables:
133 // mode: c++
134 // fill-column: 100
135 // comment-column: 40
136 // c-file-style: "senf"
137 // indent-tabs-mode: nil
138 // ispell-local-dictionary: "american"
139 // compile-command: "scons -u test"
140 // End: