Ver Fonte

extract functions of string <-> boost::filesystem::path to TextOperations.h

kdmcser há 6 meses atrás
pai
commit
c3c8b5048a

+ 3 - 13
lib/filesystem/CFilesystemLoader.cpp

@@ -13,8 +13,7 @@
 #include "CFileInputStream.h"
 
 #include "../ExceptionsCommon.h"
-
-#include <boost/locale/encoding_utf.hpp>
+#include "../texts/TextOperations.h"
 
 VCMI_LIB_NAMESPACE_BEGIN
 
@@ -93,11 +92,7 @@ bool CFilesystemLoader::createResource(const std::string & requestedFilename, bo
 	}
 
 	filename = filename.substr(mountPoint.size());
-#ifdef VCMI_WINDOWS
-			boost::filesystem::path filePath(boost::locale::conv::utf_to_utf<wchar_t>(filename));
-#else
-			boost::filesystem::path filePath(filename.string());
-#endif
+	boost::filesystem::path filePath = TextOperations::Utf8TofilesystemPath(filename);
 
 	if (!update)
 	{
@@ -180,12 +175,7 @@ std::unordered_map<ResourcePath, boost::filesystem::path> CFilesystemLoader::lis
 				filename = it->path().filename();
 
 			std::string resName;
-
-#ifdef VCMI_WINDOWS
-			std::string filenameUtf8 = boost::locale::conv::utf_to_utf<char>(filename.native());
-#else
-			std::string filenameUtf8 = filename.string();
-#endif
+			std::string filenameUtf8 = TextOperations::filesystemPathToUtf8(filename);
 
 			if (boost::filesystem::path::preferred_separator != '/')
 			{

+ 1 - 7
lib/mapping/CMapInfo.cpp

@@ -27,8 +27,6 @@
 #include "../IGameSettings.h"
 #include "../CConfigHandler.h"
 
- #include <boost/locale/encoding_utf.hpp>
-
 VCMI_LIB_NAMESPACE_BEGIN
 
 CMapInfo::CMapInfo()
@@ -45,11 +43,7 @@ CMapInfo::~CMapInfo()
 std::string CMapInfo::getFullFileURI(const ResourcePath & file) const
 {
 	auto path = boost::filesystem::canonical(*CResourceHandler::get()->getResourceName(file));
-#ifdef VCMI_WINDOWS
-	return boost::locale::conv::utf_to_utf<char>(path.native());
-#else
-	return path.string();
-#endif
+	return TextOperations::filesystemPathToUtf8(path);
 }
 
 void CMapInfo::mapInit(const std::string & fname)

+ 18 - 0
lib/texts/TextOperations.cpp

@@ -405,4 +405,22 @@ std::optional<int> TextOperations::textSearchSimilarityScore(const std::string &
 	return (minDist > maxAllowedDistance) ? std::nullopt : std::optional<int>{ minDist };
 }
 
+std::string TextOperations::filesystemPathToUtf8(const boost::filesystem::path& path)
+{
+#ifdef VCMI_WINDOWS
+	return boost::locale::conv::utf_to_utf<char>(path.native());
+#else
+	return path.string();
+#endif
+}
+
+boost::filesystem::path TextOperations::Utf8TofilesystemPath(const std::string& path)
+{
+#ifdef VCMI_WINDOWS
+	return boost::filesystem::path(boost::locale::conv::utf_to_utf<wchar_t>(path));
+#else
+	return boost::filesystem::path(path.string());
+#endif
+}
+
 VCMI_LIB_NAMESPACE_END

+ 7 - 0
lib/texts/TextOperations.h

@@ -84,6 +84,13 @@ namespace TextOperations
 	/// other values = Levenshtein distance, returns std::nullopt for unrelated word (bad match).
 	DLL_LINKAGE std::optional<int> textSearchSimilarityScore(const std::string & s, const std::string & t);
 
+	/// This function is mainly used to avoid garbled text when reading or writing files
+	/// with non-ASCII (e.g. Chinese) characters in the path, especially on Windows.
+	/// Call this before passing the path to file I/O functions that take std::string.
+	DLL_LINKAGE std::string filesystemPathToUtf8(const boost::filesystem::path& path);
+
+	// Used for handling paths with non-ASCII characters.
+	DLL_LINKAGE boost::filesystem::path Utf8TofilesystemPath(const std::string& path);
 };
 
 template<typename Arithmetic>