diff --git a/engine/shared/library/sharedFile/src/CMakeLists.txt b/engine/shared/library/sharedFile/src/CMakeLists.txt index 042e7d78..b3ad05f4 100644 --- a/engine/shared/library/sharedFile/src/CMakeLists.txt +++ b/engine/shared/library/sharedFile/src/CMakeLists.txt @@ -52,6 +52,7 @@ include_directories( ${SWG_ENGINE_SOURCE_DIR}/shared/library/sharedFoundation/include/public ${SWG_ENGINE_SOURCE_DIR}/shared/library/sharedFoundationTypes/include/public ${SWG_ENGINE_SOURCE_DIR}/shared/library/sharedMemoryManager/include/public + ${SWG_EXTERNALS_SOURCE_DIR}/ours/library/fileInterface/include/public ) add_library(sharedFile STATIC diff --git a/external/ours/library/CMakeLists.txt b/external/ours/library/CMakeLists.txt index d24bef6d..29b88ce8 100644 --- a/external/ours/library/CMakeLists.txt +++ b/external/ours/library/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(archive) +add_subdirectory(fileInterface) add_subdirectory(unicode) diff --git a/external/ours/library/fileInterface/CMakeLists.txt b/external/ours/library/fileInterface/CMakeLists.txt new file mode 100644 index 00000000..01d1afd6 --- /dev/null +++ b/external/ours/library/fileInterface/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 2.8) + +project(fileInterface) + +if(WIN32) + add_definitions(/D_CRT_SECURE_NO_WARNINGS) +endif() + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/public) + +add_subdirectory(src) diff --git a/external/ours/library/fileInterface/include/public/fileInterface/AbstractFile.h b/external/ours/library/fileInterface/include/public/fileInterface/AbstractFile.h new file mode 100644 index 00000000..75fbbc29 --- /dev/null +++ b/external/ours/library/fileInterface/include/public/fileInterface/AbstractFile.h @@ -0,0 +1 @@ +#include "../../src/shared/AbstractFile.h" diff --git a/external/ours/library/fileInterface/include/public/fileInterface/FirstFileInterface.h b/external/ours/library/fileInterface/include/public/fileInterface/FirstFileInterface.h new file mode 100644 index 00000000..2a916984 --- /dev/null +++ b/external/ours/library/fileInterface/include/public/fileInterface/FirstFileInterface.h @@ -0,0 +1 @@ +#include "../../src/shared/FirstFileInterface.h" diff --git a/external/ours/library/fileInterface/include/public/fileInterface/StdioFile.h b/external/ours/library/fileInterface/include/public/fileInterface/StdioFile.h new file mode 100644 index 00000000..f7a2072b --- /dev/null +++ b/external/ours/library/fileInterface/include/public/fileInterface/StdioFile.h @@ -0,0 +1 @@ +#include "../../src/shared/StdioFile.h" diff --git a/external/ours/library/fileInterface/src/CMakeLists.txt b/external/ours/library/fileInterface/src/CMakeLists.txt new file mode 100644 index 00000000..b2ef51d5 --- /dev/null +++ b/external/ours/library/fileInterface/src/CMakeLists.txt @@ -0,0 +1,26 @@ + + +set(SHARED_SOURCES + shared/AbstractFile.cpp + shared/AbstractFile.h + shared/FirstFileInterface.h + shared/StdioFile.cpp + shared/StdioFile.h +) + +if(WIN32) + set(PLATFORM_SOURCES + win32/FirstFileInterface.cpp + ) + + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/win32) +else() + set(PLATFORM_SOURCES "") +endif() + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/shared) + +add_library(fileInterface + ${SHARED_SOURCES} + ${PLATFORM_SOURCES} +) diff --git a/external/ours/library/fileInterface/src/shared/AbstractFile.cpp b/external/ours/library/fileInterface/src/shared/AbstractFile.cpp new file mode 100644 index 00000000..dd575c36 --- /dev/null +++ b/external/ours/library/fileInterface/src/shared/AbstractFile.cpp @@ -0,0 +1,92 @@ +// ====================================================================== +// +// AbstractFile.cpp +// copyright (c) 2001,2002 Sony Online Entertainment +// +// ====================================================================== + +#include "fileInterface/FirstFileInterface.h" +#include "fileInterface/AbstractFile.h" + +#include +#include + +// ====================================================================== + +namespace AbstractFileNamespace +{ + AbstractFile::AudioServeFunction s_audioServeFunction = NULL; +} + +using namespace AbstractFileNamespace; + +// ---------------------------------------------------------------------- + +void AbstractFile::setAudioServe(AudioServeFunction audioServeFunction) +{ + s_audioServeFunction = audioServeFunction; +} + +// ====================================================================== + +AbstractFile::AbstractFile(PriorityType priority) +: + m_priority(priority) +{ +} + +// ---------------------------------------------------------------------- + +AbstractFile::~AbstractFile() +{ +} + +// ---------------------------------------------------------------------- + +void AbstractFile::flush() +{ +} + +// ---------------------------------------------------------------------- + +byte *AbstractFile::readEntireFileAndClose() +{ + if (s_audioServeFunction != NULL) + (*s_audioServeFunction)(); + + seek(SeekBegin, 0); + + const int fileLength = length(); + byte *buffer = new byte[fileLength]; + const int bytesRead = read(buffer, fileLength); + static_cast(bytesRead); + assert(bytesRead == fileLength); + close(); + + return buffer; +} + +// ---------------------------------------------------------------------- + +bool AbstractFile::isZlibCompressed() const +{ + return false; +} + +// ---------------------------------------------------------------------- + +int AbstractFile::getZlibCompressedLength() const +{ + return -1; +} + +// ---------------------------------------------------------------------- + +void AbstractFile::getZlibCompressedDataAndClose(byte *& compressedBuffer, int & compressedBufferLength) +{ + compressedBuffer = NULL; + compressedBufferLength = -1; +} + +// ====================================================================== + diff --git a/external/ours/library/fileInterface/src/shared/AbstractFile.h b/external/ours/library/fileInterface/src/shared/AbstractFile.h new file mode 100644 index 00000000..b2e9d9cd --- /dev/null +++ b/external/ours/library/fileInterface/src/shared/AbstractFile.h @@ -0,0 +1,155 @@ +// ====================================================================== +// +// AbstractFile.h +// copyright (c) 2001,2002 Sony Online Entertainment +// +// ====================================================================== + +#ifndef INCLUDED_AbstractFile_H +#define INCLUDED_AbstractFile_H + +// ====================================================================== + +/** + * This class represents the abstract interface for a file. It is a pure virtual class and must be + * inherited from. + */ + +class AbstractFile +{ +public: + + typedef void (*AudioServeFunction) (); + static void setAudioServe(AudioServeFunction audioServeFunction); + + /** + * An enum used to specify the vaarious seek origins. + */ + enum SeekType + { + SeekBegin, + SeekCurrent, + SeekEnd + }; + + /** + * An enum used to specify how time-sensitive the data begin read is. + */ + enum PriorityType + { + PriorityLow, + PriorityData, + PriorityAudioVideo + }; + +public: + + AbstractFile(PriorityType priority); + virtual ~AbstractFile(); + + /** + * Is the file open + * @return true if this object is holding an open file, else false + */ + virtual bool isOpen() const = 0; + + /** + * Retrieve the length of the current file. Returns 0 if there is no open file. + * + * @return the length of the file + */ + virtual int length() const = 0; + + /** + * Retrieve the current file position. Does nothing if there is no open file. + * + * @return the current file position + */ + virtual int tell() const = 0; + + /** + * Move the file pointer to a new location. Does nothing if there is no open file + * + * @param seekType where the origin of the seek is, i.e. the beginning, end, or current file position + * @param offset the offset from the origin to move + * @return true on successful movement of the file pointer, else false + * @see AbstractFile::SeekType + */ + virtual bool seek(SeekType seekType, int offset) = 0; + + /** + * Read data from a file. Does nothing if there is no open file. + * @param num_bytes the number of bytes to attempt to read + * @param dest_buffer the memory location to store the read data, must be preallocated + * @return the number of bytes read + */ + virtual int read(void *destinationBuffer, int numberOfBytes) = 0; + + /** + * Write data to a file. Does nothing if there is no open file. + * + * @param numberOfBytes the number of bytes to attempt to write + * @param source_buffer the memory location to use to write data from + * @return the number of bytes written + */ + virtual int write(int numberOfBytes, const void *sourceBuffer) = 0; + + /** + * Flush pending write data to the file. This is simply a hint, and + * may not force a flush depending on the particular file type's + * implementation. + */ + virtual void flush(); + + /** + * Close an open file. Returns trivially if there is no currently open file. + * This allows the user to manually close the file when they can't control the scope + * of the AbstractFile-derived object. + */ + virtual void close() = 0; + + /** + * Read the entire file into a memory buffer. The client is responsible for deleting the buffer + * using operator delete(). The file will be closed after the read completes. + * + * @return null if an error occured + */ + virtual unsigned char *readEntireFileAndClose(); + + virtual bool isZlibCompressed() const; + virtual int getZlibCompressedLength() const; + virtual void getZlibCompressedDataAndClose(unsigned char * & compressedBuffer, int & compressedBufferLength); + +private: + + /** + * Disabled. + */ + AbstractFile(); + + /** + * Disabled. + */ + AbstractFile(const AbstractFile &); + + /** + * Disabled. + */ + AbstractFile &operator =(const AbstractFile &); + +protected: + + const PriorityType m_priority; +}; + +// ====================================================================== + +class AbstractFileFactory +{ +public: + virtual AbstractFile* createFile(const char *filename, const char *openType) = 0; +}; + +// ====================================================================== + +#endif diff --git a/external/ours/library/fileInterface/src/shared/FirstFileInterface.h b/external/ours/library/fileInterface/src/shared/FirstFileInterface.h new file mode 100644 index 00000000..2bbf038c --- /dev/null +++ b/external/ours/library/fileInterface/src/shared/FirstFileInterface.h @@ -0,0 +1,30 @@ +// ====================================================================== +// +// FirstFileInterface.h +// Copyright 2002, Sony Online Entertainment Inc. +// All Rights Reserved. +// +// ====================================================================== + +#ifndef INCLUDED_FirstFileInterface_H +#define INCLUDED_FirstFileInterface_H + +// ====================================================================== + +typedef unsigned char byte; + +#ifdef _WIN32 + +#pragma warning(disable: 4291 4514 4702 4710 4786) + +void * __cdecl operator new(size_t size); +void * __cdecl operator new[](size_t size); + +void operator delete(void *pointer); +void operator delete[](void *pointer); + +#endif + +// ====================================================================== + +#endif diff --git a/external/ours/library/fileInterface/src/shared/StdioFile.cpp b/external/ours/library/fileInterface/src/shared/StdioFile.cpp new file mode 100644 index 00000000..53acb5ce --- /dev/null +++ b/external/ours/library/fileInterface/src/shared/StdioFile.cpp @@ -0,0 +1,146 @@ +// ====================================================================== +// +// StdioFile.cpp +// copyright (c) 2001 Sony Online Entertainment +// +// ====================================================================== + +#include "fileInterface/FirstFileInterface.h" +#include "fileInterface/StdioFile.h" + +#include +#include +#include + +// ====================================================================== + +StdioFile::StdioFile(const char *fileName, const char *openType) +: AbstractFile(AbstractFile::PriorityData), + m_file(fopen(fileName, openType)), + m_justWrote(false) +{ +} + +// ---------------------------------------------------------------------- + +StdioFile::~StdioFile() +{ + close(); +} + +// ---------------------------------------------------------------------- + +void StdioFile::close() +{ + if(m_file) + { + fclose(m_file); + m_file = NULL; + } +} + +// ---------------------------------------------------------------------- + +bool StdioFile::isOpen() const +{ + return m_file != NULL; +} + +// ---------------------------------------------------------------------- +// This routine is not exactly bitwise const but is sematically const in the end +int StdioFile::length() const +{ + const int oldPosition = tell (); + const_cast(this)->seek (AbstractFile::SeekEnd, 0); + const int fileLength = tell (); + const_cast(this)->seek (AbstractFile::SeekBegin, oldPosition); + return fileLength; +} + +// ---------------------------------------------------------------------- + +int StdioFile::tell() const +{ + if (!m_file) + return 0; + + return static_cast(ftell(m_file)); +} + +// ---------------------------------------------------------------------- + +bool StdioFile::seek(SeekType seekType, int offset) +{ + assert(m_file != NULL); + + m_justWrote = false; + int result = 0; + switch(seekType) + { + case SeekBegin: + result = fseek(m_file, offset, SEEK_SET); + break; + + case SeekCurrent: + result = fseek(m_file, offset, SEEK_CUR); + break; + + case SeekEnd: + result = fseek(m_file, offset, SEEK_END); + break; + } + + return (result == 0); +} + +// ---------------------------------------------------------------------- + +int StdioFile::read(void* dest_buffer, int num_bytes) +{ + assert(m_file != NULL); + resyncStream(); + return static_cast(fread(dest_buffer, 1, static_cast(num_bytes), m_file)); +} + +// ---------------------------------------------------------------------- + +int StdioFile::write(int num_bytes, const void* source_buffer) +{ + assert(m_file != NULL); + m_justWrote = true; + return static_cast(fwrite(source_buffer, 1, static_cast(num_bytes), m_file)); +} + +// ---------------------------------------------------------------------- + +void StdioFile::flush() +{ + assert(m_file != NULL); + fflush(m_file); + m_justWrote = false; +} + +// ---------------------------------------------------------------------- + +void StdioFile::resyncStream() +{ + if (m_justWrote) + { + fseek(m_file, 0, SEEK_CUR); + m_justWrote = false; + } +} + +// ================================================================ + +AbstractFile* StdioFileFactory::createFile(const char *fileName, const char *openType) +{ + if(!fileName || !openType) + return NULL; + else if(strlen(fileName) == 0 || strlen(openType) == 0) + return NULL; + else + return new StdioFile(fileName, openType); +} + +// ================================================================ diff --git a/external/ours/library/fileInterface/src/shared/StdioFile.h b/external/ours/library/fileInterface/src/shared/StdioFile.h new file mode 100644 index 00000000..905ab15f --- /dev/null +++ b/external/ours/library/fileInterface/src/shared/StdioFile.h @@ -0,0 +1,65 @@ +// ====================================================================== +// +// StdioFile.h +// copyright (c) 2001 Sony Online Entertainment +// +// ====================================================================== + +#ifndef INCLUDED_StdioFile_H +#define INCLUDED_StdioFile_H + +#include "fileInterface/AbstractFile.h" + +#include + +// ====================================================================== +/** + * Implements access to stdio functions using the AbstractFile interface. + * Instances of this class are always created using the StdioFileFactory and + * always referenced through an AbstractFile pointer, to maintain the AbstractFactory pattern + * (see "Design Patterns"). + * @see AbstractFile + */ +class StdioFile : public AbstractFile +{ +public: + StdioFile(const char *filename, const char *open_type); + virtual ~StdioFile(); + + virtual void close(); + + virtual bool isOpen() const; + virtual int length() const; + virtual int tell() const; + + virtual bool seek(SeekType seekType, int offset); + virtual int read(void* dest_buffer, int num_bytes); + virtual int write(int num_bytes, const void* source_buffer); + virtual void flush(); + +private: + ///disabled + StdioFile(); + StdioFile(const StdioFile& copy); + StdioFile &operator =(const StdioFile& rhs); + +private: + ///pointer to the stdio file + FILE *m_file; + ///indicates that we need to resync the stream before reading using resyncStream + bool m_justWrote; + + void resyncStream(); +}; + +// ====================================================================== + +class StdioFileFactory : public AbstractFileFactory +{ +public: + virtual AbstractFile* createFile(const char *filename, const char *openType); +}; + +// ====================================================================== + +#endif diff --git a/external/ours/library/fileInterface/src/win32/FirstFileInterface.cpp b/external/ours/library/fileInterface/src/win32/FirstFileInterface.cpp new file mode 100644 index 00000000..33fdbcbc --- /dev/null +++ b/external/ours/library/fileInterface/src/win32/FirstFileInterface.cpp @@ -0,0 +1 @@ +#include "fileInterface/FirstFileInterface.h"