use c file io wrapper in geojson/topojson plugins

This commit is contained in:
Dane Springmeyer 2014-10-15 11:36:57 -07:00
parent 9684880062
commit 4e8125d2ad
2 changed files with 11 additions and 23 deletions

View file

@ -42,6 +42,7 @@
#include <mapnik/projection.hpp> #include <mapnik/projection.hpp>
#include <mapnik/util/geometry_to_ds_type.hpp> #include <mapnik/util/geometry_to_ds_type.hpp>
#include <mapnik/util/variant.hpp> #include <mapnik/util/variant.hpp>
#include <mapnik/util/file_io.hpp>
#include <mapnik/json/feature_collection_grammar.hpp> #include <mapnik/json/feature_collection_grammar.hpp>
#include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/qi.hpp>
@ -126,21 +127,14 @@ geojson_datasource::geojson_datasource(parameters const& params)
} }
else else
{ {
#ifdef _WINDOWS mapnik::util::file file(filename_);
std::unique_ptr<std::FILE, int (*)(std::FILE *)> file(_wfopen(mapnik::utf8_to_utf16(filename_).c_str(), L"rb"), fclose); if (!file.open())
#else
std::unique_ptr<std::FILE, int (*)(std::FILE *)> file(std::fopen(filename_.c_str(),"rb"), std::fclose);
#endif
if (file == nullptr)
{ {
throw mapnik::datasource_exception("GeoJSON Plugin: could not open: '" + filename_ + "'"); throw mapnik::datasource_exception("TopoJSON Plugin: could not open: '" + filename_ + "'");
} }
std::fseek(file.get(), 0, SEEK_END);
std::size_t file_size = std::ftell(file.get());
std::fseek(file.get(), 0, SEEK_SET);
std::string file_buffer; std::string file_buffer;
file_buffer.resize(file_size); file_buffer.resize(file.size());
std::fread(&file_buffer[0], file_size, 1, file.get()); std::fread(&file_buffer[0], file.size(), 1, file.get());
parse_geojson(file_buffer); parse_geojson(file_buffer);
} }
} }

View file

@ -37,6 +37,7 @@
#include <mapnik/json/topojson_grammar_impl.hpp> #include <mapnik/json/topojson_grammar_impl.hpp>
#include <mapnik/json/topojson_utils.hpp> #include <mapnik/json/topojson_utils.hpp>
#include <mapnik/util/variant.hpp> #include <mapnik/util/variant.hpp>
#include <mapnik/util/file_io.hpp>
#include <mapnik/make_unique.hpp> #include <mapnik/make_unique.hpp>
using mapnik::datasource; using mapnik::datasource;
@ -170,21 +171,14 @@ topojson_datasource::topojson_datasource(parameters const& params)
} }
else else
{ {
#ifdef _WINDOWS mapnik::util::file file(filename_);
std::unique_ptr<std::FILE, int (*)(std::FILE *)> file(_wfopen(mapnik::utf8_to_utf16(filename_).c_str(), L"rb"), fclose); if (!file.open())
#else
std::unique_ptr<std::FILE, int (*)(std::FILE *)> file(std::fopen(filename_.c_str(),"rb"), std::fclose);
#endif
if (file == nullptr)
{ {
throw mapnik::datasource_exception("TopoJSON Plugin: could not open: '" + filename_ + "'"); throw mapnik::datasource_exception("TopoJSON Plugin: could not open: '" + filename_ + "'");
} }
std::fseek(file.get(), 0, SEEK_END);
std::size_t file_size = std::ftell(file.get());
std::fseek(file.get(), 0, SEEK_SET);
std::string file_buffer; std::string file_buffer;
file_buffer.resize(file_size); file_buffer.resize(file.size());
std::fread(&file_buffer[0], file_size, 1, file.get()); std::fread(&file_buffer[0], file.size(), 1, file.get());
parse_topojson(file_buffer); parse_topojson(file_buffer);
} }
} }