From f6645cd9119bd9f4ec456d15c45b98243d234c8a Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Mon, 3 Sep 2012 01:26:42 +0100 Subject: [PATCH] [Add] Wrapper for InputStream.. Allows reading of files etc etc... -- I'm going to bed. Night! --- Bin/LibD.pro | 2 + src/System/Filesystem/InputStreamWrapper.cpp | 83 ++++++++++++++++++++ src/System/Filesystem/InputStreamWrapper.h | 16 ++++ 3 files changed, 101 insertions(+) create mode 100644 src/System/Filesystem/InputStreamWrapper.cpp create mode 100644 src/System/Filesystem/InputStreamWrapper.h diff --git a/Bin/LibD.pro b/Bin/LibD.pro index a16cd08..0b1751c 100644 --- a/Bin/LibD.pro +++ b/Bin/LibD.pro @@ -45,6 +45,7 @@ HEADERS += ../src/Actor/Player.h \ ../src/System/Filesystem/FileList.h \ ../src/System/Filesystem/InputFileStream.h \ ../src/System/Filesystem/FilePackageManager.h \ + ../src/System/Filesystem/InputStreamWrapper.h \ ../src/Texture/Texture.h \ ../src/Sound/Music.h \ ../src/TMXParser/base64.h \ @@ -99,6 +100,7 @@ SOURCES += ../src/Actor/Player.cpp \ ../src/System/Filesystem/FileList.cpp \ ../src/System/Filesystem/InputFileStream.cpp \ ../src/System/Filesystem/FilePackageManager.cpp \ + ../src/System/Filesystem/InputStreamWrapper.cpp \ ../src/Texture/Texture.cpp \ ../src/Sound/Music.cpp \ ../src/Actor/NPC.cpp \ diff --git a/src/System/Filesystem/InputStreamWrapper.cpp b/src/System/Filesystem/InputStreamWrapper.cpp new file mode 100644 index 0000000..2bf7684 --- /dev/null +++ b/src/System/Filesystem/InputStreamWrapper.cpp @@ -0,0 +1,83 @@ +#include + +#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 (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. + diff --git a/src/System/Filesystem/InputStreamWrapper.h b/src/System/Filesystem/InputStreamWrapper.h new file mode 100644 index 0000000..d643a20 --- /dev/null +++ b/src/System/Filesystem/InputStreamWrapper.h @@ -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. +