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 <fstream>
#include <Containers/Array.h>
#include <Utility/Assert.h>
#include <Utility/Directory.h>
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<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);
doOpenData(Utility::Directory::read(filename));
}
void AbstractImporter::close() {

20
src/Shader.cpp

@ -24,9 +24,9 @@
#include "Shader.h"
#include <fstream>
#include <Containers/Array.h>
#include <Utility/Assert.h>
#include <Utility/Directory.h>
#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;
}

16
src/Text/AbstractFont.cpp

@ -24,8 +24,8 @@
#include "AbstractFont.h"
#include <fstream>
#include <Containers/Array.h>
#include <Utility/Directory.h>
#include <Utility/Unicode.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", {});
/* 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<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);
return doOpenSingleData(Utility::Directory::read(filename), size);
}
void AbstractFont::close() {

42
src/Text/AbstractFontConverter.cpp

@ -25,9 +25,9 @@
#include "AbstractFontConverter.h"
#include <algorithm>
#include <fstream>
#include <Containers/Array.h>
#include <Utility/Assert.h>
#include <Utility/Directory.h>
#include <Utility/Unicode.h>
#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<const char*>(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<const char*>(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<GlyphCache> 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<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);
return doImportGlyphCacheFromSingleData(Utility::Directory::read(filename));
}
#ifndef __MINGW32__

9
src/Trade/AbstractImageConverter.cpp

@ -24,9 +24,9 @@
#include "AbstractImageConverter.h"
#include <fstream>
#include <Containers/Array.h>
#include <Utility/Assert.h>
#include <Utility/Directory.h>
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<const char*>(data.begin()), data.size());
return true;
}

16
src/Trade/AbstractImporter.cpp

@ -24,9 +24,9 @@
#include "AbstractImporter.h"
#include <fstream>
#include <Containers/Array.h>
#include <Utility/Assert.h>
#include <Utility/Directory.h>
#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<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);
doOpenData(Utility::Directory::read(filename));
}
void AbstractImporter::close() {

Loading…
Cancel
Save