Browse Source

Using Corrade::Utility::Directory::{read,write}() where possible.

Much shorter code now with less redundancy.
pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
d945395800
  1. 16
      src/Audio/AbstractImporter.cpp
  2. 20
      src/Shader.cpp
  3. 16
      src/Text/AbstractFont.cpp
  4. 42
      src/Text/AbstractFontConverter.cpp
  5. 9
      src/Trade/AbstractImageConverter.cpp
  6. 16
      src/Trade/AbstractImporter.cpp

16
src/Audio/AbstractImporter.cpp

@ -24,9 +24,9 @@
#include "AbstractImporter.h" #include "AbstractImporter.h"
#include <fstream>
#include <Containers/Array.h> #include <Containers/Array.h>
#include <Utility/Assert.h> #include <Utility/Assert.h>
#include <Utility/Directory.h>
namespace Magnum { namespace Audio { 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", ); CORRADE_ASSERT(features() & Feature::OpenData, "Audio::AbstractImporter::openFile(): not implemented", );
/* Open file */ /* Open file */
std::ifstream in(filename.data(), std::ios::binary); if(!Utility::Directory::fileExists(filename)) {
if(!in.good()) {
Error() << "Trade::AbstractImporter::openFile(): cannot open file" << filename; Error() << "Trade::AbstractImporter::openFile(): cannot open file" << filename;
return; return;
} }
/* Create array to hold file contents */ doOpenData(Utility::Directory::read(filename));
in.seekg(0, std::ios::end);
Containers::Array<unsigned char> data(in.tellg());
/* Read data, close */
in.seekg(0, std::ios::beg);
in.read(reinterpret_cast<char*>(data.begin()), data.size());
in.close();
doOpenData(data);
} }
void AbstractImporter::close() { void AbstractImporter::close() {

20
src/Shader.cpp

@ -24,9 +24,9 @@
#include "Shader.h" #include "Shader.h"
#include <fstream>
#include <Containers/Array.h> #include <Containers/Array.h>
#include <Utility/Assert.h> #include <Utility/Assert.h>
#include <Utility/Directory.h>
#include "Extensions.h" #include "Extensions.h"
#include "Implementation/State.h" #include "Implementation/State.h"
@ -604,22 +604,10 @@ Shader& Shader::addSource(std::string source) {
} }
Shader& Shader::addFile(const std::string& filename) { Shader& Shader::addFile(const std::string& filename) {
/* Open file */ CORRADE_ASSERT(Utility::Directory::fileExists(filename),
std::ifstream file(filename); "Shader file " << '\'' + filename + '\'' << " cannot be read.", *this);
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));
addSource(Utility::Directory::readString(filename));
return *this; return *this;
} }

16
src/Text/AbstractFont.cpp

