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. */
|
2013-01-08 13:10:42 +00:00
|
|
|
#include "cmVisualStudioSlnParser.h"
|
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "cmVisualStudioSlnData.h"
|
2017-04-11 20:00:21 +00:00
|
|
|
#include "cmsys/FStream.hxx"
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <stack>
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
namespace {
|
|
|
|
enum LineFormat
|
2013-01-08 13:10:42 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
LineMultiValueTag,
|
|
|
|
LineSingleValueTag,
|
|
|
|
LineKeyValuePair,
|
|
|
|
LineVerbatim
|
|
|
|
};
|
2013-01-08 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class cmVisualStudioSlnParser::ParsedLine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool IsComment() const;
|
|
|
|
bool IsKeyValuePair() const;
|
|
|
|
|
|
|
|
const std::string& GetTag() const { return this->Tag; }
|
|
|
|
const std::string& GetArg() const { return this->Arg.first; }
|
|
|
|
std::string GetArgVerbatim() const;
|
|
|
|
size_t GetValueCount() const { return this->Values.size(); }
|
|
|
|
const std::string& GetValue(size_t idxValue) const;
|
|
|
|
std::string GetValueVerbatim(size_t idxValue) const;
|
|
|
|
|
|
|
|
void SetTag(const std::string& tag) { this->Tag = tag; }
|
|
|
|
void SetArg(const std::string& arg) { this->Arg = StringData(arg, false); }
|
|
|
|
void SetQuotedArg(const std::string& arg)
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
this->Arg = StringData(arg, true);
|
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
void AddValue(const std::string& value)
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
this->Values.push_back(StringData(value, false));
|
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
void AddQuotedValue(const std::string& value)
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
this->Values.push_back(StringData(value, true));
|
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
void CopyVerbatim(const std::string& line) { this->Tag = line; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef std::pair<std::string, bool> StringData;
|
|
|
|
std::string Tag;
|
|
|
|
StringData Arg;
|
|
|
|
std::vector<StringData> Values;
|
|
|
|
static const std::string BadString;
|
|
|
|
static const std::string Quote;
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string cmVisualStudioSlnParser::ParsedLine::BadString;
|
|
|
|
const std::string cmVisualStudioSlnParser::ParsedLine::Quote("\"");
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::ParsedLine::IsComment() const
|
|
|
|
{
|
|
|
|
assert(!this->Tag.empty());
|
2016-05-16 14:34:04 +00:00
|
|
|
return (this->Tag[0] == '#');
|
2013-01-08 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::ParsedLine::IsKeyValuePair() const
|
|
|
|
{
|
|
|
|
assert(!this->Tag.empty());
|
|
|
|
return this->Arg.first.empty() && this->Values.size() == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmVisualStudioSlnParser::ParsedLine::GetArgVerbatim() const
|
|
|
|
{
|
|
|
|
if (this->Arg.second)
|
|
|
|
return Quote + this->Arg.first + Quote;
|
|
|
|
else
|
|
|
|
return this->Arg.first;
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
const std::string& cmVisualStudioSlnParser::ParsedLine::GetValue(
|
|
|
|
size_t idxValue) const
|
2013-01-08 13:10:42 +00:00
|
|
|
{
|
|
|
|
if (idxValue < this->Values.size())
|
|
|
|
return this->Values[idxValue].first;
|
|
|
|
else
|
|
|
|
return BadString;
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
std::string cmVisualStudioSlnParser::ParsedLine::GetValueVerbatim(
|
|
|
|
size_t idxValue) const
|
2013-01-08 13:10:42 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (idxValue < this->Values.size()) {
|
2013-01-08 13:10:42 +00:00
|
|
|
const StringData& data = this->Values[idxValue];
|
|
|
|
if (data.second)
|
|
|
|
return Quote + data.first + Quote;
|
|
|
|
else
|
|
|
|
return data.first;
|
2016-05-16 14:34:04 +00:00
|
|
|
} else
|
2013-01-08 13:10:42 +00:00
|
|
|
return BadString;
|
|
|
|
}
|
|
|
|
|
|
|
|
class cmVisualStudioSlnParser::State
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit State(DataGroupSet requestedData);
|
|
|
|
|
|
|
|
size_t GetCurrentLine() const { return this->CurrentLine; }
|
|
|
|
bool ReadLine(std::istream& input, std::string& line);
|
|
|
|
|
|
|
|
LineFormat NextLineFormat() const;
|
|
|
|
|
|
|
|
bool Process(const cmVisualStudioSlnParser::ParsedLine& line,
|
2016-05-16 14:34:04 +00:00
|
|
|
cmSlnData& output, cmVisualStudioSlnParser::ResultData& result);
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
bool Finished(cmVisualStudioSlnParser::ResultData& result);
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum FileState
|
|
|
|
{
|
|
|
|
FileStateStart,
|
|
|
|
FileStateTopLevel,
|
|
|
|
FileStateProject,
|
|
|
|
FileStateProjectDependencies,
|
|
|
|
FileStateGlobal,
|
|
|
|
FileStateSolutionConfigurations,
|
|
|
|
FileStateProjectConfigurations,
|
|
|
|
FileStateSolutionFilters,
|
|
|
|
FileStateGlobalSection,
|
|
|
|
FileStateIgnore
|
|
|
|
};
|
|
|
|
std::stack<FileState> Stack;
|
|
|
|
std::string EndIgnoreTag;
|
|
|
|
DataGroupSet RequestedData;
|
|
|
|
size_t CurrentLine;
|
|
|
|
|
|
|
|
void IgnoreUntilTag(const std::string& endTag);
|
|
|
|
};
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmVisualStudioSlnParser::State::State(DataGroupSet requestedData)
|
|
|
|
: RequestedData(requestedData)
|
|
|
|
, CurrentLine(0)
|
2013-01-08 13:10:42 +00:00
|
|
|
{
|
|
|
|
if (this->RequestedData.test(DataGroupProjectDependenciesBit))
|
|
|
|
this->RequestedData.set(DataGroupProjectsBit);
|
|
|
|
this->Stack.push(FileStateStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::State::ReadLine(std::istream& input,
|
|
|
|
std::string& line)
|
|
|
|
{
|
|
|
|
++this->CurrentLine;
|
|
|
|
return !std::getline(input, line).fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
LineFormat cmVisualStudioSlnParser::State::NextLineFormat() const
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
switch (this->Stack.top()) {
|
|
|
|
case FileStateStart:
|
|
|
|
return LineVerbatim;
|
|
|
|
case FileStateTopLevel:
|
|
|
|
return LineMultiValueTag;
|
|
|
|
case FileStateProject:
|
|
|
|
return LineSingleValueTag;
|
|
|
|
case FileStateProjectDependencies:
|
|
|
|
return LineKeyValuePair;
|
|
|
|
case FileStateGlobal:
|
|
|
|
return LineSingleValueTag;
|
|
|
|
case FileStateSolutionConfigurations:
|
|
|
|
return LineKeyValuePair;
|
|
|
|
case FileStateProjectConfigurations:
|
|
|
|
return LineKeyValuePair;
|
|
|
|
case FileStateSolutionFilters:
|
|
|
|
return LineKeyValuePair;
|
|
|
|
case FileStateGlobalSection:
|
|
|
|
return LineKeyValuePair;
|
|
|
|
case FileStateIgnore:
|
|
|
|
return LineVerbatim;
|
2013-01-08 13:10:42 +00:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return LineVerbatim;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::State::Process(
|
2016-05-16 14:34:04 +00:00
|
|
|
const cmVisualStudioSlnParser::ParsedLine& line, cmSlnData& output,
|
|
|
|
cmVisualStudioSlnParser::ResultData& result)
|
2013-01-08 13:10:42 +00:00
|
|
|
{
|
|
|
|
assert(!line.IsComment());
|
2016-05-16 14:34:04 +00:00
|
|
|
switch (this->Stack.top()) {
|
2013-01-08 13:10:42 +00:00
|
|
|
case FileStateStart:
|
|
|
|
if (!cmSystemTools::StringStartsWith(
|
2016-05-16 14:34:04 +00:00
|
|
|
line.GetTag().c_str(), "Microsoft Visual Studio Solution File")) {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
this->Stack.pop();
|
|
|
|
this->Stack.push(FileStateTopLevel);
|
|
|
|
break;
|
|
|
|
case FileStateTopLevel:
|
2016-05-16 14:34:04 +00:00
|
|
|
if (line.GetTag().compare("Project") == 0) {
|
|
|
|
if (line.GetValueCount() != 3) {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (this->RequestedData.test(DataGroupProjectsBit)) {
|
|
|
|
if (!output.AddProject(line.GetValue(2), line.GetValue(0),
|
|
|
|
line.GetValue(1))) {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputData, this->GetCurrentLine());
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
this->Stack.push(FileStateProject);
|
|
|
|
} else
|
2013-01-08 13:10:42 +00:00
|
|
|
this->IgnoreUntilTag("EndProject");
|
2016-05-16 14:34:04 +00:00
|
|
|
} else if (line.GetTag().compare("Global") == 0)
|
2013-01-08 13:10:42 +00:00
|
|
|
this->Stack.push(FileStateGlobal);
|
2016-05-16 14:34:04 +00:00
|
|
|
else {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
break;
|
|
|
|
case FileStateProject:
|
|
|
|
if (line.GetTag().compare("EndProject") == 0)
|
|
|
|
this->Stack.pop();
|
2016-05-16 14:34:04 +00:00
|
|
|
else if (line.GetTag().compare("ProjectSection") == 0) {
|
2013-01-08 13:10:42 +00:00
|
|
|
if (line.GetArg().compare("ProjectDependencies") == 0 &&
|
2016-05-16 14:34:04 +00:00
|
|
|
line.GetValue(0).compare("postProject") == 0) {
|
2013-01-08 13:10:42 +00:00
|
|
|
if (this->RequestedData.test(DataGroupProjectDependenciesBit))
|
|
|
|
this->Stack.push(FileStateProjectDependencies);
|
|
|
|
else
|
|
|
|
this->IgnoreUntilTag("EndProjectSection");
|
2016-05-16 14:34:04 +00:00
|
|
|
} else
|
2013-01-08 13:10:42 +00:00
|
|
|
this->IgnoreUntilTag("EndProjectSection");
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
break;
|
|
|
|
case FileStateProjectDependencies:
|
|
|
|
if (line.GetTag().compare("EndProjectSection") == 0)
|
|
|
|
this->Stack.pop();
|
|
|
|
else if (line.IsKeyValuePair())
|
|
|
|
// implement dependency storing here, once needed
|
|
|
|
;
|
2016-05-16 14:34:04 +00:00
|
|
|
else {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
break;
|
|
|
|
case FileStateGlobal:
|
|
|
|
if (line.GetTag().compare("EndGlobal") == 0)
|
|
|
|
this->Stack.pop();
|
2016-05-16 14:34:04 +00:00
|
|
|
else if (line.GetTag().compare("GlobalSection") == 0) {
|
2013-01-08 13:10:42 +00:00
|
|
|
if (line.GetArg().compare("SolutionConfigurationPlatforms") == 0 &&
|
2016-05-16 14:34:04 +00:00
|
|
|
line.GetValue(0).compare("preSolution") == 0) {
|
2013-01-08 13:10:42 +00:00
|
|
|
if (this->RequestedData.test(DataGroupSolutionConfigurationsBit))
|
|
|
|
this->Stack.push(FileStateSolutionConfigurations);
|
|
|
|
else
|
|
|
|
this->IgnoreUntilTag("EndGlobalSection");
|
2016-05-16 14:34:04 +00:00
|
|
|
} else if (line.GetArg().compare("ProjectConfigurationPlatforms") ==
|
|
|
|
0 &&
|
|
|
|
line.GetValue(0).compare("postSolution") == 0) {
|
2013-01-08 13:10:42 +00:00
|
|
|
if (this->RequestedData.test(DataGroupProjectConfigurationsBit))
|
|
|
|
this->Stack.push(FileStateProjectConfigurations);
|
|
|
|
else
|
|
|
|
this->IgnoreUntilTag("EndGlobalSection");
|
2016-05-16 14:34:04 +00:00
|
|
|
} else if (line.GetArg().compare("NestedProjects") == 0 &&
|
|
|
|
line.GetValue(0).compare("preSolution") == 0) {
|
2013-01-08 13:10:42 +00:00
|
|
|
if (this->RequestedData.test(DataGroupSolutionFiltersBit))
|
|
|
|
this->Stack.push(FileStateSolutionFilters);
|
|
|
|
else
|
|
|
|
this->IgnoreUntilTag("EndGlobalSection");
|
2016-05-16 14:34:04 +00:00
|
|
|
} else if (this->RequestedData.test(DataGroupGenericGlobalSectionsBit))
|
2013-01-08 13:10:42 +00:00
|
|
|
this->Stack.push(FileStateGlobalSection);
|
|
|
|
else
|
|
|
|
this->IgnoreUntilTag("EndGlobalSection");
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
break;
|
|
|
|
case FileStateSolutionConfigurations:
|
|
|
|
if (line.GetTag().compare("EndGlobalSection") == 0)
|
|
|
|
this->Stack.pop();
|
|
|
|
else if (line.IsKeyValuePair())
|
|
|
|
// implement configuration storing here, once needed
|
|
|
|
;
|
2016-05-16 14:34:04 +00:00
|
|
|
else {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
break;
|
|
|
|
case FileStateProjectConfigurations:
|
|
|
|
if (line.GetTag().compare("EndGlobalSection") == 0)
|
|
|
|
this->Stack.pop();
|
|
|
|
else if (line.IsKeyValuePair())
|
|
|
|
// implement configuration storing here, once needed
|
|
|
|
;
|
2016-05-16 14:34:04 +00:00
|
|
|
else {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
break;
|
|
|
|
case FileStateSolutionFilters:
|
|
|
|
if (line.GetTag().compare("EndGlobalSection") == 0)
|
|
|
|
this->Stack.pop();
|
|
|
|
else if (line.IsKeyValuePair())
|
|
|
|
// implement filter storing here, once needed
|
|
|
|
;
|
2016-05-16 14:34:04 +00:00
|
|
|
else {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
break;
|
|
|
|
case FileStateGlobalSection:
|
|
|
|
if (line.GetTag().compare("EndGlobalSection") == 0)
|
|
|
|
this->Stack.pop();
|
|
|
|
else if (line.IsKeyValuePair())
|
|
|
|
// implement section storing here, once needed
|
|
|
|
;
|
2016-05-16 14:34:04 +00:00
|
|
|
else {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
break;
|
|
|
|
case FileStateIgnore:
|
2016-05-16 14:34:04 +00:00
|
|
|
if (line.GetTag() == this->EndIgnoreTag) {
|
2013-01-08 13:10:42 +00:00
|
|
|
this->Stack.pop();
|
2017-09-15 23:26:49 +00:00
|
|
|
this->EndIgnoreTag.clear();
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result.SetError(ResultErrorBadInternalState, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::State::Finished(
|
|
|
|
cmVisualStudioSlnParser::ResultData& result)
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->Stack.top() != FileStateTopLevel) {
|
2013-01-08 13:10:42 +00:00
|
|
|
result.SetError(ResultErrorInputStructure, this->GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
result.Result = ResultOK;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmVisualStudioSlnParser::State::IgnoreUntilTag(const std::string& endTag)
|
|
|
|
{
|
|
|
|
this->Stack.push(FileStateIgnore);
|
|
|
|
this->EndIgnoreTag = endTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmVisualStudioSlnParser::ResultData::ResultData()
|
|
|
|
: Result(ResultOK)
|
|
|
|
, ResultLine(0)
|
2016-05-16 14:34:04 +00:00
|
|
|
{
|
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
void cmVisualStudioSlnParser::ResultData::Clear()
|
|
|
|
{
|
|
|
|
*this = ResultData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmVisualStudioSlnParser::ResultData::SetError(ParseResult error,
|
|
|
|
size_t line)
|
|
|
|
{
|
|
|
|
this->Result = error;
|
|
|
|
this->ResultLine = line;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cmVisualStudioSlnParser::DataGroupSet
|
2016-05-16 14:34:04 +00:00
|
|
|
cmVisualStudioSlnParser::DataGroupProjects(
|
|
|
|
1 << cmVisualStudioSlnParser::DataGroupProjectsBit);
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
const cmVisualStudioSlnParser::DataGroupSet
|
2016-05-16 14:34:04 +00:00
|
|
|
cmVisualStudioSlnParser::DataGroupProjectDependencies(
|
|
|
|
1 << cmVisualStudioSlnParser::DataGroupProjectDependenciesBit);
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
const cmVisualStudioSlnParser::DataGroupSet
|
2016-05-16 14:34:04 +00:00
|
|
|
cmVisualStudioSlnParser::DataGroupSolutionConfigurations(
|
|
|
|
1 << cmVisualStudioSlnParser::DataGroupSolutionConfigurationsBit);
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
const cmVisualStudioSlnParser::DataGroupSet
|
2016-05-16 14:34:04 +00:00
|
|
|
cmVisualStudioSlnParser::DataGroupProjectConfigurations(
|
|
|
|
1 << cmVisualStudioSlnParser::DataGroupProjectConfigurationsBit);
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
const cmVisualStudioSlnParser::DataGroupSet
|
2016-05-16 14:34:04 +00:00
|
|
|
cmVisualStudioSlnParser::DataGroupSolutionFilters(
|
|
|
|
1 << cmVisualStudioSlnParser::DataGroupSolutionFiltersBit);
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
const cmVisualStudioSlnParser::DataGroupSet
|
2016-05-16 14:34:04 +00:00
|
|
|
cmVisualStudioSlnParser::DataGroupGenericGlobalSections(
|
|
|
|
1 << cmVisualStudioSlnParser::DataGroupGenericGlobalSectionsBit);
|
2013-01-08 13:10:42 +00:00
|
|
|
|
|
|
|
const cmVisualStudioSlnParser::DataGroupSet
|
2016-05-16 14:34:04 +00:00
|
|
|
cmVisualStudioSlnParser::DataGroupAll(~0);
|
2013-01-08 13:10:42 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
bool cmVisualStudioSlnParser::Parse(std::istream& input, cmSlnData& output,
|
2013-01-08 13:10:42 +00:00
|
|
|
DataGroupSet dataGroups)
|
|
|
|
{
|
|
|
|
this->LastResult.Clear();
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!this->IsDataGroupSetSupported(dataGroups)) {
|
2013-01-08 13:10:42 +00:00
|
|
|
this->LastResult.SetError(ResultErrorUnsupportedDataGroup, 0);
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
State state(dataGroups);
|
|
|
|
return this->ParseImpl(input, output, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::ParseFile(const std::string& file,
|
|
|
|
cmSlnData& output,
|
|
|
|
DataGroupSet dataGroups)
|
|
|
|
{
|
|
|
|
this->LastResult.Clear();
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!this->IsDataGroupSetSupported(dataGroups)) {
|
2013-01-08 13:10:42 +00:00
|
|
|
this->LastResult.SetError(ResultErrorUnsupportedDataGroup, 0);
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-01-04 05:47:13 +00:00
|
|
|
cmsys::ifstream f(file.c_str());
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!f) {
|
2013-01-08 13:10:42 +00:00
|
|
|
this->LastResult.SetError(ResultErrorOpeningInput, 0);
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
State state(dataGroups);
|
|
|
|
return this->ParseImpl(f, output, state);
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmVisualStudioSlnParser::ParseResult cmVisualStudioSlnParser::GetParseResult()
|
|
|
|
const
|
2013-01-08 13:10:42 +00:00
|
|
|
{
|
|
|
|
return this->LastResult.Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t cmVisualStudioSlnParser::GetParseResultLine() const
|
|
|
|
{
|
|
|
|
return this->LastResult.ResultLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::GetParseHadBOM() const
|
|
|
|
{
|
|
|
|
return this->LastResult.HadBOM;
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
bool cmVisualStudioSlnParser::IsDataGroupSetSupported(
|
|
|
|
DataGroupSet dataGroups) const
|
2013-01-08 13:10:42 +00:00
|
|
|
{
|
|
|
|
return (dataGroups & DataGroupProjects) == dataGroups;
|
2016-05-16 14:34:04 +00:00
|
|
|
// only supporting DataGroupProjects for now
|
2013-01-08 13:10:42 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
bool cmVisualStudioSlnParser::ParseImpl(std::istream& input, cmSlnData& output,
|
2013-01-08 13:10:42 +00:00
|
|
|
State& state)
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
// Does the .sln start with a Byte Order Mark?
|
|
|
|
if (!this->ParseBOM(input, line, state))
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
do {
|
2013-01-08 13:10:42 +00:00
|
|
|
line = cmSystemTools::TrimWhitespace(line);
|
|
|
|
if (line.empty())
|
|
|
|
continue;
|
|
|
|
ParsedLine parsedLine;
|
2016-05-16 14:34:04 +00:00
|
|
|
switch (state.NextLineFormat()) {
|
2013-01-08 13:10:42 +00:00
|
|
|
case LineMultiValueTag:
|
|
|
|
if (!this->ParseMultiValueTag(line, parsedLine, state))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case LineSingleValueTag:
|
|
|
|
if (!this->ParseSingleValueTag(line, parsedLine, state))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case LineKeyValuePair:
|
|
|
|
if (!this->ParseKeyValuePair(line, parsedLine, state))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case LineVerbatim:
|
|
|
|
parsedLine.CopyVerbatim(line);
|
|
|
|
break;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
if (parsedLine.IsComment())
|
|
|
|
continue;
|
|
|
|
if (!state.Process(parsedLine, output, this->LastResult))
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
} while (state.ReadLine(input, line));
|
2013-01-08 13:10:42 +00:00
|
|
|
return state.Finished(this->LastResult);
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
bool cmVisualStudioSlnParser::ParseBOM(std::istream& input, std::string& line,
|
2013-01-08 13:10:42 +00:00
|
|
|
State& state)
|
|
|
|
{
|
|
|
|
char bom[4];
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!input.get(bom, 4)) {
|
2013-01-08 13:10:42 +00:00
|
|
|
this->LastResult.SetError(ResultErrorReadingInput, 1);
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
this->LastResult.HadBOM =
|
|
|
|
(bom[0] == char(0xEF) && bom[1] == char(0xBB) && bom[2] == char(0xBF));
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!state.ReadLine(input, line)) {
|
2013-01-08 13:10:42 +00:00
|
|
|
this->LastResult.SetError(ResultErrorReadingInput, 1);
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
if (!this->LastResult.HadBOM)
|
2016-05-16 14:34:04 +00:00
|
|
|
line = bom + line; // it wasn't a BOM, prepend it to first line
|
2013-01-08 13:10:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::ParseMultiValueTag(const std::string& line,
|
|
|
|
ParsedLine& parsedLine,
|
|
|
|
State& state)
|
|
|
|
{
|
|
|
|
size_t idxEqualSign = line.find('=');
|
|
|
|
const std::string& fullTag = line.substr(0, idxEqualSign);
|
|
|
|
if (!this->ParseTag(fullTag, parsedLine, state))
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (idxEqualSign != line.npos) {
|
2013-01-08 13:10:42 +00:00
|
|
|
size_t idxFieldStart = idxEqualSign + 1;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (idxFieldStart < line.size()) {
|
2013-01-08 13:10:42 +00:00
|
|
|
size_t idxParsing = idxFieldStart;
|
|
|
|
bool inQuotes = false;
|
2016-05-16 14:34:04 +00:00
|
|
|
for (;;) {
|
2013-01-08 13:10:42 +00:00
|
|
|
idxParsing = line.find_first_of(",\"", idxParsing);
|
|
|
|
bool fieldOver = false;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (idxParsing == line.npos) {
|
2013-01-08 13:10:42 +00:00
|
|
|
fieldOver = true;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (inQuotes) {
|
2013-01-08 13:10:42 +00:00
|
|
|
this->LastResult.SetError(ResultErrorInputStructure,
|
|
|
|
state.GetCurrentLine());
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
} else if (line[idxParsing] == ',' && !inQuotes)
|
2013-01-08 13:10:42 +00:00
|
|
|
fieldOver = true;
|
|
|
|
else if (line[idxParsing] == '"')
|
|
|
|
inQuotes = !inQuotes;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (fieldOver) {
|
|
|
|
if (!this->ParseValue(
|
|
|
|
line.substr(idxFieldStart, idxParsing - idxFieldStart),
|
|
|
|
parsedLine))
|
2013-01-08 13:10:42 +00:00
|
|
|
return false;
|
|
|
|
if (idxParsing == line.npos)
|
2016-05-16 14:34:04 +00:00
|
|
|
break; // end of last field
|
2013-01-08 13:10:42 +00:00
|
|
|
idxFieldStart = idxParsing + 1;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
++idxParsing;
|
2013-01-08 13:10:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::ParseSingleValueTag(const std::string& line,
|
|
|
|
ParsedLine& parsedLine,
|
|
|
|
State& state)
|
|
|
|
{
|
|
|
|
size_t idxEqualSign = line.find('=');
|
|
|
|
const std::string& fullTag = line.substr(0, idxEqualSign);
|
|
|
|
if (!this->ParseTag(fullTag, parsedLine, state))
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (idxEqualSign != line.npos) {
|
2013-01-08 13:10:42 +00:00
|
|
|
if (!this->ParseValue(line.substr(idxEqualSign + 1), parsedLine))
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::ParseKeyValuePair(const std::string& line,
|
|
|
|
ParsedLine& parsedLine,
|
|
|
|
State& /*state*/)
|
|
|
|
{
|
|
|
|
size_t idxEqualSign = line.find('=');
|
2016-05-16 14:34:04 +00:00
|
|
|
if (idxEqualSign == line.npos) {
|
2013-01-08 13:10:42 +00:00
|
|
|
parsedLine.CopyVerbatim(line);
|
|
|
|
return true;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
const std::string& key = line.substr(0, idxEqualSign);
|
|
|
|
parsedLine.SetTag(cmSystemTools::TrimWhitespace(key));
|
|
|
|
const std::string& value = line.substr(idxEqualSign + 1);
|
|
|
|
parsedLine.AddValue(cmSystemTools::TrimWhitespace(value));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::ParseTag(const std::string& fullTag,
|
2016-05-16 14:34:04 +00:00
|
|
|
ParsedLine& parsedLine, State& state)
|
2013-01-08 13:10:42 +00:00
|
|
|
{
|
|
|
|
size_t idxLeftParen = fullTag.find('(');
|
2016-05-16 14:34:04 +00:00
|
|
|
if (idxLeftParen == fullTag.npos) {
|
2013-01-08 13:10:42 +00:00
|
|
|
parsedLine.SetTag(cmSystemTools::TrimWhitespace(fullTag));
|
|
|
|
return true;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
parsedLine.SetTag(
|
|
|
|
cmSystemTools::TrimWhitespace(fullTag.substr(0, idxLeftParen)));
|
|
|
|
size_t idxRightParen = fullTag.rfind(')');
|
2016-05-16 14:34:04 +00:00
|
|
|
if (idxRightParen == fullTag.npos) {
|
2013-01-08 13:10:42 +00:00
|
|
|
this->LastResult.SetError(ResultErrorInputStructure,
|
|
|
|
state.GetCurrentLine());
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2013-01-08 13:10:42 +00:00
|
|
|
const std::string& arg = cmSystemTools::TrimWhitespace(
|
|
|
|
fullTag.substr(idxLeftParen + 1, idxRightParen - idxLeftParen - 1));
|
2016-05-16 14:34:04 +00:00
|
|
|
if (arg[0] == '"') {
|
|
|
|
if (arg[arg.size() - 1] != '"') {
|
2013-01-08 13:10:42 +00:00
|
|
|
this->LastResult.SetError(ResultErrorInputStructure,
|
|
|
|
state.GetCurrentLine());
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
parsedLine.SetQuotedArg(arg.substr(1, arg.size() - 2));
|
|
|
|
} else
|
2013-01-08 13:10:42 +00:00
|
|
|
parsedLine.SetArg(arg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmVisualStudioSlnParser::ParseValue(const std::string& value,
|
|
|
|
ParsedLine& parsedLine)
|
|
|
|
{
|
|
|
|
const std::string& trimmed = cmSystemTools::TrimWhitespace(value);
|
|
|
|
if (trimmed.empty())
|
|
|
|
parsedLine.AddValue(trimmed);
|
|
|
|
else if (trimmed[0] == '"' && trimmed[trimmed.size() - 1] == '"')
|
|
|
|
parsedLine.AddQuotedValue(trimmed.substr(1, trimmed.size() - 2));
|
|
|
|
else
|
|
|
|
parsedLine.AddValue(trimmed);
|
|
|
|
return true;
|
|
|
|
}
|