nsnake
Classic snake game for the terminal
Functions
Utils::File Namespace Reference

File I/O and Operational System's utilities. More...

Functions

bool exists (std::string path)
 Tells if #path exists. More...
 
off_t size (std::string path)
 Returns the file size of #path in bytes. More...
 
void mkdir_p (std::string path)
 Creates #path directory hierarchy recursively, just like UNIX command mkdir -p. More...
 
void rm_rf (std::string path)
 Removes recursively all files within directory at #path, just like UNIX command rm -rf. More...
 
void rm_f (std::string path)
 Forcibly removes file within #path. More...
 
bool create (std::string path)
 Creates empty file #path. More...
 
void write (std::string path, std::string contents)
 Writes #contents to #path. More...
 
bool isDirectory (std::string path)
 Tells if #path is a directory. More...
 
bool isFile (std::string path)
 Tells if #path is a regular file (not a directory, socket, FIFO device or whatever). More...
 
std::vector< std::string > ls (std::string path)
 Lists all files withing #path. More...
 
std::string getHome ()
 Gets the full path of the home directory for the user running this program. More...
 
std::string getUser ()
 Gets the user name of the person running this program.
 
std::string basename (std::string path)
 Returns the component of a pathname (file name and extension). More...
 
std::string dropBasename (std::string path)
 Returns the full pathname up to the last component. More...
 
std::string extension (std::string path)
 Returns the extension of a file. More...
 
std::string dropExtension (std::string path)
 Returns the filename without it's extension. More...
 

Detailed Description

File I/O and Operational System's utilities.

Note, I'm using several POSIX functions. So the following functions surely aren't portable to Windows. Other systems are kinda unpredictable.

Function Documentation

std::string Utils::File::basename ( std::string  path)

Returns the component of a pathname (file name and extension).

  • If we have "/path/to/something.txt" it returns "something.txt"
  • If we have "something.txt" it returns "something.txt"
Note
It auto-detects the separator for Windows ('\') and UNIX-based systems ('/')

Thanks to this huge list of OS-specific defines: http://sourceforge.net/p/predef/wiki/OperatingSystems/

Definition at line 263 of file Utils.cpp.

bool Utils::File::create ( std::string  path)

Creates empty file #path.

Note
If file already exists, will erase everything inside!
Returns
If we could create the file at all.

Definition at line 164 of file Utils.cpp.

std::string Utils::File::dropBasename ( std::string  path)

Returns the full pathname up to the last component.

  • If we have "/path/to/something.txt" it returns "/path/to"
  • If we have "something.txt" it returns ""

Definition at line 280 of file Utils.cpp.

std::string Utils::File::dropExtension ( std::string  path)

Returns the filename without it's extension.

Note
Works with full paths or single filenames.

Definition at line 305 of file Utils.cpp.

bool Utils::File::exists ( std::string  path)

Tells if #path exists.

Note
It could be a file, directory or whatever.

Definition at line 84 of file Utils.cpp.

std::string Utils::File::extension ( std::string  path)

Returns the extension of a file.

Note
It doesn't return the dot.
  • If we have "/path/to/file.txt" it returns "txt"
  • If we have "filename.DLL" it returns "DLL"
  • If we have ".hidden" it returns ""
  • If we have "none" it returns ""
Note
Works with full paths or single filenames.

Definition at line 294 of file Utils.cpp.

std::string Utils::File::getHome ( )

Gets the full path of the home directory for the user running this program.

Returns
The path or an empty string.
Note
We guarantee that the path has a trailing '/'.

Definition at line 235 of file Utils.cpp.

bool Utils::File::isDirectory ( std::string  path)

Tells if #path is a directory.

Note
Returns false also if something wrong happened.

Definition at line 179 of file Utils.cpp.

bool Utils::File::isFile ( std::string  path)

Tells if #path is a regular file (not a directory, socket, FIFO device or whatever).

Note
Returns false also if something wrong happened.

Definition at line 190 of file Utils.cpp.

std::vector< std::string > Utils::File::ls ( std::string  path)

Lists all files withing #path.

Note
The returned vecor is not ordered and all file names contain the full #path before them.

Definition at line 201 of file Utils.cpp.

void Utils::File::mkdir_p ( std::string  path)

Creates #path directory hierarchy recursively, just like UNIX command mkdir -p.

Definition at line 97 of file Utils.cpp.

void Utils::File::rm_f ( std::string  path)

Forcibly removes file within #path.

Note
It doesn't work with directories.

Definition at line 152 of file Utils.cpp.

void Utils::File::rm_rf ( std::string  path)

Removes recursively all files within directory at #path, just like UNIX command rm -rf.

Definition at line 117 of file Utils.cpp.

off_t Utils::File::size ( std::string  path)

Returns the file size of #path in bytes.

Returns
It's size or -1 if it doesn't exist (or something strange happened).

Definition at line 88 of file Utils.cpp.

void Utils::File::write ( std::string  path,
std::string  contents 
)

Writes #contents to #path.

Note
If #path doesn't exist, creates it.
If #path exist, overwrites everything on it.

Definition at line 173 of file Utils.cpp.