Skip to content

Commit

Permalink
add search paths arg to resolveURI
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 committed Feb 28, 2024
1 parent a83998c commit 70e897e
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 47 deletions.
8 changes: 0 additions & 8 deletions include/sdf/ParserConfig.hh
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,6 @@ class SDFORMAT_VISIBLE ParserConfig
/// store them. False to preserve original URIs
public: bool StoreResolvedURIs() const;

/// \brief Set the path to search for URIs with relative path.
/// \param[in] _path Path to search.
public: void SetRelativeURISearchPath(const std::string &_path);

/// \brief Get the path to search for URIs with relative path.
/// \return Path to search
public: const std::string &RelativeURISearchPath() const;

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
16 changes: 9 additions & 7 deletions src/Heightmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ Errors HeightmapTexture::Load(ElementPtr _sdf, const ParserConfig &_config)

if (_sdf->HasElement("diffuse"))
{
auto path = std::filesystem::path(
this->dataPtr->sdf->FilePath()).parent_path();
this->dataPtr->diffuse = resolveURI(
_sdf->Get<std::string>(errors, "diffuse", this->dataPtr->diffuse).first,
_config, errors);
_config, errors, {path.string()});
}
else
{
Expand All @@ -146,9 +148,11 @@ Errors HeightmapTexture::Load(ElementPtr _sdf, const ParserConfig &_config)

if (_sdf->HasElement("normal"))
{
auto path = std::filesystem::path(
this->dataPtr->sdf->FilePath()).parent_path();
this->dataPtr->normal = resolveURI(
_sdf->Get<std::string>(errors, "normal", this->dataPtr->normal).first,
_config, errors);
_config, errors, {path.string()});
}
else
{
Expand Down Expand Up @@ -325,14 +329,12 @@ Errors Heightmap::Load(ElementPtr _sdf, const ParserConfig &_config)
return errors;
}

auto config = _config;
auto path = std::filesystem::path(this->dataPtr->filePath).parent_path();
config.SetRelativeURISearchPath(path.string());
if (_sdf->HasElement("uri"))
{
auto path = std::filesystem::path(this->dataPtr->filePath).parent_path();
this->dataPtr->uri = resolveURI(
_sdf->Get<std::string>(errors, "uri", "").first,
config, errors);
_config, errors, {path.string()});
}
else
{
Expand All @@ -353,7 +355,7 @@ Errors Heightmap::Load(ElementPtr _sdf, const ParserConfig &_config)
this->dataPtr->sampling).first;

Errors textureLoadErrors = loadRepeated<HeightmapTexture>(_sdf,
"texture", this->dataPtr->textures, config);
"texture", this->dataPtr->textures, _config);
errors.insert(errors.end(), textureLoadErrors.begin(),
textureLoadErrors.end());

Expand Down
5 changes: 2 additions & 3 deletions src/Material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ Errors Material::Load(sdf::ElementPtr _sdf, const sdf::ParserConfig &_config)
"<uri> element is empty."});
}

auto config = _config;
auto path = std::filesystem::path(this->dataPtr->filePath).parent_path();
config.SetRelativeURISearchPath(path.string());
this->dataPtr->scriptUri = resolveURI(uriPair.first, config, errors);
this->dataPtr->scriptUri = resolveURI(uriPair.first, _config, errors,
{path.string()});

std::pair<std::string, bool> namePair =
elem->Get<std::string>(errors, "name", "");
Expand Down
4 changes: 1 addition & 3 deletions src/Mesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ Errors Mesh::Load(ElementPtr _sdf, const ParserConfig &_config)

