Jopnal Engine  alpha 0.4
Simple Component Based 2D/3D Game Engine
Public Types | Public Member Functions | Static Public Member Functions | List of all members
jop::FileLoader Class Reference

#include <FileLoader.hpp>

Public Types

enum  Directory { Directory::Executable, Directory::Resource, Directory::User }
 

Public Member Functions

 FileLoader ()
 Default constructor. More...
 
 FileLoader (const std::string &path)
 Overloaded constructor. More...
 
 FileLoader (const Directory dir, const std::string &path, const bool append)
 Overloaded constructor. More...
 
 FileLoader (FileLoader &&other)
 Move constructor. More...
 
FileLoaderoperator= (FileLoader &&other)
 Move assignment operator. More...
 
 ~FileLoader ()
 Destructor. More...
 
bool open (const std::string &path)
 Open a file for reading. More...
 
bool open (const Directory dir, const std::string &path, const bool append)
 Open a file for writing. More...
 
void flush ()
 Flush the file stream. More...
 
void close ()
 Close the file handle. More...
 
bool isValid () const
 Check if a file handle is open. More...
 
int64 read (void *data, const uint64 size)
 Read data. More...
 
int64 write (const void *data, const uint64 size)
 Write data. More...
 
bool seek (const uint64 position)
 Move the file cursor to the given position. More...
 
int64 tell () const
 Get the current position of the cursor. More...
 
int64 getSize () const
 Get the size of the opened file. More...
 
 operator bool () const
 Check if a file handle is open. More...
 

Static Public Member Functions

static bool fileExists (const std::string &path)
 Check if a file exists. More...
 
static void listFiles (const std::string &path, std::vector< std::string > &list)
 Enumerate all files within a directory. More...
 
static void listFilesRecursive (const std::string &path, std::vector< std::string > &list)
 Enumerate all files within a directory recursively. More...
 
static bool deleteFile (const Directory dir, const std::string &file)
 Delete a file. More...
 
static bool readTextfile (const std::string &path, std::string &file)
 Read a text file. More...
 
static bool readBinaryfile (const std::string &path, std::vector< uint8 > &buffer)
 Read a binary file. More...
 
static bool writeTextfile (const Directory dir, const std::string &path, const std::string &text, const bool append=false)
 Write a text file. More...
 
static bool writeBinaryfile (const Directory dir, const std::string &path, const void *data, const std::size_t bytes, const bool append=false)
 Write a binary file. More...
 
static bool makeDirectory (const Directory dir, const std::string &path)
 Create a directory. More...
 
static const std::string & getDirectory (const Directory dir)
 Get a base directory as string. More...
 
static char getDirectorySeparator ()
 Get the OS-specific directory separator. More...
 
static void enableErrorChecks (const bool enable)
 Enable/disable file system error checks. More...
 
static bool errorChecksEnabled ()
 Check if file system error checks are enabled. More...
 

Detailed Description

Definition at line 55 of file FileLoader.hpp.

Member Enumeration Documentation

Base directory for writing files

Enumerator
Executable 

Executable directory.

Resource 

Resource folder. On Android this is the memory card directory.

User 

User folder. On Android this is the same directory as Executable.

Definition at line 65 of file FileLoader.hpp.

Constructor & Destructor Documentation

jop::FileLoader::FileLoader ( )

Default constructor.

Doesn't initialize any file handles.

jop::FileLoader::FileLoader ( const std::string &  path)
explicit

Overloaded constructor.

This will open the file for reading if found

Parameters
pathPath to file to open
See also
isValid()
jop::FileLoader::FileLoader ( const Directory  dir,
const std::string &  path,
const bool  append 
)

Overloaded constructor.

This will open the file for writing if found.

Parameters
dirBase write directory
pathPath to file to open
appendAppend to the file. False to clear the file before writing
See also
isValid()
jop::FileLoader::FileLoader ( FileLoader &&  other)

Move constructor.

jop::FileLoader::~FileLoader ( )

Destructor.

Will close the file handle if open.

Member Function Documentation

void jop::FileLoader::close ( )

Close the file handle.

If no file handle is open, this will have no effect. When writing, calling this means saving the file.

static bool jop::FileLoader::deleteFile ( const Directory  dir,
const std::string &  file 
)
static

Delete a file.

Parameters
dirThe base write directory
filePath to the file
Returns
True if file was successfully deleted
static void jop::FileLoader::enableErrorChecks ( const bool  enable)
static

Enable/disable file system error checks.

Parameters
enableTrue to enable
static bool jop::FileLoader::errorChecksEnabled ( )
static

