comparison src/core/filesystem.cpp @ 59:327e9a5c72f1

filesystem: add basename and stem functions
author Paper <mrpapersonic@gmail.com>
date Thu, 28 Sep 2023 13:45:45 -0400
parents 4b05bc7668eb
children d417e9381ca5
comparison
equal deleted inserted replaced
58:b7a1c0010ffd 59:327e9a5c72f1
20 #include "core/config.h" 20 #include "core/config.h"
21 #include <limits.h> 21 #include <limits.h>
22 22
23 namespace Filesystem { 23 namespace Filesystem {
24 24
25 bool CreateDirectories(std::string path) { 25 bool CreateDirectories(const std::string& path) {
26 std::string temp = ""; 26 std::string temp = "";
27 size_t start; 27 size_t start;
28 size_t end = 0; 28 size_t end = 0;
29 temp.append(path.substr(0, path.find_first_not_of(DELIM, end))); 29 temp.append(path.substr(0, path.find_first_not_of(DELIM, end)));
30 30
43 temp.append(DELIM); 43 temp.append(DELIM);
44 } 44 }
45 return true; 45 return true;
46 } 46 }
47 47
48 bool Exists(std::string path) { 48 bool Exists(const std::string& path) {
49 #if WIN32 49 #if WIN32
50 return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES; 50 return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES;
51 #else 51 #else
52 struct stat st; 52 struct stat st;
53 return stat(path.c_str(), &st) == 0; 53 return stat(path.c_str(), &st) == 0;
54 #endif 54 #endif
55 } 55 }
56 56
57 std::string GetParentDirectory(std::string path) { 57 std::string Basename(const std::string& path) {
58 size_t pos = 0; 58 return path.substr(0, path.find_last_of(DELIM));
59 pos = path.find_last_of(DELIM); 59 }
60 return path.substr(0, pos); 60
61 std::string Stem(const std::string& path) {
62 std::string basename = Basename(path);
63 return basename.substr(0, path.find_last_of("."));
64 }
65
66 std::string GetParentDirectory(const std::string& path) {
67 return path.substr(0, path.find_last_of(DELIM));
61 } 68 }
62 69
63 std::string GetDotPath(void) { 70 std::string GetDotPath(void) {
64 std::string ret = ""; 71 std::string ret = "";
65 #ifdef WIN32 72 #ifdef WIN32