Bug Fix - Cmd

- The two load* methods were conflicting - if loadCustomReports was
  called first, it stomped on the commands list and prevented
  loadCommands from running.  Now there is only one method.
- Rewrote util.cpp/autoComplete to use STL over libc.  Might reduce
  code size.
This commit is contained in:
Paul Beckingham 2009-06-15 12:18:04 -04:00
parent 9f1880e050
commit b742712bb1
3 changed files with 18 additions and 24 deletions

View file

@ -25,6 +25,7 @@
// //
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include <iostream> // TODO Remove
#include "Cmd.h" #include "Cmd.h"
#include "Context.h" #include "Context.h"
#include "util.h" #include "util.h"
@ -55,8 +56,7 @@ Cmd::~Cmd ()
// report name. // report name.
bool Cmd::valid (const std::string& input) bool Cmd::valid (const std::string& input)
{ {
loadCommands (); load ();
loadCustomReports ();
std::vector <std::string> matches; std::vector <std::string> matches;
autoComplete (lowerCase (input), commands, matches); autoComplete (lowerCase (input), commands, matches);
@ -67,7 +67,7 @@ bool Cmd::valid (const std::string& input)
// Determines whether the string represents a valid custom report name. // Determines whether the string represents a valid custom report name.
bool Cmd::validCustom (const std::string& input) bool Cmd::validCustom (const std::string& input)
{ {
loadCustomReports (); load ();
std::vector <std::string> matches; std::vector <std::string> matches;
autoComplete (lowerCase (input), customReports, matches); autoComplete (lowerCase (input), customReports, matches);
@ -77,8 +77,7 @@ bool Cmd::validCustom (const std::string& input)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Cmd::parse (const std::string& input) void Cmd::parse (const std::string& input)
{ {
loadCommands (); load ();
loadCustomReports ();
std::string candidate = lowerCase (input); std::string candidate = lowerCase (input);
@ -103,7 +102,7 @@ void Cmd::parse (const std::string& input)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Cmd::loadCommands () void Cmd::load ()
{ {
if (commands.size () == 0) if (commands.size () == 0)
{ {
@ -133,14 +132,8 @@ void Cmd::loadCommands ()
commands.push_back (context.stringtable.get (CMD_UNDELETE, "undelete")); commands.push_back (context.stringtable.get (CMD_UNDELETE, "undelete"));
commands.push_back (context.stringtable.get (CMD_UNDO, "undo")); commands.push_back (context.stringtable.get (CMD_UNDO, "undo"));
commands.push_back (context.stringtable.get (CMD_VERSION, "version")); commands.push_back (context.stringtable.get (CMD_VERSION, "version"));
}
}
//////////////////////////////////////////////////////////////////////////////// // Now load the custom reports.
void Cmd::loadCustomReports ()
{
if (customReports.size () == 0)
{
std::vector <std::string> all; std::vector <std::string> all;
context.config.all (all); context.config.all (all);
@ -155,6 +148,8 @@ void Cmd::loadCustomReports ()
report = report.substr (0, columns); report = report.substr (0, columns);
// A custom report is also a command. // A custom report is also a command.
// TODO Make sure a custom report does not clash with a built-in
// command.
customReports.push_back (report); customReports.push_back (report);
commands.push_back (report); commands.push_back (report);
} }

View file

@ -50,8 +50,7 @@ public:
std::string command; std::string command;
private: private:
void loadCommands (); void load ();
void loadCustomReports ();
private: private:
std::vector <std::string> commands; std::vector <std::string> commands;

View file

@ -41,6 +41,7 @@
#include "text.h" #include "text.h"
#include "main.h" #include "main.h"
#include "i18n.h" #include "i18n.h"
#include "util.h"
#include "../auto.h" #include "../auto.h"
extern Context context; extern Context context;
@ -152,26 +153,25 @@ int autoComplete (
const std::vector<std::string>& list, const std::vector<std::string>& list,
std::vector<std::string>& matches) std::vector<std::string>& matches)
{ {
matches.erase (matches.begin (), matches.end ()); matches.clear ();
// Handle trivial case. // Handle trivial case.
unsigned int length = partial.length (); unsigned int length = partial.length ();
if (length) if (length)
{ {
for (unsigned int i = 0; i < list.size (); ++i) foreach (item, list)
{ {
// Special case where there is an exact match. if (partial == *item)
if (partial == list[i])
{ {
matches.erase (matches.begin (), matches.end ()); matches.clear ();
matches.push_back (list[i]); matches.push_back (*item);
return 1; return 1;
} }
// Maintain a list of partial matches. // Maintain a list of partial matches.
if (length <= list[i].length () && if (length <= item->length () &&
! strncmp (partial.c_str (), list[i].c_str (), length)) partial == item->substr (0, length))
matches.push_back (list[i]); matches.push_back (*item);
} }
} }