Browse code

Use C++17 std::filesystem functions instead of my own.

Naram Qashat authored on 2021/03/20 20:05:50
Showing 7 changed files
... ...
@@ -10,6 +10,7 @@
10 10
  * http://desmume.org/
11 11
  */
12 12
 
13
+#include <filesystem>
13 14
 #include <memory>
14 15
 #include <zlib.h>
15 16
 #include "convert.h"
... ...
@@ -146,9 +147,9 @@ bool XSFPlayer_2SF::RecursiveLoad2SF(XSFFile *xSFToLoad, int level)
146 147
 	if (level <= 10 && xSFToLoad->GetTagExists("_lib"))
147 148
 	{
148 149
 #ifdef _WIN32
149
-		auto libxSF = std::make_unique<XSFFile>(ConvertFuncs::StringToWString(ExtractDirectoryFromPath(xSFToLoad->GetFilename()) + xSFToLoad->GetTagValue("_lib")), 4, 8);
150
+		auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSFToLoad->GetFilename()).parent_path() / xSFToLoad->GetTagValue("_lib")).wstring(), 4, 8);
150 151
 #else
151
-		auto libxSF = std::make_unique<XSFFile>(ExtractDirectoryFromPath(xSFToLoad->GetFilename()) + xSFToLoad->GetTagValue("_lib"), 4, 8);
152
+		auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSFToLoad->GetFilename()).parent_path() / xSFToLoad->GetTagValue("_lib")).string(), 4, 8);
152 153
 #endif
153 154
 		if (!this->RecursiveLoad2SF(libxSF.get(), level + 1))
154 155
 			return false;
... ...
@@ -167,9 +168,9 @@ bool XSFPlayer_2SF::RecursiveLoad2SF(XSFFile *xSFToLoad, int level)
167 168
 		{
168 169
 			found = true;
169 170
 #ifdef _WIN32
170
-			auto libxSF = std::make_unique<XSFFile>(ConvertFuncs::StringToWString(ExtractDirectoryFromPath(xSFToLoad->GetFilename()) + xSFToLoad->GetTagValue(libTag)), 4, 8);
171
+			auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSFToLoad->GetFilename()).parent_path() / xSFToLoad->GetTagValue(libTag)).wstring(), 4, 8);
171 172
 #else
172
-			auto libxSF = std::make_unique<XSFFile>(ExtractDirectoryFromPath(xSFToLoad->GetFilename()) + xSFToLoad->GetTagValue(libTag), 4, 8);
173
+			auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSFToLoad->GetFilename()).parent_path() / xSFToLoad->GetTagValue(libTag)).string(), 4, 8);
173 174
 #endif
174 175
 			if (!this->RecursiveLoad2SF(libxSF.get(), level + 1))
175 176
 				return false;
... ...
@@ -10,6 +10,7 @@
10 10
  * http://vba-m.com/
11 11
  */
12 12
 
13
+#include <filesystem>
13 14
 #include <memory>
14 15
 #include <zlib.h>
15 16
 #include "convert.h"
... ...
@@ -152,9 +153,9 @@ static bool RecursiveLoadGSF(XSFFile *xSF, int level)
152 153
 	if (level <= 10 && xSF->GetTagExists("_lib"))
153 154
 	{
154 155
 #ifdef _WIN32
155
-		auto libxSF = std::make_unique<XSFFile>(ConvertFuncs::StringToWString(ExtractDirectoryFromPath(xSF->GetFilename()) + xSF->GetTagValue("_lib")), 8, 12);
156
+		auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSF->GetFilename()).parent_path() / xSF->GetTagValue("_lib")).wstring(), 8, 12);
156 157
 #else
157
-		auto libxSF = std::make_unique<XSFFile>(ExtractDirectoryFromPath(xSF->GetFilename()) + xSF->GetTagValue("_lib"), 8, 12);
158
+		auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSF->GetFilename()).parent_path() / xSF->GetTagValue("_lib")).string(), 8, 12);
158 159
 #endif
