From b68482754b596731a29f25d0a3a09400b4c83fe7 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 2 Jul 2023 10:11:42 -0600 Subject: [PATCH] Use `std::filesystem::path` for paths --- examples/cocoa/CocoaAppDelegate.mm | 8 +++++--- include/SFML/Network/Ftp.hpp | 8 ++++---- src/SFML/Network/Ftp.cpp | 18 +++++------------- test/System/FileInputStream.test.cpp | 8 ++++---- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/examples/cocoa/CocoaAppDelegate.mm b/examples/cocoa/CocoaAppDelegate.mm index 7f4bb5d0..12dae616 100644 --- a/examples/cocoa/CocoaAppDelegate.mm +++ b/examples/cocoa/CocoaAppDelegate.mm @@ -27,6 +27,8 @@ #import "NSString+stdstring.h" +#include + // These define are used for converting the color of the NSPopUpButton #define BLUE @"Blue" #define GREEN @"Green" @@ -39,8 +41,8 @@ struct SFMLmainWindow { SFMLmainWindow(sf::WindowHandle win) : renderWindow(win), text(font), sprite(logo), background(sf::Color::Blue) { - std::string resPath = [[[NSBundle mainBundle] resourcePath] tostdstring]; - if (!logo.loadFromFile(resPath + "/logo.png")) + std::filesystem::path resPath = [[[NSBundle mainBundle] resourcePath] tostdstring]; + if (!logo.loadFromFile(resPath / "logo.png")) NSLog(@"Couldn't load the logo image"); logo.setSmooth(true); @@ -54,7 +56,7 @@ struct SFMLmainWindow unsigned int wh = renderWindow.getSize().y; sprite.setPosition(sf::Vector2f(ww, wh) / 2.f); - if (!font.loadFromFile(resPath + "/tuffy.ttf")) + if (!font.loadFromFile(resPath / "tuffy.ttf")) NSLog(@"Couldn't load the font"); text.setFillColor(sf::Color::White); diff --git a/include/SFML/Network/Ftp.hpp b/include/SFML/Network/Ftp.hpp index 9429885e..09800f75 100644 --- a/include/SFML/Network/Ftp.hpp +++ b/include/SFML/Network/Ftp.hpp @@ -502,10 +502,10 @@ public: /// \see download /// //////////////////////////////////////////////////////////// - [[nodiscard]] Response upload(const std::string& localFile, - const std::string& remotePath, - TransferMode mode = TransferMode::Binary, - bool append = false); + [[nodiscard]] Response upload(const std::filesystem::path& localFile, + const std::filesystem::path& remotePath, + TransferMode mode = TransferMode::Binary, + bool append = false); //////////////////////////////////////////////////////////// /// \brief Send a command to the FTP server diff --git a/src/SFML/Network/Ftp.cpp b/src/SFML/Network/Ftp.cpp index 6201453d..97ea71c5 100644 --- a/src/SFML/Network/Ftp.cpp +++ b/src/SFML/Network/Ftp.cpp @@ -322,31 +322,23 @@ Ftp::Response Ftp::download(const std::filesystem::path& remoteFile, const std:: //////////////////////////////////////////////////////////// -Ftp::Response Ftp::upload(const std::string& localFile, const std::string& remotePath, TransferMode mode, bool append) +Ftp::Response Ftp::upload(const std::filesystem::path& localFile, + const std::filesystem::path& remotePath, + TransferMode mode, + bool append) { // Get the contents of the file to send std::ifstream file(localFile, std::ios_base::binary); if (!file) return Response(Response::Status::InvalidFile); - // Extract the filename from the file path - std::string filename = localFile; - const std::string::size_type pos = filename.find_last_of("/\\"); - if (pos != std::string::npos) - filename = filename.substr(pos + 1); - - // Make sure the destination path ends with a slash - std::string path = remotePath; - if (!path.empty() && (path[path.size() - 1] != '\\') && (path[path.size() - 1] != '/')) - path += "/"; - // Open a data channel using the given transfer mode DataChannel data(*this); Response response = data.open(mode); if (response.isOk()) { // Tell the server to start the transfer - response = sendCommand(append ? "APPE" : "STOR", path + filename); + response = sendCommand(append ? "APPE" : "STOR", (remotePath / localFile.filename()).string()); if (response.isOk()) { // Send the file data diff --git a/test/System/FileInputStream.test.cpp b/test/System/FileInputStream.test.cpp index fed2a4ba..d2af51e4 100644 --- a/test/System/FileInputStream.test.cpp +++ b/test/System/FileInputStream.test.cpp @@ -12,7 +12,7 @@ namespace { -std::string getTemporaryFilePath() +std::filesystem::path getTemporaryFilePath() { static int counter = 0; @@ -24,13 +24,13 @@ std::string getTemporaryFilePath() result /= std::filesystem::temp_directory_path(); result /= oss.str(); - return result.string(); + return result; } class TemporaryFile { private: - std::string m_path; + std::filesystem::path m_path; public: // Create a temporary file with a randomly generated path, containing 'contents'. @@ -56,7 +56,7 @@ public: TemporaryFile& operator=(const TemporaryFile&) = delete; // Return the randomly generated path. - const std::string& getPath() const + const std::filesystem::path& getPath() const { return m_path; }