Commit 41703451 authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #15851 from alalek:fixup_15842

parents a165f555 dcf72e49
...@@ -1900,18 +1900,19 @@ int FileStorage::getFormat() const ...@@ -1900,18 +1900,19 @@ int FileStorage::getFormat() const
FileNode FileStorage::operator [](const char* key) const FileNode FileStorage::operator [](const char* key) const
{ {
if( p->roots.empty() ) return this->operator[](std::string(key));
return FileNode();
return p->roots[0][key];
} }
FileNode FileStorage::operator [](const std::string& key) const FileNode FileStorage::operator [](const std::string& key) const
{ {
if( p->roots.empty() ) FileNode res;
return FileNode(); for (size_t i = 0; i < p->roots.size(); i++)
{
return p->roots[0][key]; res = p->roots[i][key];
if (!res.empty())
break;
}
return res;
} }
String FileStorage::releaseAndGetString() String FileStorage::releaseAndGetString()
......
...@@ -770,7 +770,7 @@ public: ...@@ -770,7 +770,7 @@ public:
bool first = true; bool first = true;
bool ok = true; bool ok = true;
FileNode root_collection(fs->getFS(), 0, 0); FileNode root_collection(fs->getFS(), 0, 0);
FileNode root_node = fs->addNode(root_collection, std::string(), FileNode::NONE);
for(;;) for(;;)
{ {
// 0. skip leading comments and directives and ... // 0. skip leading comments and directives and ...
...@@ -821,6 +821,7 @@ public: ...@@ -821,6 +821,7 @@ public:
if( memcmp( ptr, "...", 3 ) != 0 ) if( memcmp( ptr, "...", 3 ) != 0 )
{ {
// 2. parse the collection // 2. parse the collection
FileNode root_node = fs->addNode(root_collection, std::string(), FileNode::NONE);
ptr = parseValue( ptr, root_node, 0, false ); ptr = parseValue( ptr, root_node, 0, false );
if( !root_node.isMap() && !root_node.isSeq() ) if( !root_node.isMap() && !root_node.isSeq() )
......
...@@ -1654,9 +1654,19 @@ TEST(Core_InputOutput, FileStorage_YAML_parse_multiple_documents) ...@@ -1654,9 +1654,19 @@ TEST(Core_InputOutput, FileStorage_YAML_parse_multiple_documents)
fs.release(); fs.release();
fs.open(filename, FileStorage::READ); fs.open(filename, FileStorage::READ);
ASSERT_EQ(42, (int)fs["a"]);
ASSERT_EQ(1988, (int)fs["b"]); EXPECT_EQ(42, (int)fs["a"]);
EXPECT_EQ(1988, (int)fs["b"]);
EXPECT_EQ(42, (int)fs.root(0)["a"]);
EXPECT_TRUE(fs.root(0)["b"].empty());
EXPECT_TRUE(fs.root(1)["a"].empty());
EXPECT_EQ(1988, (int)fs.root(1)["b"]);
fs.release(); fs.release();
ASSERT_EQ(0, std::remove(filename.c_str()));
} }
}} // namespace }} // namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment