[Add] Wrapper for InputStream.. Allows reading of files etc etc...
-- I'm going to bed. Night!
This commit is contained in:
parent
f182e6749c
commit
f6645cd911
@ -45,6 +45,7 @@ HEADERS += ../src/Actor/Player.h \
|
|||||||
../src/System/Filesystem/FileList.h \
|
../src/System/Filesystem/FileList.h \
|
||||||
../src/System/Filesystem/InputFileStream.h \
|
../src/System/Filesystem/InputFileStream.h \
|
||||||
../src/System/Filesystem/FilePackageManager.h \
|
../src/System/Filesystem/FilePackageManager.h \
|
||||||
|
../src/System/Filesystem/InputStreamWrapper.h \
|
||||||
../src/Texture/Texture.h \
|
../src/Texture/Texture.h \
|
||||||
../src/Sound/Music.h \
|
../src/Sound/Music.h \
|
||||||
../src/TMXParser/base64.h \
|
../src/TMXParser/base64.h \
|
||||||
@ -99,6 +100,7 @@ SOURCES += ../src/Actor/Player.cpp \
|
|||||||
../src/System/Filesystem/FileList.cpp \
|
../src/System/Filesystem/FileList.cpp \
|
||||||
../src/System/Filesystem/InputFileStream.cpp \
|
../src/System/Filesystem/InputFileStream.cpp \
|
||||||
../src/System/Filesystem/FilePackageManager.cpp \
|
../src/System/Filesystem/FilePackageManager.cpp \
|
||||||
|
../src/System/Filesystem/InputStreamWrapper.cpp \
|
||||||
../src/Texture/Texture.cpp \
|
../src/Texture/Texture.cpp \
|
||||||
../src/Sound/Music.cpp \
|
../src/Sound/Music.cpp \
|
||||||
../src/Actor/NPC.cpp \
|
../src/Actor/NPC.cpp \
|
||||||
|
83
src/System/Filesystem/InputStreamWrapper.cpp
Normal file
83
src/System/Filesystem/InputStreamWrapper.cpp
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "../Debug.h"
|
||||||
|
#include "InputStreamWrapper.h"
|
||||||
|
#include "FilePackageManager.h"
|
||||||
|
|
||||||
|
namespace saracraft {
|
||||||
|
namespace filesystem {
|
||||||
|
namespace {
|
||||||
|
int openFileAmount = 0;
|
||||||
|
|
||||||
|
struct Tracker {
|
||||||
|
Tracker(void) {}
|
||||||
|
|
||||||
|
~Tracker(void) { assert(openFileAmount == 0); }
|
||||||
|
};
|
||||||
|
|
||||||
|
Tracker tracker;
|
||||||
|
} // Namespace Unamed.
|
||||||
|
|
||||||
|
struct FB_FILE {
|
||||||
|
InputStream stream;
|
||||||
|
|
||||||
|
FB_FILE(InputStream& stream_) : stream(stream_) {
|
||||||
|
++openFileAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
~FB_FILE(void) {
|
||||||
|
--openFileAmount;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
FB_FILE* fb_fopen(const char* filename, const char*) {
|
||||||
|
if(!filename)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
FilePackageManager& manager = FilePackageManager::GetInstance();
|
||||||
|
manager.SetInputStreamErrorReporting(false);
|
||||||
|
InputStream stream = manager.GetFile(filename);
|
||||||
|
manager.SetInputStreamErrorReporting(true);
|
||||||
|
|
||||||
|
if(stream.IsEof())
|
||||||
|
return 0;
|
||||||
|
return new FB_FILE(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fb_fread(void* buffer, size_t size, size_t count, FB_FILE* stream) {
|
||||||
|
if(!stream) {
|
||||||
|
Debug::logger->message("fb_fread - Attempt to read when no stream is available.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(stream->stream.IsEof()) {
|
||||||
|
Debug::logger->message("fb_fread - Attempt to read past the end of the file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char* charBuffer = reinterpret_cast<unsigned char*> (buffer);
|
||||||
|
stream->stream.Read(charBuffer, size * count);
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t fb_fsize(FB_FILE* stream) {
|
||||||
|
if(!stream)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return stream->stream.GetSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
int fb_feof(FB_FILE* stream) {
|
||||||
|
if(!stream || stream->stream.IsEof())
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fb_fclose(FB_FILE* stream) {
|
||||||
|
delete stream;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // Namespace filesystem.
|
||||||
|
} // Namespace saracraft.
|
||||||
|
|
16
src/System/Filesystem/InputStreamWrapper.h
Normal file
16
src/System/Filesystem/InputStreamWrapper.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace saracraft {
|
||||||
|
namespace filesystem {
|
||||||
|
|
||||||
|
struct FB_FILE;
|
||||||
|
|
||||||
|
FB_FILE* fb_fopen(const char* filename, const char*);
|
||||||
|
size_t fb_fread(void* buffer, size_t size, size_t count, FB_FILE* stream);
|
||||||
|
size_t fb_fsize(FB_FILE* stream);
|
||||||
|
int fb_fclose(FB_FILE* stream);
|
||||||
|
int fb_feof(FB_FILE* stream);
|
||||||
|
|
||||||
|
} // Namespace filesystem.
|
||||||
|
} // Namespace saracraft.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user