libxcks  0.1.0.1
pathutil.hpp File Reference

Path utilities. More...

#include <filesystem>
#include "libxcks/types.hpp"
Include dependency graph for pathutil.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

std::vector< std::filesystem::path > libxcks::getDirectories (const std::filesystem::path &p)
 Gets directories of a path. More...
 
ArrayString libxcks::getDirectoriesAsArrayString (const std::filesystem::path &p)
 Gets directories of a path. More...
 
int libxcks::compareFileName (const std::string &fn1, const std::string &fn2)
 Compares two file names. More...
 
int libxcks::compareFileName (const std::filesystem::path &p1, const std::filesystem::path &p2)
 Compares file names of two paths. More...
 
std::filesystem::path libxcks::constructPath (const ArrayString &dirs, const std::string &filename)
 Constructs a path from an array of directories and an optional filename. More...
 
std::filesystem::path libxcks::ensureEndsWithPathSeparator (const std::filesystem::path &p)
 Ensures the path ends with a path separator. More...
 
bool libxcks::isFileNameValid (const std::string &filename)
 Checks if the name of the file is valid. More...
 

Detailed Description

Path utilities.

Definition in file pathutil.hpp.

Function Documentation

◆ compareFileName() [1/2]

int libxcks::compareFileName ( const std::filesystem::path &  p1,
const std::filesystem::path &  p2 
)

Compares file names of two paths.

Under Windows the comparison is case insensitive.

Parameters
p1First path.
p2Second path.
Returns
A negative value if f1 < f2, a positive value if f1 > f2, 0 if f1 == f2.

Definition at line 137 of file pathutil.cpp.

◆ compareFileName() [2/2]

int libxcks::compareFileName ( const std::string &  fn1,
const std::string &  fn2 
)

Compares two file names.

Under Windows the comparison is case insensitive.

Parameters
fn1First file name.
fn2Second file name.
Returns
A negative value if f1 < f2, a positive value if f1 > f2, 0 if f1 == f2.

Definition at line 108 of file pathutil.cpp.

◆ constructPath()

std::filesystem::path libxcks::constructPath ( const ArrayString dirs,
const std::string &  filename 
)

Constructs a path from an array of directories and an optional filename.

Parameters
[in]dirsThe array of directories.
[in]filenameThe filename. Can be empty.
Returns
The constructed path.

Definition at line 170 of file pathutil.cpp.

◆ ensureEndsWithPathSeparator()

std::filesystem::path libxcks::ensureEndsWithPathSeparator ( const std::filesystem::path &  p)

Ensures the path ends with a path separator.

Warning
This function does not check the type of the path (directory, file, or others).
Parameters
[in]pThe path to ensure it ends with a path separator.
Returns
The path with a path separator at end.

Definition at line 209 of file pathutil.cpp.

◆ getDirectories()

std::vector< std::filesystem::path > libxcks::getDirectories ( const std::filesystem::path &  p)

Gets directories of a path.

Parameters
[in]pThe path the directories are wanted.
Returns
The directories in path vector form.

Definition at line 64 of file pathutil.cpp.

◆ getDirectoriesAsArrayString()

ArrayString libxcks::getDirectoriesAsArrayString ( const std::filesystem::path &  p)

Gets directories of a path.

Parameters
[in]pThe path the directories are wanted.
Returns
The directories in path vector form.

Definition at line 81 of file pathutil.cpp.

◆ isFileNameValid()

bool libxcks::isFileNameValid ( const std::string &  filename)

Checks if the name of the file is valid.

The name of the file to check must be a full name (including extension but excluding directories).

The forbidden characters are:

  • On Unix: / (forward slash) and NUL byte.
  • On Windows: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
    • < (less than)
    • > (greater than)
    • : (colon)
    • " (double quote)
    • / (forward slash)
    • \ (backslash)
    • | (vertical bar or pipe)
    • ? (question mark)
    • * (asterisk)
    • NUL byte
  • On Mac: / (forward slash), : (colon) and NUL byte (even if this one is officially supported).

On Windows the following is also considered as invalid:

  • bytes 1 to 31 (ASCII control characters)
  • the following reserved name of file (case insensitive):
    • CON, PRN, AUX, NUL
    • COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8 and COM9
    • LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8 and LPT9
  • Names of file ending with a space or a dot.

An empty name is invalid.

The full names . and .. are also invalid on Unix and Windows.

Parameters
filenameThe name of file to check (see upper).
Returns
true if the name of file is valid on the current OS, false otherwise.

Definition at line 266 of file pathutil.cpp.