Gorgon Game Engine
Gorgon::Filesystem Namespace Reference

Contains filesystem functions. More...

Namespaces

 internal
 

Classes

class  EntryPoint
 This class represents filesystem entry points (roots, drives). More...
 
class  Iterator
 This iterator allows iteration of directories. More...
 
class  PathNotFoundError
 This object is thrown from functions that return information rather than status. More...
 

Functions

std::string Canonical (const std::string &path)
 Canonicalizes a given relative path. More...
 
bool ChangeDirectory (const std::string &path)
 Changes current working directory. More...
 
template<template< class > class C_>
bool Copy (const C_< std::string > &source, const std::string &target)
 Copies list of files and/or directories from the given source to destination. More...
 
template<template< class, class > class C_, class A_ >
bool Copy (const C_< std::string, A_ > &source, const std::string &target)
 Copies list of files and/or directories from the given source to destination. More...
 
template<class I_ >
bool Copy (const I_ &begin, const I_ &end, const std::string &target)
 Copies list of files and/or directories from the given source to destination. More...
 
bool Copy (const std::string &source, const std::string &target)
 Copies a file or directory from the given source to destination. More...
 
template<template< class > class C_>
bool Copy (const std::string &sourcedir, const C_< std::string > &source, const std::string &target)
 Copies list of files and/or directories from the given source to destination. More...
 
template<class I_ >
bool Copy (const std::string &sourcedir, const I_ &begin, const I_ &end, const std::string &target)
 Copies list of files and/or directories from the given source to destination. More...
 
bool CreateDirectory (const std::string &path)
 Creates a new directory. More...
 
std::string CurrentDirectory ()
 Returns the current working directory. More...
 
bool Delete (const std::string &path)
 Deletes the given file or directory. More...
 
std::vector< EntryPointEntryPoints ()
 This function returns all entry points in the current system. More...
 
std::string ExeDirectory ()
 Returns the directory where the program resides. More...
 
std::string ExePath ()
 Returns the the full path of the application. More...
 
std::string GetBasename (std::string path)
 Returns the filename from the given path, without extension. More...
 
std::string GetDirectory (std::string filepath)
 Returns the directory portion of a file path. More...
 
std::string GetExtension (std::string path)
 Returns the extension of the given path, also converts the extension to lower case. More...
 
std::string GetFilename (std::string path)
 Returns the filename portion of a file path. More...
 
void Initialize ()
 Initializes the filesystem module. More...
 
bool IsDirectory (const std::string &path)
 Checks whether the given path is a directory. More...
 
bool IsExists (const std::string &path)
 Checks whether the given path exists. More...
 
bool IsFile (const std::string &path)
 Checks whether the given path is a file. More...
 
bool IsHidden (const std::string &path)
 Checks whether the given path is hidden. More...
 
bool IsWritable (const std::string &path)
 Checks whether the given path is writable. More...
 
std::string Join (std::string path1, const std::string &path2)
 Joins two given paths or a path and filename. More...
 
std::string Load (const std::string &filename)
 Loads the given file and returns it in a string form. More...
 
std::string LocateResource (const std::string &path, const std::string &directory="", bool localonly=true)
 Locates the given file or directory. More...
 
bool Move (const std::string &source, const std::string &target)
 Moves a given file or directory. More...
 
std::string Relative (std::string path, std::string base=".")
 Determine shortest relative path from the given path. More...
 
bool Save (const std::string &filename, const std::string &data, bool append=false)
 Saves a given data into the filename. More...
 
unsigned long long Size (const std::string &filename)
 Returns the size of the given file. More...
 
std::string StartupDirectory ()
 Returns the directory where the program is started from. More...
 
void swap (Iterator &l, Iterator &r)
 Swaps two iterators. More...
 

Detailed Description

Contains filesystem functions.

All file related functions in Gorgon uses forward slash as directory separator. This includes return values from these functions as well.