159 160
 		if (!RecursiveLoadGSF(libxSF.get(), level + 1))
160 161
 			return false;
... ...
@@ -173,9 +174,9 @@ static bool RecursiveLoadGSF(XSFFile *xSF, int level)
173 174
 		{
174 175
 			found = true;
175 176
 #ifdef _WIN32
176
-			auto libxSF = std::make_unique<XSFFile>(ConvertFuncs::StringToWString(ExtractDirectoryFromPath(xSF->GetFilename()) + xSF->GetTagValue(libTag)), 8, 12);
177
+			auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSF->GetFilename()).parent_path() / xSF->GetTagValue(libTag)).wstring(), 8, 12);
177 178
 #else
178
-			auto libxSF = std::make_unique<XSFFile>(ExtractDirectoryFromPath(xSF->GetFilename()) + xSF->GetTagValue(libTag), 8, 12);
179
+			auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSF->GetFilename()).parent_path() / xSF->GetTagValue(libTag)).string(), 8, 12);
179 180
 #endif
180 181
 			if (!RecursiveLoadGSF(libxSF.get(), level + 1))
181 182
 				return false;
... ...
@@ -8,6 +8,7 @@
8 8
  * https://github.com/fincs/FSS
9 9
  */
10 10
 
11
+#include <filesystem>
11 12
 #include <memory>
12 13
 #include <cstdlib>
13 14
 #include <ctime>
... ...
@@ -67,9 +68,9 @@ bool XSFPlayer_NCSF::RecursiveLoadNCSF(XSFFile *xSFToLoad, int level)
67 68
 	if (level <= 10 && xSFToLoad->GetTagExists("_lib"))
68 69
 	{
69 70
 #ifdef _WIN32
70
-		auto libxSF = std::make_unique<XSFFile>(ConvertFuncs::StringToWString(ExtractDirectoryFromPath(xSFToLoad->GetFilename()) + xSFToLoad->GetTagValue("_lib")), 8, 12);
71
+		auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSFToLoad->GetFilename()).parent_path() / xSFToLoad->GetTagValue("_lib")).wstring(), 8, 12);
71 72
 #else
72
-		auto libxSF = std::make_unique<XSFFile>(ExtractDirectoryFromPath(xSFToLoad->GetFilename()) + xSFToLoad->GetTagValue("_lib"), 8, 12);
73
+		auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSFToLoad->GetFilename()).parent_path() / xSFToLoad->GetTagValue("_lib")).string(), 8, 12);
73 74
 #endif
74 75
 		if (!this->RecursiveLoadNCSF(libxSF.get(), level + 1))
75 76
 			return false;
... ...
@@ -88,9 +89,9 @@ bool XSFPlayer_NCSF::RecursiveLoadNCSF(XSFFile *xSFToLoad, int level)
88 89
 		{
89 90
 			found = true;
90 91
 #ifdef _WIN32
91
-			auto libxSF = std::make_unique<XSFFile>(ConvertFuncs::StringToWString(ExtractDirectoryFromPath(xSFToLoad->GetFilename()) + xSFToLoad->GetTagValue(libTag)), 8, 12);
92
+			auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSFToLoad->GetFilename()).parent_path() / xSFToLoad->GetTagValue(libTag)).wstring(), 8, 12);
92 93
 #else
93
-			auto libxSF = std::make_unique<XSFFile>(ExtractDirectoryFromPath(xSFToLoad->GetFilename()) + xSFToLoad->GetTagValue(libTag), 8, 12);
94
+			auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSFToLoad->GetFilename()).parent_path() / xSFToLoad->GetTagValue(libTag)).string(), 8, 12);
94 95
 #endif
95 96
 			if (!this->RecursiveLoadNCSF(libxSF.get(), level + 1))
96 97
 				return false;
... ...
@@ -11,6 +11,7 @@
11 11
  * http://www.snes9x.com/
12 12
  */
13 13
 
14
+#include <filesystem>
14 15
 #include <zlib.h>
15 16
 #include "convert.h"
16 17
 #include "XSFPlayer.h"
