Skip to content

Commit 49095e5

Browse files
committed
path_utils: new mp_stripext shorthand
This functionality has at least two users, screenshot template expansion of %F and the filename/no-ext property getter. Generalize what the latter does, so it can be reused by the former, which currently relies on a private stripext function. Also the check in mp_splitext, if there is a leading dot in the path did not work on paths that were not basenames already. It would've also split a partial path, in case there were more than one. And the check for / in the split portion, was ignoring the possibility of windows paths which use \ as separator. Thus only work with the basename of the provided path.
1 parent b89f7c7 commit 49095e5

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

misc/path_utils.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,27 @@ void mp_path_strip_trailing_separator(char *path)
9292
char *mp_splitext(const char *path, bstr *root)
9393
{
9494
mp_assert(path);
95-
int skip = (*path == '.'); // skip leading dot for "hidden" unix files
96-
const char *split = strrchr(path + skip, '.');
97-
if (!split || !split[1] || strchr(split, '/'))
95+
char *bn = mp_basename(path);
96+
97+
// Skip all leading dots, not just for "hidden" unix files, otherwise we
98+
// end up splitting a part of the filename sans leading dot.
99+
while (*bn == '.')
100+
bn++ ;
101+
102+
const char *split = strrchr(bn, '.');
103+
if (!split || !split[1])
98104
return NULL;
99105
if (root)
100106
*root = (bstr){(char *)path, split - path};
101107
return (char *)split + 1;
102108
}
103109

110+
char *mp_stripext(void *talloc_ctx, const char *s)
111+
{
112+
bstr root;
113+
return mp_splitext(s, &root) ? bstrto0(talloc_ctx, root) : (char *)s;
114+
}
115+
104116
bool mp_path_is_absolute(struct bstr path)
105117
{
106118
if (path.len && strchr(mp_path_separators, path.start[0]))

misc/path_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ char *mp_basename(const char *path);
3737
*/
3838
char *mp_splitext(const char *path, bstr *root);
3939

40+
// This is a shorthand to remove the extension
41+
char *mp_stripext(void *talloc_ctx, const char *s);
42+
4043
/* Return struct bstr referencing directory part of path, or if that
4144
* would be empty, ".".
4245
*/

0 commit comments

Comments
 (0)