Add has_path_prefix util
This commit is contained in:
39
utils.c
39
utils.c
@@ -112,6 +112,45 @@ strfreev (char **str_array)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Compares if str has a specific path prefix. This differs
|
||||||
|
from a regular prefix in two ways. First of all there may
|
||||||
|
be multiple slashes separating the path elements, and
|
||||||
|
secondly, if a prefix is matched that has to be en entire
|
||||||
|
path element. For instance /a/prefix matches /a/prefix/foo/bar,
|
||||||
|
but not /a/prefixfoo/bar. */
|
||||||
|
bool
|
||||||
|
has_path_prefix (const char *str,
|
||||||
|
const char *prefix)
|
||||||
|
{
|
||||||
|
while (TRUE)
|
||||||
|
{
|
||||||
|
/* Skip consecutive slashes to reach next path
|
||||||
|
element */
|
||||||
|
while (*str == '/')
|
||||||
|
str++;
|
||||||
|
while (*prefix == '/')
|
||||||
|
prefix++;
|
||||||
|
|
||||||
|
/* No more prefix path elements? Done! */
|
||||||
|
if (*prefix == 0)
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
|
/* Compare path element */
|
||||||
|
while (*prefix != 0 && *prefix != '/')
|
||||||
|
{
|
||||||
|
if (*str != *prefix)
|
||||||
|
return FALSE;
|
||||||
|
str++;
|
||||||
|
prefix++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Matched prefix path element,
|
||||||
|
must be entire str path element */
|
||||||
|
if (*str != '/' && *str != 0)
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
has_prefix (const char *str,
|
has_prefix (const char *str,
|
||||||
const char *prefix)
|
const char *prefix)
|
||||||
|
2
utils.h
2
utils.h
@@ -70,6 +70,8 @@ char* strdup_printf (const char *format,
|
|||||||
...);
|
...);
|
||||||
bool has_prefix (const char *str,
|
bool has_prefix (const char *str,
|
||||||
const char *prefix);
|
const char *prefix);
|
||||||
|
bool has_path_prefix (const char *str,
|
||||||
|
const char *prefix);
|
||||||
int fdwalk (int proc_fd,
|
int fdwalk (int proc_fd,
|
||||||
int (*cb)(void *data, int fd),
|
int (*cb)(void *data, int fd),
|
||||||
void *data);
|
void *data);
|
||||||
|
Reference in New Issue
Block a user