Added fileInterface library

This commit is contained in:
Anonymous
2014-01-14 00:35:50 -07:00
parent cae5de9425
commit 1f331006e1
13 changed files with 531 additions and 0 deletions
@@ -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
+1
View File
@@ -1,3 +1,4 @@
add_subdirectory(archive)
add_subdirectory(fileInterface)
add_subdirectory(unicode)
+11
View File
@@ -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)
@@ -0,0 +1 @@
#include "../../src/shared/AbstractFile.h"
@@ -0,0 +1 @@
#include "../../src/shared/FirstFileInterface.h"
@@ -0,0 +1 @@
#include "../../src/shared/StdioFile.h"
+26
View File
@@ -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}
)
@@ -0,0 +1,92 @@
// ======================================================================
//
// AbstractFile.cpp
// copyright (c) 2001,2002 Sony Online Entertainment
//
// ======================================================================
#include "fileInterface/FirstFileInterface.h"
#include "fileInterface/AbstractFile.h"
#include <assert.h>
#include <cstdio>
// ======================================================================
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<void>(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;
}
// ======================================================================
@@ -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
@@ -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
@@ -0,0 +1,146 @@
// ======================================================================
//
// StdioFile.cpp
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#include "fileInterface/FirstFileInterface.h"
#include "fileInterface/StdioFile.h"
#include <cstdio>
#include <assert.h>
#include <string.h>
// ======================================================================
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<StdioFile*>(this)->seek (AbstractFile::SeekEnd, 0);
const int fileLength = tell ();
const_cast<StdioFile*>(this)->seek (AbstractFile::SeekBegin, oldPosition);
return fileLength;
}
// ----------------------------------------------------------------------
int StdioFile::tell() const
{
if (!m_file)
return 0;
return static_cast<int>(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<int>(fread(dest_buffer, 1, static_cast<unsigned int>(num_bytes), m_file));
}
// ----------------------------------------------------------------------
int StdioFile::write(int num_bytes, const void* source_buffer)
{
assert(m_file != NULL);
m_justWrote = true;
return static_cast<int>(fwrite(source_buffer, 1, static_cast<unsigned int>(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);
}
// ================================================================
@@ -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 <cstdio>
// ======================================================================
/**
* 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
@@ -0,0 +1 @@
#include "fileInterface/FirstFileInterface.h"