From 663a408ce24d699a85ea481032c983f5e5bfc9a1 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 6 Feb 2005 21:30:09 +0000 Subject: [PATCH] PhysFS initial integration --- devcpp/photon.dev | 36 ++++-- include/util/FileBuffer.h | 120 ++++++++++++++++++++ include/util/filesys/filesys.h | 193 +++++++++++++++++++++++++++++++++ src/util/FileBuffer.cpp | 121 +++++++++++++++++++++ src/util/filesys/filesys.cpp | 165 ++++++++++++++++++++++++++++ 5 files changed, 627 insertions(+), 8 deletions(-) create mode 100644 include/util/FileBuffer.h create mode 100644 include/util/filesys/filesys.h create mode 100644 src/util/FileBuffer.cpp create mode 100644 src/util/filesys/filesys.cpp diff --git a/devcpp/photon.dev b/devcpp/photon.dev index 3035889..4748da9 100644 --- a/devcpp/photon.dev +++ b/devcpp/photon.dev @@ -1,12 +1,12 @@ [Project] FileName=photon.dev Name=photon -UnitCount=13 +UnitCount=17 Type=2 Ver=1 ObjFiles= -Includes=../include -Libs= +Includes=../include;../external/include +Libs=../external/lib PrivateResource= ResourceIncludes= MakeIncludes= @@ -20,7 +20,7 @@ ObjectOutput=..\devcpp OverrideOutput=1 OverrideOutputName=libphoton.a HostApplication= -Folders=include,include/util,src,src/util +Folders=external,external/include,external/src,include,include/util,src,src/util CommandLine= UseCustomMakefile=0 CustomMakefile= @@ -158,9 +158,9 @@ ProductVersion= AutoIncBuildNr=0 [Unit14] -FileName=..\src\exceptions.cpp +FileName=..\include\util\FileBuffer.h CompileCpp=1 -Folder=src +Folder=include/util Compile=1 Link=1 Priority=1000 @@ -168,9 +168,9 @@ OverrideBuildCmd=0 BuildCmd= [Unit15] -FileName=..\include\exceptions.h +FileName=..\src\util\FileBuffer.cpp CompileCpp=1 -Folder=include +Folder=src/util Compile=1 Link=1 Priority=1000 @@ -197,3 +197,23 @@ Priority=1000 OverrideBuildCmd=0 BuildCmd= +[Unit17] +FileName=..\include\util\filesys\filesys.h +CompileCpp=1 +Folder=include/util +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + +[Unit16] +FileName=..\src\util\filesys\filesys.cpp +CompileCpp=1 +Folder=src/util +Compile=1 +Link=1 +Priority=1000 +OverrideBuildCmd=0 +BuildCmd= + diff --git a/include/util/FileBuffer.h b/include/util/FileBuffer.h new file mode 100644 index 0000000..5d433a8 --- /dev/null +++ b/include/util/FileBuffer.h @@ -0,0 +1,120 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: FileBuffer.h,v 1.1 2005/02/06 21:30:10 cozman Exp $ +// +// Revisions: +// $Log: FileBuffer.h,v $ +// Revision 1.1 2005/02/06 21:30:10 cozman +// PhysFS initial integration +// +// + +#ifndef PHOTON_UTIL_FILEBUFFER_H +#define PHOTON_UTIL_FILEBUFFER_H + +#include "types.h" + +#include "physfs.h" + +#include +#include + +namespace photon +{ +namespace util +{ + +// Class: FileBuffer +// Class for reading data from a file, uses PhysFS +// and is capable of reading from archives on the search path. +class FileBuffer +{ + +// Group: (Con/De)structors +public: + // Function: FileBuffer + // Initializes empty buffer. + FileBuffer(); + + // Function: FileBuffer + // Initializing constructor, calls . + // + // Parameters: + // filename - Name of file to load. + FileBuffer(std::string filename); + + // Function: ~FileBuffer + // Destructor, calls . + ~FileBuffer(); + + +// Group: General +public: + // Function: open + // Loads a file into the FileBuffer. + // + // Parameters: + // filename - Name of file to load. + void open(std::string filename); + + // Function: close + // Frees memory occupied by loaded data. + void close(); + +// Group: Accessors +public: + + // Function: getData + // Loads an amount of data, returns a pointer to the loaded data. + // If the requested amount of data wasn't available, returns only + // what could be loaded. + // + // Parameters: + // amount - maximum amount of data to load + // + // Returns: + // Vector containing loaded data, empty if nothing loaded. + std::vector getData(int amount); + + // Function: getPosition + // Gets position of internal cursor inside data. + // + // Returns: + // Position of cursor reading in data. + uint getPosition() const; + + // Function: getSize + // Gets size of data. + // + // Returns: + // Size of currently loaded data. + uint getSize() const; + + // Function: eof + // Checks if internal cursor is at end of file. + // + // Returns: + // True iff eof, false otherwise. + bool isEOF() const; + + // Function: isOpen + // Checks if file is open. + // + // Returns: + // True iff file is open, false otherwise. + bool isOpen() const; + +private: + // PHYSFS_file* for the buffer + PHYSFS_file* file_; +}; + +} +} + +#endif //PHOTON_UTIL_FILEBUFFER_H diff --git a/include/util/filesys/filesys.h b/include/util/filesys/filesys.h new file mode 100644 index 0000000..0d4ec63 --- /dev/null +++ b/include/util/filesys/filesys.h @@ -0,0 +1,193 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: filesys.h,v 1.1 2005/02/06 21:30:10 cozman Exp $ +// +// Revisions: +// $Log: filesys.h,v $ +// Revision 1.1 2005/02/06 21:30:10 cozman +// PhysFS initial integration +// +// + +#ifndef PHOTON_UTIL_FILESYS_FILESYS_H +#define PHOTON_UTIL_FILESYS_FILESYS_H + +#include "exceptions.h" + +#include "physfs.h" + +#include +#include + +// Title: File System + +namespace photon +{ +namespace util +{ +namespace filesys +{ + +// Function: getDirSeparator +// Gets the system standard directory separator. +// (/ on unix, \\ on windows, : on MacOS) +// +// Returns: +// System directory separator. +std::string getDirSeparator(); + +// Function: permitSymbolicLinks +// Enables or disables symbolic linking. (which is off by default) +// +// Parameters: +// allow - true if you wish to enable linking, false if you wish to disable it +void permitSymbolicLinks(bool allow); + + +// Group: System Directories /////////////////////////////////////////////////// + +// Function: getCDDirs +// Gets a listing of the CD directories on a system (not supported on all +// systems) +// +// Returns: +// A vector of strings containing the path to the CD directories. +std::vector getCDDirs(); + +// Function: getBaseDir +// Get the path to the directory that the application is running in. +// +// Returns: +// Path to directory that application is running from. +std::string getBaseDir(); + +// Function: getUserDir +// Get the path to the directory that the OS specifies for the user's home. +// +// Returns: +// Path to user's home directory. +std::string getUserDir(); + + +// Group: Search Path ////////////////////////////////////////////////////////// + +// Function: addToSearchPath +// Attempts to add a directory to the search path. +// +// Parameters: +// dir - Directory to add to the search path +// append - if true, directory will be added to end of path +// if false, directory will be added to front of path +// +// See Also: +// +// +void addToSearchPath(std::string dir, bool append); + +// Function: removeFromSearchPath +// Removes a directory from the search path, if it exists on the path. +// +// Parameters: +// dir - Directory to remove from the search path, if it doesn't exist +// nothing happens. +// +// See Also: +// +// +void removeFromSearchPath(std::string dir); + +// Function: getSearchPath +// Obtain the currently configured search path. +// +// Returns: +// List of strings in search path. +// +// See Also: +// +// +std::vector getSearchPath(); + +// Group: Manipulation ///////////////////////////////////////////////////////// + +// Function: mkdir +// Attempts to create a directory. +// +// Parameters: +// dir - name of directory to create +// +// Returns: +// true iff directory was created, false if not +bool mkdir(std::string dir); + +// Function: remove +// Attempts to remove a file or directory. +// +// Parameters: +// remove - name of file or directory to remove +// +// Returns: +// true iff file/directory was removed, false if not +bool remove(std::string item); + +// Group: Searching //////////////////////////////////////////////////////////// + +// Function: listDir +// Lists the contents of a directory. +// +// Parameters: +// dir - name of directory to get contents of +// +// Returns: +// list of strings representing items found in 'dir' +std::vector listDir(std::string dir); + +// Function: exists +// Checks if a file/directory exists. +// +// Parameters: +// item - file/directory to check existance of +// +// Returns: +// true iff file/directory exists, false if not +bool exists(std::string item); + +// Function: isDirectory +// Checks if a name refers to a directory. +// +// Parameters: +// item - name to check +// +// Returns: +// true iff item is a directory, false if not +bool isDirectory(std::string item); + +// Function: isSymbolicLink +// Checks if a name refers to a symbolic link. +// +// Parameters: +// item - name to check +// +// Returns: +// true iff item is a symbolic link, false if not +bool isSymbolicLink(std::string item); + +// Function: getModTime +// Gets last modification time for a file. +// +// Parameters: +// item - name of item to get last modification time of +// +// Returns: +// Last modification time of a file in seconds since the epoch. +PHYSFS_sint64 getModTime(std::string item); + +} +} +} + +#endif //PHOTON_UTIL_FILESYS_FILESYS_H diff --git a/src/util/FileBuffer.cpp b/src/util/FileBuffer.cpp new file mode 100644 index 0000000..4aa0fd6 --- /dev/null +++ b/src/util/FileBuffer.cpp @@ -0,0 +1,121 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: FileBuffer.cpp,v 1.1 2005/02/06 21:30:10 cozman Exp $ +// +// Revisions: +// $Log: FileBuffer.cpp,v $ +// Revision 1.1 2005/02/06 21:30:10 cozman +// PhysFS initial integration +// +// + +#include "util/FileBuffer.h" + +#include "exceptions.h" + +namespace photon +{ +namespace util +{ + +FileBuffer::FileBuffer() : + file_(0) +{} + + +FileBuffer::FileBuffer(std::string filename) : + file_( PHYSFS_openRead(filename.c_str()) ) +{} + +FileBuffer::~FileBuffer() +{} + +void FileBuffer::open(std::string filename) +{ + file_ = PHYSFS_openRead(filename.c_str()); +} + +void FileBuffer::close() +{ + if(file_ == 0) + { + throw PreconditionException("No file open in FileBuffer::close"); + } + + PHYSFS_close(file_); +} + +std::vector FileBuffer::getData(int amount) +{ + if(file_ == 0) + { + throw PreconditionException("No file open in FileBuffer::getData"); + } + + std::vector buffer(amount); //create buffer + + // try to read 'amount' bytes into buffer + PHYSFS_sint64 bytesRead = PHYSFS_read(file_, &buffer[0], 1, amount); + + // if -1 is returned (an error) or fewer bytes were read than asked but + // eof hasn't been reached, PhysFS encountered an error + if( bytesRead == -1 || (bytesRead < amount && PHYSFS_eof(file_) == 0) ) + { + throw APIError(std::string("PhysFS failure in FileBuffer::getData (") + + PHYSFS_getLastError() + ")"); + } + + buffer.resize(bytesRead); //shrink to size()==bytesRead + + return buffer; +} + +uint FileBuffer::getPosition() const +{ + if(file_ == 0) + { + throw PreconditionException("No file open in FileBuffer::getPosition"); + } + + PHYSFS_sint64 pos = PHYSFS_tell(file_); + + if(pos == -1) + { + throw APIError( + std::string("PhysFS failure in FileBuffer::getPosition (") + + PHYSFS_getLastError() + ")"); + } + + return pos; +} + +uint FileBuffer::getSize() const +{ + if(file_ == 0) + { + throw PreconditionException("No file open in FileBuffer::getSize"); + } + + PHYSFS_sint64 size = PHYSFS_fileLength(file_); + + if(size == -1) + { + throw APIError(std::string("PhysFS failure in FileBuffer::getSize (") + + PHYSFS_getLastError() + ")"); + } + + return size; +} + +bool FileBuffer::isOpen() const +{ + return file_ != 0; +} + +} +} diff --git a/src/util/filesys/filesys.cpp b/src/util/filesys/filesys.cpp new file mode 100644 index 0000000..c330b79 --- /dev/null +++ b/src/util/filesys/filesys.cpp @@ -0,0 +1,165 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: filesys.cpp,v 1.1 2005/02/06 21:30:10 cozman Exp $ +// +// Revisions: +// $Log: filesys.cpp,v $ +// Revision 1.1 2005/02/06 21:30:10 cozman +// PhysFS initial integration +// +// + + +#include "util/filesys/filesys.h" + +namespace photon +{ +namespace util +{ +namespace filesys +{ + +std::string getDirSeparator() +{ + return PHYSFS_getDirSeparator(); +} + +void permitSymbolicLinks(bool allow) +{ + PHYSFS_permitSymbolicLinks(allow); +} + +std::vector getCDDirs() +{ + std::vector dirs; + char** buf( PHYSFS_getCdRomDirs() ); + + if(buf == 0) + { + throw APIError(std::string("getCDDirs failed (") + + PHYSFS_getLastError() + ")"); + } + + //iterate over list, adding dirs + for(char** i(buf); *i != 0; ++i) + { + dirs.push_back(*i); + } + + PHYSFS_freeList(buf); //free the memory + + return dirs; +} + +std::string getBaseDir() +{ + return PHYSFS_getBaseDir(); +} + +std::string getUserDir() +{ + return PHYSFS_getUserDir(); +} + +void addToSearchPath(std::string dir, bool append) +{ + //only attempt if dir exists + if(exists(dir)) + { + int success = PHYSFS_addToSearchPath(dir.c_str(), append); + if(!success) + { + throw APIError(std::string("addToSearchPath failed (") + + PHYSFS_getLastError() + ")"); + } + } +} + +void removeFromSearchPath(std::string dir) +{ + //ignore return value (useless) + PHYSFS_removeFromSearchPath(dir.c_str()); +} + +std::vector getSearchPath() +{ + std::vector dirs; + char** buf( PHYSFS_getSearchPath() ); + + if(buf == 0) + { + throw APIError(std::string("getSearchPath failed (") + + PHYSFS_getLastError() + ")"); + } + + //iterate over list, adding dirs + for(char** i(buf); *i != 0; ++i) + { + dirs.push_back(*i); + } + + PHYSFS_freeList(buf); //free the memory + + return dirs; +} + +bool mkdir(std::string dir) +{ + return PHYSFS_mkdir(dir.c_str()) != 0; +} + +bool remove(std::string item) +{ + return PHYSFS_delete(item.c_str()) != 0; +} + +std::vector listDir(std::string dir) +{ + std::vector files; + char** buf( PHYSFS_enumerateFiles(dir.c_str()) ); + + if(buf == 0) + { + throw APIError(std::string("listDir failed (") + + PHYSFS_getLastError() + ")"); + } + + //iterate over list, adding dirs + for(char** i(buf); *i != 0; ++i) + { + files.push_back(*i); + } + + PHYSFS_freeList(buf); //free the memory + + return files; +} + +bool exists(std::string item) +{ + return PHYSFS_exists(item.c_str()) != 0; +} + +bool isDirectory(std::string item) +{ + return PHYSFS_isDirectory(item.c_str()) != 0; +} + +bool isSymbolicLink(std::string item) +{ + return PHYSFS_isSymbolicLink(item.c_str()) != 0; +} + +PHYSFS_sint64 getModTime(std::string item) +{ + return PHYSFS_getLastModTime(item.c_str()); +} + +} +} +}