Warning
This is a rudimentary filesystem module. Its not meant to be used in serious filesystem tasks. Most functions will not provide any choices related to hard/symbolic links. Additionally, Copy, Delete functions are fully blocking without any reporting facilities.

Function Documentation

◆ Canonical()

std::string Canonical ( const std::string &  path)

Canonicalizes a given relative path.

This method always return with a path that contains at least one slash. However, a slash should be appended to string as it never leaves a slash at the end unless, the path is a root path therefore, it is save to append an extra slash.

Parameters
pathis the file/directory to be canonized. Should contain forward slash as directory separator.
Returns
the full path of the given relative path. Forward slashes are used as directory separator. If the directory is a root directory this method will return a path ending with slash, otherwise it will never return a path ending with slash.
Exceptions
PathNotFoundErrorif canonize fails.

◆ ChangeDirectory()

bool ChangeDirectory ( const std::string &  path)

Changes current working directory.

Parameters
pathis the directory to become current directory. Should contain forward slash as directory separator.
Returns
false if directory does not exist

◆ Copy() [1/6]

bool Gorgon::Filesystem::Copy ( const C_< std::string > &  source,
const std::string &  target 
)

Copies list of files and/or directories from the given source to destination.

Hard link sources will be copied instead of links themselves.

Parameters
sourcelist of source files or directories
targetis directory to copy files or directories into. This directory should exist before calling this function
Returns
true on success

References Copy(), and GetFilename().

◆ Copy() [2/6]

bool Gorgon::Filesystem::Copy ( const C_< std::string, A_ > &  source,
const std::string &  target 
)

Copies list of files and/or directories from the given source to destination.

Hard link sources will be copied instead of links themselves.

Parameters
sourcelist of source files or directories
targetis directory to copy files or directories into. This directory should exist before calling this function
Returns
true on success

References Copy(), and GetFilename().

◆ Copy() [3/6]

bool Gorgon::Filesystem::Copy ( const I_ &  begin,
const I_ &  end,
const std::string &  target 
)

Copies list of files and/or directories from the given source to destination.

Hard link sources will be copied instead of links themselves.

Parameters
beginstarting iterator
endending iterator, will not be dereferenced
targetis directory to copy files or directories into. This directory should exist before calling this function
Returns
true on success

References Gorgon::begin(), Copy(), Gorgon::end(), and GetFilename().

◆ Copy() [4/6]

bool Copy ( const std::string &  source,
const std::string &  target 
)

Copies a file or directory from the given source to destination.

Hard link sources will be copied instead of links themselves.

Parameters
sourcefile or directory. Should either be a single file, or single directory
targetis the new filename or directory name
Returns
true on success

References IsDirectory(), and IsFile().

◆ Copy() [5/6]

bool Gorgon::Filesystem::Copy ( const std::string &  sourcedir,
const C_< std::string > &  source,
const std::string &  target 
)

Copies list of files and/or directories from the given source to destination.

Hard link sources will be copied instead of links themselves.

Parameters
sourcedirthe directory contains source files
sourcelist of source files or directories
targetis directory to copy files or directories into. This directory should exist before calling this function
Returns
true on success

References Copy().

◆ Copy() [6/6]

bool Gorgon::Filesystem::Copy ( const std::string &  sourcedir,
const I_ &  begin,
const I_ &  end,
const std::string &  target 
)

Copies list of files and/or directories from the given source to destination.

Hard link sources will be copied instead of links themselves.

Parameters
sourcedirthe directory contains source files
beginstarting iterator
endending iterator, will not be dereferenced
targetis directory to copy files or directories into. This directory should exist before calling this function
Returns
true on success

References Gorgon::begin(), Copy(), and Gorgon::end().

◆ CreateDirectory()

bool CreateDirectory ( const std::string &  path)

Creates a new directory.

This function works recursively to create any missing parent directories as well. If directory exists, this function returns true.

