zz::os Namespace Reference

Namespace for OS specific implementations. More...

Functions

int system (const char *const command, const char *const moduleName)
 Execute sub-process using system call. More...
 
std::size_t thread_id ()
 Get thread id. More...
 
int is_atty ()
 Check if stdout is associated with console. More...
 
Size console_size ()
 Return the Size of console window. More...
 
std::tm localtime (std::time_t t)
 Thread-safe version of localtime. More...
 
std::tm gmtime (std::time_t t)
 Thread-safe version of gmtime. More...
 
std::wstring utf8_to_wstring (std::string &u8str)
 Convert UTF-8 string to wstring. More...
 
std::string wstring_to_utf8 (std::wstring &wstr)
 Convert wstring to UTF-8. More...
 
std::vector< std::string > path_split (std::string path)
 Split path into hierachical sub-folders. More...
 
std::string path_join (std::vector< std::string > elems)
 Join path from sub-folders This function will handle system dependent path formats such as '/' for unix like OS, and '\' for windows. More...
 
std::string path_split_filename (std::string path)
 Split filename from path. More...
 
std::string path_split_directory (std::string path)
 Split the deepest directory. More...
 
std::string path_split_extension (std::string path)
 Split extension if any. More...
 
std::string path_split_basename (std::string path)
 Split basename. More...
 
std::string path_append_basename (std::string origPath, std::string whatToAppend)
 Append string to basename directly, rather than append to extension This is more practically useful because nobody want to change extension in most situations. Appending string to basename in order to change filename happens. More...
 
void fstream_open (std::fstream &stream, std::string filename, std::ios::openmode openmode)
 Open fstream using UTF-8 string This is a wrapper function because Windows will require wstring to process unicode filename/path. More...
 
void ifstream_open (std::ifstream &stream, std::string filename, std::ios::openmode openmode)
 Open ifstream using UTF-8 string This is a wrapper function because Windows will require wstring to process unicode filename/path. More...
 
bool rename (std::string oldName, std::string newName)
 Rename file, support unicode filename/path. More...
 
bool copyfile (std::string src, std::string dst, bool replaceDst=false)
 Copy file, support unicode filename/path. More...
 
bool movefile (std::string src, std::string dst, bool replaceDst=false)
 Move file, support unicode filename/path. More...
 
bool remove_all (std::string path)
 Remove path, whatever file or directory. Dangerous! Cannot revert. More...
 
bool remove_dir (std::string root, bool recursive=true)
 Remove directory and all sub-directories and files if set recursive to true. If recursive set to false, will try to delete an empty one only. Dangerous! Cannot revert. More...
 
bool remove_file (std::string path)
 Remove file. Dangerous! Cannot revert. More...
 
std::string last_error ()
 Retrieve the last error in errno. More...
 
std::string endl ()
 Gets the OS dependent line end characters. More...
 
std::string path_delim ()
 Gets the OS dependent path delim. More...
 
std::string current_working_directory ()
 Get current working directory. More...
 
bool path_exists (std::string path, bool considerFile=true)
 Check if path exist in filesystem. More...
 
bool is_file (std::string path)
 Check if path is file and exists. More...
 
bool is_directory (std::string path)
 Check if path is directory and exists. More...
 
bool path_identical (std::string first, std::string second, bool forceCaseSensitve=false)
 Compare identical path according to OS. By default, windows paths are case-INsensitive. More...
 
std::string absolute_path (std::string reletivePath)
 Convert reletive path to absolute path. More...
 
bool create_directory (std::string path)
 Create directory if not exist. More...
 
bool create_directory_recursive (std::string path)
 Create directory recursively if not exist. More...
 
std::vector< std::string > list_directory (std::string root)
 List directory contents. More...
 

Detailed Description

Namespace for OS specific implementations.

Function Documentation

std::string zz::os::absolute_path ( std::string  reletivePath)

Convert reletive path to absolute path.

Parameters
reletivePath
Returns
Absolute path
Size zz::os::console_size ( )

Return the Size of console window.

Returns
Size, which contains width and height
bool zz::os::copyfile ( std::string  src,
std::string  dst,
bool  replaceDst = false 
)

Copy file, support unicode filename/path.

Parameters
oldName
newName
replaceDstIf true, existing dst file will be replaced
Returns
true on success
bool zz::os::create_directory ( std::string  path)

Create directory if not exist.

Parameters
path
Returns
True if directory created/already exist
Note
Will not create recursively, fail if parent directory inexist. Use create_directory_recursive instead.
bool zz::os::create_directory_recursive ( std::string  path)

Create directory recursively if not exist.

Parameters
path
Returns
True if directory created/already exist, false when failed to create the final directory Function create_directory_recursive will create directory recursively. For example, create_directory_recursive("/a/b/c") will create /a->/a/b->/a/b/c recursively.
std::string zz::os::current_working_directory ( )

Get current working directory.

Returns
Current working directory
std::string zz::os::endl ( )

Gets the OS dependent line end characters.

Returns
A std::string.
void zz::os::fstream_open ( std::fstream &  stream,
std::string  filename,
std::ios::openmode  openmode 
)

