Fetch a network resource.
std::auto_ptr<openvrml::resource_istream>
my_resource_fetcher::do_get_resource(const std::string & uri)
{
using std::auto_ptr;
using std::invalid_argument;
using std::string;
class file_resource_istream : public resource_istream {
std::string url_;
std::filebuf buf_;
public:
explicit file_resource_istream(
const std::string &
path):
resource_istream(&this->buf_)
{
if (!this->buf_.open(path.c_str(),
ios_base::in | ios_base::binary)) {
this->setstate(ios_base::badbit);
}
}
void url(const std::string & str)
{
this->url_ = str;
}
private:
virtual const std::string url() const
{
return this->url_;
}
virtual const std::string type() const
{
using std::find;
using std::string;
using boost::algorithm::iequals;
using boost::next;
string media_type = "application/octet-stream";
const string::const_reverse_iterator dot_pos =
find(this->url_.rbegin(), this->url_.rend(), '.');
if (dot_pos == this->url_.rend()
|| next(dot_pos.base()) == this->url_.end()) {
return media_type;
}
const string::const_iterator hash_pos =
find(next(dot_pos.base()), this->url_.end(), '#');
const string ext(dot_pos.base(), hash_pos);
if (iequals(ext, "wrl")) {
media_type = "model/vrml";
} else if (iequals(ext, "png")) {
media_type = "image/png";
} else if (iequals(ext, "jpg") || iequals(ext, "jpeg")) {
media_type = "image/jpeg";
}
return media_type;
}
virtual bool data_available() const
{
return !!(*this);
}
};
const string scheme = uri.substr(0, uri.find_first_of(':'));
if (scheme != "file") {
throw invalid_argument('\"' + scheme + "\" URI scheme not "
"supported");
}
string path = uri.substr(uri.find_first_of('/', 7));
auto_ptr<resource_istream> in(new file_resource_istream(path));
static_cast<file_resource_istream *>(in.get())->url(uri);
return in;
}