Recursive directory traversal with filters for hidden files, regular files vs. directories, and user-supplied callbacks.
Source code:
get_ordered_filename_array()¶
Retrieves an ordered list of filenames from a specified directory, optionally filtered by a pattern and search options.
int get_ordered_filename_array(
hgobj gobj,
const char *root_dir,
const char *re,
wd_option opt,
dir_array_t *da
);Parameters
| Key | Type | Description |
|---|---|---|
gobj | hgobj | A handle to the GObj instance, used for logging and error reporting. |
root_dir | const char * | The root directory from which to retrieve filenames. |
re | const char * | A regex pattern to filter filenames. If NULL, all files are included. |
opt | wd_option | Options for directory traversal, such as recursion and file type filtering. |
da | dir_array_t * | Pointer to a dir_array_t structure that will receive the results. |
Returns
Returns 0 on success, or a negative value on error. Results are stored in the da structure. Free with dir_array_free().
Notes
This function uses qsort() to sort the filenames. The returned array must be freed properly to avoid memory leaks.
walk_dir_tree()¶
The walk_dir_tree() function traverses a directory tree starting from root_dir, applying a user-defined callback function cb to each file or directory that matches the specified pattern and opt options.
int walk_dir_tree(
hgobj gobj,
const char *root_dir,
const char *pattern,
wd_option opt,
walkdir_cb cb,
void *user_data
);Parameters
| Key | Type | Description |
|---|---|---|
gobj | hgobj | A handle to the Yuneta framework object, used for logging and error handling. |
root_dir | const char * | The root directory from which the traversal begins. |
pattern | const char * | A regular expression pattern to match file or directory names. |
opt | wd_option | Options controlling the traversal behavior, such as recursion, hidden file inclusion, and file type matching. |
cb | walkdir_cb | A callback function that is invoked for each matching file or directory. |
user_data | void * | A user-defined pointer passed to the callback function. |
Returns
Returns 0 on success, or -1 if an error occurs (e.g., if root_dir does not exist or pattern is invalid).
Notes
The callback function cb should return TRUE to continue traversal or FALSE to stop. The function uses regcomp() to compile the pattern and regexec() to match file names.
dir_array_free()¶
Frees all memory associated with a directory array structure.
void dir_array_free(
dir_array_t *da
);Parameters
| Key | Type | Description |
|---|---|---|
da | dir_array_t * | Pointer to the directory array structure to free. |
Returns
This function does not return a value.
dir_array_sort()¶
Sorts the filenames in a directory array in lexicographic order using qsort().
void dir_array_sort(
dir_array_t *da
);Parameters
| Key | Type | Description |
|---|---|---|
da | dir_array_t * | Pointer to the directory array structure to sort. |
Returns
This function does not return a value.
find_files_with_suffix_array()¶
Finds all regular files in a directory with a given suffix.
int find_files_with_suffix_array(
hgobj gobj,
const char *directory,
const char *suffix,
dir_array_t *da
);Parameters
| Key | Type | Description |
|---|---|---|
gobj | hgobj | A handle to the GObj instance, used for logging and error reporting. |
directory | const char * | The directory to search in. |
suffix | const char * | The file suffix to match (e.g., ".json"). |
da | dir_array_t * | Pointer to a dir_array_t structure that will receive the results. |
Returns
Returns 0 on success, or -1 on error.
walk_dir_array()¶
Recursively traverses a directory tree and populates an array with paths matching a regex pattern.
int walk_dir_array(
hgobj gobj,
const char *root_dir,
const char *re,
wd_option opt,
dir_array_t *da
);Parameters
| Key | Type | Description |
|---|---|---|
gobj | hgobj | A handle to the GObj instance, used for logging and error reporting. |
root_dir | const char * | The root directory from which the traversal begins. |
re | const char * | A regex pattern to filter filenames. If NULL, all entries are included. |
opt | wd_option | Options controlling the traversal behavior, such as recursion and file type filtering. |
da | dir_array_t * | Pointer to a dir_array_t structure that will receive the results. |
Returns
Returns 0 on success, or -1 on error.