Open fstream using UTF-8 string This is a wrapper function because Windows will require wstring to process unicode filename/path.

Parameters
stream
filename
openmode
std::tm zz::os::gmtime ( std::time_t  t)

Thread-safe version of gmtime.

Parameters
tstd::time_t
Returns
std::tm format UTC time
void zz::os::ifstream_open ( std::ifstream &  stream,
std::string  filename,
std::ios::openmode  openmode 
)

Open ifstream using UTF-8 string This is a wrapper function because Windows will require wstring to process unicode filename/path.

Parameters
stream
filename
openmode
int zz::os::is_atty ( )

Check if stdout is associated with console.

Returns
None zero value if stdout is not redirected to file
bool zz::os::is_directory ( std::string  path)

Check if path is directory and exists.

Parameters
path
Returns
true if directory exists, not file
bool zz::os::is_file ( std::string  path)

Check if path is file and exists.

Parameters
path
Returns
true if file exists, not directory
std::string zz::os::last_error ( )

Retrieve the last error in errno.

Returns
Human readable error string
std::vector< std::string > zz::os::list_directory ( std::string  root)

List directory contents.

Parameters
rootRoot of the directory
Returns
A std::vecotr<std::string>, vector of absolute paths of files and sub-directories
std::tm zz::os::localtime ( std::time_t  t)

Thread-safe version of localtime.

Parameters
tstd::time_t
Returns
std::tm format localtime
bool zz::os::movefile ( std::string  src,
std::string  dst,
bool  replaceDst = false 
)

Move file, support unicode filename/path.

Parameters
oldName
newName
replaceDstIf true, existing dst file will be replaced
Returns
true on success
std::string zz::os::path_append_basename ( std::string  origPath,
std::string  whatToAppend 
)

Append string to basename directly, rather than append to extension This is more practically useful because nobody want to change extension in most situations. Appending string to basename in order to change filename happens.

Parameters
origPath
whatToAppend
Returns
New filename with appended string. path_append_basename("/home/test/abc.jpg", "_01") = "/home/test/abc_01.jpg"
std::string zz::os::path_delim ( )

Gets the OS dependent path delim.

Returns
A std::string.
bool zz::os::path_exists ( std::string  path,
bool  considerFile = true 
)

Check if path exist in filesystem.

Parameters
path
considerFileConsider file as well?
Returns
true if path exists
bool zz::os::path_identical ( std::string  first,
std::string  second,
bool  forceCaseSensitve = false 
)

Compare identical path according to OS. By default, windows paths are case-INsensitive.

Parameters
first
second
forceCaseSensitveForce compare using case sensitivity.
Returns
True if paths are indentical
std::string zz::os::path_join ( std::vector< std::string >  elems)

Join path from sub-folders This function will handle system dependent path formats such as '/' for unix like OS, and '\' for windows.

Parameters
elems
Returns
Long path path_join({"/home/abc/","def"} = "/home/abc/def"
std::vector< std::string > zz::os::path_split ( std::string  path)

Split path into hierachical sub-folders.

Parameters
path
Returns
std::vector<std::string> of sub-folders path_split("/usr/local/bin/xxx/")= {"usr","local","bin","xxx"}
std::string zz::os::path_split_basename ( std::string  path)

Split basename.

Parameters
path
Returns
Basename without extension
std::string zz::os::path_split_directory ( std::string  path)

Split the deepest directory.

Parameters
path
Returns
Deepest directory name. E.g. "abc/def/ghi/xxx.xx"->"ghi"
std::string zz::os::path_split_extension ( std::string  path)

Split extension if any.

Parameters
path
Returns
Extension or "" if no extension exists
std::string zz::os::path_split_filename ( std::string  path)

Split filename from path.

Parameters
path
Returns
Full filename with extension
bool zz::os::remove_all ( std::string  path)

Remove path, whatever file or directory. Dangerous! Cannot revert.

Parameters
path
Returns
true as long as path does not exists anymore
bool zz::os::remove_dir ( std::string  root,
bool  recursive = true 
)

Remove directory and all sub-directories and files if set recursive to true. If recursive set to false, will try to delete an empty one only. Dangerous! Cannot revert.

Parameters
root
recursiveDelete all contents inside of it.
Returns
true as long as directory does not exists anymore(file with same name may exist)
bool zz::os::remove_file ( std::string  path)

Remove file. Dangerous! Cannot revert.

Parameters
path
Returns
true as long as file does not exists anymore
bool zz::os::rename ( std::string  oldName,
std::string  newName 
)

Rename file, support unicode filename/path.

Parameters
oldName
newName
Returns
true on success
int zz::os::system ( const char *const  command,
const char *const  moduleName 
)

Execute sub-process using system call.

Parameters
command
module_name
Returns
Return code of sub-process
std::size_t zz::os::thread_id ( )

Get thread id.

Returns
Current thread id
std::wstring zz::os::utf8_to_wstring ( std::string &  u8str)

Convert UTF-8 string to wstring.

Parameters
u8str
Returns
Converted wstring
std::string zz::os::wstring_to_utf8 ( std::wstring &  wstr)

Convert wstring to UTF-8.

Parameters
wstr
Returns
Converted UTF-8 string