Parameters
pathis the path of the directory to be created. Should contain forward slash as directory separator.
Returns
true if the directory is ready to be used, false otherwise

References IsDirectory().

◆ CurrentDirectory()

std::string CurrentDirectory ( )

Returns the current working directory.

Returns
the current working directory Forward slashes are used as directory separator

◆ Delete()

bool Delete ( const std::string &  path)

Deletes the given file or directory.

If the directory is not empty, this function will delete all its contents.

Parameters
pathis the file/directory to be deleted. Should contain forward slash as directory separator.
Returns
true if the given path is deleted. If the given path does not exists, this function will still return true.

References IsDirectory(), Iterator::IsValid(), and Iterator::Next().

◆ EntryPoints()

std::vector< EntryPoint > EntryPoints ( )

This function returns all entry points in the current system.

This function does not perform caching and should be used sparingly. It may cause Windows systems to read external devices. On Linux systems, home, root and removable devices are listed.

Returns
The list of entry points

References Canonical(), IsDirectory(), Iterator::IsValid(), Load(), EntryPoint::Name, Iterator::Next(), EntryPoint::Path, EntryPoint::Readable, EntryPoint::Removable, and EntryPoint::Writable.

◆ ExeDirectory()

std::string ExeDirectory ( )

Returns the directory where the program resides.

Can be used to locate resources. May not be same as StartupDirectory

References Canonical(), and GetDirectory().

◆ ExePath()

std::string ExePath ( )

Returns the the full path of the application.

References Canonical().

◆ GetBasename()

std::string Gorgon::Filesystem::GetBasename ( std::string  path)

Returns the filename from the given path, without extension.

References GetFilename().

◆ GetDirectory()

std::string Gorgon::Filesystem::GetDirectory ( std::string  filepath)

Returns the directory portion of a file path.

If the file path does not contain any directory related information, this method returns current directory. This function expects the input to have / as directory separator.

Parameters
filepathpath that contains the filename

References CurrentDirectory().

◆ GetExtension()

std::string GetExtension ( std::string  path)

Returns the extension of the given path, also converts the extension to lower case.

References Gorgon::String::ToLower().

◆ GetFilename()

std::string Gorgon::Filesystem::GetFilename ( std::string  path)

Returns the filename portion of a file path.

This function expects the input to have / as directory separator. If path does not contain any /, it will return whole input.

Parameters
pathpath that contains the filename

◆ Initialize()

void Initialize ( )

Initializes the filesystem module.

Gorgon system requires every module to have initialization function even if they are not used. Currently used for following tasks:

  • Set startup directory

References CurrentDirectory().

◆ IsDirectory()

bool IsDirectory ( const std::string &  path)

Checks whether the given path is a directory.

Parameters
pathis the directory to be checked. Should contain forward slash as directory separator.
Returns
true if the given path is present and a directory

◆ IsExists()

bool IsExists ( const std::string &  path)

Checks whether the given path exists.

Parameters
pathis the file to be checked. Should contain forward slash as directory separator.
Returns
true if the given path is present

◆ IsFile()

bool IsFile ( const std::string &  path)

Checks whether the given path is a file.

Parameters
pathis the file to be checked. Should contain forward slash as directory separator.
Returns
true if the given path is present and a file

◆ IsHidden()

bool IsHidden ( const std::string &  path)

Checks whether the given path is hidden.

Parameters
pathis the directory to be checked. Should contain forward slash as directory separator.
Returns
true if the path should be hidden.

◆ IsWritable()

bool IsWritable ( const std::string &  path)

Checks whether the given path is writable.

This does not check if the file is locked or not. Also, even if the file is not marked as writable, a write operation might succeed.

Parameters
pathis the directory or file to be checked. Should contain forward slash as directory separator.
Returns
true if the given file/path exists and is writable.

◆ Join()

std::string Gorgon::Filesystem::Join ( std::string  path1,
const std::string &  path2 
)

Joins two given paths or a path and filename.

◆ Load()

