Skip to content

Commit

Permalink
Add readAll() to Resource and ResourceRetriever (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 authored Apr 18, 2017
1 parent b53ab22 commit 52bbd15
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 50 deletions.
2 changes: 1 addition & 1 deletion dart/collision/ode/OdeCollisionDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "dart/dynamics/CapsuleShape.hpp"
#include "dart/dynamics/ConeShape.hpp"
#include "dart/dynamics/PlaneShape.hpp"
#include "dart/dynamics/MultiSphereShape.hpp"
#include "dart/dynamics/MultiSphereConvexHullShape.hpp"
#include "dart/dynamics/MeshShape.hpp"
#include "dart/dynamics/SoftMeshShape.hpp"
#include "dart/collision/CollisionFilter.hpp"
Expand Down
2 changes: 1 addition & 1 deletion dart/collision/ode/OdeCollisionObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "dart/dynamics/CapsuleShape.hpp"
#include "dart/dynamics/ConeShape.hpp"
#include "dart/dynamics/PlaneShape.hpp"
#include "dart/dynamics/MultiSphereShape.hpp"
#include "dart/dynamics/MultiSphereConvexHullShape.hpp"
#include "dart/dynamics/MeshShape.hpp"
#include "dart/dynamics/SoftMeshShape.hpp"
#include "dart/collision/ode/OdeTypes.hpp"
Expand Down
55 changes: 55 additions & 0 deletions dart/common/Resource.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2017, Graphics Lab, Georgia Tech Research Corporation
* Copyright (c) 2017, Personal Robotics Lab, Carnegie Mellon University
* All rights reserved.
*
* This file is provided under the following "BSD-style" License:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "dart/common/Resource.hpp"

#include <string>
#include <exception>
#include "dart/common/Console.hpp"

namespace dart {
namespace common {

//==============================================================================
std::string Resource::readAll()
{
std::string content;
content.resize(getSize());
const auto result = read(&content.front(), content.size(), 1);
// Safe because std::string is guaranteed to be contiguous in C++11.

if (result != 1)
throw std::runtime_error("Failed reading data from a resource.");

return content;
}

} // namespace common
} // namespace dart
6 changes: 6 additions & 0 deletions dart/common/Resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class Resource
/// \param[in] _count Number of elements, each of _size bytes.
/// \note This method has the same API as the standard fread function.
virtual std::size_t read(void *_buffer, std::size_t _size, std::size_t _count) = 0;

/// Reads all data from this resource, and returns it as a string.
///
/// \return The string retrieved from the resource.
/// \throw std::runtime_error when failed to read sucessfully.
virtual std::string readAll();
};

using ResourcePtr = std::shared_ptr<Resource>;
Expand Down
55 changes: 55 additions & 0 deletions dart/common/ResourceRetriever.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2017, Graphics Lab, Georgia Tech Research Corporation
* Copyright (c) 2017, Personal Robotics Lab, Carnegie Mellon University
* All rights reserved.
*
* This file is provided under the following "BSD-style" License:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "dart/common/ResourceRetriever.hpp"

#include <sstream>
#include "dart/common/Console.hpp"

namespace dart {
namespace common {

//==============================================================================
std::string ResourceRetriever::readAll(const Uri& uri)
{
auto resource = retrieve(uri);

if (!resource)
{
std::stringstream ss;
ss << "Failed retrieving a resource from '" << uri.toString() << "'.";
throw std::runtime_error(ss.str());
}

return resource->readAll();
}

} // namespace common
} // namespace dart
17 changes: 12 additions & 5 deletions dart/common/ResourceRetriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,18 @@ class ResourceRetriever
/// \brief Return the resource specified by a URI or nullptr on failure.
virtual ResourcePtr retrieve(const Uri& _uri) = 0;

// We don't const-qualify for exists and retrieve here. Derived classes of
// ResourceRetriever will be interacting with external resources that you
// don't necessarily have control over so we cannot guarantee that you get the
// same result every time with the same input Uri. Indeed, const-qualification
// for those functions is pointless.
/// Reads all data from the resource of uri, and returns it as a string.
///
/// \param[in] uri URI to the resource to be retrieved.
/// \return The string retrieved from the resource.
/// \throw std::runtime_error when failed to read sucessfully.
virtual std::string readAll(const Uri& uri);

