diff --git a/src/Cmd.cpp b/src/Cmd.cpp index 560babffa..bd23d21d2 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -25,6 +25,7 @@ // //////////////////////////////////////////////////////////////////////////////// +#include // TODO Remove #include "Cmd.h" #include "Context.h" #include "util.h" @@ -55,8 +56,7 @@ Cmd::~Cmd () // report name. bool Cmd::valid (const std::string& input) { - loadCommands (); - loadCustomReports (); + load (); std::vector 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. bool Cmd::validCustom (const std::string& input) { - loadCustomReports (); + load (); std::vector matches; autoComplete (lowerCase (input), customReports, matches); @@ -77,8 +77,7 @@ bool Cmd::validCustom (const std::string& input) //////////////////////////////////////////////////////////////////////////////// void Cmd::parse (const std::string& input) { - loadCommands (); - loadCustomReports (); + load (); 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) { @@ -133,14 +132,8 @@ void Cmd::loadCommands () 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_VERSION, "version")); - } -} -//////////////////////////////////////////////////////////////////////////////// -void Cmd::loadCustomReports () -{ - if (customReports.size () == 0) - { + // Now load the custom reports. std::vector all; context.config.all (all); @@ -155,6 +148,8 @@ void Cmd::loadCustomReports () report = report.substr (0, columns); // 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); commands.push_back (report); } diff --git a/src/Cmd.h b/src/Cmd.h index c2f9dd40a..fab4a7053 100644 --- a/src/Cmd.h +++ b/src/Cmd.h @@ -50,8 +50,7 @@ public: std::string command; private: - void loadCommands (); - void loadCustomReports (); + void load (); private: std::vector commands; diff --git a/src/util.cpp b/src/util.cpp index ca0ec667d..4afb0f3a8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -41,6 +41,7 @@ #include "text.h" #include "main.h" #include "i18n.h" +#include "util.h" #include "../auto.h" extern Context context; @@ -152,26 +153,25 @@ int autoComplete ( const std::vector& list, std::vector& matches) { - matches.erase (matches.begin (), matches.end ()); + matches.clear (); // Handle trivial case. unsigned int length = partial.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 == list[i]) + if (partial == *item) { - matches.erase (matches.begin (), matches.end ()); - matches.push_back (list[i]); + matches.clear (); + matches.push_back (*item); return 1; } // Maintain a list of partial matches. - if (length <= list[i].length () && - ! strncmp (partial.c_str (), list[i].c_str (), length)) - matches.push_back (list[i]); + if (length <= item->length () && + partial == item->substr (0, length)) + matches.push_back (*item); } }