diff --git a/src/Audio/AbstractImporter.cpp b/src/Audio/AbstractImporter.cpp index ab3178154..5272673f6 100644 --- a/src/Audio/AbstractImporter.cpp +++ b/src/Audio/AbstractImporter.cpp @@ -24,9 +24,9 @@ #include "AbstractImporter.h" -#include #include #include +#include namespace Magnum { namespace Audio { @@ -57,22 +57,12 @@ void AbstractImporter::doOpenFile(const std::string& filename) { CORRADE_ASSERT(features() & Feature::OpenData, "Audio::AbstractImporter::openFile(): not implemented", ); /* Open file */ - std::ifstream in(filename.data(), std::ios::binary); - if(!in.good()) { + if(!Utility::Directory::fileExists(filename)) { Error() << "Trade::AbstractImporter::openFile(): cannot open file" << filename; return; } - /* Create array to hold file contents */ - in.seekg(0, std::ios::end); - Containers::Array data(in.tellg()); - - /* Read data, close */ - in.seekg(0, std::ios::beg); - in.read(reinterpret_cast(data.begin()), data.size()); - in.close(); - - doOpenData(data); + doOpenData(Utility::Directory::read(filename)); } void AbstractImporter::close() { diff --git a/src/Shader.cpp b/src/Shader.cpp index 664871931..2b928309d 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -24,9 +24,9 @@ #include "Shader.h" -#include #include #include +#include #include "Extensions.h" #include "Implementation/State.h" @@ -604,22 +604,10 @@ Shader& Shader::addSource(std::string source) { } Shader& Shader::addFile(const std::string& filename) { - /* Open file */ - std::ifstream file(filename); - CORRADE_ASSERT(file.good(), "Shader file " << '\'' + filename + '\'' << " cannot be opened.", *this); - - /* Get size of shader and initialize buffer */ - file.seekg(0, std::ios::end); - std::string source(std::size_t(file.tellg()), '\0'); - - /* Read data, close */ - file.seekg(0, std::ios::beg); - file.read(&source[0], source.size()); - file.close(); - - /* Move to sources */ - addSource(std::move(source)); + CORRADE_ASSERT(Utility::Directory::fileExists(filename), + "Shader file " << '\'' + filename + '\'' << " cannot be read.", *this); + addSource(Utility::Directory::readString(filename)); return *this; } diff --git a/src/Text/AbstractFont.cpp b/src/Text/AbstractFont.cpp index 0d92fc077..86c17749c 100644 --- a/src/Text/AbstractFont.cpp +++ b/src/Text/AbstractFont.cpp @@ -24,8 +24,8 @@ #include "AbstractFont.h" -#include #include +#include #include #include "Text/GlyphCache.h" @@ -86,22 +86,12 @@ std::pair AbstractFont::doOpenFile(const std::string& filename, co "Text::AbstractFont::openFile(): not implemented", {}); /* Open file */ - std::ifstream in(filename.data(), std::ios::binary); - if(!in.good()) { + if(!Utility::Directory::fileExists(filename)) { Error() << "Trade::AbstractFont::openFile(): cannot open file" << filename; return {}; } - /* Create array to hold file contents */ - in.seekg(0, std::ios::end); - Containers::Array data(std::size_t(in.tellg())); - - /* Read data, close */ - in.seekg(0, std::ios::beg); - in.read(reinterpret_cast(data.begin()), data.size()); - in.close(); - - return doOpenSingleData(data, size); + return doOpenSingleData(Utility::Directory::read(filename), size); } void AbstractFont::close() { diff --git a/src/Text/AbstractFontConverter.cpp b/src/Text/AbstractFontConverter.cpp index 9dd5e4320..e375e5fd7 100644 --- a/src/Text/AbstractFontConverter.cpp +++ b/src/Text/AbstractFontConverter.cpp @@ -25,9 +25,9 @@ #include "AbstractFontConverter.h" #include -#include #include #include +#include #include #include "Text/GlyphCache.h" @@ -96,16 +96,9 @@ bool AbstractFontConverter::doExportFontToFile(AbstractFont& font, GlyphCache& c /* Export all data */ const auto data = doExportFontToData(font, cache, filename, characters); - for(const auto& d: data) { - /* Open file */ - std::ofstream out(d.first.data(), std::ios::binary); - if(!out.good()) { - Error() << "Text::AbstractFontConverter::exportFontToFile(): cannot write to file" << d.first; - return false; - } - - /* Write data, close */ - out.write(reinterpret_cast(d.second.begin()), d.second.size()); + for(const auto& d: data) if(!Utility::Directory::write(d.first, d.second)) { + Error() << "Text::AbstractFontConverter::exportFontToFile(): cannot write to file" << d.first; + return false; } return true; @@ -154,16 +147,9 @@ bool AbstractFontConverter::doExportGlyphCacheToFile(GlyphCache& cache, const st /* Export all data */ const auto data = doExportGlyphCacheToData(cache, filename); - for(const auto& d: data) { - /* Open file */ - std::ofstream out(d.first.data(), std::ios::binary); - if(!out.good()) { - Error() << "Text::AbstractFontConverter::exportGlyphCacheToFile(): cannot write to file" << d.first; - return false; - } - - /* Write data, close */ - out.write(reinterpret_cast(d.second.begin()), d.second.size()); + for(const auto& d: data) if(!Utility::Directory::write(d.first, d.second)) { + Error() << "Text::AbstractFontConverter::exportGlyphCacheToFile(): cannot write to file" << d.first; + return false; } return true; @@ -213,22 +199,12 @@ std::unique_ptr AbstractFontConverter::doImportGlyphCacheFromFile(co "Text::AbstractFontConverter::importGlyphCacheFromFile(): not implemented", nullptr); /* Open file */ - std::ifstream in(filename.data(), std::ios::binary); - if(!in.good()) { + if(!Utility::Directory::fileExists(filename)) { Error() << "Trade::AbstractFontConverter::importGlyphCacheFromFile(): cannot open file" << filename; return nullptr; } - /* Create array to hold file contents */ - in.seekg(0, std::ios::end); - Containers::Array data(std::size_t(in.tellg())); - - /* Read data, close */ - in.seekg(0, std::ios::beg); - in.read(reinterpret_cast(data.begin()), data.size()); - in.close(); - - return doImportGlyphCacheFromSingleData(data); + return doImportGlyphCacheFromSingleData(Utility::Directory::read(filename)); } #ifndef __MINGW32__ diff --git a/src/Trade/AbstractImageConverter.cpp b/src/Trade/AbstractImageConverter.cpp index 5bdf5e8b4..705bef996 100644 --- a/src/Trade/AbstractImageConverter.cpp +++ b/src/Trade/AbstractImageConverter.cpp @@ -24,9 +24,9 @@ #include "AbstractImageConverter.h" -#include #include #include +#include namespace Magnum { namespace Trade { @@ -63,18 +63,15 @@ bool AbstractImageConverter::exportToFile(const ImageReference2D& image, const s bool AbstractImageConverter::doExportToFile(const ImageReference2D& image, const std::string& filename) const { CORRADE_ASSERT(features() & Feature::ConvertData, "Trade::AbstractImageConverter::exportToFile(): not implemented", false); - auto data = doExportToData(image); + const auto data = doExportToData(image); if(!data) return false; /* Open file */ - std::ofstream out(filename.data(), std::ios::binary); - if(!out.good()) { + if(!Utility::Directory::write(filename, data)) { Error() << "Trade::AbstractImageConverter::exportToFile(): cannot write to file" << filename; return false; } - /* Write data, close */ - out.write(reinterpret_cast(data.begin()), data.size()); return true; } diff --git a/src/Trade/AbstractImporter.cpp b/src/Trade/AbstractImporter.cpp index 76ec4021a..53dedd74b 100644 --- a/src/Trade/AbstractImporter.cpp +++ b/src/Trade/AbstractImporter.cpp @@ -24,9 +24,9 @@ #include "AbstractImporter.h" -#include #include #include +#include #include "Trade/AbstractMaterialData.h" #include "Trade/CameraData.h" @@ -68,22 +68,12 @@ void AbstractImporter::doOpenFile(const std::string& filename) { CORRADE_ASSERT(features() & Feature::OpenData, "Trade::AbstractImporter::openFile(): not implemented", ); /* Open file */ - std::ifstream in(filename.data(), std::ios::binary); - if(!in.good()) { + if(!Utility::Directory::fileExists(filename)) { Error() << "Trade::AbstractImporter::openFile(): cannot open file" << filename; return; } - /* Create array to hold file contents */ - in.seekg(0, std::ios::end); - Containers::Array data(std::size_t(in.tellg())); - - /* Read data, close */ - in.seekg(0, std::ios::beg); - in.read(reinterpret_cast(data.begin()), data.size()); - in.close(); - - doOpenData(data); + doOpenData(Utility::Directory::read(filename)); } void AbstractImporter::close() {