// We don't const-qualify for exists, retrieve, and readAll here. Derived
// classes of ResourceRetriever will be interacting with external resources
// that you don't necessarily have control over so we cannot guarantee that
// you get the same result every time with the same input Uri. Indeed,
// const-qualification for those functions is pointless.
};

using ResourceRetrieverPtr = std::shared_ptr<ResourceRetriever>;
Expand Down
22 changes: 2 additions & 20 deletions dart/utils/XmlHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,26 +652,8 @@ void openXMLFile(tinyxml2::XMLDocument& doc,
else
retriever = std::make_shared<common::LocalResourceRetriever>();

const common::ResourcePtr resource = retriever->retrieve(uri);
if(!resource)
{
dtwarn << "[openXMLFile] Failed opening URI '"
<< uri.toString() << "'.\n";
throw std::runtime_error("Failed opening URI.");
}

// C++11 guarantees that std::string has contiguous storage.
const std::size_t size = resource->getSize();
std::string content;
content.resize(size);
if(resource->read(&content.front(), size, 1) != 1)
{
dtwarn << "[openXMLFile] Failed reading from URI '"
<< uri.toString() << "'.\n";
throw std::runtime_error("Failed reading from URI.");
}

int const result = doc.Parse(&content.front());
const auto content = retriever->readAll(uri);
const auto result = doc.Parse(&content.front());
if(result != tinyxml2::XML_SUCCESS)
{
dtwarn << "[openXMLFile] Failed parsing XML: TinyXML2 returned error"
Expand Down
5 changes: 1 addition & 4 deletions dart/utils/urdf/DartLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,7 @@ bool DartLoader::readFileToString(
if (!resource)
return false;

// Safe because std::string is guaranteed to be contiguous in C++11.
const std::size_t size = resource->getSize();
_output.resize(size);
resource->read(&_output.front(), size, 1);
_output = _resourceRetriever->readAll(_uri);

return true;
}
Expand Down
20 changes: 1 addition & 19 deletions dart/utils/urdf/urdf_world_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,7 @@ std::shared_ptr<World> parseWorldURDF(
entity.uri = absoluteUri;

// Parse model
const common::ResourcePtr resource = retriever->retrieve(absoluteUri);
if(!resource)
{
dtwarn << "[openXMLFile] Failed opening URI '"
<< absoluteUri.toString() << "'.\n";
throw std::runtime_error("Failed opening URI.");
}

// C++11 guarantees that std::string has contiguous storage.
const std::size_t size = resource->getSize();
std::string xml_model_string;
xml_model_string.resize(size);
if(resource->read(&xml_model_string.front(), size, 1) != 1)
{
dtwarn << "[openXMLFile] Failed reading from URI '"
<< absoluteUri.toString() << "'.\n";
throw std::runtime_error("Failed reading from URI.");
}

const auto xml_model_string = retriever->readAll(absoluteUri);
entity.model = urdf::parseURDF( xml_model_string );

if( !entity.model )
Expand Down
13 changes: 13 additions & 0 deletions unittests/unit/test_LocalResourceRetriever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ TEST(LocalResourceRetriever, retrieve_Path)
ASSERT_TRUE(resource != nullptr);
}

TEST(LocalResourceRetriever, readAll)
{
LocalResourceRetriever retriever;
auto resource = retriever.retrieve(DART_DATA_PATH "test/hello_world.txt");
ASSERT_TRUE(resource != nullptr);

auto content = resource->readAll();
ASSERT_TRUE(content == std::string("Hello World"));

ASSERT_TRUE(retriever.readAll(DART_DATA_PATH "test/hello_world.txt")
== std::string("Hello World"));
}

TEST(LocalResourceRetriever, retrieve_ResourceOperations)
{
const std::string content = "Hello World";
Expand Down

0 comments on commit 52bbd15

Please sign in to comment.