Code Cleanup

- Removed 9 useless methods by making members public.  Accessors are nice
  sometimes, but also an unnecessary layer.
This commit is contained in:
Paul Beckingham 2013-08-30 14:39:22 -07:00
parent 50d4b37519
commit ac1497ff1a
4 changed files with 54 additions and 124 deletions

View file

@ -104,16 +104,15 @@ bool A3t::canonicalize (
// autoCompletes to a valid command/report. // autoCompletes to a valid command/report.
void A3t::findBinary () void A3t::findBinary ()
{ {
if (_tree->branches () >= 1) if (_tree->_branches.size () >= 1)
{ {
_tree->operator[](0)->tag ("BINARY"); _tree->_branches[0]->tag ("BINARY");
std::string binary = _tree->_branches[0]->attribute ("raw");
std::string binary = _tree->operator[](0)->attribute ("raw");
std::string::size_type slash = binary.rfind ('/'); std::string::size_type slash = binary.rfind ('/');
if (slash != std::string::npos) if (slash != std::string::npos)
binary = binary.substr (slash + 1); binary = binary.substr (slash + 1);
_tree->operator[](0)->attribute ("basename", binary); _tree->_branches[0]->attribute ("basename", "binary");
} }
} }
@ -122,15 +121,15 @@ void A3t::findBinary ()
// all args in the raw state. // all args in the raw state.
void A3t::findTerminator () void A3t::findTerminator ()
{ {
std::string command; std::vector <Tree*>::iterator i;
for (int i = 0; i < _tree->branches (); ++i) for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i)
{ {
if (_tree->operator[](i)->attribute ("raw") == "--") if ((*i)->attribute ("raw") == "--")
{ {
_tree->operator[](i)->tag ("TERMINATOR"); (*i)->tag ("TERMINATOR");
break; break;
} }
} }
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -139,38 +138,39 @@ void A3t::findTerminator ()
void A3t::findCommand () void A3t::findCommand ()
{ {
std::string command; std::string command;
for (int i = 0; i < _tree->branches (); ++i) std::vector <Tree*>::iterator i;
for (i = _tree->_branches.begin (); i != _tree->_branches.end (); ++i)
{ {
// Parser override operator. // Parser override operator.
if (_tree->operator[](i)->attribute ("raw") == "--") if ((*i)->attribute ("raw") == "--")
break; break;
if (canonicalize (command, "report", _tree->operator[](i)->attribute ("raw"))) if (canonicalize (command, "report", (*i)->attribute ("raw")))
{ {
_tree->operator[](i)->attribute ("canonical", command); (*i)->attribute ("canonical", command);
_tree->operator[](i)->tag ("REPORT"); (*i)->tag ("REPORT");
_tree->operator[](i)->tag ("CMD"); (*i)->tag ("CMD");
} }
else if (canonicalize (command, "readcmd", _tree->operator[](i)->attribute ("raw"))) else if (canonicalize (command, "readcmd", (*i)->attribute ("raw")))
{ {
_tree->operator[](i)->attribute ("canonical", command); (*i)->attribute ("canonical", command);
_tree->operator[](i)->tag ("READCMD"); (*i)->tag ("READCMD");
_tree->operator[](i)->tag ("CMD"); (*i)->tag ("CMD");
} }
else if (canonicalize (command, "writecmd", _tree->operator[](i)->attribute ("raw"))) else if (canonicalize (command, "writecmd", (*i)->attribute ("raw")))
{ {
_tree->operator[](i)->attribute ("canonical", command); (*i)->attribute ("canonical", command);
_tree->operator[](i)->tag ("WRITECMD"); (*i)->tag ("WRITECMD");
_tree->operator[](i)->tag ("CMD"); (*i)->tag ("CMD");
} }
else if (canonicalize (command, "specialcmd", _tree->operator[](i)->attribute ("raw"))) else if (canonicalize (command, "specialcmd", (*i)->attribute ("raw")))
{ {
_tree->operator[](i)->attribute ("canonical", command); (*i)->attribute ("canonical", command);
_tree->operator[](i)->tag ("SPECIALCMD"); (*i)->tag ("SPECIALCMD");
_tree->operator[](i)->tag ("CMD"); (*i)->tag ("CMD");
} }
} }
} }

View file

@ -65,16 +65,6 @@ Tree& Tree::operator= (const Tree& other)
return *this; return *this;
} }
////////////////////////////////////////////////////////////////////////////////
Tree* Tree::operator[] (const int branch)
{
if (branch < 0 ||
branch > (int) _branches.size () - 1)
throw "Tree::operator[] out of range";
return _branches[branch];
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Tree* Tree::addBranch (Tree* branch) Tree* Tree::addBranch (Tree* branch)
{ {
@ -115,24 +105,6 @@ void Tree::replaceBranch (Tree* from, Tree* to)
} }
} }
////////////////////////////////////////////////////////////////////////////////
int Tree::branches ()
{
return _branches.size ();
}
////////////////////////////////////////////////////////////////////////////////
void Tree::name (const std::string& name)
{
_name = name;
}
////////////////////////////////////////////////////////////////////////////////
std::string Tree::name () const
{
return _name;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Accessor for attributes. // Accessor for attributes.
void Tree::attribute (const std::string& name, const std::string& value) void Tree::attribute (const std::string& name, const std::string& value)
@ -173,28 +145,11 @@ void Tree::removeAttribute (const std::string& name)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int Tree::attributes () const // Recursively builds a list of Tree* objects, left to right, depth first. The
{ // reason for the depth-first enumeration is that a client may wish to traverse
return _attributes.size (); // the tree and delete nodes. With a depth-first iteration, this is a safe
} // mechanism, and a node pointer will never be dereferenced after it has been
// deleted.
////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> Tree::allAttributes () const
{
std::vector <std::string> names;
std::map <std::string, std::string>::const_iterator it;
for (it = _attributes.begin (); it != _attributes.end (); ++it)
names.push_back (it->first);
return names;
}
////////////////////////////////////////////////////////////////////////////////
// Recursively completes a list of Tree* objects, left to right, depth first.
// The reason for the depth-first enumeration is that a client may wish to
// traverse the tree and delete nodes. With a depth-first iteration, this is a
// safe mechanism, and a node pointer will never be dereferenced after it has
// been deleted.
void Tree::enumerate (std::vector <Tree*>& all) const void Tree::enumerate (std::vector <Tree*>& all) const
{ {
for (std::vector <Tree*>::const_iterator i = _branches.begin (); for (std::vector <Tree*>::const_iterator i = _branches.begin ();
@ -206,12 +161,6 @@ void Tree::enumerate (std::vector <Tree*>& all) const
} }
} }
////////////////////////////////////////////////////////////////////////////////
Tree* Tree::parent () const
{
return _trunk;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool Tree::hasTag (const std::string& tag) const bool Tree::hasTag (const std::string& tag) const
{ {
@ -228,18 +177,6 @@ void Tree::tag (const std::string& tag)
_tags.push_back (tag); _tags.push_back (tag);
} }
////////////////////////////////////////////////////////////////////////////////
int Tree::tags () const
{
return _tags.size ();
}
////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> Tree::allTags () const
{
return _tags;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int Tree::count () const int Tree::count () const
{ {
@ -265,7 +202,7 @@ Tree* Tree::find (const std::string& path)
// Must start at the trunk. // Must start at the trunk.
Tree* cursor = this; Tree* cursor = this;
std::vector <std::string>::iterator it = elements.begin (); std::vector <std::string>::iterator it = elements.begin ();
if (cursor->name () != *it) if (cursor->_name != *it)
return NULL; return NULL;
// Perhaps the trunk is what is needed? // Perhaps the trunk is what is needed?
@ -278,11 +215,12 @@ Tree* Tree::find (const std::string& path)
bool found = false; bool found = false;
// If the cursor has a branch that matches *it, proceed. // If the cursor has a branch that matches *it, proceed.
for (int i = 0; i < cursor->branches (); ++i) std::vector <Tree*>::iterator i;
for (i = cursor->_branches.begin (); i != cursor->_branches.end (); ++i)
{ {
if ((*cursor)[i]->name () == *it) if ((*i)->_name == *it)
{ {
cursor = (*cursor)[i]; cursor = *i;
found = true; found = true;
break; break;
} }
@ -302,18 +240,18 @@ void Tree::dumpNode (Tree* t, int depth)
for (int i = 0; i < depth; ++i) for (int i = 0; i < depth; ++i)
std::cout << " "; std::cout << " ";
std::cout << t << " \033[1m" << t->name () << "\033[0m"; std::cout << t << " \033[1m" << t->_name << "\033[0m";
// Dump attributes. // Dump attributes.
std::string atts; std::string atts;
std::vector <std::string> attributes = t->allAttributes ();
std::vector <std::string>::iterator it; std::map <std::string, std::string>::iterator a;
for (it = attributes.begin (); it != attributes.end (); ++it) for (a = t->_attributes.begin (); a != t->_attributes.end (); ++a)
{ {
if (it != attributes.begin ()) if (a != t->_attributes.begin ())
atts += " "; atts += " ";
atts += *it + "='\033[33m" + t->attribute (*it) + "\033[0m'"; atts += a->first + "='\033[33m" + a->second + "\033[0m'";
} }
if (atts.length ()) if (atts.length ())
@ -321,9 +259,9 @@ void Tree::dumpNode (Tree* t, int depth)
// Dump tags. // Dump tags.
std::string tags; std::string tags;
std::vector <std::string> allTags = t->allTags (); std::vector <std::string>::iterator tag;
for (it = allTags.begin (); it != allTags.end (); ++it) for (tag = t->_tags.begin (); tag != t->_tags.end (); ++tag)
tags += (tags.length () ? " " : "") + *it; tags += (tags.length () ? " " : "") + *tag;
if (tags.length ()) if (tags.length ())
std::cout << " \033[32m" << tags << "\033[0m"; std::cout << " \033[32m" << tags << "\033[0m";
@ -331,8 +269,9 @@ void Tree::dumpNode (Tree* t, int depth)
std::cout << "\n"; std::cout << "\n";
// Recurse for branches. // Recurse for branches.
for (int i = 0; i < t->branches (); ++i) std::vector <Tree*>::iterator b;
dumpNode ((*t)[i], depth + 1); for (b = t->_branches.begin (); b != t->_branches.end (); ++b)
dumpNode (*b, depth + 1);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -40,30 +40,21 @@ public:
~Tree (); ~Tree ();
Tree (const Tree&); Tree (const Tree&);
Tree& operator= (const Tree&); Tree& operator= (const Tree&);
Tree* operator[] (const int);
Tree* addBranch (Tree*); Tree* addBranch (Tree*);
void removeBranch (Tree*); void removeBranch (Tree*);
void replaceBranch (Tree*, Tree*); void replaceBranch (Tree*, Tree*);
int branches ();
void name (const std::string&);
std::string name () const;
void attribute (const std::string&, const std::string&); void attribute (const std::string&, const std::string&);
void attribute (const std::string&, const int); void attribute (const std::string&, const int);
void attribute (const std::string&, const double); void attribute (const std::string&, const double);
std::string attribute (const std::string&); std::string attribute (const std::string&);
void removeAttribute (const std::string&); void removeAttribute (const std::string&);
int attributes () const;
std::vector <std::string> allAttributes () const; void enumerate (std::vector <Tree*>& all) const;
bool hasTag (const std::string&) const; bool hasTag (const std::string&) const;
void tag (const std::string&); void tag (const std::string&);
int tags () const;
std::vector <std::string> allTags () const;
void enumerate (std::vector <Tree*>& all) const;
Tree* parent () const;
int count () const; int count () const;
@ -74,7 +65,7 @@ public:
private: private:
void dumpNode (Tree*, int); void dumpNode (Tree*, int);
private: public:
Tree* _trunk; // Parent. Tree* _trunk; // Parent.
std::string _name; // Name. std::string _name; // Name.
std::vector <Tree*> _branches; // Children. std::vector <Tree*> _branches; // Children.

View file

@ -9,5 +9,5 @@
#echo; ./args rc:x rc.debug:1 a.b\<c \(one or two\) modify 'quoted string' \'not quoted\' due:eom+1wk+1d #echo; ./args rc:x rc.debug:1 a.b\<c \(one or two\) modify 'quoted string' \'not quoted\' due:eom+1wk+1d
echo; ./args 123 mod pro:'P 1' +home /from/to/g echo; ./args 123 mod pro:'P 1' +home /from/to/g
echo; ./args add -- modify echo; ./args add -- modify +tag /from/to/g name:value