X_FILESYSTEM_VERSION_MAJOR
#define X_FILESYSTEM_VERSION_MAJOR 1
STDX - Filesystem Utilities Part of the STDX General Purpose C Library by marciovmf License: MIT https://github.com/marciovmf/stdx
Provides a cross-platform filesystem abstraction including:
To compile the implementation define X_IMPL_FILESYSTEM
in one source file before including this header.
To customize how this module allocates memory, define
X_FILESYSTEM_ALLOC / X_FILESYSTEM_CALLOC / X_FILESYSTEM_FREE before including.
stdx_string.h
#define X_FILESYSTEM_VERSION_MAJOR 1
#define X_FILESYSTEM_VERSION_MINOR 0
#define X_FILESYSTEM_VERSION_PATCH 0
#define X_FILESYSTEM_VERSION(X_FILESYSTEM_VERSION_MAJOR *10000+X_FILESYSTEM_VERSION_MINOR *100+X_FILESYSTEM_VERSION_PATCH)
#define X_FS_PAHT_MAX_LENGTH 512
#define X_FS_PATH_SEPARATOR '\\'
#define X_FS_ALT_PATH_SEPARATOR '/'
#define X_FS_PATH_SEPARATOR '/'
#define X_FS_ALT_PATH_SEPARATOR '\\'
typedef XSmallstr XFSPath;
typedef struct XFSDireEntry_t XFSDireEntry;
typedef struct XFSDireHandle_t XFSDireHandle;
typedef struct XFSWatch_t XFSWatch;
struct XFSDireEntry_t{
char name[X_FS_PAHT_MAX_LENGTH];
size_t size;
time_t last_modified;
int32_t is_directory;
};
typedef enum{
x_fs_watch_CREATED,
x_fs_watch_DELETED,
x_fs_watch_MODIFIED,
x_fs_watch_RENAMED_FROM,
x_fs_watch_RENAMED_TO,
x_fs_watch_UNKNOWN
}XFSWatchEventType;
typedef struct{
XFSWatchEventType action;
const char *filename;
}XFSWatchEvent;
typedef struct XFSTime{
int year;
int month;
int day;
int hour;
int minute;
int second;
}XFSTime;
#define x_fs_path(out, ...) x_fs_path_(out, __VA_ARGS__, 0)
#define x_fs_path_join(path, ...) x_fs_path_join_(path, __VA_ARGS__, 0)
#define x_fs_path_join_slice(path, ...) x_fs_path_join_slice_(path, __VA_ARGS__, 0)
typedef struct{
size_t size;
time_t creation_time;
time_t modification_time;
uint32_t permissions;
}FSFileStat;
Begin a directory enumeration and retrieve the first entry.
X_FILESYSTEM_API XFSDireHandle * x_fs_find_first_file(
const char *path,
XFSDireEntry *entry
);
const char *pathXFSDireEntry *entryHandle used for subsequent enumeration, or NULL on failure.
Create a directory.
X_FILESYSTEM_API bool x_fs_directory_create(const char *path);
const char *pathTrue on success, false on failure.
Create a directory and any missing parent directories.
X_FILESYSTEM_API bool x_fs_directory_create_recursive(const char *path);
const char *pathTrue on success, false on failure.
Delete an empty directory.
X_FILESYSTEM_API bool x_fs_directory_delete(const char *directory);
const char *directoryTrue on success, false on failure.
Copy a file to a new path.
X_FILESYSTEM_API bool x_fs_file_copy(
const char *file,
const char *newFile
);
const char *fileconst char *newFileTrue on success, false on failure.
Rename (move) a file to a new path.
X_FILESYSTEM_API bool x_fs_file_rename(
const char *file,
const char *newFile
);
const char *fileconst char *newFileTrue on success, false on failure.
Continue a directory enumeration and retrieve the next entry.
X_FILESYSTEM_API bool x_fs_find_next_file(
XFSDireHandle *dirHandle,
XFSDireEntry *entry
);
XFSDireHandle *dirHandleXFSDireEntry *entryTrue if an entry was written, false if no more entries or on failure.
Get the system temporary folder path.
X_FILESYSTEM_API size_t x_fs_get_temp_folder(XFSPath *out);
XFSPath *outLength of the written path (in bytes/chars, excluding terminator), or 0 on failure.
Get the current working directory.
X_FILESYSTEM_API size_t x_fs_cwd_get(XFSPath *path);
XFSPath *pathLength of the written path (in bytes/chars, excluding terminator), or 0 on failure.
Set the current working directory.
X_FILESYSTEM_API size_t x_fs_cwd_set(const char *path);
const char *pathNon-zero on success, 0 on failure.
Set the current working directory to the executable's directory.
X_FILESYSTEM_API size_t x_fs_cwd_set_from_executable_path(void);
Non-zero on success, 0 on failure.
Close a directory enumeration handle.
X_FILESYSTEM_API void x_fs_find_close(XFSDireHandle *dirHandle);
XFSDireHandle *dirHandleNothing.
Open a filesystem watcher for the given path.
X_FILESYSTEM_API XFSWatch * x_fs_watch_open(const char *path);
const char *pathWatch handle, or NULL on failure.
Close a filesystem watcher.
X_FILESYSTEM_API void x_fs_watch_close(XFSWatch *fw);
XFSWatch *fwNothing.
Poll a filesystem watcher for pending events.
X_FILESYSTEM_API int32_t x_fs_watch_poll(
XFSWatch *fw,
XFSWatchEvent *out_events,
int32_t max_events
);
XFSWatch *fwXFSWatchEvent *out_eventsint32_t max_eventsNumber of events written, or a negative value on error.
Normalize a path in-place (e.g., separators, ".", ".." where possible).
X_FILESYSTEM_API XFSPath * x_fs_path_normalize(XFSPath *input);
XFSPath *inputPointer to the normalized path (same as input).
Get the filename stem (basename without extension) from a path string.
X_FILESYSTEM_API XSlice x_fs_path_stem_cstr(const char *input);
const char *inputSlice pointing into input containing the stem.
X_FILESYSTEM_API XSlice x_fs_path_stem_as_slice(const XFSPath *input);
X_FILESYSTEM_API size_t x_fs_path_stem(
const XFSPath *input,
XFSPath *out
);
Get the basename (final component) from a path string.
X_FILESYSTEM_API XSlice x_fs_path_basename_cstr(const char *input);
const char *inputSlice pointing into input containing the basename.
X_FILESYSTEM_API XSlice x_fs_path_basename_as_slice(const XFSPath *input);
X_FILESYSTEM_API size_t x_fs_path_basename(
const XFSPath *input,
XFSPath *out
);
Get the directory name (everything before the final component) from a path string.
X_FILESYSTEM_API XSlice x_fs_path_dirname_cstr(const char *input);
const char *inputSlice pointing into input containing the directory portion.
X_FILESYSTEM_API XSlice x_fs_path_dirname_as_slice(const XFSPath *input);
X_FILESYSTEM_API size_t x_fs_path_dirname(
const XFSPath *input,
XFSPath *out
);
Build a path from one or more C string components (variadic), writing to out.
X_FILESYSTEM_API bool x_fs_path_(
XFSPath *out,
...
);
XFSPath *outTrue on success, false on failure.
X_FILESYSTEM_API size_t x_fs_path_join_(
XFSPath *path,
...
);
X_FILESYSTEM_API size_t x_fs_path_join_slice_(
XFSPath *path,
...
);
X_FILESYSTEM_API XSlice x_fs_path_as_slice(const XFSPath *path);
Check whether a path exists on disk.
X_FILESYSTEM_API bool x_fs_path_exists(const XFSPath *path);
const XFSPath *pathTrue if it exists, false otherwise.
Check whether a C-string path exists on disk.
X_FILESYSTEM_API bool x_fs_path_exists_cstr(const char *path);
const char *pathTrue if it exists, false otherwise.
Check whether a path exists on disk using a faster, less thorough method.
X_FILESYSTEM_API bool x_fs_path_exists_quick(const XFSPath *path);
const XFSPath *pathTrue if it exists, false otherwise.
Check whether a C-string path exists on disk using a faster, less thorough method.
X_FILESYSTEM_API bool x_fs_path_exists_quick_cstr(const char *path);
const char *pathTrue if it exists, false otherwise.
Check whether a path is absolute.
X_FILESYSTEM_API bool x_fs_path_is_absolute(const XFSPath *path);
const XFSPath *pathTrue if absolute, false otherwise.
Check whether a C-string path is absolute.
X_FILESYSTEM_API bool x_fs_path_is_absolute_cstr(const char *path);
const char *pathTrue if absolute, false otherwise.
Check whether a path is absolute using native platform rules.
X_FILESYSTEM_API bool x_fs_path_is_absolute_native(const XFSPath *path);
const XFSPath *pathTrue if absolute, false otherwise.
Check whether a C-string path is absolute using native platform rules.
X_FILESYSTEM_API bool x_fs_path_is_absolute_native_cstr(const char *path);
const char *pathTrue if absolute, false otherwise.
Check whether a path points to an existing directory.
X_FILESYSTEM_API bool x_fs_path_is_directory(const XFSPath *path);
const XFSPath *pathTrue if it is a directory, false otherwise.
Check whether a C-string path points to an existing directory.
X_FILESYSTEM_API bool x_fs_path_is_directory_cstr(const char *path);
const char *pathTrue if it is a directory, false otherwise.
Check whether a path points to an existing file.
X_FILESYSTEM_API bool x_fs_path_is_file(const XFSPath *path);
const XFSPath *pathTrue if it is a file, false otherwise.
Check whether a C-string path points to an existing file.
X_FILESYSTEM_API bool x_fs_path_is_file_cstr(const char *path);
const char *pathTrue if it is a file, false otherwise.
Check whether a path is relative.
X_FILESYSTEM_API bool x_fs_path_is_relative(const XFSPath *path);
const XFSPath *pathTrue if relative, false otherwise.
Check whether a C-string path is relative.
X_FILESYSTEM_API bool x_fs_path_is_relative_cstr(const char *path);
const char *pathTrue if relative, false otherwise.
Get a NUL-terminated C string view of an XFSPath.
X_FILESYSTEM_API const char * x_fs_path_cstr(const XFSPath *p);
const XFSPath *pPointer to the internal NUL-terminated string.
Append a single component to a path.
X_FILESYSTEM_API size_t x_fs_path_append(
XFSPath *p,
const char *comp
);
XFSPath *pconst char *compNew length of the path after appending.
Replace or set the extension of a path.
X_FILESYSTEM_API size_t x_fs_path_change_extension(
XFSPath *path,
const char *new_ext
);
XFSPath *pathconst char *new_extNew length of the path after the change.
Compare two paths, ignoring separator type.
X_FILESYSTEM_API size_t x_fs_path_compare(
const XFSPath *a,
const XFSPath *b
);
0 if equal, <0 if a < b, >0 if a > b.
Compare a path against a C string path, ignoring separator type.
X_FILESYSTEM_API int32_t x_fs_path_compare_cstr(
const XFSPath *a,
const char *cstr
);
const XFSPath *aconst char *cstr0 if equal, <0 if a < cstr, >0 if a > cstr.
Check whether a path equals a small string.
X_FILESYSTEM_API bool x_fs_path_eq(
const XFSPath *a,
const XSmallstr *b
);
True if equal, false otherwise.
Check whether a path equals a C string path.
X_FILESYSTEM_API bool x_fs_path_eq_cstr(
const XFSPath *a,
const char *b
);
const XFSPath *aconst char *bTrue if equal, false otherwise.
Get the extension from a path string.
X_FILESYSTEM_API XSlice x_fs_path_extension_cstr(const char *input);
const char *inputSlice pointing into input containing the extension (may be empty).
X_FILESYSTEM_API XSlice x_fs_path_extension_as_slice(const XFSPath *input);
X_FILESYSTEM_API size_t x_fs_path_extension(
const XFSPath *input,
XFSPath *out
);
Convert a slice into an XFSPath.
X_FILESYSTEM_API size_t x_fs_path_from_slice(
XSlice sv,
XFSPath *out
);
Length written to out.
Compute a relative path from one C-string path to another.
X_FILESYSTEM_API size_t x_fs_path_relative_to_cstr(
const char *from_path,
const char *to_path,
XFSPath *out_path
);
const char *from_pathconst char *to_pathXFSPath *out_pathLength of the written relative path, or 0 on failure.
Compute the common prefix of two paths.
X_FILESYSTEM_API bool x_fs_path_common_prefix(
const char *from_path,
const char *to_path,
XFSPath *out_path
);
const char *from_pathconst char *to_pathXFSPath *out_pathTrue if a common prefix was written (may be empty depending on implementation), false on failure.
Set a path from a C string.
X_FILESYSTEM_API size_t x_fs_path_set(
XFSPath *p,
const char *cstr
);
XFSPath *pconst char *cstrLength written to p.
Set a path from a stdx slice.
X_FILESYSTEM_API size_t x_fs_path_set_slice(
XFSPath *p,
XSlice slice
);
Length written to p.
Split a path string into components.
X_FILESYSTEM_API bool x_fs_path_split(
const char *input,
XFSPath *out_components,
size_t max_components,
size_t *out_count
);
const char *inputXFSPath *out_componentssize_t max_componentssize_t *out_countTrue on success, false on failure.
Get the executable path (or executable directory, depending on implementation).
X_FILESYSTEM_API size_t x_fs_path_from_executable(XFSPath *out);
XFSPath *outLength written to out, or 0 on failure.
Retrieve metadata about a file or directory.
X_FILESYSTEM_API bool x_fs_file_stat(
const char *path,
FSFileStat *out_stat
);
const char *pathFSFileStat *out_statTrue on success, false on failure.
Retrieve the last modification time of a file or directory.
X_FILESYSTEM_API bool x_fs_file_modification_time(
const char *path,
time_t *out_time
);
const char *pathtime_t *out_timeTrue on success, false on failure.
Retrieve the creation time of a file or directory.
X_FILESYSTEM_API bool x_fs_file_creation_time(
const char *path,
time_t *out_time
);
const char *pathtime_t *out_timeTrue on success, false on failure.
Retrieve permissions metadata for a file or directory.
X_FILESYSTEM_API bool x_fs_file_permissions(
const char *path,
uint32_t *out_permissions
);
const char *pathuint32_t *out_permissionsTrue on success, false on failure.
Set permissions metadata for a file or directory.
X_FILESYSTEM_API bool x_fs_file_set_permissions(
const char *path,
uint32_t permissions
);
const char *pathuint32_t permissionsTrue on success, false on failure.
Check whether a path points to an existing regular file.
X_FILESYSTEM_API bool x_fs_is_file(const char *path);
const char *pathTrue if it is a file, false otherwise.
Check whether a path points to an existing directory.
X_FILESYSTEM_API bool x_fs_is_directory(const char *path);
const char *pathTrue if it is a directory, false otherwise.
Check whether a path is a symbolic link.
X_FILESYSTEM_API bool x_fs_is_symlink(const char *path);
const char *pathTrue if it is a symlink, false otherwise.
Read the target of a symbolic link.
X_FILESYSTEM_API bool x_fs_read_symlink(
const char *path,
XFSPath *out_path
);
const char *pathXFSPath *out_pathTrue on success, false on failure.
Create a temporary file with the given prefix.
X_FILESYSTEM_API bool x_fs_make_temp_file(
const char *prefix,
XFSPath *out_path
);
const char *prefixXFSPath *out_pathTrue on success, false on failure.
Create a temporary directory with the given prefix.
X_FILESYSTEM_API bool x_fs_make_temp_directory(
const char *prefix,
XFSPath *out_path
);
const char *prefixXFSPath *out_pathTrue on success, false on failure.
Convert a time value from epoch to XFSTime.
X_FILESYSTEM_API XFSTime x_fs_time_from_epoch(time_t t);
time_t tXFSTime representation of the given epoch time.
Compute a relative path from one path to another.
X_FILESYSTEM_API size_t x_fs_path_relative_to(
const XFSPath *from_path,
const XFSPath *to_path,
XFSPath *out_path
);
Length of the resulting path on success, 0 on failure.
#define MAX_PATH PATH_MAX
#define X_FS_MAX_PATH MAX_PATH
Internal macro for allocating memory. To override how this header allocates memory, define this macro with a different implementation before including this header.
#define X_FILESYSTEM_ALLOC(sz) malloc(sz)
szInternal macro for allocating zeroed memory. To override how this header allocates zeroed memory, define this macro with a different implementation before including this header.
#define X_FILESYSTEM_CALLOC(n, sz) calloc((n), (sz))
szInternal macro for freeing memory. To override how this header frees memory, define this macro with a different implementation before including this header.
#define X_FILESYSTEM_FREE(p) free(p)
pstruct dirent *entry;
struct stat fileStat;
X_FILESYSTEM_API int32_t x_fs_path_join_one_slice(
XFSPath *out,
const XSlice segment
);
X_FILESYSTEM_API int32_t x_fs_path_join_one(
XFSPath *out,
const char *segment
);
X_FILESYSTEM_API void x_fs_path_clone(
XFSPath *out,
const XFSPath *path
);
X_FILESYSTEM_API void x_fs_path_init(XFSPath *p);