diff --git a/content.h b/content.h index 298f4db..9f9ac25 100644 --- a/content.h +++ b/content.h @@ -19,7 +19,7 @@ namespace YAML virtual ~Content(); 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 ::const_iterator& it) const { return false; } virtual bool GetBegin(std::map ::const_iterator& it) const { return false; } diff --git a/main.cpp b/main.cpp index 29c885f..ef99a1d 100644 --- a/main.cpp +++ b/main.cpp @@ -73,10 +73,7 @@ int main() YAML::Node doc; parser.GetNextDocument(doc); - - Level level; - doc >> level; - std::cout << level; + std::cout << doc; } catch(YAML::Exception&) { std::cout << "Error parsing the yaml!\n"; } diff --git a/map.cpp b/map.cpp index 25cb504..b448265 100644 --- a/map.cpp +++ b/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;ifirst->Write(out, indent + 2); + if((startedLine && !onlyOneCharOnLine) || it != m_data.begin()) { + for(int i=0;ifirst->Write(out, indent + 1, true, it!= m_data.begin() || !startedLine || onlyOneCharOnLine); + + for(int i=0;isecond->Write(out, indent + 2); + out << ": "; + it->second->Write(out, indent + 1, true, true); } + + if(m_data.empty()) + out << std::endl; } } diff --git a/map.h b/map.h index 14ddd30..d3a327c 100644 --- a/map.h +++ b/map.h @@ -17,7 +17,7 @@ namespace YAML virtual bool GetBegin(std::map ::const_iterator& it) const; virtual bool GetEnd(std::map ::const_iterator& it) const; 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: void ParseBlock(Scanner *pScanner, const ParserState& state); diff --git a/node.cpp b/node.cpp index 39ff958..c998a45 100644 --- a/node.cpp +++ b/node.cpp @@ -112,28 +112,30 @@ namespace YAML 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 != "") { - for(int i=0;i "; + startedLine = true; + onlyOneCharOnLine = false; } if(!m_pContent) { - for(int i=0;iWrite(out, indent); + m_pContent->Write(out, indent, startedLine, onlyOneCharOnLine); } } @@ -266,4 +268,10 @@ namespace YAML node.m_pContent->Read(c); } + + std::ostream& operator << (std::ostream& out, const Node& node) + { + node.Write(out, 0, false, false); + return out; + } } diff --git a/node.h b/node.h index a86ba15..988ee1c 100644 --- a/node.h +++ b/node.h @@ -46,7 +46,7 @@ namespace YAML void Clear(); 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 Iterator begin() const; @@ -92,6 +92,9 @@ namespace YAML friend void operator >> (const Node& node, double& d); friend void operator >> (const Node& node, char& c); + // insertion + friend std::ostream& operator << (std::ostream& out, const Node& node); + private: void ParseHeader(Scanner *pScanner, const ParserState& state); void ParseTag(Scanner *pScanner, const ParserState& state); diff --git a/scalar.cpp b/scalar.cpp index c33ae32..20b2671 100644 --- a/scalar.cpp +++ b/scalar.cpp @@ -21,14 +21,19 @@ namespace YAML 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;iWrite(out, indent + 1); + if(startedLine && !onlyOneCharOnLine) + out << std::endl; + + for(unsigned i=0;i 0) { + for(int j=0;jWrite(out, indent + 1, true, i > 0 || !startedLine || onlyOneCharOnLine); + } + + if(m_data.empty()) + out << std::endl; } } diff --git a/sequence.h b/sequence.h index 5be190e..d305073 100644 --- a/sequence.h +++ b/sequence.h @@ -20,7 +20,7 @@ namespace YAML virtual unsigned GetSize() const; 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: void ParseBlock(Scanner *pScanner, const ParserState& state); diff --git a/test.yaml b/test.yaml index 8a9714c..f7be014 100644 --- a/test.yaml +++ b/test.yaml @@ -1,97 +1,17 @@ ---- -model: - file: data/models/compound.model - textures: data/materials/compound -rooms: - - name: "Room #1" - pos: [0, 0, 0] - size: [1000, 1000, 500] - height: 500 - stairtype: none - display: [] - pathfinding: - tilesize: 50 - size: [24, 24] - map: | - ----------------------- - -+++++++++++++++++++++- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+--------------------- - -+--------------------- - -+--------------------- - -+--------------------- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+-------------------+- - -+++++++++++++++++++++- - ----------------------- - - 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] +literal: | + Here's a literal scalar. + That's a newline. + + Let's go... +folded: > + Here's a folded scalar that + wraps over to a newline. + + Let's go... +regular: Here's a regular + scalar that keeps + on wrapping... + + + Let's go! +and last key: so it doesn't go bonkers \ No newline at end of file