|
CX Framework
Cross-platform C utility framework
|
Macros | |
| #define | pathJoin(out, ...) _pathJoin(out, count_macro_args(__VA_ARGS__), (strref[]) { __VA_ARGS__ }) |
Enumerations | |
| enum | PathMatchFlags { PATH_LeadingDir = 1 , PATH_IgnorePath = 2 , PATH_Smart = 4 , PATH_CaseInsensitive = 8 } |
Functions | |
| void | pathNormalize (string *path) |
| bool | pathDecompose (string *ns, sa_string *components, strref path) |
| bool | pathCompose (string *out, strref ns, sa_string components) |
| bool | pathIsAbsolute (strref path) |
| bool | pathParent (string *out, strref path) |
| bool | pathFilename (string *out, strref path) |
| bool | pathSplitNS (string *nspart, string *pathpart, strref path) |
| void | pathAddExt (string *out, strref path, strref ext) |
| bool | pathRemoveExt (string *out, strref path) |
| bool | pathGetExt (string *out, strref path) |
| void | pathSetExt (string *out, strref path, strref ext) |
| bool | pathMatch (strref path, strref pattern, uint32 flags) |
All pathnames are UTF-8 encoded.
CX uses a unified path format: namespace:/path/to/file
The namespace prefix is optional and its meaning depends on context:
Path separators are forward slashes (/). Backslashes are converted to forward slashes during normalization.
| #define pathJoin | ( | out, | |
| ... | |||
| ) | _pathJoin(out, count_macro_args(__VA_ARGS__), (strref[]) { __VA_ARGS__ }) |
bool pathJoin(string *out, ...);
Joins multiple path components with separators
Concatenates path components using forward slashes as separators. This is a convenience function for building paths. Empty components are skipped. If an absolute path appears after the first argument, the operation fails.
| out | Receives the joined path (prior contents destroyed) |
| ... | Variable number of path components (strref) to join |
Example:
| enum PathMatchFlags |
Path pattern matching flags
Control how wildcard patterns are matched against paths.
| void pathAddExt | ( | string * | out, |
| strref | path, | ||
| strref | ext | ||
| ) |
Adds a file extension to a path
Appends a period and the specified extension to the path. Does not check if an extension already exists - use pathSetExt() to replace an existing extension.
| out | Receives path with extension added (prior contents destroyed) |
| path | Base path |
| ext | Extension to add (without leading period) |
Example:
| bool pathCompose | ( | string * | out, |
| strref | ns, | ||
| sa_string | components | ||
| ) |
Reconstructs a path from namespace and components
Builds a complete path string from separated parts. This is the inverse operation of pathDecompose(). Components are joined with forward slashes.
| out | Receives the composed path (prior contents destroyed) |
| ns | Namespace prefix (can be empty/NULL for no namespace) |
| components | Array of path components to join |
| bool pathDecompose | ( | string * | ns, |
| sa_string * | components, | ||
| strref | path | ||
| ) |
Decomposes a path into namespace and component parts
Breaks a path into its constituent parts for analysis or manipulation. The path is normalized during decomposition (backslashes converted, etc.). Empty components (from redundant slashes) and current/parent directory references are resolved and removed.
For absolute paths, the first component may be an empty string if the path represents a namespace root (i.e. c:/).
| ns | Receives the namespace portion (empty if no namespace) |
| components | Array to receive path components (will be cleared first) |
| path | Path to decompose |
Example:
| bool pathFilename | ( | string * | out, |
| strref | path | ||
| ) |
Extracts the filename from a path
Returns the last component of a path, which is typically the filename. This includes the file extension if present. For directory paths ending in a separator, returns an empty string.
| out | Receives the filename (prior contents destroyed) |
| path | Path to extract filename from |
Example:
| bool pathGetExt | ( | string * | out, |
| strref | path | ||
| ) |
Extracts the file extension from a path
Returns just the extension portion (without the period) of the last component in the path. If the last component has no extension or is a directory, returns false.
| out | Receives the extension without period (prior contents destroyed) |
| path | Path to extract extension from |
Example:
| bool pathIsAbsolute | ( | strref | path | ) |
Checks if a path is absolute or relative
A path is considered absolute if:
All other paths are relative.
| path | Path to check |
| bool pathMatch | ( | strref | path, |
| strref | pattern, | ||
| uint32 | flags | ||
| ) |
Tests if a path matches a wildcard pattern
Supports standard wildcards:
* - Matches zero or more characters (excluding / unless PATH_IgnorePath)? - Matches exactly one character (excluding / unless PATH_IgnorePath)By default, matching is path-aware (/ is a special separator). Use PATH_IgnorePath to treat paths as simple strings.
PATH_Smart mode provides intuitive behavior:
| path | Path to test |
| pattern | Wildcard pattern |
| flags | Combination of PathMatchFlags |
Examples:
| void pathNormalize | ( | string * | path | ) |
Normalizes a path by eliminating redundant elements
Performs the following transformations:
The path is modified in place. If already normalized, no changes are made.
| path | Path to normalize (modified in place) |
Example:
| bool pathParent | ( | string * | out, |
| strref | path | ||
| ) |
Extracts the parent directory from a path
Returns the directory containing the given path. For a file path, this is the directory containing the file. For a directory path, this is the parent directory.
Special cases:
| out | Receives the parent directory path (prior contents destroyed) |
| path | Path to get parent of |
Example:
| bool pathRemoveExt | ( | string * | out, |
| strref | path | ||
| ) |
Removes the file extension from a path
Strips the extension (including the period) from a filename. Only the last extension is removed.
| out | Receives path without extension (prior contents destroyed) |
| path | Path to remove extension from |
Example:
| void pathSetExt | ( | string * | out, |
| strref | path, | ||
| strref | ext | ||
| ) |
Replaces or sets the file extension of a path
Removes any existing extension and adds the specified one. This is equivalent to calling pathRemoveExt() followed by pathAddExt().
| out | Receives path with new extension (prior contents destroyed) |
| path | Path to modify |
| ext | New extension (without leading period) |
Example:
| bool pathSplitNS | ( | string * | nspart, |
| string * | pathpart, | ||
| strref | path | ||
| ) |
Splits a path into namespace and path portions
Separates the namespace prefix (if any) from the rest of the path. The colon (:) separator is removed. If no namespace exists, nspart will be empty and pathpart will contain the entire path.
| nspart | Receives the namespace portion (prior contents destroyed) |
| pathpart | Receives the path portion (prior contents destroyed) |
| path | Path to split |
Example: