libdragon
Loading...
Searching...
No Matches
Enumerations | Functions
dir.c File Reference

Directory traversal, pattern matching, and globbing utilities. More...

Enumerations

enum  fnmatch_result_t { NO_MATCH = 0 , PARTIAL_MATCH = 1 , FULL_MATCH = 2 }
 

Functions

int dir_walk (const char *path, dir_walk_callback_t cb, void *data)
 Walk a directory tree.
 
bool dir_fnmatch (const char *pattern, const char *fullpath)
 Check if a filename matches a pattern.
 
int dir_glob (const char *pattern, const char *path, dir_walk_callback_t cb, void *data)
 Glob a directory tree using a pattern.
 

Detailed Description

Directory traversal, pattern matching, and globbing utilities.

Author
Giovanni Bajo giova.nosp@m.nnib.nosp@m.ajo@g.nosp@m.mail.nosp@m..com

Enumeration Type Documentation

◆ fnmatch_result_t

Return values for fnmatch_partial

Function Documentation

◆ dir_walk()

int dir_walk ( const char *  path,
dir_walk_callback_t  cb,
void *  data 
)

Walk a directory tree.

This function walks a directory tree, calling the callback for each file and directory found.

The callback is of type dir_walk_callback_t, and its return value determines the behavior of the walk. In fact, the callback can request to abort the walk (DIR_WALK_ABORT), skip the current directory (DIR_WALK_SKIPDIR), or stop walking the current directory and return up one level (DIR_WALK_GOUP). See dir_walk_callback_t for more information.

Parameters
pathThe path to the directory to walk
cbThe callback function to call for each file and directory
dataUser data to pass to the callback
Returns
0 on success
-1 on error (errno will be set)
-2 if abort was requested by the callback via DIR_WALK_ABORT

◆ dir_fnmatch()

bool dir_fnmatch ( const char *  pattern,
const char *  fullpath 
)

Check if a filename matches a pattern.

This function is a simplified version of fnmatch that only supports the following special characters:

  • ? - Matches any single character
  • * - Matches any sequence of characters, except '/'. It can be used to match multiple files in a single directory.
  • ** - Matches any sequence of characters, including '/'. It can be used to match files within directory trees

Example of patterns:

*.txt - Matches all files with a .txt extension
**/*.txt - Matches all files with a .txt extension in all directories
under the starting directory
hero/**/*.sprite - Matches all files with a .sprite extension in the
hero directory and all its subdirectories
catalog?.dat - Matches catalog1.dat, catalog2.dat, etc.
*w*/*.txt - Matches all files with a .txt extension in directories
that contain the letter 'w'
Parameters
patternThe pattern to match against
fullpathThe full path to match
Returns
true The filename matches the pattern
false The filename does not match the pattern

◆ dir_glob()

int dir_glob ( const char *  pattern,
const char *  path,
dir_walk_callback_t  cb,
void *  data 
)

Glob a directory tree using a pattern.

This function walks a directory tree searching for files and directories that match a pattern. The pattern is a simplified version of fnmatch; see dir_fnmatch for more information about the supported special characters.

The callback function is called for each file and directory that matches the pattern. The callback can then decide how to proceed using its return value (see dir_walk_callback_t for more information).

int mycb(const char *fn, dir_t *dir, void *data) {
debugf("Found sprite file: %s\n", fn);
}
// Search for all files with a .sprite extension in all directories under rom:/
dir_glob("**/*.sprite", "rom:/", mycb, NULL);
#define debugf(msg,...)
Write a message to the debugging channel.
Definition debug.h:181
int dir_glob(const char *pattern, const char *path, dir_walk_callback_t cb, void *data)
Glob a directory tree using a pattern.
Definition dir.c:170
@ DIR_WALK_CONTINUE
Continue walking.
Definition dir.h:94
Directory entry structure.
Definition dir.h:37
Note
the glob pattern is matched against pathnames relative to the starting directory. For example, if you start the search at "rom:/sprites", the pattern "hero/*.sprite" will match all files with a .sprite extension in the "rom:/sprites/hero" directory.
Parameters
patternThe pattern to match against (see dir_fnmatch)
pathThe path to the directory to start the search
cbThe callback function to call for each file and directory
dataUser data to pass to the callback
Returns
0 on success,
-1 on error (errno will be set)
-2 if abort was requested by the callback via DIR_WALK_ABORT