PhysFS initial integration

This commit is contained in:
James Turk 2005-02-06 21:30:09 +00:00
parent 33e2c3a5d5
commit 663a408ce2
5 changed files with 627 additions and 8 deletions

View File

@ -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=

120
include/util/FileBuffer.h Normal file
View File

@ -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 <string>
#include <vector>
namespace photon
{
namespace util
{
// Class: FileBuffer
// Class for reading data from a file, uses PhysFS <http://physfs.icculus.org>
// 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 <open>.
//
// Parameters:
// filename - Name of file to load.
FileBuffer(std::string filename);
// Function: ~FileBuffer
// Destructor, calls <close>.
~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<ubyte> 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

View File

@ -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 <string>
#include <vector>
// 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<std::string> 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:
// <removeFromSearchPath>
// <getSearchPath>
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:
// <addToSearchPath>
// <getSearchPath>
void removeFromSearchPath(std::string dir);
// Function: getSearchPath
// Obtain the currently configured search path.
//
// Returns:
// List of strings in search path.
//
// See Also:
// <addToSearchPath>
// <removeFromSearchPath>
std::vector<std::string> 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<std::string> 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

121
src/util/FileBuffer.cpp Normal file
View File

@ -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<ubyte> FileBuffer::getData(int amount)
{
if(file_ == 0)
{
throw PreconditionException("No file open in FileBuffer::getData");
}
std::vector<ubyte> 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;
}
}
}

View File

@ -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<std::string> getCDDirs()
{
std::vector<std::string> 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<std::string> getSearchPath()
{
std::vector<std::string> 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<std::string> listDir(std::string dir)
{
std::vector<std::string> 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());
}
}
}
}