2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2007-07-02 19:43:21 +00:00
|
|
|
#include "cmCommandArgumentsHelper.h"
|
|
|
|
|
2012-08-13 17:42:58 +00:00
|
|
|
cmCommandArgument::cmCommandArgument(cmCommandArgumentsHelper* args,
|
|
|
|
const char* key,
|
2007-07-02 19:43:21 +00:00
|
|
|
cmCommandArgumentGroup* group)
|
2016-05-16 14:34:04 +00:00
|
|
|
: Key(key)
|
|
|
|
, Group(group)
|
|
|
|
, WasActive(false)
|
|
|
|
, ArgumentsBeforeEmpty(true)
|
|
|
|
, CurrentIndex(0)
|
2007-07-02 19:43:21 +00:00
|
|
|
{
|
2017-08-22 21:42:36 +00:00
|
|
|
if (args != nullptr) {
|
2007-07-02 19:43:21 +00:00
|
|
|
args->AddArgument(this);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2012-08-13 17:42:58 +00:00
|
|
|
|
2017-08-22 21:42:36 +00:00
|
|
|
if (this->Group != nullptr) {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->Group->ContainedArguments.push_back(this);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgument::Reset()
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
this->WasActive = false;
|
2007-07-02 19:43:21 +00:00
|
|
|
this->CurrentIndex = 0;
|
|
|
|
this->DoReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgument::Follows(const cmCommandArgument* arg)
|
|
|
|
{
|
2007-08-23 20:13:15 +00:00
|
|
|
this->ArgumentsBeforeEmpty = false;
|
2007-07-02 19:43:21 +00:00
|
|
|
this->ArgumentsBefore.insert(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgument::FollowsGroup(const cmCommandArgumentGroup* group)
|
|
|
|
{
|
2017-08-22 21:42:36 +00:00
|
|
|
if (group != nullptr) {
|
2007-08-23 20:13:15 +00:00
|
|
|
this->ArgumentsBeforeEmpty = false;
|
2014-11-25 15:33:00 +00:00
|
|
|
this->ArgumentsBefore.insert(group->ContainedArguments.begin(),
|
|
|
|
group->ContainedArguments.end());
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmCommandArgument::MayFollow(const cmCommandArgument* current) const
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->ArgumentsBeforeEmpty) {
|
2007-07-02 19:43:21 +00:00
|
|
|
return true;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2016-09-16 21:16:54 +00:00
|
|
|
return this->ArgumentsBefore.find(current) != this->ArgumentsBefore.end();
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmCommandArgument::KeyMatches(const std::string& key) const
|
|
|
|
{
|
2017-08-22 21:42:36 +00:00
|
|
|
if ((this->Key == nullptr) || (this->Key[0] == '\0')) {
|
2007-07-02 19:43:21 +00:00
|
|
|
return true;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
return (key == this->Key);
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgument::ApplyOwnGroup()
|
|
|
|
{
|
2017-08-22 21:42:36 +00:00
|
|
|
if (this->Group != nullptr) {
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<cmCommandArgument*>::const_iterator it =
|
|
|
|
this->Group->ContainedArguments.begin();
|
|
|
|
it != this->Group->ContainedArguments.end(); ++it) {
|
|
|
|
if (*it != this) {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->ArgumentsBefore.insert(*it);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgument::Activate()
|
|
|
|
{
|
2012-08-13 17:42:58 +00:00
|
|
|
this->WasActive = true;
|
2007-07-02 19:43:21 +00:00
|
|
|
this->CurrentIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmCommandArgument::Consume(const std::string& arg)
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
bool res = this->DoConsume(arg, this->CurrentIndex);
|
2007-07-02 19:43:21 +00:00
|
|
|
this->CurrentIndex++;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-08-13 17:42:58 +00:00
|
|
|
cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper* args,
|
|
|
|
const char* key,
|
2007-07-02 19:43:21 +00:00
|
|
|
cmCommandArgumentGroup* group)
|
2016-05-16 14:34:04 +00:00
|
|
|
: cmCommandArgument(args, key, group)
|
2017-08-22 21:42:36 +00:00
|
|
|
, Ignore(nullptr)
|
2007-07-02 19:43:21 +00:00
|
|
|
{
|
2017-08-22 21:42:36 +00:00
|
|
|
if ((key == nullptr) || (*key == 0)) {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->DataStart = 0;
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->DataStart = 1;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
bool cmCAStringVector::DoConsume(const std::string& arg, unsigned int index)
|
2007-07-02 19:43:21 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (index >= this->DataStart) {
|
2017-08-22 21:42:36 +00:00
|
|
|
if ((this->Ignore == nullptr) || (arg != this->Ignore)) {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->Vector.push_back(arg);
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCAStringVector::DoReset()
|
|
|
|
{
|
|
|
|
this->Vector.clear();
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmCAString::cmCAString(cmCommandArgumentsHelper* args, const char* key,
|
2007-07-02 19:43:21 +00:00
|
|
|
cmCommandArgumentGroup* group)
|
2016-05-16 14:34:04 +00:00
|
|
|
: cmCommandArgument(args, key, group)
|
2007-07-02 19:43:21 +00:00
|
|
|
{
|
2017-08-22 21:42:36 +00:00
|
|
|
if ((key == nullptr) || (*key == 0)) {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->DataStart = 0;
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->DataStart = 1;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (index == this->DataStart) {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->String = arg;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
|
|
|
|
return index >= this->DataStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCAString::DoReset()
|
|
|
|
{
|
2012-07-11 19:03:21 +00:00
|
|
|
this->String = "";
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args, const char* key,
|
2007-07-02 19:43:21 +00:00
|
|
|
cmCommandArgumentGroup* group)
|
2016-05-16 14:34:04 +00:00
|
|
|
: cmCommandArgument(args, key, group)
|
|
|
|
, Enabled(false)
|
|
|
|
{
|
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
|
2007-07-02 20:46:18 +00:00
|
|
|
bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
|
2007-07-02 19:43:21 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (index == 0) {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->Enabled = true;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCAEnabler::DoReset()
|
|
|
|
{
|
|
|
|
this->Enabled = false;
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args, const char* key,
|
2007-07-02 19:43:21 +00:00
|
|
|
cmCommandArgumentGroup* group)
|
2016-05-16 14:34:04 +00:00
|
|
|
: cmCommandArgument(args, key, group)
|
|
|
|
, Enabled(true)
|
|
|
|
{
|
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
|
2007-07-02 20:46:18 +00:00
|
|
|
bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
|
2007-07-02 19:43:21 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (index == 0) {
|
2007-07-02 19:43:21 +00:00
|
|
|
this->Enabled = false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCADisabler::DoReset()
|
|
|
|
{
|
|
|
|
this->Enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<cmCommandArgument*>::iterator it =
|
|
|
|
this->ContainedArguments.begin();
|
|
|
|
it != this->ContainedArguments.end(); ++it) {
|
2007-07-02 19:43:21 +00:00
|
|
|
(*it)->Follows(arg);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<cmCommandArgument*>::iterator it =
|
|
|
|
this->ContainedArguments.begin();
|
|
|
|
it != this->ContainedArguments.end(); ++it) {
|
2007-07-02 19:43:21 +00:00
|
|
|
(*it)->FollowsGroup(group);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
2012-08-13 17:42:58 +00:00
|
|
|
void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args,
|
2007-07-02 19:43:21 +00:00
|
|
|
std::vector<std::string>* unconsumedArgs)
|
|
|
|
{
|
2017-08-22 21:42:36 +00:00
|
|
|
if (args == nullptr) {
|
2007-07-02 19:43:21 +00:00
|
|
|
return;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<cmCommandArgument*>::iterator argIt =
|
|
|
|
this->Arguments.begin();
|
|
|
|
argIt != this->Arguments.end(); ++argIt) {
|
2007-07-02 19:43:21 +00:00
|
|
|
(*argIt)->ApplyOwnGroup();
|
|
|
|
(*argIt)->Reset();
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
|
2017-08-22 21:42:36 +00:00
|
|
|
cmCommandArgument* activeArgument = nullptr;
|
|
|
|
const cmCommandArgument* previousArgument = nullptr;
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<std::string>::const_iterator it = args->begin();
|
|
|
|
it != args->end(); ++it) {
|
|
|
|
for (std::vector<cmCommandArgument*>::iterator argIt =
|
|
|
|
this->Arguments.begin();
|
|
|
|
argIt != this->Arguments.end(); ++argIt) {
|
|
|
|
if ((*argIt)->KeyMatches(*it) &&
|
|
|
|
((*argIt)->MayFollow(previousArgument))) {
|
2007-07-02 19:43:21 +00:00
|
|
|
activeArgument = *argIt;
|
|
|
|
activeArgument->Activate();
|
|
|
|
break;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
if (activeArgument) {
|
2007-07-02 19:43:21 +00:00
|
|
|
bool argDone = activeArgument->Consume(*it);
|
|
|
|
previousArgument = activeArgument;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (argDone) {
|
2017-08-22 21:42:36 +00:00
|
|
|
activeArgument = nullptr;
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2017-08-22 21:42:36 +00:00
|
|
|
if (unconsumedArgs != nullptr) {
|
2007-07-02 19:43:21 +00:00
|
|
|
unconsumedArgs->push_back(*it);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
|
|
|
|
{
|
|
|
|
this->Arguments.push_back(arg);
|
|
|
|
}
|