StringUuid: Implement new string(UUID) sub-command.

This commit is contained in:
Nils Gladitz
2014-08-25 22:44:06 +02:00
parent c2a47a9ac3
commit 328e869433
26 changed files with 478 additions and 0 deletions

View File

@@ -20,6 +20,7 @@
#include <time.h>
#include <cmTimestamp.h>
#include <cmUuid.h>
//----------------------------------------------------------------------------
bool cmStringCommand
@@ -105,6 +106,10 @@ bool cmStringCommand
{
return this->HandleGenexStripCommand(args);
}
else if(subCommand == "UUID")
{
return this->HandleUuidCommand(args);
}
std::string e = "does not recognize sub-command "+subCommand;
this->SetError(e);
@@ -981,3 +986,114 @@ bool cmStringCommand
return true;
}
bool cmStringCommand
::HandleUuidCommand(std::vector<std::string> const& args)
{
#if defined(CMAKE_BUILD_WITH_CMAKE)
unsigned int argsIndex = 1;
if(args.size() < 2)
{
this->SetError("UUID sub-command requires an output variable.");
return false;
}
const std::string &outputVariable = args[argsIndex++];
std::string uuidNamespaceString;
std::string uuidName;
std::string uuidType;
bool uuidUpperCase = false;
while(args.size() > argsIndex)
{
if(args[argsIndex] == "NAMESPACE")
{
++argsIndex;
if(argsIndex >= args.size())
{
this->SetError("UUID sub-command, NAMESPACE requires a value.");
return false;
}
uuidNamespaceString = args[argsIndex++];
}
else if(args[argsIndex] == "NAME")
{
++argsIndex;
if(argsIndex >= args.size())
{
this->SetError("UUID sub-command, NAME requires a value.");
return false;
}
uuidName = args[argsIndex++];
}
else if(args[argsIndex] == "TYPE")
{
++argsIndex;
if(argsIndex >= args.size())
{
this->SetError("UUID sub-command, TYPE requires a value.");
return false;
}
uuidType = args[argsIndex++];
}
else if(args[argsIndex] == "UPPER")
{
++argsIndex;
uuidUpperCase = true;
}
else
{
std::string e = "UUID sub-command does not recognize option " +
args[argsIndex] + ".";
this->SetError(e);
return false;
}
}
std::string uuid;
cmUuid uuidGenerator;
std::vector<unsigned char> uuidNamespace;
if(!uuidGenerator.StringToBinary(uuidNamespaceString, uuidNamespace))
{
this->SetError("UUID sub-command, malformed NAMESPACE UUID.");
return false;
}
if(uuidType == "MD5")
{
uuid = uuidGenerator.FromMd5(uuidNamespace, uuidName);
}
else if(uuidType == "SHA1")
{
uuid = uuidGenerator.FromSha1(uuidNamespace, uuidName);
}
else
{
std::string e = "UUID sub-command, unknown TYPE '" + uuidType + "'.";
this->SetError(e);
return false;
}
if(uuid.empty())
{
this->SetError("UUID sub-command, generation failed.");
return false;
}
if(uuidUpperCase)
{
uuid = cmSystemTools::UpperCase(uuid);
}
this->Makefile->AddDefinition(outputVariable, uuid.c_str());
return true;
#else
cmOStringStream e;
e << args[0] << " not available during bootstrap";
this->SetError(e.str().c_str());
return false;
#endif
}