Veethree D7 SDK
Loading...
Searching...
No Matches
The utils library

Detailed Description

Utility functions and templates for common operations.

Concepts

concept  vsdk::utils::StringLike
 Type constraint for string-like types.

Typedefs

template<typename T>
using vsdk::utils::unique_ptr_free = std::unique_ptr<T, decltype([](T* t) { free(t); })>
 Convenience wrapper on top of std::unique_ptr that uses free() instead of delete() for managing objects allocated by malloc/calloc/etc Useful when receiving pointers allocated by C libraries (e.g. libuboot)

Functions

std::string vsdk::utils::path_separator (const StringLike auto &base)
 Utility for prepending OS-specific path separator. Needed for the path_join function.
template<StringLike... Components>
std::string vsdk::utils::path_join (const StringLike auto &base, Components... comp)
 Utility to construct OS-aware paths. Similar to Python's os.path.join(). On Windows the path components will be joined with backslashes (), everywhere else with forward slashed (/).
std::errc vsdk::utils::file_remove (const std::string &path)
 Remove a file.
std::errc vsdk::utils::mkdir (const std::string &path)
 Make a directory. This behaves as mkdir -p. It will create intermediary path components automatically.
std::errc vsdk::utils::rmdir (const std::string &path)
 Delete a directory recursively. Behaves as rm -rf.
std::string vsdk::utils::canonical_path (const std::string &path)
 Get the canonical path. The canonical path will not contain dot, dotdot or symlinks.
std::string vsdk::utils::temp_directory_path ()
 Get the path for the temp directory on the OS. Similar semantics to std::filesystem::temp_directory_path.
bool vsdk::utils::file_exists (const std::string &path)
 Utility to check if a path exists. Same semantics as std::filesystem::exists, but won't do character conversion on Windows.
template<typename... Args>
void vsdk::utils::print (fmt::format_string< Args... > fmt, Args &&...args) noexcept
 Exception-safe fmt::print wrapper.
template<typename T, size_t N>
T & vsdk::utils::at (T(&arr)[N], size_t index)
 Return a reference to an array element.
template<typename T, size_t N>
size_t vsdk::utils::array_size (T(&arr)[N])
 Return the size of a fixed size array.
int vsdk::utils::open (const std::string &path, int flags)
 Wrapper implementation that deals with C++ linter (clang-tidy)
template<typename... Args>
int vsdk::utils::ioctl (Args &&... args)
 Wrapper implementation that deals with C++ linter (clang-tidy)
int vsdk::utils::system (const std::string &cmd)
 Wrapper implementation that deals with C++ linter (clang-tidy)
std::string vsdk::utils::errno_string (int err)
 Wrapper for strerror that returns a std::string.
template<typename... Args>
int vsdk::utils::fcntl (int fd, int cmd, Args &&... args)
 Wrapper for fcntl that deals with C++ linter (clang-tidy)
std::string vsdk::utils::version ()
 Get the VSDK version used to build the application.
std::string vsdk::utils::platform_version ()
 Return the installed SDK version. Reads from /etc/vsdk-version.txt on target. Failing that, reads relative to OECORE_NATIVE_SYSROOT, to support host.

Detailed Description

Utility functions and templates for common operations.

Function Documentation

◆ array_size()

template<typename T, size_t N>
size_t vsdk::utils::array_size ( T(&) arr[N])

Return the size of a fixed size array.

Parameters
arrA fixed size array.
Returns
The array size.

◆ at()

template<typename T, size_t N>
T & vsdk::utils::at ( T(&) arr[N],
size_t index )

Return a reference to an array element.

Parameters
arrA fixed size array.
indexThe index of the element.
Returns
A reference to the element indexed by index.

◆ canonical_path()

std::string vsdk::utils::canonical_path ( const std::string & path)

Get the canonical path. The canonical path will not contain dot, dotdot or symlinks.

Parameters
pathThe path to canonicalize.
Returns
The canonicalized path.

◆ errno_string()

std::string vsdk::utils::errno_string ( int err)

Wrapper for strerror that returns a std::string.

Parameters
errThe error code
Returns
A std::string describing the error code, otherwise "unknown"

◆ fcntl()

template<typename... Args>
int vsdk::utils::fcntl ( int fd,
int cmd,
Args &&... args )

Wrapper for fcntl that deals with C++ linter (clang-tidy)

Template Parameters
Args
Parameters
fdThe file descriptor to modify
cmdThe command to execute on the file descriptor
argsThe arguments for the command
Returns
-1 if unsuccessful, >= 0 otherwise

◆ file_exists()

bool vsdk::utils::file_exists ( const std::string & path)
inline

Utility to check if a path exists. Same semantics as std::filesystem::exists, but won't do character conversion on Windows.

Parameters
pathThe path to check
Returns
true if the path leads to an existing file or directory
false otherwise, or if the path is a dangling link

◆ file_remove()

std::errc vsdk::utils::file_remove ( const std::string & path)

Remove a file.

Parameters
pathThe path to the file to remove. See also rmdir.
Returns
std::errc{} if the file was removed or it did not exist, an error code describing the error otherwise

◆ ioctl()

template<typename... Args>
int vsdk::utils::ioctl ( Args &&... args)

Wrapper implementation that deals with C++ linter (clang-tidy)

Note
This does nothing and returns 0 for host builds
Parameters
argsSame arguments as standard ioctl call. (man 2 ioctl for reference)
Returns
Same return values from standard ioctl call, -1 means error, 0 success.

◆ mkdir()

std::errc vsdk::utils::mkdir ( const std::string & path)

Make a directory. This behaves as mkdir -p. It will create intermediary path components automatically.

Parameters
pathThe path to create.
Returns
std::errc{} if successful, std::errc describing the error otherwise.
Note
If any of the path components already exist but are not directories, this function will return std::errc::not_a_directory.
On Linux, the permissions of the created directories will be 0700. Need to manually change them if needed.

◆ open()

int vsdk::utils::open ( const std::string & path,
int flags )

Wrapper implementation that deals with C++ linter (clang-tidy)

Parameters
pathPath to file to be opened.
flagsFlags for the standard open call. (man 2 open for reference)
Returns
Same return values from standard open call, -1 means error.

◆ path_join()

template<StringLike... Components>
std::string vsdk::utils::path_join ( const StringLike auto & base,
Components... comp )

Utility to construct OS-aware paths. Similar to Python's os.path.join(). On Windows the path components will be joined with backslashes (), everywhere else with forward slashed (/).

Parameters
baseThe first component of the resulting path
compThe rest of the component paths.
Returns
The resulting path.

◆ path_separator()

std::string vsdk::utils::path_separator ( const StringLike auto & base)

Utility for prepending OS-specific path separator. Needed for the path_join function.

Parameters
baseStringLike component to prepend the separator to.

◆ print()

template<typename... Args>
void vsdk::utils::print ( fmt::format_string< Args... > fmt,
Args &&... args )
noexcept

Exception-safe fmt::print wrapper.

Parameters
fmtFormat string as defined by fmtlib documentation
argsAny arguments usually used by fmt::print

◆ rmdir()

std::errc vsdk::utils::rmdir ( const std::string & path)

Delete a directory recursively. Behaves as rm -rf.

Parameters
pathThe path to remove.
Returns
std::errc{} if the deletion of the directory and all of its contents succeeds, an error code describing the error otherwise.
Note
If any of the intermediary paths don't exist, this is considered successful.

◆ system()

int vsdk::utils::system ( const std::string & cmd)

Wrapper implementation that deals with C++ linter (clang-tidy)

Parameters
cmdCommand to be executed
Returns
Same return values from standard system call.