std::string Load ( const std::string &  filename)

Loads the given file and returns it in a string form.

Notice that there is no size restriction over the file. This function can handle binary data. Throws PathNotFoundError if the file cannot be found. Before deciding on file is not found, this function checks if there is a lzma compressed file as filename.lzma

Parameters
filenameis the file to be loaded
Returns
the data loaded from the file
Exceptions
PathNotFoundErrorif the file cannot be read or does not exits

References Gorgon::end().

◆ LocateResource()

std::string Gorgon::Filesystem::LocateResource ( const std::string &  path,
const std::string &  directory = "",
bool  localonly = true 
)

Locates the given file or directory.

If localonly is true, this function only searches locations that are in the working directory. If it is set to false standard system locations like user home directory or application data directory is also searched. While looking for the resource, if the directory parameter is not empty, the resource is expected to be in the given directory under the local or system wide directory. Additionally, if the file is found as a lzma compressed file, it will be extracted. For instance, if directory parameter is "images", localonly is false, systemname is system and we are looking for icon.png, this function checks whether file exists in the following forms. The first one found is returned, if none exists, PathNotFoundError exception is thrown. The following list assume user path to be ~ application data path to be ~/apps images/icon.png, ../images/icon.png, icon.png, ../icon.png, images/icon.png.lzma, ../images/icon.png.lzma, icon.png.lzma, ../icon.png.lzma, ~/.system/images/icon.png, ~/.system/images/icon.png.lzma, ~/apps/.system/images/icon.png, ~/apps/.system/images/icon.png.lzma, ~/system/images/icon.png, ~/system/images/icon.png.lzma, ~/apps/system/images/icon.png, ~/apps/system/images/icon.png.lzma, ~/images/icon.png, ~/images/icon.png.lzma, ~/apps/images/icon.png, ~/apps/images/icon.png.lzma, ~/system/icon.png, ~/system/icon.png.lzma, ~/apps/system/icon.png, ~/apps/system/icon.png.lzma If compressed file is found, it will be extracted in place, and the extracted filename will be returned. Therefore, its not necessary to check if the file is compressed or not.

Parameters
pathis the filename or directory to be searched. Only compressed files are handled.
directoryis the directory the resource expected to be in. Should be relative.
localonlyif set, no system or user directories will be searched
Returns
the full path of the resource
Exceptions
PathNotFoundErrorif the file cannot be found
std::runtime_errorif the file cannot be read

◆ Move()

bool Move ( const std::string &  source,
const std::string &  target 
)

Moves a given file or directory.

Target is the new path rather than the target directory. This function can also be used to rename a file or directory.

Parameters
sourcefile or directory to be moved or renamed
targetpath
Returns
true on success. On Windows this function may not work across different drives. If this case is possible, use Copy then Delete.

◆ Relative()

std::string Relative ( std::string  path,
std::string  base = "." 
)

Determine shortest relative path from the given path.

Using "." in second parameter will return the path relative to current directory. This function never returns a path ending with a slash unless its safe to append another slash (i.e. a root path)

Parameters
pathis the path to be relativized.
baseis the base path that will be used to find relative path
Returns
relative path. Note that in Windows it is may be impossible to find relative path.

References Canonical().

◆ Save()

bool Save ( const std::string &  filename,
const std::string &  data,
bool  append = false 
)

Saves a given data into the filename.

If the file exists, it will be appended if append parameter is set to true. This function can handle binary data.

◆ Size()

unsigned long long Size ( const std::string &  filename)

Returns the size of the given file.

If the file is not found 0 is returned.

Parameters
filenameis the name of the file
Returns
size of the given file

◆ StartupDirectory()

std::string StartupDirectory ( )

Returns the directory where the program is started from.

This will always return the same value through out the execution.

◆ swap()

void Gorgon::Filesystem::swap ( Iterator l,
Iterator r 
)

Swaps two iterators.

References Iterator::Swap().