From 2a9214fb795e97aa754ac46346310f94b7e61bfd Mon Sep 17 00:00:00 2001 From: timerix Date: Tue, 14 Mar 2023 01:12:26 +0600 Subject: [PATCH] path_basename --- src/Filesystem/path.c | 12 ++++++++++++ src/Filesystem/path.h | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Filesystem/path.c b/src/Filesystem/path.c index b3d046e..ef977ce 100644 --- a/src/Filesystem/path.c +++ b/src/Filesystem/path.c @@ -70,3 +70,15 @@ char* path_parentDir(char* dir){ } return copy; } + + +char* path_basename(char* path, bool with_extension){ + i32 nameIndex=cptr_lastIndexOfChar(path, path_sep)+1; + string rezult=string_fromCptr(path+nameIndex); + if(!with_extension){ + i32 extIndex=cptr_lastIndexOfChar(rezult.ptr, '.'); + if(extIndex!=0 && extIndex!=-1) + rezult.length=extIndex; + } + return string_extract(rezult); +} diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h index 8b3d1cc..29571aa 100644 --- a/src/Filesystem/path.h +++ b/src/Filesystem/path.h @@ -31,7 +31,11 @@ char* path_fixSeparators(const char* path); /// @return Maybe Maybe path_throwIfEscapes(const char* path); -char* path_parentDir(char* dir); +///@return path of parent dir +char* path_parentDir(char* path); + +///@return file name +char* path_basename(char* path, bool with_extension); #if __cplusplus }