diff src/core/filesystem.cpp @ 11:fc1bf97c528b

*: use C++11 standard I've been meaning to do this for a while, but I didn't want to reimplement the filesystem code. Now we are on C++11 and most compilers from the past 5 centuries should support this now
author Paper <mrpapersonic@gmail.com>
date Sun, 17 Sep 2023 06:14:30 -0400
parents 5c0397762b53
children cde8f67a7c7d
line wrap: on
line diff
--- a/src/core/filesystem.cpp	Sat Sep 16 02:06:01 2023 -0400
+++ b/src/core/filesystem.cpp	Sun Sep 17 06:14:30 2023 -0400
@@ -5,31 +5,82 @@
 #elif defined(__linux__)
 #include <pwd.h>
 #include <sys/types.h>
-#include <unistd.h>
 #endif
-#include "core/config.h"
+
+#ifdef WIN32
+#define DELIM "\\"
+#else
+#define DELIM "/"
+#include <unistd.h>
+#include <errno.h>
+#endif
+
 #include "core/filesystem.h"
-#include "core/strings.h"
-#include <QMessageBox>
-#include <filesystem>
+#include "core/config.h"
 #include <limits.h>
 
-std::filesystem::path get_config_path(void) {
-	std::filesystem::path cfg_path;
+namespace Filesystem {
+
+bool CreateDirectories(std::string path) {
+	std::string temp = "";
+	size_t start;
+	size_t end = 0;
+
+	while ((start = path.find_first_not_of(DELIM, end)) != std::string::npos)
+	{
+		end = path.find(DELIM, start);
+		temp.append(path.substr(start, end - start));
+#ifdef WIN32
+		if (!CreateDirectoryA(temp.c_str(), NULL) && GetLastError() == ERROR_PATH_NOT_FOUND)
+			/* ERROR_PATH_NOT_FOUND should NOT happen here */
+			return false;
+#else
+		if (mkdir(temp.c_str(), 0755))
+			return false;
+#endif
+		temp.append(DELIM);
+	}
+	return true;
+}
+
+bool Exists(std::string path) {
+#if WIN32
+	return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES;
+#else
+	struct stat st;
+	return stat(path.c_str(), &st) == 0;
+#endif
+}
+
+std::string GetParentDirectory(std::string path) {
+	size_t pos = 0;
+	pos = path.find_last_of(DELIM);
+	return path.substr(0, pos);
+}
+
+std::string GetConfigPath(void) {
+	std::string ret = "";
 #ifdef WIN32
 	char buf[PATH_MAX + 1];
-	if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK)
-		cfg_path = std::filesystem::path(buf) / CONFIG_NAME;
+	if (SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, CONFIG_DIR, buf) == S_OK) {
+		ret += buf;
+		ret += DELIM CONFIG_NAME;
+	}
 #elif defined(MACOSX)
-	/* pass all of our problems to */
-	cfg_path = std::filesystem::path(osx::GetApplicationSupportDirectory()) / CONFIG_DIR / CONFIG_NAME;
+	/* pass all of our problems to objc++ code */
+	ret += osx::GetApplicationSupportDirectory();
+	ret += DELIM CONFIG_DIR DELIM CONFIG_NAME;
 #else // just assume POSIX
 	if (getenv("HOME") != NULL)
-		cfg_path = std::filesystem::path(getenv("HOME")) / ".config" / CONFIG_DIR / CONFIG_NAME;
+		ret += getenv("HOME");
 #ifdef __linux__
 	else
-		cfg_path = std::filesystem::path(getpwuid(getuid())->pw_dir) / ".config" / CONFIG_DIR / CONFIG_NAME;
+		ret += getpwuid(getuid())->pw_dir;
 #endif // __linux__
+	if (!ret.empty())
+		ret += DELIM ".config" DELIM CONFIG_DIR DELIM CONFIG_NAME;
 #endif // !WIN32 && !MACOSX
-	return cfg_path;
+	return ret;
 }
+
+}