diff --git a/README.md b/README.md index 9273c6a..2a50b63 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ Various API calls: std::string tagValue=GetTagValue("Date"); // Get the value of a tag Access to moves: - pgnp::HalfMove *move=pgn.GetMoves(); // Get the tree of half moves + pgnp::HalfMove *move=new pgnp::HalfMove(); + pgn.GetMoves(move); // Get the tree of half moves (do not forget to call "delete move") int length=move->GetLength(); // Get the number of half moves in the move MainLine // Public members: // move->variations contains variations of the current move diff --git a/src/pgnp.cpp b/src/pgnp.cpp index 7f9c450..3262a5e 100644 --- a/src/pgnp.cpp +++ b/src/pgnp.cpp @@ -16,7 +16,7 @@ namespace pgnp { -HalfMove::HalfMove() : isBlack(false), MainLine(NULL) {} +HalfMove::HalfMove() : count(-1), isBlack(false), MainLine(NULL) {} HalfMove::~HalfMove() { for (auto *move : variations) { @@ -55,6 +55,26 @@ int HalfMove::GetLength() { return length; } +void HalfMove::Copy(HalfMove* copy){ + copy->count=count; + copy->isBlack=isBlack; + copy->move=move; + copy->comment=comment; + + // Copy MainLine + if(MainLine!=NULL){ + copy->MainLine=new HalfMove(); + MainLine->Copy(copy->MainLine); + } + + // Copy variation + for(HalfMove *var:variations){ + HalfMove *new_var=new HalfMove(); + copy->variations.push_back(new_var); + var->Copy(new_var); + } +} + PGN::~PGN() { if (moves != NULL) delete moves; @@ -265,7 +285,7 @@ int PGN::ParseNextTag(int start_loc) { return (valueloc + 1); // +1 For the last char of the tag which is ']' } -HalfMove *PGN::GetMoves() { return (moves); } +void PGN::GetMoves(HalfMove* copy) { moves->Copy(copy); } std::vector PGN::GetTagList() { return tagkeys; } diff --git a/src/pgnp.hpp b/src/pgnp.hpp index 53a7a59..492dfc0 100644 --- a/src/pgnp.hpp +++ b/src/pgnp.hpp @@ -29,6 +29,7 @@ public: int GetLength(); /// @brief Dump move and all its variations void Dump(); + void Copy(HalfMove* copy); }; class PGN { @@ -52,7 +53,7 @@ public: std::vector GetTagList(); std::string GetTagValue(std::string); std::string GetResult(); - HalfMove *GetMoves(); + void GetMoves(HalfMove*); private: /// @brief Populate @a tags with by parsing the one starting at location in diff --git a/tests/tests.cpp b/tests/tests.cpp index d766abd..551e9d4 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -8,7 +8,9 @@ TEST_CASE("Valid PGN", "[valid/pgn1]") { REQUIRE_NOTHROW(pgn.FromFile("pgn_files/valid/pgn1.pgn")); REQUIRE_THROWS(pgn.STRCheck()); - HalfMove *m = pgn.GetMoves(); + HalfMove *m = new HalfMove(); + pgn.GetMoves(m); + HalfMove *m_backup = m; REQUIRE(m->GetLength() == 6); SECTION("Main line move checks") { @@ -31,7 +33,7 @@ TEST_CASE("Valid PGN", "[valid/pgn1]") { } SECTION("Main line color checks") { - m = pgn.GetMoves(); + m=m_backup; CHECK_FALSE(m->isBlack); m = m->MainLine; @@ -63,7 +65,9 @@ TEST_CASE("Valid PGN", "[valid/pgn2]") { PGN pgn; REQUIRE_NOTHROW(pgn.FromFile("pgn_files/valid/pgn2.pgn")); REQUIRE_THROWS(pgn.STRCheck()); - REQUIRE(pgn.GetMoves()->GetLength() == 66); + HalfMove *m = new HalfMove(); + pgn.GetMoves(m); + REQUIRE(m->GetLength() == 66); CHECK(pgn.GetResult() == "0-1"); } @@ -71,6 +75,8 @@ TEST_CASE("Seven Tag Roster", "[std/pgn1]") { PGN pgn; REQUIRE_NOTHROW(pgn.FromFile("pgn_files/str/pgn1.pgn")); REQUIRE_NOTHROW(pgn.STRCheck()); - REQUIRE(pgn.GetMoves()->GetLength() == 85); + HalfMove *m = new HalfMove(); + pgn.GetMoves(m); + REQUIRE(m->GetLength() == 85); CHECK(pgn.GetResult() == "1/2-1/2"); }