fixed bugs in path functions

This commit is contained in:
Timerix 2025-08-04 16:33:58 +03:00
parent e0f1941c82
commit ab55f85160
2 changed files with 25 additions and 8 deletions

View File

@ -46,4 +46,5 @@ Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin){
"Can't seek (offset: " IFWIN("%lli", "%li") ", origin: %i) in file: %s", "Can't seek (offset: " IFWIN("%lli", "%li") ", origin: %i) in file: %s",
offset, origin, strerror(errno)); offset, origin, strerror(errno));
} }
return RESULT_VOID;
} }

View File

@ -1,8 +1,16 @@
#include "tlibc/filesystem.h" #include "tlibc/filesystem.h"
str path_dirname(str path){ str path_dirname(str path){
i32 sepIndex = str_seekCharReverse(path, path_sep, 0); if(path.size == 0)
if(sepIndex <= 0) return path;
// remove trailing slash (name/)
if(path.data[path.size - 1] == path_sep){
path.size -= 1;
}
i32 sepIndex = str_seekCharReverse(path, path_sep, -1);
if(sepIndex < 0)
return path; return path;
path.size = sepIndex; path.size = sepIndex;
@ -11,13 +19,21 @@ str path_dirname(str path){
} }
str path_basename(str path, bool remove_ext){ str path_basename(str path, bool remove_ext){
i32 nameIndex = str_seekCharReverse(path, path_sep, 0) + 1; if(path.size == 0)
if(nameIndex == path.size)
return path; return path;
// remove trailing slash (name/)
if(path.data[path.size - 1] == path_sep){
path.size -= 1;
}
i32 nameIndex = str_seekCharReverse(path, path_sep, -1) + 1;
if((u32)nameIndex != 0){
path.data += nameIndex; path.data += nameIndex;
path.size -= nameIndex; path.size -= nameIndex;
if(remove_ext) }
if(!remove_ext)
return path; return path;
i32 extIndex = str_seekCharReverse(path, '.', -1); i32 extIndex = str_seekCharReverse(path, '.', -1);