Ignore comment character if it appears in quotes

This commit is contained in:
Dan Paulat 2023-07-17 21:57:04 -05:00
parent 6767c0c50a
commit c99e24f2c1
2 changed files with 15 additions and 6 deletions

View file

@ -33,7 +33,7 @@ public:
Placefile(Placefile&&) noexcept;
Placefile& operator=(Placefile&&) noexcept;
bool IsValid() const;
bool IsValid() const;
static std::shared_ptr<Placefile> Load(const std::string& filename);
static std::shared_ptr<Placefile> Load(std::istream& is);
@ -43,5 +43,5 @@ private:
std::unique_ptr<Impl> p;
};
} // namespace common
} // namespace gr
} // namespace scwx

View file

@ -110,11 +110,20 @@ std::shared_ptr<Placefile> Placefile::Load(std::istream& is)
while (scwx::util::getline(is, line))
{
// Find position of comment (;)
std::size_t lineEnd = line.find(';');
if (lineEnd == std::string::npos)
bool inQuotes = false;
for (std::size_t i = 0; i < line.size(); ++i)
{
// Remove comment
line.erase(lineEnd);
if (!inQuotes && line[i] == ';')
{
// Remove comment
line.erase(i);
break;
}
else if (line[i] == '"')
{
// Toggle quote state
inQuotes = !inQuotes;
}
}
// Remove extra spacing from line