Skip to content

Commit

Permalink
Implemented RenameFile() and RenameFileUtf8() with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
end2endzone committed Oct 26, 2024
1 parent a596515 commit 8848eb9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/shared/SaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,31 @@ bool HasDirectoryWriteAccessUtf8(const std::string& path)
return true;
}

bool RenameFile(const std::string& old_path, const std::string& new_path)
{
if (std::rename(old_path.c_str(), new_path.c_str()) < 0)
{
return false;
}
return true;
}

bool RenameFileUtf8(const std::string& old_path, const std::string& new_path)
{
#ifndef WIN32
// Win32 API not available, proceed with a normal ansi rename
return RenameFile(old_path, new_path);
#else
std::wstring old_path_w = ra::unicode::Utf8ToUnicode(old_path);
std::wstring new_path_w = ra::unicode::Utf8ToUnicode(new_path);
if (_wrename(old_path_w.c_str(), new_path_w.c_str()) < 0)
{
return false;
}
return true;
#endif
}

bool IsFirstApplicationRun(const std::string& name, const std::string& version)
{
std::string key = ra::strings::Format("HKEY_CURRENT_USER\\Software\\%s\\%s", name.c_str(), version.c_str());
Expand Down
14 changes: 14 additions & 0 deletions src/shared/SaUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ bool HasDirectoryWriteAccess(const std::string& path);
/// <returns>Returns true if write access is granted. Returns false otherwise.</returns>
bool HasDirectoryWriteAccessUtf8(const std::string& path);

/// <summary>
/// Rename a file name to another name.
/// Paths must be specified in as absolute path. If the source directory and the target directories are not the same, the file will be also moved.
/// </summary>
/// <returns>Returns true if the operation is successful. Returns false otherwise.</returns>
bool RenameFile(const std::string& old_path, const std::string& new_path);

/// <summary>
/// Rename a file name to another name.
/// Paths must be specified in as absolute path. If the source directory and the target directories are not the same, the file will be also moved.
/// </summary>
/// <returns>Returns true if the operation is successful. Returns false otherwise.</returns>
bool RenameFileUtf8(const std::string& old_path, const std::string& new_path);

/// <summary>
/// Returns true if the application is run for the first time.
/// Note, for Windows users, the implementation is based on registry keys in HKEY_CURRENT_USER\Software\name\version.
Expand Down
31 changes: 31 additions & 0 deletions src/tests/TestSaUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

#include "TestSaUtils.h"
#include "SaUtils.h"
#include "rapidassist/testing_utf8.h"
#include "rapidassist/environment_utf8.h"
#include "rapidassist/random.h"
#include "rapidassist/filesystem_utf8.h"

namespace shellanything
{
Expand Down Expand Up @@ -64,6 +68,33 @@ namespace shellanything
ASSERT_TRUE(IsPrintableUtf8("Espa" "\xc3" "\xb1" "ol")); // Español
}
//--------------------------------------------------------------------------------------------------
TEST_F(TestSaUtils, testRenameFileUtf8)
{
static const std::string filename_characters = "abcdefghijklmnopqrstuvwxyz0123456789_";
std::string temp_dir = ra::environment::GetEnvironmentVariableUtf8("TEMP");
std::string old_filename = ra::random::GetRandomString(16, filename_characters.c_str()) + ".tmp";
std::string new_filename = ra::random::GetRandomString(16, filename_characters.c_str()) + ".tmp";

std::string old_path = temp_dir + "\\" + old_filename;
std::string new_path = temp_dir + "\\" + new_filename;

// assert pre-state
ASSERT_TRUE(ra::testing::CreateFileUtf8(old_path.c_str(), 10240)) << "Failed to create file: " << old_path;
ASSERT_FALSE(ra::filesystem::FileExistsUtf8(new_path.c_str()));

// assert operation is successful
bool renamed = RenameFileUtf8(old_path, new_path);
ASSERT_TRUE(renamed);

// assert files are actualy renamed
ASSERT_FALSE(ra::filesystem::FileExistsUtf8(old_path.c_str()));
ASSERT_TRUE (ra::filesystem::FileExistsUtf8(new_path.c_str()));

// cleanup
ra::filesystem::DeleteFileUtf8(old_path.c_str());
ra::filesystem::DeleteFileUtf8(new_path.c_str());
}
//--------------------------------------------------------------------------------------------------

} //namespace test
} //namespace shellanything

0 comments on commit 8848eb9

Please sign in to comment.