... ...
@@ -167,9 +168,9 @@ static bool RecursiveLoadSNSF(XSFFile *xSF, int level)
167 168
 	if (level <= 10 && xSF->GetTagExists("_lib"))
168 169
 	{
169 170
 #ifdef _WIN32
170
-		auto libxSF = std::make_unique<XSFFile>(ConvertFuncs::StringToWString(ExtractDirectoryFromPath(xSF->GetFilename()) + xSF->GetTagValue("_lib")), 4, 8);
171
+		auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSF->GetFilename()).parent_path() / xSF->GetTagValue("_lib")).wstring(), 4, 8);
171 172
 #else
172
-		auto libxSF = std::make_unique<XSFFile>(ExtractDirectoryFromPath(xSF->GetFilename()) + xSF->GetTagValue("_lib"), 4, 8);
173
+		auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSF->GetFilename()).parent_path() / xSF->GetTagValue("_lib")).string(), 4, 8);
173 174
 #endif
174 175
 		if (!RecursiveLoadSNSF(libxSF.get(), level + 1))
175 176
 			return false;
... ...
@@ -188,9 +189,9 @@ static bool RecursiveLoadSNSF(XSFFile *xSF, int level)
188 189
 		{
189 190
 			found = true;
190 191
 #ifdef _WIN32
191
-			auto libxSF = std::make_unique<XSFFile>(ConvertFuncs::StringToWString(ExtractDirectoryFromPath(xSF->GetFilename()) + xSF->GetTagValue(libTag)), 4, 8);
192
+			auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSF->GetFilename()).parent_path() / xSF->GetTagValue(libTag)).wstring(), 4, 8);
192 193
 #else
193
-			auto libxSF = std::make_unique<XSFFile>(ExtractDirectoryFromPath(xSF->GetFilename()) + xSF->GetTagValue(libTag), 4, 8);
194
+			auto libxSF = std::make_unique<XSFFile>((std::filesystem::path(xSF->GetFilename()).parent_path() / xSF->GetTagValue(libTag)).string(), 4, 8);
194 195
 #endif
195 196
 			if (!RecursiveLoadSNSF(libxSF.get(), level + 1))
196 197
 				return false;
... ...
@@ -9,9 +9,6 @@
9 9
 
10 10
 #include <limits>
11 11
 #include <fstream>
12
-#if defined(_WIN32) && !defined(_MSC_VER)
13
-# include "fstream_wfopen.h"
14
-#endif
15 12
 #include <string>
16 13
 #define _USE_MATH_DEFINES
17 14
 #include <cmath>
... ...
@@ -40,24 +37,6 @@ inline uint32_t Get32BitsLE(std::ifstream &input)
40 37
 	return Get32BitsLE(bytes);
41 38
 }
42 39
 
43
-// This gets the directory for the path, including the final forward/backward slash
44
-template<typename T> inline std::basic_string<T> ExtractDirectoryFromPath(const std::basic_string<T> &fullPath)
45
-{
46
-	auto lastSlash = fullPath.rfind('\\');
47
-	if (lastSlash == std::basic_string<T>::npos)
48
-		lastSlash = fullPath.rfind('/');
49
-	return lastSlash != std::basic_string<T>::npos ? fullPath.substr(0, lastSlash + 1) : std::basic_string<T>();
50
-}
51
-
52
-// This gets the filename for the path
53
-template<typename T> inline std::basic_string<T> ExtractFilenameFromPath(const std::basic_string<T> &fullPath)
54
-{
55
-	auto lastSlash = fullPath.rfind('\\');
56
-	if (lastSlash == std::basic_string<T>::npos)
57
-		lastSlash = fullPath.rfind('/');
58
-	return lastSlash != std::basic_string<T>::npos ? fullPath.substr(lastSlash + 1) : std::basic_string<T>();
59
-}
60
-
61 40
 // Code from the following answer on Stack Overflow:
62 41
 // http://stackoverflow.com/a/15479212
63 42
 template<typename T> inline T NextHighestPowerOf2(T value)
