Enhancement - id sequences

- Recognizes id sequences on the command line.  Doesn't do anything
  with them yet.
This commit is contained in:
Paul Beckingham 2009-05-04 22:24:43 -04:00
parent f73281ee30
commit d135dc2337
2 changed files with 96 additions and 11 deletions

View file

@ -50,6 +50,8 @@ public:
int getId () const { return mId; }
void setId (int id) { mId = id; }
std::vector <int> getAllIds () const { return mSequence; }
void addId (int id) { mSequence.push_back (id); }
status getStatus () const { return mStatus; }
void setStatus (status s) { mStatus = s; }
@ -95,6 +97,7 @@ private:
status mStatus;
std::string mUUID;
int mId;
std::vector <int> mSequence;
std::string mDescription;
std::vector<std::string> mTags;
std::vector<std::string> mRemoveTags;

View file

@ -306,6 +306,60 @@ static bool validId (const std::string& input)
return true;
}
////////////////////////////////////////////////////////////////////////////////
// 1,2-4,6
static bool validSequence (
const std::string& input,
std::vector <int>& ids)
{
std::vector <std::string> ranges;
split (ranges, input, ',');
std::vector <std::string>::iterator it;
for (it = ranges.begin (); it != ranges.end (); ++it)
{
std::vector <std::string> range;
split (range, *it, '-');
switch (range.size ())
{
case 1:
if (! validId (range[0]))
return false;
int id = ::atoi (range[0].c_str ());
ids.push_back (id);
// std::cout << "# seq: " << id << std::endl;
break;
case 2:
{
if (! validId (range[0]) ||
! validId (range[1]))
return false;
int low = ::atoi (range[0].c_str ());
int high = ::atoi (range[1].c_str ());
if (low >= high)
return false;
for (int i = low; i <= high; ++i)
// {
ids.push_back (i);
// std::cout << "# seq: " << i << std::endl;
// }
}
break;
default:
return false;
break;
}
}
return ids.size () > 1 ? true : false;
}
////////////////////////////////////////////////////////////////////////////////
static bool validTag (const std::string& input)
{
@ -390,15 +444,25 @@ bool validDuration (std::string& input)
}
////////////////////////////////////////////////////////////////////////////////
// Token Distinguishing characteristic
// ------- -----------------------------
// command first positional
// id \d+
// description default, accumulate
// substitution /\w+/\w*/
// tags [-+]\w+
// attributes \w+:.+
// Token EBNF
// ------- ----------------------------------
// command first non-id recognized argument
//
// id ::= \d+
//
// substitution ::= "/" from "/" to "/g"
// | "/" from "/" to "/" ;
//
// tags ::= "+" word
// | "-" word ;
//
// attributes ::= word ":" value
// | word ":"
//
// sequence ::= id "," sequence
// | id "-" id ;
//
// description (whatever isn't one of the above)
void parse (
std::vector <std::string>& args,
std::string& command,
@ -420,12 +484,30 @@ void parse (
std::string from;
std::string to;
bool global;
std::vector <int> sequence;
// An id is the first argument found that contains all digits.
if (lowerCase (command) != "add" && // "add" doesn't require an ID
task.getId () == 0 &&
validId (arg))
if (lowerCase (command) != "add" &&
/*
task.getSequenceCount () == 0 &&
*/
validSequence (arg, sequence))
{
/*
for (?)
task.addSequence (?)
*/
std::cout << "# look like a sequence" << std::endl;
foreach (id, sequence)
task.addId (*id);
}
else if (lowerCase (command) != "add" && // "add" doesn't require an ID
task.getId () == 0 &&
validId (arg))
{
task.setId (::atoi (arg.c_str ()));
}
// Tags begin with + or - and contain arbitrary text.
else if (validTag (arg))