certik / yaml-cpp

Automatically exported from code.google.com/p/yaml-cpp
MIT License
0 stars 0 forks source link

YAML::Parser::GetNextDocument in the new API #206

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please enlighten me, if I'm mistaken, but it seems, that in the new API there 
is no equivalent to the following code:

std::ifstream fin("test.yaml");
YAML::Parser parser(fin);

YAML::Node doc;
while(parser.GetNextDocument(doc)) {
   // ...
}

I don't want an event based parsing as in parser.HandleNextDocument, but just 
want to get a YAML::Node for each document in the stream, one after another.

This is related to the issue, that YAML::Parser always reads in the whole 
stream, because that's why the following new API code won't work:

std::ifstream fin("test.yaml");

YAML::Node doc;
while (1)
{
    doc.Load (fin);
    if (doc.IsNull ())
        break;
    // ...
}

As YAML::Node::Load creates a YAML::Parser that consumes the whole file.

So if there is already a possibility to do what I want with the new API, please 
tell me, otherwise I'd consider this a serious flaw. Possible solutions would 
be to reimplement YAML::Parser::GetNextDocument, to make the NodeBuilder class 
public, so that YAML::Parser::HandleNextDocument can be used easily for this 
purpose, or (ultimately the best solution) to make YAML::Load or actually 
YAML::Parser only consume one document at a time, so that the code above can be 
used.

You might think: Why not just use YAML::Node::LoadAll? It's probably true that 
this will have the same effect right at the moment, but actually I don't want 
to read all documents into memory at once, but one document at a time, so that 
the memory requirements at each time are as low as possible, so I'd prefer 
having another possibility in the API that for now might just actually be 
equivalent to use YAML::Node::LoadAll, but in the future can be changed to 
reading only the one required document into memory.

Therefore as I see it the best solution is to fix YAML::Parser, whereas the 
easiest solution would be to make NodeBuilder publicly accessible.

Original issue reported on code.google.com by ekpy...@googlemail.com on 6 Jun 2013 at 6:59

GoogleCodeExporter commented 9 years ago
You're right, this is a good idea.

Original comment by jbe...@gmail.com on 6 Jun 2013 at 3:52