Skip to main content
Version: 🚧 Nightly

Class f3d::utils

Definition: utils.h (line 26)

Class containing useful static functions.

A class containing useful static functions unrelated to other classes.

Inner classes

Members

Public types

Enumeration type KnownFolder

Definition: utils.h (line 102)

enum KnownFolder {
ROAMINGAPPDATA,
LOCALAPPDATA,
PICTURES
}

Enumeration of supported Windows known folders

Enumerator ROAMINGAPPDATA

Enumerator LOCALAPPDATA

Enumerator PICTURES

Public static functions

Function textDistance

static unsigned int f3d::utils::textDistance(std::string_view strA, std::string_view strB)

Compute the Levenshtein distance between two strings. Can be useful for spell checking and typo detection.

Parameters:

  • std::string_view strA
  • std::string_view strB

Return type: unsigned int

Function tokenize

static std::vector< std::string > f3d::utils::tokenize(std::string_view str, bool keepComments=true)

Tokenize provided string_view into a vector of strings, using the same logic as bash.

  • Split by spaces unless between quotes

  • Split by quoted section and remove the quotes

  • Supported quotes are: '"`

  • Use escaped \ quotes, spaces and escape to add them verbatim
  • When keepComments argument is true, comments are supported with #, any characters after are ignored otherwise '#' and any characters after will be handled as standard character
  • Use escaped # to add it verbatim when using keepComments = true
  • Other escaped characters are also added verbatim

Throw a tokenize_exception if a quoted section is not closed or if finishing with an escape

Examples:

  • set scene.up.direction +Z -> set scene.up.direction +Z
  • set render.hdri.file "/path/to/file with spaces.png" -> set, render.hdri.file, /path/to/file with spaces.png
  • set render.hdri.file '/path/to/file with spaces.png' -> set, render.hdri.file, /path/to/file with spaces.png
  • set render.hdri.file "/path/to/file'with'quotes.png" -> set, render.hdri.file, /path/to/file'with'quotes.png
  • set render.hdri.file /path/to/file\ spaces\ \'quotes\".png -> set, render.hdri.file, /path/to/file spaces 'quotes".png
  • set render.hdri.file C:\path\to\windows\file.png -> set, render.hdri.file, C:\path\to\windows\file.png

  • set scene.up.direction +Z # A comment -> set, scene.up.direction, +Z

  • set scene.up.direction +\Z -> set, scene.up.direction, +Z

  • set scene.up.direction "+Z -> tokenize_exception

  • set scene.up.direction +Z\ -> tokenize_exception

Parameters:

  • std::string_view str
  • bool keepComments = true

Return type: std::vector< std::string >

Function collapsePath

static std::filesystem::path f3d::utils::collapsePath(const std::filesystem::path &path, const std::filesystem::path &baseDirectory={})

Collapse a string filesystem path by:

  • Expanding tilda ~ into home dir in a cross-platform way

  • Transform relative path into an absolute path based on basedDirectory if provided, or the current directory if not

  • Remove any .. if any

Rely on vtksys::SystemTools::CollapseFullPath but return empty string if the provided string is empty.

Parameters:

  • const std::filesystem::path & path
  • const std::filesystem::path & baseDirectory =

Return type: std::filesystem::path

Function globToRegex

static std::string f3d::utils::globToRegex(std::string_view glob, char pathSeparator='/')

Converts a glob expression to a regular expression. When a glob contains **, the pathSeparator (/ by default) is used to help generate the regular expression.

It handles the following glob features:

  • *: Matches zero or more characters (except path separators when also using **)

  • ?: Matches exactly one character (except path separators when also using **)

  • [...]: Character class, matches any of the given characters

  • [!...] or [^...]: Negated character class, matches none of the given characters

  • {a,b,c}: Alternation, matches any of the given comma-separated patterns

  • **: Matches any number of characters including path separators

Throws a utils::glob_exception if a character class or alternation is not closed or the expression ends with an escape.

Parameters:

  • std::string_view glob
  • char pathSeparator = '/'

Return type: std::string

Function getEnv

static std::optional< std::string > f3d::utils::getEnv(const std::string &env)

Get an environment variable value, returns std::nullopt if not set

Parameters:

  • const std::string & env

Return type: std::optional< std::string >

Function getKnownFolder

static std::optional< std::string > f3d::utils::getKnownFolder(KnownFolder knownFolder)

Get an Windows known folder, returns std::nullopt in case of error. Return std::nullopt on non-Windows platforms.

Parameters:

Return type: std::optional< std::string >