@ -24,8 +24,8 @@
#include "AbstractFont.h" #include "AbstractFont.h"
#include <fstream>
#include <Containers/Array.h> #include <Containers/Array.h>
#include <Utility/Directory.h>
#include <Utility/Unicode.h> #include <Utility/Unicode.h>
#include "Text/GlyphCache.h" #include "Text/GlyphCache.h"
@ -86,22 +86,12 @@ std::pair<Float, Float> AbstractFont::doOpenFile(const std::string& filename, co
"Text::AbstractFont::openFile(): not implemented", {}); "Text::AbstractFont::openFile(): not implemented", {});
/* Open file */ /* Open file */
std::ifstream in(filename.data(), std::ios::binary); if(!Utility::Directory::fileExists(filename)) {
if(!in.good()) {
Error() << "Trade::AbstractFont::openFile(): cannot open file" << filename; Error() << "Trade::AbstractFont::openFile(): cannot open file" << filename;
return {}; return {};
} }
/* Create array to hold file contents */ return doOpenSingleData(Utility::Directory::read(filename), size);
in.seekg(0, std::ios::end);
Containers::Array<unsigned char> data(std::size_t(in.tellg()));
/* Read data, close */
in.seekg(0, std::ios::beg);
in.read(reinterpret_cast<char*>(data.begin()), data.size());
in.close();
return doOpenSingleData(data, size);
} }
void AbstractFont::close() { void AbstractFont::close() {

42
src/Text/AbstractFontConverter.cpp

@ -25,9 +25,9 @@
#include "AbstractFontConverter.h" #include "AbstractFontConverter.h"
#include <algorithm> #include <algorithm>
#include <fstream>
#include <Containers/Array.h> #include <Containers/Array.h>
#include <Utility/Assert.h> #include <Utility/Assert.h>
#include <Utility/Directory.h>
#include <Utility/Unicode.h> #include <Utility/Unicode.h>
#include "Text/GlyphCache.h" #include "Text/GlyphCache.h"
@ -96,16 +96,9 @@ bool AbstractFontConverter::doExportFontToFile(AbstractFont& font, GlyphCache& c
/* Export all data */ /* Export all data */
const auto data = doExportFontToData(font, cache, filename, characters); const auto data = doExportFontToData(font, cache, filename, characters);
for(const auto& d: data) { for(const auto& d: data) if(!Utility::Directory::write(d.first, d.second)) {
/* Open file */ Error() << "Text::AbstractFontConverter::exportFontToFile(): cannot write to file" << d.first;
std::ofstream out(d.first.data(), std::ios::binary); return false;
if(!out.good()) {
Error() << "Text::AbstractFontConverter::exportFontToFile(): cannot write to file" << d.first;
return false;
}
/* Write data, close */
out.write(reinterpret_cast<const char*>(d.second.begin()), d.second.size());
} }
return true; return true;
@ -154,16 +147,9 @@ bool AbstractFontConverter::doExportGlyphCacheToFile(GlyphCache& cache, const st
/* Export all data */ /* Export all data */
const auto data = doExportGlyphCacheToData(cache, filename); const auto data = doExportGlyphCacheToData(cache, filename);
for(const auto& d: data) { for(const auto& d: data) if(!Utility::Directory::write(d.first, d.second)) {
/* Open file */ Error() << "Text::AbstractFontConverter::exportGlyphCacheToFile(): cannot write to file" << d.first;
std::ofstream out(d.first.data(), std::ios::binary); return false;
if(!out.good()) {
Error() << "Text::AbstractFontConverter::exportGlyphCacheToFile(): cannot write to file" << d.first;
return false;
}
/* Write data, close */
out.write(reinterpret_cast<const char*>(d.second.begin()), d.second.size());
} }
return true; return true;
@ -213,22 +199,12 @@ std::unique_ptr<GlyphCache> AbstractFontConverter::doImportGlyphCacheFromFile(co
"Text::AbstractFontConverter::importGlyphCacheFromFile(): not implemented", nullptr); "Text::AbstractFontConverter::importGlyphCacheFromFile(): not implemented", nullptr);
/* Open file */ /* Open file */
std::ifstream in(filename.data(), std::ios::binary); if(!Utility::Directory::fileExists(filename)) {
if(!in.good()) {
Error() << "Trade::AbstractFontConverter::importGlyphCacheFromFile(): cannot open file" << filename; Error() << "Trade::AbstractFontConverter::importGlyphCacheFromFile(): cannot open file" << filename;
return nullptr; return nullptr;
} }
/* Create array to hold file contents */ return doImportGlyphCacheFromSingleData(Utility::Directory::read(filename));
in.seekg(0, std::ios::end);
Containers::Array<unsigned char> data(std::size_t(in.tellg()));
/* Read data, close */
in.seekg(0, std::ios::beg);
in.read(reinterpret_cast<char*>(data.begin()), data.size());
in.close();
return doImportGlyphCacheFromSingleData(data);
} }
#ifndef __MINGW32__ #ifndef __MINGW32__

9
src/Trade/AbstractImageConverter.cpp

@ -24,9 +24,9 @@
#include "AbstractImageConverter.h" #include "AbstractImageConverter.h"
#include <fstream>
#include <Containers/Array.h> #include <Containers/Array.h>
#include <Utility/Assert.h> #include <Utility/Assert.h>
#include <Utility/Directory.h>
namespace Magnum { namespace Trade { 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 { bool AbstractImageConverter::doExportToFile(const ImageReference2D& image, const std::string& filename) const {
CORRADE_ASSERT(features() & Feature::ConvertData, "Trade::AbstractImageConverter::exportToFile(): not implemented", false); CORRADE_ASSERT(features() & Feature::ConvertData, "Trade::AbstractImageConverter::exportToFile(): not implemented", false);
auto data = doExportToData(image); const auto data = doExportToData(image);
if(!data) return false; if(!data) return false;
/* Open file */ /* Open file */
std::ofstream out(filename.data(), std::ios::binary); if(!Utility::Directory::write(filename, data)) {
if(!out.good()) {
Error() << "Trade::AbstractImageConverter::exportToFile(): cannot write to file" << filename; Error() << "Trade::AbstractImageConverter::exportToFile(): cannot write to file" << filename;
return false; return false;
} }
/* Write data, close */
out.write(reinterpret_cast<const char*>(data.begin()), data.size());
return true; return true;
} }

16
src/Trade/AbstractImporter.cpp

@ -24,9 +24,9 @@
#include "AbstractImporter.h" #include "AbstractImporter.h"
#include <fstream>
#include <Containers/Array.h> #include <Containers/Array.h>
#include <Utility/Assert.h> #include <Utility/Assert.h>
#include <Utility/Directory.h>
#include "Trade/AbstractMaterialData.h" #include "Trade/AbstractMaterialData.h"
#include "Trade/CameraData.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", ); CORRADE_ASSERT(features() & Feature::OpenData, "Trade::AbstractImporter::openFile(): not implemented", );
/* Open file */ /* Open file */
std::ifstream in(filename.data(), std::ios::binary); if(!Utility::Directory::fileExists(filename)) {
if(!in.good()) {
Error() << "Trade::AbstractImporter::openFile(): cannot open file" << filename; Error() << "Trade::AbstractImporter::openFile(): cannot open file" << filename;
return; return;
} }
/* Create array to hold file contents */ doOpenData(Utility::Directory::read(filename));
in.seekg(0, std::ios::end);
Containers::Array<unsigned char> data(std::size_t(in.tellg()));
/* Read data, close */
in.seekg(0, std::ios::beg);
in.read(reinterpret_cast<char*>(data.begin()), data.size());
in.close();
doOpenData(data);
} }
void AbstractImporter::close() { void AbstractImporter::close() {

Loading…
Cancel
Save