comparison src/core/filesystem.cpp @ 60:d417e9381ca5

filesystem: WIP class-ification of paths
author Paper <mrpapersonic@gmail.com>
date Fri, 29 Sep 2023 13:52:50 -0400
parents 327e9a5c72f1
children 327568ad9be9
comparison
equal deleted inserted replaced
59:327e9a5c72f1 60:d417e9381ca5
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(const std::string& path) { 25 /* FIXME: This is a very C-like way of doing this.
26 Make a path class. */
27 class Path {
28 public:
29 Path();
30 Path(std::string path);
31 Path(const Path& path);
32 bool CreateDirectories() const;
33 bool Exists() const;
34 std::string Basename();
35 std::string Stem();
36 std::string Extension();
37 Path GetParent();
38 void SetPath();
39
40 private:
41 std::string _path;
42 }
43
44 bool Path::CreateDirectories() const {
26 std::string temp = ""; 45 std::string temp = "";
27 size_t start; 46 size_t start;
28 size_t end = 0; 47 size_t end = 0;
29 temp.append(path.substr(0, path.find_first_not_of(DELIM, end))); 48 temp.append(_path.substr(0, _path.find_first_not_of(DELIM, end)));
30 49
31 while ((start = path.find_first_not_of(DELIM, end)) != std::string::npos) { 50 while ((start = _path.find_first_not_of(DELIM, end)) != std::string::npos) {
32 end = path.find(DELIM, start); 51 end = _path.find(DELIM, start);
33 temp.append(path.substr(start, end - start)); 52 temp.append(_path.substr(start, end - start));
34 #ifdef WIN32 53 #ifdef WIN32
35 if (!CreateDirectoryA(temp.c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND) 54 if (!CreateDirectoryA(temp.c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND)
36 /* ERROR_PATH_NOT_FOUND should NOT happen here */ 55 /* ERROR_PATH_NOT_FOUND should NOT happen here */
37 return false; 56 return false;
38 #else 57 #else
43 temp.append(DELIM); 62 temp.append(DELIM);
44 } 63 }
45 return true; 64 return true;
46 } 65 }
47 66
48 bool Exists(const std::string& path) { 67 bool Path::Exists() const {
49 #if WIN32 68 #if WIN32
50 return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES; 69 return GetFileAttributes(_path.c_str()) != INVALID_FILE_ATTRIBUTES;
51 #else 70 #else
52 struct stat st; 71 struct stat st;
53 return stat(path.c_str(), &st) == 0; 72 return stat(_path.c_str(), &st) == 0;
54 #endif 73 #endif
55 } 74 }
56 75
57 std::string Basename(const std::string& path) { 76 std::string Path::Basename() const {
58 return path.substr(0, path.find_last_of(DELIM)); 77 return _path.substr(0, path.find_last_of(DELIM));
59 } 78 }
60 79
61 std::string Stem(const std::string& path) { 80 std::string Path::Stem() const {
62 std::string basename = Basename(path); 81 std::string basename = Basename();
63 return basename.substr(0, path.find_last_of(".")); 82 return basename.substr(0, basename.find_last_of("."));
64 } 83 }
65 84
66 std::string GetParentDirectory(const std::string& path) { 85 std::string Path::Extension() const {
67 return path.substr(0, path.find_last_of(DELIM)); 86 std::string basename = Basename(_path);
87 return basename.substr(basename.find_last_of("."), basename.end());
88 }
89
90 std::string Path::GetParent() const {
91 return _path.substr(0, _path.find_last_of(DELIM));
68 } 92 }
69 93
70 std::string GetDotPath(void) { 94 std::string GetDotPath(void) {
71 std::string ret = ""; 95 std::string ret = "";
72 #ifdef WIN32 96 #ifdef WIN32