Browse Source

Remade file_getFolderPath into _file_getParentFolder, which is more reusable.

David Piuva 3 years ago
parent
commit
2feb0444da
2 changed files with 8 additions and 9 deletions
  1. 4 5
      Source/DFPSR/api/fileAPI.cpp
  2. 4 4
      Source/DFPSR/api/fileAPI.h

+ 4 - 5
Source/DFPSR/api/fileAPI.cpp

@@ -148,8 +148,8 @@ ReadableString file_getPathlessName(const ReadableString &path) {
 	return string_after(path, getLastSeparator(path, -1));
 }
 
-ReadableString file_getFolderPath(const ReadableString &path) {
-	return string_before(path, getLastSeparator(path, string_length(path)));
+ReadableString file_getParentFolder(const ReadableString &path) {
+	return string_before(path, getLastSeparator(path, 0));
 }
 
 bool file_hasRoot(const ReadableString &path) {
@@ -184,16 +184,15 @@ static String file_getApplicationFilePath() {
 
 String file_getApplicationFolder(bool allowFallback) {
 	#ifdef USE_MICROSOFT_WINDOWS
-		return file_getFolderPath(file_getApplicationFilePath());
+		return file_getParentFolder(file_getApplicationFilePath());
 	#else
 		#ifdef USE_LINUX
-			// TODO: Implement using "/proc/self/exe" on Linux
 			//       https://www.wikitechy.com/tutorials/linux/how-to-find-the-location-of-the-executable-in-c
 			NativeChar resultBuffer[maxLength + 1] = {0};
 			//"/proc/curproc/file" on FreeBSD, which is not yet supported
     		//"/proc/self/path/a.out" on Solaris, which is not yet supported
 			readlink("/proc/self/exe", resultBuffer, maxLength);
-			return file_getFolderPath(fromNativeString(resultBuffer));
+			return file_getParentFolder(fromNativeString(resultBuffer));
 		#else
 			if (allowFallback) {
 				return file_getCurrentPath();

+ 4 - 4
Source/DFPSR/api/fileAPI.h

@@ -78,13 +78,13 @@ namespace dsr {
 	//   file_getFolderPath(U"MyFolder/Folder2")  == U"Folder2"
 	ReadableString file_getPathlessName(const ReadableString &path);
 
-	// Returns the file's folder path with the file removed, or path unchanged if it was already a path.
-	// Does not include the last slash.
+	// Returns the parent folder path with anything after the last slash removed, or empty if there was no slash left.
 	// Examples with / as the path separator:
 	//   file_getFolderPath(U"MyFolder/Documents/Cars.txt") == U"MyFolder/Documents"
 	//   file_getFolderPath(U"MyFolder/Documents/")         == U"MyFolder/Documents"
-	//   file_getFolderPath(U"MyFolder/Documents")          == U"MyFolder/Documents"
-	ReadableString file_getFolderPath(const ReadableString &path);
+	//   file_getFolderPath(U"MyFolder/Documents")          == U"MyFolder"
+	//   file_getFolderPath(U"MyFolder")                    == U""
+	ReadableString file_getParentFolder(const ReadableString &path);
 
 	// Combines two parts into a path and automatically adding a local separator when needed.
 	// Can be used to get the full path of a file in a folder or add another folder to the path.