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

Extend functions that process SDF files to also accept SDF strings #417

Merged
merged 1 commit into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 14 additions & 15 deletions scenario/src/gazebo/include/scenario/gazebo/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,27 @@ namespace scenario::gazebo::utils {
std::string getSdfString(const std::string& fileName);

/**
* Get the name of a model from a SDF file.
* Get the name of a model from a SDF file or SDF string.
*
* @note sdformat supports only one model per SDF file.
* @note sdformat supports only one model per SDF.
*
* @param fileName An SDF file. It could be either an absolute path
* to the file or the file name if the parent folder is part
* of the ``IGN_GAZEBO_RESOURCE_PATH`` environment variable.
* @return The name of the model if the file was found and is valid,
* an empty string otherwise.
* @param fileName An SDF file or string. It could be an absolute path to
* the file, the file name if the parent folder is part of the
* ``IGN_GAZEBO_RESOURCE_PATH`` environment variable, or a SDF string.
* @return The name of the model if the SDF is valid, an empty string
* otherwise.
*/
std::string getModelNameFromSdf(const std::string& fileName);

/**
* Get the name of a world from a SDF file.
* Get the name of a world from a SDF file or SDF string.
*
* @param fileName An SDF file or string. It could be an absolute path to
* the file, the file name if the parent folder is part of the
* ``IGN_GAZEBO_RESOURCE_PATH`` environment variable, or a SDF string.
* @return The name of the world if the SDF is valid, an empty string
* otherwise.
*
* @param fileName An SDF file. It could be either an absolute path
* to the file or the file name if the parent folder is part
* of the ``IGN_GAZEBO_RESOURCE_PATH`` environment variable.
* @param worldIndex The index of the world in the SDF file. By
* default it finds the first world.
* @return The name of the world if the file was found and is valid,
* an empty string otherwise.
*/
std::string getWorldNameFromSdf(const std::string& fileName,
const size_t worldIndex = 0);
Expand Down
37 changes: 22 additions & 15 deletions scenario/src/gazebo/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,20 @@ std::string utils::getSdfString(const std::string& fileName)
{
// NOTE: We could use std::filesystem for the following, but compilers
// support is still rough even with C++17 enabled :/
std::string sdfFileAbsPath;
std::string sdfFileAbsPath = fileName;

if (!ignition::common::isFile(fileName)) {
std::shared_ptr<sdf::Root> root;

if (ignition::common::isFile(fileName)) {
sdfFileAbsPath = findSdfFile(fileName);
root = getSdfRootFromFile(fileName);
}

if (sdfFileAbsPath.empty()) {
return {};
else {
root = getSdfRootFromString(sdfFileAbsPath);
}

auto root = getSdfRootFromString(sdfFileAbsPath);

if (!root) {
sError << "Failed to get SDF string" << std::endl;
return {};
}

Expand All @@ -109,24 +110,30 @@ std::string utils::getSdfString(const std::string& fileName)

std::string utils::getModelNameFromSdf(const std::string& fileName)
{
std::string absFileName = findSdfFile(fileName);
// NOTE: We could use std::filesystem for the following, but compilers
// support is still rough even with C++17 enabled :/
std::string sdfFileAbsPath = fileName;

if (absFileName.empty()) {
sError << "Failed to find file " << fileName << std::endl;
return {};
}
std::shared_ptr<sdf::Root> root;

const auto root = utils::getSdfRootFromFile(absFileName);
if (ignition::common::isFile(fileName)) {
sdfFileAbsPath = findSdfFile(fileName);
root = getSdfRootFromFile(fileName);
}
else {
root = getSdfRootFromString(sdfFileAbsPath);
}

if (!root) {
sError << "Failed to get model name from SDF" << std::endl;
return {};
}

if (const auto model = root->Model()) {
if (const auto& model = root->Model()) {
return model->Name();
}

sError << "No model found in file " << fileName << std::endl;
sError << "No model found in SDF" << std::endl;
return {};
}

Expand Down