... ...
@@ -79,24 +58,6 @@ template<typename T1, typename T2> inline void clamp(T1 &valueToClamp, const T2
79 58
 		valueToClamp = maxValue;
80 59
 }
81 60
 
82
-inline bool FileExists(const std::string &filename)
83
-{
84
-	std::ifstream file(filename.c_str());
85
-	return !!file;
86
-}
87
-
88
-#ifdef _WIN32
89
-inline bool FileExists(const std::wstring &filename)
90
-{
91
-#ifdef _MSC_VER
92
-	std::ifstream file(filename.c_str());
93
-#else
94
-	ifstream_wfopen file(filename.c_str());
95
-#endif
96
-	return !!file;
97
-}
98
-#endif
99
-
100 61
 inline void CopyToString(const std::wstring &src, wchar_t *dst)
101 62
 {
102 63
 	wcscpy(dst, src.c_str());
... ...
@@ -5,6 +5,7 @@
5 5
  * Partially based on the vio*sf framework
6 6
  */
7 7
 
8
+#include <filesystem>
8 9
 #include "XSFConfig.h"
9 10
 #include "XSFCommon.h"
10 11
 #include "winamp/in2.h"
... ...
@@ -50,7 +51,7 @@ XSFConfigIO_Winamp::XSFConfigIO_Winamp() : iniFilename(L"")
50 51
 		if (!result)
51 52
 			throw std::runtime_error("Unable to get path to plugin.");
52 53
 
53
-		this->iniFilename = ExtractDirectoryFromPath(std::wstring(executablePath.begin(), executablePath.begin() + result)) + L"plugins.ini";
54
+		this->iniFilename = (std::filesystem::path(std::wstring(executablePath.begin(), executablePath.begin() + result)).parent_path() / L"plugins.ini").wstring();
54 55
 	}
55 56
 }
56 57
 
... ...
@@ -6,8 +6,12 @@
6 6
  */
7 7
 
8 8
 #include <algorithm>
9
+#include <filesystem>
9 10
 #include <functional>
10 11
 #include <stdexcept>
12
+#if defined(_WIN32) && !defined(_MSC_VER)
13
+# include "fstream_wfopen.h"
14
+#endif
11 15
 #include "zlib.h"
12 16
 #include "XSFFile.h"
13 17
 #include "XSFCommon.h"
... ...
@@ -80,7 +84,7 @@ XSFFile::XSFFile(const std::wstring &filename, uint32_t programSizeOffset, uint3
80 84
 
81 85
 void XSFFile::ReadXSF(const std::string &filename, uint32_t programSizeOffset, uint32_t programHeaderSize, bool readTagsOnly)
82 86
 {
83
-	if (!FileExists(filename))
87
+	if (!std::filesystem::is_regular_file(filename))
84 88
 		throw std::logic_error("File " + filename + " does not exist.");
85 89
 
86 90
 	std::ifstream xSF;
... ...
@@ -95,7 +99,7 @@ void XSFFile::ReadXSF(const std::string &filename, uint32_t programSizeOffset, u
95 99
 #ifdef _WIN32
96 100
 void XSFFile::ReadXSF(const std::wstring &filename, uint32_t programSizeOffset, uint32_t programHeaderSize, bool readTagsOnly)
97 101
 {
98
-	if (!FileExists(filename))
102
+	if (!std::filesystem::is_regular_file(filename))
99 103
 		throw std::logic_error("File " + ConvertFuncs::WStringToString(filename) + " does not exist.");
100 104
 
101 105
 #if defined(_WIN32) && !defined(_MSC_VER)
... ...
@@ -460,7 +464,7 @@ std::string XSFFile::GetFilename() const
460 464
 
461 465
 std::string XSFFile::GetFilenameWithoutPath() const
462 466
 {
463
-	return ExtractFilenameFromPath(this->fileName);
467
+	return std::filesystem::path(this->fileName).filename().string();
464 468
 }
465 469
 
466 470
 void XSFFile::SaveFile() const