ItemSet Parse(std::istream& in) { ItemSet items; std::string currentItem; int openBraces = 0; while (!in.eof() && !in.fail()) { std::string currentLine; std::getline(in, currentLine); auto commentPos = currentLine.find('#'); if (commentPos != std::string::npos) currentLine.erase(commentPos); // remove any comment if (!currentItem.empty()) currentItem.push_back('\n'); // preserve new-lines within an item currentItem += currentLine; openBraces += std::count(currentLine.begin(), currentLine.end(), '{'); openBraces -= std::count(currentLine.begin(), currentLine.end(), '}'); if (openBraces <= 0) { auto lastCharPos = currentItem.find_last_not_of(" \t"); if (lastCharPos != std::string::npos && currentItem[lastCharPos] != '=') { auto parsedItem = ParseItem(currentItem); if (parsedItem) items.push_back(std::move(parsedItem)); currentItem.clear(); openBraces = 0; } } } return items; }