changeset 61:327568ad9be9

core/fs: finish class-ification of paths
author Paper <mrpapersonic@gmail.com>
date Fri, 29 Sep 2023 15:52:31 -0400
parents d417e9381ca5
children 4c6dd5999b39
files include/core/filesystem.h src/core/config.cpp src/core/filesystem.cpp
diffstat 3 files changed, 48 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/include/core/filesystem.h	Fri Sep 29 13:52:50 2023 -0400
+++ b/include/core/filesystem.h	Fri Sep 29 15:52:31 2023 -0400
@@ -4,14 +4,27 @@
 
 namespace Filesystem {
 
-bool CreateDirectories(const std::string& path);
-bool Exists(const std::string& path);
-std::string Basename(const std::string& path);
-std::string Stem(const std::string& path);
-std::string GetParentDirectory(const std::string& path);
-std::string GetDotPath(); // %APPDATA%/minori, ~/Library/Application Support/minori, ~/.config/minori...
-std::string GetConfigPath(); // (dotpath)/config.json
-std::string GetAnimeDBPath(); // (dotpath)/anime/db.json
+class Path {
+        public:
+                Path();
+                Path(const std::string& path);
+                Path(const Path& path);
+                bool CreateDirectories() const;
+                bool Exists() const;
+                std::string Basename() const;
+                std::string Stem() const;
+                std::string Extension() const;
+                Path GetParent() const;
+                void SetPath(const std::string& path);
+		std::string GetPath() const;
+
+        private:
+                std::string _path;
+};
+
+Path GetDotPath(); // %APPDATA%/minori, ~/Library/Application Support/minori, ~/.config/minori...
+Path GetConfigPath(); // (dotpath)/config.json
+Path GetAnimeDBPath(); // (dotpath)/anime/db.json
 
 } // namespace Filesystem
 
--- a/src/core/config.cpp	Fri Sep 29 13:52:50 2023 -0400
+++ b/src/core/config.cpp	Fri Sep 29 15:52:31 2023 -0400
@@ -47,10 +47,10 @@
 };
 
 int Config::Load() {
-	std::string cfg_path = Filesystem::GetConfigPath();
-	if (!Filesystem::Exists(cfg_path))
+	Filesystem::Path cfg_path = Filesystem::GetConfigPath();
+	if (!cfg_path.Exists())
 		return 0;
-	std::ifstream config(cfg_path.c_str(), std::ifstream::in);
+	std::ifstream config(cfg_path.GetPath(), std::ifstream::in);
 	auto config_js = nlohmann::json::parse(config);
 	service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)];
 	anime_list.language = StringToAnimeTitleMap[JSON::GetString(
@@ -72,10 +72,10 @@
 }
 
 int Config::Save() {
-	std::string cfg_path = Filesystem::GetConfigPath();
-	if (!Filesystem::Exists(Filesystem::GetParentDirectory(cfg_path)))
-		Filesystem::CreateDirectories(Filesystem::GetParentDirectory(cfg_path));
-	std::ofstream config(cfg_path.c_str(), std::ofstream::out | std::ofstream::trunc);
+	Filesystem::Path cfg_path = Filesystem::GetConfigPath();
+	if (!cfg_path.GetParent().Exists())
+		cfg_path.GetParent().CreateDirectories();
+	std::ofstream config(cfg_path.GetPath(), std::ofstream::out | std::ofstream::trunc);
 	// clang-format off
 	nlohmann::json config_js = {
 		{"General",	{
--- a/src/core/filesystem.cpp	Fri Sep 29 13:52:50 2023 -0400
+++ b/src/core/filesystem.cpp	Fri Sep 29 15:52:31 2023 -0400
@@ -22,24 +22,9 @@
 
 namespace Filesystem {
 
-/* FIXME: This is a very C-like way of doing this.
-   Make a path class. */
-class Path {
-	public:
-		Path();
-		Path(std::string path);
-		Path(const Path& path);
-		bool CreateDirectories() const;
-		bool Exists() const;
-		std::string Basename();
-		std::string Stem();
-		std::string Extension();
-		Path GetParent();
-		void SetPath();
-
-	private:
-		std::string _path;
-}
+Path::Path() { _path = ""; }
+Path::Path(const std::string& path) { _path = path; }
+Path::Path(const Path& path) { _path = path.GetPath(); }
 
 bool Path::CreateDirectories() const {
 	std::string temp = "";
@@ -74,7 +59,7 @@
 }
 
 std::string Path::Basename() const {
-	return _path.substr(0, path.find_last_of(DELIM));
+	return _path.substr(0, _path.find_last_of(DELIM));
 }
 
 std::string Path::Stem() const {
@@ -83,15 +68,23 @@
 }
 
 std::string Path::Extension() const {
-	std::string basename = Basename(_path);
-	return basename.substr(basename.find_last_of("."), basename.end());
+	std::string basename = Basename();
+	return basename.substr(basename.find_last_of("."), basename.length());
 }
 
-std::string Path::GetParent() const {
+Path Path::GetParent() const {
 	return _path.substr(0, _path.find_last_of(DELIM));
 }
 
-std::string GetDotPath(void) {
+void Path::SetPath(const std::string& path) {
+	_path = path;
+}
+
+std::string Path::GetPath() const {
+	return _path;
+}
+
+Path GetDotPath(void) {
 	std::string ret = "";
 #ifdef WIN32
 	char buf[PATH_MAX + 1];
@@ -114,17 +107,17 @@
 	return ret;
 }
 
-std::string GetConfigPath(void) {
+Path GetConfigPath(void) {
 	std::string ret = "";
-	ret += GetDotPath();
+	ret += GetDotPath().GetPath();
 	if (!ret.empty())
 		ret += DELIM CONFIG_NAME;
 	return ret;
 }
 
-std::string GetAnimeDBPath(void) {
+Path GetAnimeDBPath(void) {
 	std::string ret = "";
-	ret += GetDotPath();
+	ret += GetDotPath().GetPath();
 	if (!ret.empty())
 		ret += DELIM "anime" DELIM "db.json";
 	return ret;