nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
Utils::File Namespace Reference

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

Functions

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

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

◆ basename()

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.

◆ create()

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.

◆ dropBasename()

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.

◆ dropExtension()

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.

◆ exists()

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.

◆ extension()

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.

◆ getHome()

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.

◆ getUser()

std::string Utils::File::getUser ( )

Gets the user name of the person running this program.

Definition at line 246 of file Utils.cpp.

◆ isDirectory()

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.

◆ isFile()

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.

◆ ls()

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.

◆ mkdir_p()

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.

◆ rm_f()

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.

◆ rm_rf()

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.

◆ size()

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.

◆ write()

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.