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
|
||||
has_prefix (const char *str,
|
||||
const char *prefix)
|
||||
|
Reference in New Issue
Block a user