if (_sdf->HasElement("uri"))
{
auto config = _config;
auto path = std::filesystem::path(this->dataPtr->filePath).parent_path();
config.SetRelativeURISearchPath(path.string());
this->dataPtr->uri = resolveURI(
_sdf->Get<std::string>(errors, "uri", "").first,
config, errors);
_config, errors, {path.string()});
}
else
{
Expand Down
12 changes: 0 additions & 12 deletions src/ParserConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,3 @@ bool ParserConfig::StoreResolvedURIs() const
{
return this->dataPtr->storeResolvedURIs;
}

/////////////////////////////////////////////////
void ParserConfig::SetRelativeURISearchPath(const std::string &_path)
{
this->dataPtr->relativeUriSearchPath = _path;
}

/////////////////////////////////////////////////
const std::string &ParserConfig::RelativeURISearchPath() const
{
return this->dataPtr->relativeUriSearchPath;
}
9 changes: 0 additions & 9 deletions src/SDF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,6 @@ std::string findFile(const std::string &_filename, bool _searchLocalPath,
{
return path;
}
else if (!_config.RelativeURISearchPath().empty())
{
path = sdf::filesystem::append(_config.RelativeURISearchPath(),
filename);
if (sdf::filesystem::exists(path))
{
return path;
}
}
}

// Next check the install path.
Expand Down
4 changes: 1 addition & 3 deletions src/Sky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,10 @@ Errors Sky::Load(ElementPtr _sdf, const ParserConfig &_config)

if (_sdf->HasElement("cubemap_uri"))
{
auto config = _config;
auto path = std::filesystem::path(_sdf->FilePath()).parent_path();
config.SetRelativeURISearchPath(path.string());
this->dataPtr->cubemapUri = resolveURI(
_sdf->Get<std::string>(errors, "cubemap_uri", "").first,
config, errors);
_config, errors, {path.string()});
}

if ( _sdf->HasElement("clouds"))
Expand Down
16 changes: 15 additions & 1 deletion src/Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <string>
#include <utility>
#include "sdf/Assert.hh"
#include "sdf/Filesystem.hh"
#include "sdf/SDFImpl.hh"
#include "Utils.hh"

Expand Down Expand Up @@ -382,11 +383,24 @@ void copyChildren(ElementPtr _sdf, tinyxml2::XMLElement *_xml,

/////////////////////////////////////////////////
std::string resolveURI(const std::string &_inputURI,
const sdf::ParserConfig &_config, sdf::Errors &_errors)
const sdf::ParserConfig &_config, sdf::Errors &_errors,
const std::unordered_set<std::string> &_searchPaths)
{
std::string resolvedURI = _inputURI;
if (_config.StoreResolvedURIs())
{
std::string sep("://");
if (!_searchPaths.empty() && _inputURI.find(sep) == std::string::npos)
{
for (const auto &sp : _searchPaths)
{
std::string fullPath = sdf::filesystem::append(sp, _inputURI);
resolvedURI = sdf::findFile(fullPath, true, false);
if (!resolvedURI.empty())
return resolvedURI;
}
}

resolvedURI = sdf::findFile(_inputURI, true, true, _config);
if (resolvedURI.empty())
{
Expand Down
6 changes: 5 additions & 1 deletion src/Utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <utility>
#include <vector>
#include <tinyxml2.h>
#include <unordered_set>
#include "sdf/Error.hh"
#include "sdf/Element.hh"
#include "sdf/InterfaceElements.hh"
Expand Down Expand Up @@ -262,10 +263,13 @@ namespace sdf
/// \param[in] _inputURI URI from parsed SDF file to resolve
/// \param[in] _config Parser configuration to use to resolve
/// \param[in, out] _errors Error vector to append to if resolution fails
/// \param[in] _searchPaths Optional additional search paths (directory)
/// when resolving URI
/// \return Resolved URI or Original URI, depending on parser configuration
std::string resolveURI(const std::string &_inputURI,
const sdf::ParserConfig &_config,
sdf::Errors &_errors);
sdf::Errors &_errors,
const std::unordered_set<std::string> &_searchPaths = {});
}
}
#endif

0 comments on commit 70e897e

Please sign in to comment.