Check if file system error checks are enabled.

Returns
True if enabled
static bool jop::FileLoader::fileExists ( const std::string &  path)
static

Check if a file exists.

Parameters
pathPath to the file to check
Returns
True if the file exists
void jop::FileLoader::flush ( )

Flush the file stream.

This function only has an effect when writing to a file. Once called all buffered file output will be flushed and written to disk.

If no file handle is open, this will have no effect.

static const std::string& jop::FileLoader::getDirectory ( const Directory  dir)
static

Get a base directory as string.

Parameters
dirThe base directory
Returns
Internal reference to the directory string
static char jop::FileLoader::getDirectorySeparator ( )
static

Get the OS-specific directory separator.

Returns
The directory separator
int64 jop::FileLoader::getSize ( ) const

Get the size of the opened file.

Returns
Size of the file
bool jop::FileLoader::isValid ( ) const

Check if a file handle is open.

Returns
True if a valid file handle exists
static void jop::FileLoader::listFiles ( const std::string &  path,
std::vector< std::string > &  list 
)
static

Enumerate all files within a directory.

Parameters
pathPath to a directory
listReference to a list to fill with the file paths found
See also
listFilesRecursive
static void jop::FileLoader::listFilesRecursive ( const std::string &  path,
std::vector< std::string > &  list 
)
static

Enumerate all files within a directory recursively.

Warning
On Android, files cannot be enumerated recursively from the .apk.
Parameters
pathPath to a directory
listReference to a list to fill with the file paths found
See also
listFiles
static bool jop::FileLoader::makeDirectory ( const Directory  dir,
const std::string &  path 
)
static

Create a directory.

If the directory already exists, this has no effect.

Parameters
dirThe base write directory
pathThe directory to create
Returns
True if successful
bool jop::FileLoader::open ( const std::string &  path)

Open a file for reading.

Parameters
pathPath to the file
Returns
True if opened successfully
bool jop::FileLoader::open ( const Directory  dir,
const std::string &  path,
const bool  append 
)

Open a file for writing.

This will open the file for writing if found.

Parameters
dirBase write directory
pathPath to file to open
appendAppend to the file. False to clear the file before writing
See also
isValid()
Returns
True if opened successfully
jop::FileLoader::operator bool ( ) const

Check if a file handle is open.

Returns
True if a valid file handle exists
FileLoader& jop::FileLoader::operator= ( FileLoader &&  other)

Move assignment operator.

Returns
Reference to self
int64 jop::FileLoader::read ( void *  data,
const uint64  size 
)

Read data.

Parameters
dataPointer to a pre-allocated data buffer
sizeAmount of data to read
Returns
Amount of data read in bytes
See also
getSize()
static bool jop::FileLoader::readBinaryfile ( const std::string &  path,
std::vector< uint8 > &  buffer 
)
static

Read a binary file.

Parameters
pathPath to the file to read
bufferReference to a buffer to fill with the data
Returns
True if successful
static bool jop::FileLoader::readTextfile ( const std::string &  path,
std::string &  file 
)
static

Read a text file.

Parameters
pathPath to the file to read
fileReference to a string to fill with the data
Returns
True if successful
bool jop::FileLoader::seek ( const uint64  position)

Move the file cursor to the given position.

Parameters
positionThe cursor position to set in bytes
Returns
True if successful
int64 jop::FileLoader::tell ( ) const

Get the current position of the cursor.

Returns
Current position of the file read/write cursor. -1 in case of failure
int64 jop::FileLoader::write ( const void *  data,
const uint64  size 
)

Write data.

Parameters
dataData to write
sizeAmount of data to write in bytes
Returns
Amount of data written in bytes
static bool jop::FileLoader::writeBinaryfile ( const Directory  dir,
const std::string &  path,
const void *  data,
const std::size_t  bytes,
const bool  append = false 
)
static

Write a binary file.

Parameters
dirThe base write directory
pathThe file path
dataThe binary data to write
bytesamount of bytes to write
appendAppend to file?
Returns
True if successful
static bool jop::FileLoader::writeTextfile ( const Directory  dir,
const std::string &  path,
const std::string &  text,
const bool  append = false 
)
static

Write a text file.

Parameters
dirThe base write directory
pathThe file path
textThe text to write
appendAppend to file?
Returns
True if successful

Member Data Documentation

AAsset* jop::FileLoader::m_asset

Android asset handle.

Definition at line 321 of file FileLoader.hpp.

PHYSFS_File* jop::FileLoader::m_file

File handle.

Definition at line 320 of file FileLoader.hpp.


The documentation for this class was generated from the following file: