htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
path.h File Reference
#include <htils/arena.h>
#include <htils/basictypes.h>
#include <htils/string.h>

Go to the source code of this file.

Functions

stringpath_canonical (arena_t *arena, const string *path)
 Gets the absolute/canonical path of path.
 
stringpath_basename (arena_t *arena, const string *path)
 Get the basename i.e the filename.extension / final dir of path.
 
stringpath_dirname (arena_t *arena, const string *path)
 Get the dirname of a path.
 
stringpath_extension (arena_t *arena, const string *path)
 Get the extension of path.
 
stringpath_stem (arena_t *arena, const string *path)
 Get the stem of path.
 
stringpath_join (arena_t *arena, const string *first, const string *second)
 Joins two paths together.
 
b32 make_dir (const string *path)
 Makes a directory.
 
b32 does_path_exist (const string *path)
 Checks if a path exists.
 
b32 path_remove (const string *path)
 Removes a file or directory at path.
 

Function Documentation

◆ does_path_exist()

b32 does_path_exist ( const string path)

Checks if a path exists.

By either:

  • For Linux: Checking if the path at path exists using stat().
  • For Windows: Checking if the path at path exists using GetFileAttributes().
Precondition
path must be valid, and cannot be null.
Parameters
pathThe path to check.
Returns
True if the path exists, false if it doesn't.
See also
stat(), GetFileAttributes()

◆ make_dir()

b32 make_dir ( const string path)

Makes a directory.

By either:

  • For Linux: Creating the directory at path using mkdir().
  • For Windows: Creating the directory at path using CreateDirectory().
Precondition
path must be valid, and cannot be null.
Parameters
pathThe path to the directory to make.
Returns
True if the directory was made, false if it failed.
See also
mkdir(), CreateDirectory()

◆ path_basename()

string * path_basename ( arena_t arena,
const string path 
)

Get the basename i.e the filename.extension / final dir of path.

By trimming previous / using pointer arithmetic and allocating the new string using arena_alloc().

Parameters
arenaThe arena to allocate from.
pathThe path to get the basename of.
Precondition
arena and path must be valid and cannot be null.
Returns
The basename of the path.
See also
arena_alloc().

◆ path_canonical()

string * path_canonical ( arena_t arena,
const string path 
)

Gets the absolute/canonical path of path.

Using realpath() and then allocating the new canonical path using arena_alloc().

Parameters
arenaThe arena to allocate from.
pathThe path to get the absolute path of.
Precondition
arena and path must be valid and cannot be null.
Returns
The absolute path.
See also
realpath(), arena_alloc()

◆ path_dirname()

string * path_dirname ( arena_t arena,
const string path 
)

Get the dirname of a path.

By trimming away the last entry of the path, and allocating the new string using arena_alloc().

Parameters
arenaThe arena to allocate from.
pathThe path to get the dirname of.
Precondition
arena and path must be valid and cannot be null.
Returns
The dirname of the path.
See also
arena_alloc().

◆ path_extension()

string * path_extension ( arena_t arena,
const string path 
)

Get the extension of path.

Trims to the last extension of path, through pointer arithmetic, and allocating the new string using arena_alloc().

Parameters
arenaThe arena to allocate from.
pathThe path to get the extension of.
Precondition
arena and path must be valid and cannot be null.
Returns
The extension of the path, if the path is a dir, return null.
See also
arena_alloc().

◆ path_join()

string * path_join ( arena_t arena,
const string first,
const string second 
)

Joins two paths together.

Works with paths accounting for missing trailing and leading slashes.

Parameters
arenaThe arena to allocate from.
firstThe first path to join.
secondThe second path to join.
Precondition
arena, first, and second must be valid and cannot be null.
Returns
The joined path.

◆ path_remove()

b32 path_remove ( const string path)

Removes a file or directory at path.

By either:

  • For Linux: Removing the file or directory at path using remove().
  • For Windows: Removing the file or directory at path using ShFileOperationW().
Precondition
path must be valid, and cannot be null.
Parameters
pathThe path to remove.
Returns
True if the path was removed, false if it failed.
See also
remove(), ShFileOperationW()

◆ path_stem()

string * path_stem ( arena_t arena,
const string path 
)

Get the stem of path.

Trims to the stem of path, i.e the extension prior to the extension, through pointer arithmetic, and allocating the new string using arena_alloc().

Parameters
arenaThe arena to allocate from.
pathThe path to get the stem of.
Precondition
arena and path must be valid and cannot be null.
Returns
The stem of the path
See also
arena_alloc().