-
Notifications
You must be signed in to change notification settings - Fork 285
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
Support for generic URIs #464
Changes from 45 commits
fbeb903
f6b2708
f2b619b
f64234f
e896ccf
755c00d
0ae6658
02028bc
2b2121c
82993bc
6bf445b
224ff23
23eaddc
1853280
8a7958c
5f05789
0e3d718
533fa53
3dd3b11
ac28d8f
ccb1316
f347112
483613e
f5e1cee
fce0a78
9fe0c18
f96e3e1
956e0c5
4467332
a08abd5
238b112
6618f95
c214bab
d469307
73fa9c3
7eb99be
8d791ec
c7e2245
f44394e
14414c5
d849427
af7cb60
450e06c
7cc7cc3
9cb8e11
2729eb4
5a737b6
ac72ac7
dc4bc2c
20bbf8d
b74ed66
a9c0042
54dceab
913e945
9171d34
7c423c3
0dd9c52
0b4de8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include <cstring> | ||
#include <iostream> | ||
#include "dart/common/Console.h" | ||
#include "LocalResource.h" | ||
|
||
namespace dart { | ||
namespace common { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually place horizontal separator before function definition as: //==============================================================================
LocalResource::LocalResource(const std::string& _path)
{
// ...
} |
||
LocalResource::LocalResource(const std::string& _path) | ||
: mFile(std::fopen(_path.c_str(), "rb")) | ||
{ | ||
if(!mFile) | ||
{ | ||
dtwarn << "[LocalResource::constructor] Failed opening file '" << _path | ||
<< "' for reading: " << std::strerror(errno) << "\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vertical alignment (here and following comments) |
||
} | ||
} | ||
|
||
LocalResource::~LocalResource() | ||
{ | ||
if (!mFile) | ||
return; | ||
|
||
if (std::fclose(mFile) == EOF) | ||
{ | ||
dtwarn << "[LocalResource::destructor] Failed closing file.\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably print There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting. The documentation I found just says that the function returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reference I linked to is basically an internet copy of the GNU Linux Programmer's manual, so it should be legit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be true on GNU/Linux, but I don't know if it's guaranteed by the C++ standard. |
||
} | ||
} | ||
|
||
bool LocalResource::isGood() const | ||
{ | ||
return !!mFile; | ||
} | ||
|
||
size_t LocalResource::getSize() | ||
{ | ||
if(!mFile) | ||
return 0; | ||
|
||
const long offset = std::ftell(mFile); | ||
if(offset == -1L) | ||
{ | ||
dtwarn << "[LocalResource::getSize] Unable to compute file size: Failed" | ||
" getting current offset: " << std::strerror(errno) << "\n"; | ||
return 0; | ||
} | ||
|
||
// The SEEK_END option is not required by the C standard. However, it is | ||
// required by POSIX. | ||
// TODO: Does this work on Windows? | ||
if(std::fseek(mFile, 0, SEEK_END) || std::ferror(mFile)) | ||
{ | ||
dtwarn << "[LocalResource::getSize] Unable to compute file size: Failed" | ||
" seeking to the end of the file.\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation I found says only that:
I don't think there is any guarantee that the value returned by Again, the documentation that you found says that |
||
return 0; | ||
} | ||
|
||
const long size = std::ftell(mFile); | ||
if(size == -1L) | ||
{ | ||
dtwarn << "[LocalResource::getSize] Unable to compute file size: Failed" | ||
" getting end of file offset: " << std::strerror(errno) << "\n"; | ||
return 0; | ||
} | ||
|
||
if(std::fseek(mFile, offset, SEEK_SET) || std::ferror(mFile)) | ||
{ | ||
dtwarn << "[LocalResource::getSize] Unable to compute file size: Failed" | ||
" seeking to the current position.\n"; | ||
return 0; | ||
} | ||
|
||
return size; | ||
} | ||
|
||
size_t LocalResource::tell() | ||
{ | ||
if(!mFile) | ||
return 0; | ||
|
||
const long offset = std::ftell(mFile); | ||
if (offset == -1L) | ||
{ | ||
dtwarn << "[LocalResource::tell] Failed" | ||
" seeking to the current position.\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use a |
||
} | ||
|
||
// We return -1 to match the beahvior of DefaultIoStream in Assimp. | ||
return offset; | ||
} | ||
|
||
bool LocalResource::seek(ptrdiff_t _offset, SeekType _mode) | ||
{ | ||
int origin; | ||
switch(_mode) | ||
{ | ||
case Resource::SEEKTYPE_CUR: | ||
origin = SEEK_CUR; | ||
break; | ||
|
||
case Resource::SEEKTYPE_END: | ||
origin = SEEK_END; | ||
break; | ||
|
||
case Resource::SEEKTYPE_SET: | ||
origin = SEEK_SET; | ||
break; | ||
|
||
default: | ||
dtwarn << "[LocalResource::Seek] Invalid origin. Expected" | ||
" SEEKTYPE_CUR, SEEKTYPE_END, or SEEKTYPE_SET.\n"; | ||
return false; | ||
} | ||
|
||
if (!std::fseek(mFile, _offset, origin) && !std::ferror(mFile)) | ||
return true; | ||
else | ||
{ | ||
dtwarn << "[LocalResource::seek] Seeking failed.\n"; | ||
return false; | ||
} | ||
} | ||
|
||
size_t LocalResource::read(void *_buffer, size_t _size, size_t _count) | ||
{ | ||
if (!mFile) | ||
return 0; | ||
|
||
const size_t result = std::fread(_buffer, _size, _count, mFile); | ||
if (std::ferror(mFile)) | ||
{ | ||
dtwarn << "[LocalResource::tell] Failed reading file.\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
} | ||
return result; | ||
} | ||
|
||
} // namespace common | ||
} // namespace dart | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) 2015, Georgia Tech Research Corporation | ||
* All rights reserved. | ||
* | ||
* Author(s): Michael Koval <mkoval@cs.cmu.edu> | ||
* | ||
* Georgia Tech Graphics Lab and Humanoid Robotics Lab | ||
* | ||
* Directed by Prof. C. Karen Liu and Prof. Mike Stilman | ||
* <karenliu@cc.gatech.edu> <mstilman@cc.gatech.edu> | ||
* | ||
* 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. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add empty line after license block |
||
#ifndef DART_COMMON_LOCALRESOURCE_H_ | ||
#define DART_COMMON_LOCALRESOURCE_H_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add empty line after header guard |
||
#include "dart/common/Resource.h" | ||
|
||
namespace dart { | ||
namespace common { | ||
|
||
class LocalResource : public virtual Resource | ||
{ | ||
public: | ||
LocalResource(const std::string& _path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. explicit LocalResource(const std::string& _path); to prevent unintended implicit type conversion |
||
virtual ~LocalResource(); | ||
|
||
LocalResource(const LocalResource& _other) = delete; | ||
LocalResource& operator=(const LocalResource& _other) = delete; | ||
|
||
/// Return if the resource is open and in a valid state. | ||
bool isGood() const; | ||
|
||
// Documentation inherited. | ||
size_t getSize() override; | ||
|
||
// Documentation inherited. | ||
size_t tell() override; | ||
|
||
// Documentation inherited. | ||
bool seek(ptrdiff_t _origin, SeekType _mode) override; | ||
|
||
// Documentation inherited. | ||
size_t read(void *_buffer, size_t _size, size_t _count) override; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please put * right after type as: size_t read(void* _buffer, size_t _size, size_t _count) override; |
||
|
||
private: | ||
std::FILE* mFile; | ||
}; | ||
|
||
} // namespace common | ||
} // namespace dart | ||
|
||
#endif // ifndef DART_COMMON_LOCALRESOURCERETRIEVER_H_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #endif // ifndef DART_COMMON_LOCALRESOURCE_H_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <iostream> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. license block missing |
||
#include <fstream> | ||
#include "dart/common/Console.h" | ||
#include "dart/common/Uri.h" | ||
#include "LocalResourceRetriever.h" | ||
#include "LocalResource.h" | ||
|
||
namespace dart { | ||
namespace common { | ||
|
||
bool LocalResourceRetriever::exists(const std::string& _uri) | ||
{ | ||
common::Uri uri; | ||
if(!uri.fromString(_uri)) | ||
{ | ||
dtwarn << "[exists] Failed parsing URI: " << _uri << "\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return false; | ||
} | ||
|
||
// Open and close the file to check if it exists. It would be more efficient | ||
// to stat() it, but that is not portable. | ||
if (uri.mScheme.get_value_or("file") != "file") | ||
return false; | ||
|
||
return std::ifstream(*uri.mPath, std::ios::binary).good(); | ||
} | ||
|
||
common::ResourcePtr LocalResourceRetriever::retrieve(const std::string& _uri) | ||
{ | ||
common::Uri uri; | ||
if(!uri.fromString(_uri)) | ||
{ | ||
dtwarn << "[exists] Failed parsing URI: " << _uri << "\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
return nullptr; | ||
} | ||
|
||
if (uri.mScheme.get_value_or("file") != "file") | ||
return nullptr; | ||
|
||
const auto resource = std::make_shared<LocalResource>(*uri.mPath); | ||
if(resource->isGood()) | ||
return resource; | ||
else | ||
return nullptr; | ||
} | ||
|
||
} // namespace common | ||
} // namespace dart |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2015, Georgia Tech Research Corporation | ||
* All rights reserved. | ||
* | ||
* Author(s): Michael Koval <mkoval@cs.cmu.edu> | ||
* | ||
* Georgia Tech Graphics Lab and Humanoid Robotics Lab | ||
* | ||
* Directed by Prof. C. Karen Liu and Prof. Mike Stilman | ||
* <karenliu@cc.gatech.edu> <mstilman@cc.gatech.edu> | ||
* | ||
* 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. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add empty line |
||
#ifndef DART_COMMON_LOCALRESOURCERETRIEVER_H_ | ||
#define DART_COMMON_LOCALRESOURCERETRIEVER_H_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add empty line |
||
#include "dart/common/ResourceRetriever.h" | ||
|
||
namespace dart { | ||
namespace common { | ||
|
||
/// LocalResourceRetriever provides access to local resources specified by | ||
/// file:// URIs by wrapping the standard C and C++ file manipulation routines. | ||
class LocalResourceRetriever : public virtual ResourceRetriever | ||
{ | ||
public: | ||
virtual ~LocalResourceRetriever() = default; | ||
|
||
// Documentation inherited. | ||
bool exists(const std::string& _uri) override; | ||
|
||
// Documentation inherited. | ||
ResourcePtr retrieve(const std::string& _uri) override; | ||
}; | ||
|
||
typedef std::shared_ptr<LocalResourceRetriever> LocalResourceRetrieverPtr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about using using LocalResourceRetrieverPtr = std::shared_ptr<LocalResourceRetriever>; They are same but it seems |
||
|
||
} // namespace common | ||
} // namespace dart | ||
|
||
#endif // ifndef DART_COMMON_LOCALRESOURCERETRIEVER_H_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
License block is missing.