mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Rewrote the output so that it emits correct YAML.
Fixed a bug in the last newline of a block folded scalar.
This commit is contained in:
@@ -19,7 +19,7 @@ namespace YAML
|
|||||||
virtual ~Content();
|
virtual ~Content();
|
||||||
|
|
||||||
virtual void Parse(Scanner *pScanner, const ParserState& state) = 0;
|
virtual void Parse(Scanner *pScanner, const ParserState& state) = 0;
|
||||||
virtual void Write(std::ostream& out, int indent) = 0;
|
virtual void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine) = 0;
|
||||||
|
|
||||||
virtual bool GetBegin(std::vector <Node *>::const_iterator& it) const { return false; }
|
virtual bool GetBegin(std::vector <Node *>::const_iterator& it) const { return false; }
|
||||||
virtual bool GetBegin(std::map <Node *, Node *>::const_iterator& it) const { return false; }
|
virtual bool GetBegin(std::map <Node *, Node *>::const_iterator& it) const { return false; }
|
||||||
|
5
main.cpp
5
main.cpp
@@ -73,10 +73,7 @@ int main()
|
|||||||
|
|
||||||
YAML::Node doc;
|
YAML::Node doc;
|
||||||
parser.GetNextDocument(doc);
|
parser.GetNextDocument(doc);
|
||||||
|
std::cout << doc;
|
||||||
Level level;
|
|
||||||
doc >> level;
|
|
||||||
std::cout << level;
|
|
||||||
} catch(YAML::Exception&) {
|
} catch(YAML::Exception&) {
|
||||||
std::cout << "Error parsing the yaml!\n";
|
std::cout << "Error parsing the yaml!\n";
|
||||||
}
|
}
|
||||||
|
27
map.cpp
27
map.cpp
@@ -125,22 +125,27 @@ namespace YAML
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Map::Write(std::ostream& out, int indent)
|
void Map::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine)
|
||||||
{
|
{
|
||||||
for(int i=0;i<indent;i++)
|
if(startedLine && !onlyOneCharOnLine)
|
||||||
out << " ";
|
out << std::endl;
|
||||||
out << "{map}\n";
|
|
||||||
|
|
||||||
for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) {
|
for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) {
|
||||||
for(int i=0;i<indent + 1;i++)
|
if((startedLine && !onlyOneCharOnLine) || it != m_data.begin()) {
|
||||||
out << " ";
|
for(int i=0;i<indent;i++)
|
||||||
out << "{key}\n";
|
out << " ";
|
||||||
it->first->Write(out, indent + 2);
|
}
|
||||||
|
|
||||||
for(int i=0;i<indent + 1;i++)
|
out << "? ";
|
||||||
|
it->first->Write(out, indent + 1, true, it!= m_data.begin() || !startedLine || onlyOneCharOnLine);
|
||||||
|
|
||||||
|
for(int i=0;i<indent;i++)
|
||||||
out << " ";
|
out << " ";
|
||||||
out << "{value}\n";
|
out << ": ";
|
||||||
it->second->Write(out, indent + 2);
|
it->second->Write(out, indent + 1, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(m_data.empty())
|
||||||
|
out << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
map.h
2
map.h
@@ -17,7 +17,7 @@ namespace YAML
|
|||||||
virtual bool GetBegin(std::map <Node *, Node *>::const_iterator& it) const;
|
virtual bool GetBegin(std::map <Node *, Node *>::const_iterator& it) const;
|
||||||
virtual bool GetEnd(std::map <Node *, Node *>::const_iterator& it) const;
|
virtual bool GetEnd(std::map <Node *, Node *>::const_iterator& it) const;
|
||||||
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
||||||
virtual void Write(std::ostream& out, int indent);
|
virtual void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ParseBlock(Scanner *pScanner, const ParserState& state);
|
void ParseBlock(Scanner *pScanner, const ParserState& state);
|
||||||
|
36
node.cpp
36
node.cpp
@@ -112,28 +112,30 @@ namespace YAML
|
|||||||
pScanner->PopNextToken();
|
pScanner->PopNextToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Node::Write(std::ostream& out, int indent)
|
void Node::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine) const
|
||||||
{
|
{
|
||||||
if(m_tag != "") {
|
// write anchor/alias
|
||||||
for(int i=0;i<indent;i++)
|
|
||||||
out << " ";
|
|
||||||
out << "{tag: " << m_tag << "}\n";
|
|
||||||
}
|
|
||||||
if(m_anchor != "") {
|
if(m_anchor != "") {
|
||||||
for(int i=0;i<indent;i++)
|
|
||||||
out << " ";
|
|
||||||
if(m_alias)
|
if(m_alias)
|
||||||
out << "{alias: " << m_anchor << "}\n";
|
out << "*";
|
||||||
else
|
else
|
||||||
out << "{anchor: " << m_anchor << "}\n";
|
out << "&";
|
||||||
|
out << m_anchor << " ";
|
||||||
|
startedLine = true;
|
||||||
|
onlyOneCharOnLine = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// write tag
|
||||||
|
if(m_tag != "") {
|
||||||
|
out << "!<" << m_tag << "> ";
|
||||||
|
startedLine = true;
|
||||||
|
onlyOneCharOnLine = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!m_pContent) {
|
if(!m_pContent) {
|
||||||
for(int i=0;i<indent;i++)
|
out << std::endl;
|
||||||
out << " ";
|
|
||||||
out << "{no content}\n";
|
|
||||||
} else {
|
} else {
|
||||||
m_pContent->Write(out, indent);
|
m_pContent->Write(out, indent, startedLine, onlyOneCharOnLine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,4 +268,10 @@ namespace YAML
|
|||||||
|
|
||||||
node.m_pContent->Read(c);
|
node.m_pContent->Read(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::ostream& operator << (std::ostream& out, const Node& node)
|
||||||
|
{
|
||||||
|
node.Write(out, 0, false, false);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
5
node.h
5
node.h
@@ -46,7 +46,7 @@ namespace YAML
|
|||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
void Parse(Scanner *pScanner, const ParserState& state);
|
void Parse(Scanner *pScanner, const ParserState& state);
|
||||||
void Write(std::ostream& out, int indent);
|
void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine) const;
|
||||||
|
|
||||||
// accessors
|
// accessors
|
||||||
Iterator begin() const;
|
Iterator begin() const;
|
||||||
@@ -92,6 +92,9 @@ namespace YAML
|
|||||||
friend void operator >> (const Node& node, double& d);
|
friend void operator >> (const Node& node, double& d);
|
||||||
friend void operator >> (const Node& node, char& c);
|
friend void operator >> (const Node& node, char& c);
|
||||||
|
|
||||||
|
// insertion
|
||||||
|
friend std::ostream& operator << (std::ostream& out, const Node& node);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ParseHeader(Scanner *pScanner, const ParserState& state);
|
void ParseHeader(Scanner *pScanner, const ParserState& state);
|
||||||
void ParseTag(Scanner *pScanner, const ParserState& state);
|
void ParseTag(Scanner *pScanner, const ParserState& state);
|
||||||
|
19
scalar.cpp
19
scalar.cpp
@@ -21,14 +21,19 @@ namespace YAML
|
|||||||
delete pToken;
|
delete pToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scalar::Write(std::ostream& out, int indent)
|
void Scalar::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine)
|
||||||
{
|
{
|
||||||
for(int i=0;i<indent;i++)
|
out << "\"";
|
||||||
out << " ";
|
for(unsigned i=0;i<m_data.size();i++) {
|
||||||
out << "{scalar}\n";
|
switch(m_data[i]) {
|
||||||
for(int i=0;i<indent;i++)
|
case '\\': out << "\\\\"; break;
|
||||||
out << " ";
|
case '\t': out << "\\t"; break;
|
||||||
out << m_data << std::endl;
|
case '\n': out << "\\n"; break;
|
||||||
|
case '\r': out << "\\r"; break;
|
||||||
|
default: out << m_data[i]; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out << "\"\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scalar::Read(std::string& s)
|
void Scalar::Read(std::string& s)
|
||||||
|
2
scalar.h
2
scalar.h
@@ -12,7 +12,7 @@ namespace YAML
|
|||||||
virtual ~Scalar();
|
virtual ~Scalar();
|
||||||
|
|
||||||
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
||||||
virtual void Write(std::ostream& out, int indent);
|
virtual void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine);
|
||||||
|
|
||||||
// extraction
|
// extraction
|
||||||
virtual void Read(std::string& s);
|
virtual void Read(std::string& s);
|
||||||
|
@@ -110,7 +110,12 @@ namespace YAML
|
|||||||
bool nextMoreIndented = (INPUT.peek() == ' ');
|
bool nextMoreIndented = (INPUT.peek() == ' ');
|
||||||
|
|
||||||
// for block scalars, we always start with a newline, so we should ignore it (not fold or keep)
|
// for block scalars, we always start with a newline, so we should ignore it (not fold or keep)
|
||||||
if(pastOpeningBreak) {
|
bool useNewLine = pastOpeningBreak;
|
||||||
|
// and for folded scalars, we don't fold the very last newline to a space
|
||||||
|
if(params.fold && !emptyLine && INPUT.column < params.indent)
|
||||||
|
useNewLine = false;
|
||||||
|
|
||||||
|
if(useNewLine) {
|
||||||
if(params.fold && !emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented)
|
if(params.fold && !emptyLine && !nextEmptyLine && !moreIndented && !nextMoreIndented)
|
||||||
scalar += " ";
|
scalar += " ";
|
||||||
else
|
else
|
||||||
|
22
sequence.cpp
22
sequence.cpp
@@ -133,12 +133,22 @@ namespace YAML
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sequence::Write(std::ostream& out, int indent)
|
void Sequence::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine)
|
||||||
{
|
{
|
||||||
for(int i=0;i<indent;i++)
|
if(startedLine && !onlyOneCharOnLine)
|
||||||
out << " ";
|
out << std::endl;
|
||||||
out << "{sequence}\n";
|
|
||||||
for(unsigned i=0;i<m_data.size();i++)
|
for(unsigned i=0;i<m_data.size();i++) {
|
||||||
m_data[i]->Write(out, indent + 1);
|
if((startedLine && !onlyOneCharOnLine) || i > 0) {
|
||||||
|
for(int j=0;j<indent;j++)
|
||||||
|
out << " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
out << "- ";
|
||||||
|
m_data[i]->Write(out, indent + 1, true, i > 0 || !startedLine || onlyOneCharOnLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(m_data.empty())
|
||||||
|
out << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -20,7 +20,7 @@ namespace YAML
|
|||||||
virtual unsigned GetSize() const;
|
virtual unsigned GetSize() const;
|
||||||
|
|
||||||
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
virtual void Parse(Scanner *pScanner, const ParserState& state);
|
||||||
virtual void Write(std::ostream& out, int indent);
|
virtual void Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ParseBlock(Scanner *pScanner, const ParserState& state);
|
void ParseBlock(Scanner *pScanner, const ParserState& state);
|
||||||
|
114
test.yaml
114
test.yaml
@@ -1,97 +1,17 @@
|
|||||||
---
|
literal: |
|
||||||
model:
|
Here's a literal scalar.
|
||||||
file: data/models/compound.model
|
That's a newline.
|
||||||
textures: data/materials/compound
|
|
||||||
rooms:
|
Let's go...
|
||||||
- name: "Room #1"
|
folded: >
|
||||||
pos: [0, 0, 0]
|
Here's a folded scalar that
|
||||||
size: [1000, 1000, 500]
|
wraps over to a newline.
|
||||||
height: 500
|
|
||||||
stairtype: none
|
Let's go...
|
||||||
display: []
|
regular: Here's a regular
|
||||||
pathfinding:
|
scalar that keeps
|
||||||
tilesize: 50
|
on wrapping...
|
||||||
size: [24, 24]
|
|
||||||
map: |
|
|
||||||
-----------------------
|
Let's go!
|
||||||
-+++++++++++++++++++++-
|
and last key: so it doesn't go bonkers
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+---------------------
|
|
||||||
-+---------------------
|
|
||||||
-+---------------------
|
|
||||||
-+---------------------
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+++++++++++++++++++++-
|
|
||||||
-----------------------
|
|
||||||
- name: Doorway
|
|
||||||
pos: [1000, 400, 0]
|
|
||||||
size: [50, 200, 500]
|
|
||||||
height: 500
|
|
||||||
stairtype: none
|
|
||||||
display: []
|
|
||||||
pathfinding:
|
|
||||||
tilesize: 50
|
|
||||||
size: [5, 9]
|
|
||||||
map: |
|
|
||||||
-----
|
|
||||||
-+++-
|
|
||||||
-----
|
|
||||||
-----
|
|
||||||
-----
|
|
||||||
-----
|
|
||||||
-----
|
|
||||||
-+++-
|
|
||||||
-----
|
|
||||||
- name: "Room #2"
|
|
||||||
pos: [1050, 0, 0]
|
|
||||||
size: [1000, 1000, 500]
|
|
||||||
height: 500
|
|
||||||
stairtype: none
|
|
||||||
display: []
|
|
||||||
pathfinding:
|
|
||||||
tilesize: 50
|
|
||||||
size: [24, 24]
|
|
||||||
map: |
|
|
||||||
-----------------------
|
|
||||||
-+++++++++++++++++++++-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
---------------------+-
|
|
||||||
---------------------+-
|
|
||||||
---------------------+-
|
|
||||||
---------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+-------------------+-
|
|
||||||
-+++++++++++++++++++++-
|
|
||||||
-----------------------
|
|
||||||
exits:
|
|
||||||
- room1: "Room #1"
|
|
||||||
room2: "Room #2"
|
|
||||||
dir: e
|
|
||||||
pos: [400, 600]
|
|
Reference in New Issue
Block a user