Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the world name #1027

Merged
merged 16 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/sdf/Root.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ namespace sdf
/// \brief Default constructor
public: Root();

/// \brief Get the name of the world without loading the entire world
azeey marked this conversation as resolved.
Show resolved Hide resolved
/// \param[in] _filename Name of the SDF file to parse.
/// \param[out] _worldName The name of the world
/// \return Errors, which is a vector of Error objects. Each Error includes
/// an error code and message. An empty vector indicates no error.
public: Errors WorldName(const std::string &_filename,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename this to WorldNameFromFile or something to indicate that this isn't returning a fully loaded world.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string &_worldName);

/// \brief Parse the given SDF file, and generate objects based on types
/// specified in the SDF file.
/// \param[in] _filename Name of the SDF file to parse.
Expand Down
34 changes: 34 additions & 0 deletions src/Root.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,40 @@ Root::Root()
{
}

Errors Root::WorldName(const std::string &_filename, std::string &_worldName)
{
Errors errors;

tinyxml2::XMLDocument doc;
if(doc.LoadFile(_filename.c_str()))
{
errors.push_back({ErrorCode::FILE_READ,
"Unable to load file[" + _filename + "]:" +
doc.ErrorStr()});
return errors;
}
tinyxml2::XMLElement * pRootElement = doc.RootElement();
if (nullptr != pRootElement)
{
tinyxml2::XMLElement * pworld = pRootElement->FirstChildElement("world");
if(pworld)
{
_worldName = pworld->Attribute("name");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error on else?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this resolved?

else
{
errors.push_back({ErrorCode::ELEMENT_INVALID,
"Failed to read the world tag."});
}
}
else
{
errors.push_back({ErrorCode::ELEMENT_INVALID,
"Failed to read the root."});
}
return errors;
}

/////////////////////////////////////////////////
Errors Root::Load(const std::string &_filename)
{
Expand Down
19 changes: 19 additions & 0 deletions src/Root_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "sdf/World.hh"
#include "sdf/Frame.hh"
#include "sdf/Root.hh"
#include "test_config.h"

/////////////////////////////////////////////////
TEST(DOMRoot, Construction)
Expand Down Expand Up @@ -65,6 +66,24 @@ TEST(DOMRoot, MoveAssignmentOperator)
EXPECT_EQ("test_root", root2.Version());
}

TEST(DOMRoot, WorldName)
{
const auto path = sdf::testing::TestFile("sdf", "basic_shapes.sdf");
sdf::Root root;
std::string worldName;
auto errors = root.WorldName(path, worldName);
EXPECT_TRUE(errors.empty());
EXPECT_EQ("shapes_world", worldName);

const auto path2 = sdf::testing::TestFile("sdf", "empty_invalid.sdf");
errors = root.WorldName(path2, worldName);
EXPECT_FALSE(errors.empty());

const auto path3 = sdf::testing::TestFile("sdf", "invalid_file_name.sdf");
errors = root.WorldName(path3, worldName);
EXPECT_FALSE(errors.empty());
}

/////////////////////////////////////////////////
TEST(DOMRoot, StringModelSdfParse)
{
Expand Down