Switched to the stdio FILE* API for handling streams to provide the expected behavior to Vorbis.

This commit is contained in:
Lukas Dürrenberger 2015-03-28 23:56:20 +01:00
parent 0076ea50db
commit 0b221921ed
2 changed files with 41 additions and 18 deletions

View File

@ -31,7 +31,8 @@
#include <SFML/Config.hpp> #include <SFML/Config.hpp>
#include <SFML/System/InputStream.hpp> #include <SFML/System/InputStream.hpp>
#include <SFML/System/Export.hpp> #include <SFML/System/Export.hpp>
#include <fstream> #include <cstdio>
#include <string>
#ifdef ANDROID #ifdef ANDROID
namespace sf namespace sf
@ -123,7 +124,7 @@ private:
#ifdef ANDROID #ifdef ANDROID
sf::priv::ResourceStream *m_file; sf::priv::ResourceStream *m_file;
#else #else
std::ifstream m_file; ///< Standard file stream std::FILE* m_file; ///< stdio file stream
#endif #endif
}; };

View File

@ -35,9 +35,7 @@ namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
FileInputStream::FileInputStream() FileInputStream::FileInputStream()
#ifdef ANDROID
: m_file(NULL) : m_file(NULL)
#endif
{ {
} }
@ -49,6 +47,9 @@ FileInputStream::~FileInputStream()
#ifdef ANDROID #ifdef ANDROID
if (m_file) if (m_file)
delete m_file; delete m_file;
#else
if (m_file)
std::fclose(m_file);
#endif #endif
} }
@ -61,8 +62,12 @@ bool FileInputStream::open(const std::string& filename)
delete m_file; delete m_file;
m_file = new sf::priv::ResourceStream(filename); m_file = new sf::priv::ResourceStream(filename);
#else #else
m_file.open(filename.c_str(), std::ios::binary); if (m_file)
return m_file.good(); std::fclose(m_file);
m_file = std::fopen(filename.c_str(), "rb");
return m_file != NULL;
#endif #endif
} }
@ -73,8 +78,10 @@ Int64 FileInputStream::read(void* data, Int64 size)
#ifdef ANDROID #ifdef ANDROID
return m_file->read(data, size); return m_file->read(data, size);
#else #else
m_file.read(static_cast<char*>(data), size); if (m_file)
return m_file.gcount(); return std::fread(data, 1, static_cast<std::size_t>(size), m_file);
else
return -1;
#endif #endif
} }
@ -85,10 +92,15 @@ Int64 FileInputStream::seek(Int64 position)
#ifdef ANDROID #ifdef ANDROID
return m_file->seek(position); return m_file->seek(position);
#else #else
if (m_file.eof() || m_file.fail()) if (m_file)
m_file.clear(); {
m_file.seekg(position); std::fseek(m_file, static_cast<std::size_t>(position), SEEK_SET);
return tell(); return tell();
}
else
{
return -1;
}
#endif #endif
} }
@ -99,7 +111,10 @@ Int64 FileInputStream::tell()
#ifdef ANDROID #ifdef ANDROID
return m_file->tell(); return m_file->tell();
#else #else
return m_file.tellg(); if (m_file)
return std::ftell(m_file);
else
return -1;
#endif #endif
} }
@ -110,11 +125,18 @@ Int64 FileInputStream::getSize()
#ifdef ANDROID #ifdef ANDROID
return m_file->getSize(); return m_file->getSize();
#else #else
std::ifstream::pos_type pos = m_file.tellg(); if (m_file)
m_file.seekg(0, std::ios::end); {
std::ifstream::pos_type size = m_file.tellg(); sf::Int64 position = tell();
m_file.seekg(pos); std::fseek(m_file, 0, SEEK_END);
sf::Int64 size = tell();
seek(position);
return size; return size;
}
else
{
return -1;
}
#endif #endif
} }