Skip to content

Commit

Permalink
Get the world name (#1027)
Browse files Browse the repository at this point in the history
Signed-off-by: ahcorde <[email protected]>
  • Loading branch information
ahcorde authored Jul 8, 2022
1 parent 1570833 commit fdd9861
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/sdf/Root.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define SDF_ROOT_HH_

#include <string>
#include <vector>
#include <gz/utils/ImplPtr.hh>

#include "sdf/OutputConfig.hh"
Expand Down Expand Up @@ -58,6 +59,17 @@ namespace sdf
/// \brief Default constructor
public: Root();

/// \brief Get the name of the world without loading the entire world
/// Users shouldn't normally need to use this API.
/// This doesn't load the world, it might return the world name even if the
/// file is not a valid SDFormat file.
/// \param[in] _filename Name of the SDF file to parse.
/// \param[out] _worldNames A vector with world names
/// \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 WorldNamesFromFile(const std::string &_filename,
std::vector<std::string> &_worldNames);

/// \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
39 changes: 39 additions & 0 deletions src/Root.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,45 @@ Root::Root()
{
}

Errors Root::WorldNamesFromFile(
const std::string &_filename,
std::vector<std::string> &_worldNames)
{
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");

while(pworld)
{
_worldNames.push_back(pworld->Attribute("name"));
pworld = pworld->NextSiblingElement("world");
}

if (_worldNames.size() == 0)
{
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
24 changes: 24 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.hh"

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

TEST(DOMRoot, WorldNamesFromFile)
{
const auto path = sdf::testing::TestFile("sdf", "basic_shapes.sdf");
sdf::Root root;
std::vector<std::string> worldNames;
auto errors = root.WorldNamesFromFile(path, worldNames);
EXPECT_TRUE(errors.empty());
EXPECT_EQ(1u, worldNames.size());
EXPECT_EQ("shapes_world", worldNames[0]);

worldNames.clear();
const auto path2 = sdf::testing::TestFile("sdf", "empty_invalid.sdf");
errors = root.WorldNamesFromFile(path2, worldNames);
EXPECT_EQ(0u, worldNames.size());
EXPECT_FALSE(errors.empty());

worldNames.clear();
const auto path3 = sdf::testing::TestFile("sdf", "invalid_file_name.sdf");
errors = root.WorldNamesFromFile(path3, worldNames);
EXPECT_EQ(0u, worldNames.size());
EXPECT_FALSE(errors.empty());
}

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

0 comments on commit fdd9861

Please sign in to comment.