Repository :
https://github.com/FarGroup/FarManager
On branch : master
Link :
https://github.com/FarGroup/FarManager/commit/a0c889f77869c42aaa5c8efbccaf840f5ba4790d
>---------------------------------------------------------------
commit a0c889f77869c42aaa5c8efbccaf840f5ba4790d
Author: Alex Alabuzhev <
alab...@gmail.com>
Date: Sun Dec 7 11:20:24 2025 +0000
Refactoring
>---------------------------------------------------------------
a0c889f77869c42aaa5c8efbccaf840f5ba4790d
far/changelog | 5 +++++
far/delete.cpp | 35 +++++++++++++----------------------
far/fileedit.cpp | 9 +--------
far/filelist.cpp | 4 +---
far/interf.cpp | 20 +++++++++++++-------
far/main.cpp | 15 ++++-----------
far/mix.cpp | 2 +-
far/pathmix.cpp | 12 ++++++++++++
far/pathmix.hpp | 3 +++
far/platform.cpp | 10 ++++++++++
far/platform.debug.cpp | 9 ++-------
far/platform.fs.cpp | 4 ++--
far/platform.fs.hpp | 2 +-
far/platform.hpp | 1 +
far/sqlitedb.cpp | 2 +-
far/usermenu.cpp | 5 +----
far/vbuild.m4 | 2 +-
17 files changed, 72 insertions(+), 68 deletions(-)
diff --git a/far/changelog b/far/changelog
index 14f28563b..3ec8e84e4 100644
--- a/far/changelog
+++ b/far/changelog
@@ -1,3 +1,8 @@
+--------------------------------------------------------------------------------
+drkns 2025-12-07 11:20:04+00:00 - build 6613
+
+1. Refactoring.
+
--------------------------------------------------------------------------------
drkns 2025-12-05 19:23:22+00:00 - build 6612
diff --git a/far/delete.cpp b/far/delete.cpp
index 69fab5f4c..b63034006 100644
--- a/far/delete.cpp
+++ b/far/delete.cpp
@@ -266,37 +266,28 @@ static bool EraseFileData(string_view const Name, progress Files, delete_progres
return true;
}
-static bool EraseFile(string_view const Name, progress Files, delete_progress const& Progress)
+static bool EraseFile(string_view const FullName, progress Files, delete_progress const& Progress)
{
- if (!os::fs::set_file_attributes(Name, FILE_ATTRIBUTE_NORMAL))
+ if (!os::fs::set_file_attributes(FullName, FILE_ATTRIBUTE_NORMAL))
return false;
- if (!EraseFileData(Name, Files, Progress))
+ if (!EraseFileData(FullName, Files, Progress))
return false;
- const auto strTempName = MakeTemp({}, false);
+ const auto strTempName = MakeTemp({}, false, path::parent_path(FullName));
- if (!os::fs::move_file(Name, strTempName))
+ if (!os::fs::move_file(FullName, strTempName))
return false;
return os::fs::delete_file(strTempName);
}
-static bool EraseDirectory(string_view const Name)
+static bool EraseDirectory(string_view const FullName)
{
- auto Path = Name;
+ const auto strTempName = MakeTemp({}, false, path::parent_path(FullName));
- if (!CutToParent(Path))
- {
- Path = {};
- }
-
- const auto strTempName = MakeTemp({}, false, Path);
-
- if (!os::fs::move_file(Name, strTempName))
- {
+ if (!os::fs::move_file(FullName, strTempName))
return false;
- }
return os::fs::remove_directory(strTempName);
}
@@ -571,14 +562,14 @@ void ShellDelete::process_item(
}
}
+ const auto strSelFullName = IsAbsolutePath(strSelName)?
+ strSelName :
+ path::join(SrcPanel->GetCurDir(), strSelName);
+
if (!DirSymLink && m_DeleteType != delete_type::recycle)
{
ScanTree ScTree(true, true, FALSE);
- const auto strSelFullName = IsAbsolutePath(strSelName)?
- strSelName :
- path::join(SrcPanel->GetCurDir(), strSelName);
-
ScTree.SetFindPath(strSelFullName, L"*"sv);
const time_check TreeTimeCheck(time_check::mode::immediate);
@@ -685,7 +676,7 @@ void ShellDelete::process_item(
bool RetryRecycleAsRemove = false;
if (ERemoveDirectory(
- strSelName,
+ strSelFullName,
m_DeleteType == delete_type::recycle && DirSymLink && !IsWindowsVistaOrGreater()?
delete_type::remove :
m_DeleteType,
diff --git a/far/fileedit.cpp b/far/fileedit.cpp
index c307dec8c..9ef391a1c 100644
--- a/far/fileedit.cpp
+++ b/far/fileedit.cpp
@@ -1950,16 +1950,9 @@ int FileEditor::SaveFile(const string_view Name, bool bSaveAs, error_state_ex& E
}
}
-static auto parent_directory(string_view const FileName)
-{
- auto Path = FileName;
- CutToParent(Path);
- return Path;
-}
-
bool FileEditor::SaveAction(bool const SaveAsIntention)
{
- const auto ParentDirectory = parent_directory(strFullFileName);
+ const auto ParentDirectory = path::parent_path(strFullFileName);
const auto SaveAs =
SaveAsIntention ||
diff --git a/far/filelist.cpp b/far/filelist.cpp
index e9df4d12b..830074568 100644
--- a/far/filelist.cpp
+++ b/far/filelist.cpp
@@ -1980,9 +1980,7 @@ bool FileList::ProcessKey(const Manager::Key& Key)
// The user could potentially save the temporary file to a different location
string Dummy;
ShellEditor->GetTypeAndName(Dummy, strFileName);
- string_view ParentDirectory = strFileName;
- CutToParent(ParentDirectory);
- if (!equal_icase(ParentDirectory, TemporaryDirectory))
+ if (!equal_icase(path::parent_path(strFileName), TemporaryDirectory))
DeleteTemporaryFile = false;
}
diff --git a/far/interf.cpp b/far/interf.cpp
index 0f9e20f71..987a9a684 100644
--- a/far/interf.cpp
+++ b/far/interf.cpp
@@ -906,11 +906,16 @@ static void string_to_cells_full_width_aware(string_view Str, size_t& CharsConsu
}
}
+static void string_to_cells(string_view Str, size_t& CharsConsumed, cells& Cells, size_t const CellsAvailable)
+{
+ (char_width::is_enabled()? string_to_cells_full_width_aware : string_to_cells_simple)(Str, CharsConsumed, Cells, CellsAvailable);
+}
+
void chars_to_cells(string_view Str, size_t& CharsConsumed, size_t const CellsAvailable, size_t& CellsConsumed)
{
cells Cells;
const auto& CellsToBeConsumed = Cells.emplace<0>();
- (char_width::is_enabled()? string_to_cells_full_width_aware : string_to_cells_simple)(Str, CharsConsumed, Cells, CellsAvailable);
+ string_to_cells(Str, CharsConsumed, Cells, CellsAvailable);
CellsConsumed = CellsToBeConsumed;
#ifdef _DEBUG
@@ -929,7 +934,7 @@ size_t Text(string_view Str, size_t const CellsAvailable)
size_t CharsConsumed = 0;
- (char_width::is_enabled()? string_to_cells_full_width_aware : string_to_cells_simple)(Str, CharsConsumed, Cells, CellsAvailable);
+ string_to_cells(Str, CharsConsumed, Cells, CellsAvailable);
Global->ScrBuf->Write(CurX, CurY, Buffer);
CurX += static_cast<int>(Buffer.size());
@@ -997,7 +1002,6 @@ enum class hi_string_state
static void HiTextBase(string_view const Str, function_ref<void(string_view, hi_string_state)> const TextHandler)
{
- bool Unescape = false;
for (size_t Offset = 0;;)
{
const auto AmpBegin = Str.find(L'&', Offset);
@@ -1026,12 +1030,11 @@ static void HiTextBase(string_view const Str, function_ref<void(string_view, hi_
if (!((AmpEnd - AmpBegin) & 1))
{
Offset = AmpEnd;
- Unescape = true;
continue;
}
if (AmpBegin)
- TextHandler(Str.substr(0, AmpBegin), Unescape? hi_string_state::needs_unescape : hi_string_state::ready);
+ TextHandler(Str.substr(0, AmpBegin), Offset? hi_string_state::needs_unescape : hi_string_state::ready);
auto CurPos = AmpBegin + 1;
@@ -1039,7 +1042,7 @@ static void HiTextBase(string_view const Str, function_ref<void(string_view, hi_
return;
// We can only use single characters as hotkeys now
- if (const auto IsSurogate = CurPos + 1 != Str.size() && is_valid_surrogate_pair(Str[CurPos], Str[CurPos + 1]); !IsSurogate)
+ if (const auto IsSurrogate = CurPos + 1 != Str.size() && is_valid_surrogate_pair(Str[CurPos], Str[CurPos + 1]); !IsSurrogate)
{
TextHandler(Str.substr(CurPos, 1), hi_string_state::highlight);
++CurPos;
@@ -1048,7 +1051,7 @@ static void HiTextBase(string_view const Str, function_ref<void(string_view, hi_
if (CurPos == Str.size())
return;
- const auto HiAmpCollapse = Str[AmpBegin + 1] == L'&' && Str[AmpBegin + 2] == L'&';
+ const auto HiAmpCollapse = Str.substr(AmpBegin).starts_with(L"&&&"sv);
const auto Tail = Str.substr(CurPos + (HiAmpCollapse? 1 : 0));
TextHandler(Tail, Tail.find(L'&') == Tail.npos? hi_string_state::ready : hi_string_state::needs_unescape);
@@ -1881,6 +1884,9 @@ TEST_CASE("interf.highlight")
{ L"1&&&2"sv, L"1&2"sv, L'&', 1, { 0, 0 }, },
{ L"&1&&&2"sv, L"1&2"sv, L'1', 0, { 0, 1 }, },
{ L"&1&2&3&"sv, L"123"sv, L'1', 0, { 2, 5 }, },
+ { L"&&1&2&"sv, L"&12"sv, L'2', 2, { 2, 4 }, },
+ { L"123&&"sv, L"123&"sv, 0, np, { 3, 4 }, },
+ { L"1&&&"sv, L"1&"sv, L'&', 1, { 1, 2 }, },
};
for (const auto& i: Tests)
diff --git a/far/main.cpp b/far/main.cpp
index e62a5d29e..3740a86a8 100644
--- a/far/main.cpp
+++ b/far/main.cpp
@@ -214,12 +214,7 @@ static int MainProcess(
const auto SetupPanel = [&](bool active)
{
- string strPath = active? apanel : ppanel;
- if (active? IsFileA : IsFileP)
- {
- CutToParent(strPath);
- }
-
+ string strPath(path::parent_path(active? apanel : ppanel));
bool Root = false;
const auto Type = ParsePath(strPath, nullptr, &Root);
if(Root && (Type == root_type::drive_letter || Type == root_type::win32nt_drive_letter || Type == root_type::volume))
@@ -413,10 +408,8 @@ static void InitProfile(string &strProfilePath, string &strLocalProfilePath)
if (!SingleProfile)
CreatePath(path::join(Global->Opt->LocalProfilePath, L"PluginsData"sv), false);
- const auto RandomName = uuid::str(os::uuid::generate());
-
- if (!os::fs::can_create_file(path::join(Global->Opt->ProfilePath, RandomName)) ||
- (!SingleProfile && !os::fs::can_create_file(path::join(Global->Opt->LocalProfilePath, RandomName))))
+ if (!os::fs::can_create_file_in(Global->Opt->ProfilePath) ||
+ (!SingleProfile && !os::fs::can_create_file_in(Global->Opt->LocalProfilePath)))
{
Global->Opt->ReadOnlyConfig = true;
}
@@ -995,7 +988,7 @@ static void premain();
static int wmain_seh()
{
- os::set_error_mode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX);
+ os::set_preferred_error_mode();
// wmain is a non-standard extension and not available in gcc.
int Argc = 0;
diff --git a/far/mix.cpp b/far/mix.cpp
index e88f33543..731dfa4aa 100644
--- a/far/mix.cpp
+++ b/far/mix.cpp
@@ -158,7 +158,7 @@ string MakeTemp(string_view Prefix, bool const WithTempPath, string_view const U
string MakeTempInSameDir(string_view FileName)
{
- return MakeTemp({}, false, CutToParent(FileName)? FileName : L"."sv);
+ return MakeTemp({}, false, CutToParent(FileName)? FileName : L""sv);
}
void PluginPanelItemToFindDataEx(const PluginPanelItem& Src, os::fs::find_data& Dest)
diff --git a/far/pathmix.cpp b/far/pathmix.cpp
index ed1698838..be9c1f2d1 100644
--- a/far/pathmix.cpp
+++ b/far/pathmix.cpp
@@ -127,6 +127,18 @@ string path::normalize_separators(string_view Path)
return normalize_separators(string(Path));
}
+string path::parent_path(string&& Path)
+{
+ CutToParent(Path);
+ return Path;
+}
+
+string_view path::parent_path(string_view Path)
+{
+ CutToParent(Path);
+ return Path;
+}
+
string nt_path(string Path)
{
if (Path.empty())
diff --git a/far/pathmix.hpp b/far/pathmix.hpp
index 91805980e..48f118391 100644
--- a/far/pathmix.hpp
+++ b/far/pathmix.hpp
@@ -98,6 +98,9 @@ namespace path
string normalize_separators(string Path);
string normalize_separators(string_view Path);
+
+ string parent_path(string&& Path);
+ string_view parent_path(string_view Path);
}
string nt_path(string_view Path);
diff --git a/far/platform.cpp b/far/platform.cpp
index 4c0d69f34..4516684b6 100644
--- a/far/platform.cpp
+++ b/far/platform.cpp
@@ -224,6 +224,16 @@ namespace os
SetErrorMode(SetErrorMode(0) & ~Mask);
}
+ void set_preferred_error_mode()
+ {
+ set_error_mode(
+ SEM_FAILCRITICALERRORS |
+ SEM_NOOPENFILEERRORBOX |
+ SEM_NOALIGNMENTFAULTEXCEPT |
+ SEM_NOGPFAULTERRORBOX
+ );
+ }
+
constexpr struct
{
size_t
diff --git a/far/platform.debug.cpp b/far/platform.debug.cpp
index 8c24128c5..9c302fb96 100644
--- a/far/platform.debug.cpp
+++ b/far/platform.debug.cpp
@@ -597,16 +597,11 @@ namespace os::debug::symbols
if (const auto FarPath = fs::get_current_process_file_name(); !FarPath.empty() && FarPath != Module)
{
- string_view FarPathView = FarPath;
- CutToParent(FarPathView);
- append_to_search_path(Path, FarPathView);
+ append_to_search_path(Path, path::parent_path(FarPath));
}
if (!Module.empty())
- {
- CutToParent(Module);
- append_to_search_path(Path, Module);
- }
+ append_to_search_path(Path, path::parent_path(Module));
for (const auto& Var: { L"_NT_SYMBOL_PATH"sv, L"_NT_ALTERNATE_SYMBOL_PATH"sv })
{
diff --git a/far/platform.fs.cpp b/far/platform.fs.cpp
index 965776595..9ba114db1 100644
--- a/far/platform.fs.cpp
+++ b/far/platform.fs.cpp
@@ -2570,9 +2570,9 @@ WARNING_POP()
return is_directory_reparse_point(Data.Attributes) && (Data.ReparseTag == IO_REPARSE_TAG_MOUNT_POINT || Data.ReparseTag == IO_REPARSE_TAG_SYMLINK);
}
- bool can_create_file(string_view const Name)
+ bool can_create_file_in(string_view const DirectoryName)
{
- return file(Name, GENERIC_WRITE, file_share_all, nullptr, CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE)? true : false;
+ return create_file(DirectoryName, FILE_ADD_FILE, file_share_all, nullptr, OPEN_EXISTING)? true : false;
}
bool CreateSymbolicLinkInternal(string_view const Object, string_view const Target, DWORD Flags)
diff --git a/far/platform.fs.hpp b/far/platform.fs.hpp
index 5b69e412a..5fc130653 100644
--- a/far/platform.fs.hpp
+++ b/far/platform.fs.hpp
@@ -638,7 +638,7 @@ namespace os::fs
bool is_directory_symbolic_link(const find_data& Data);
[[nodiscard]]
- bool can_create_file(string_view Name);
+ bool can_create_file_in(string_view DirectoryName);
[[nodiscard]]
diff --git a/far/platform.hpp b/far/platform.hpp
index 1b49de851..b3c9afafb 100644
--- a/far/platform.hpp
+++ b/far/platform.hpp
@@ -229,6 +229,7 @@ namespace os
void set_error_mode(unsigned Mask);
void unset_error_mode(unsigned Mask);
+ void set_preferred_error_mode();
[[nodiscard]]
NTSTATUS get_last_nt_status(void const* Teb);
diff --git a/far/sqlitedb.cpp b/far/sqlitedb.cpp
index 06c660137..3e5917837 100644
--- a/far/sqlitedb.cpp
+++ b/far/sqlitedb.cpp
@@ -460,7 +460,7 @@ public:
database_ptr SourceDb;
delayed_deleter Deleter(true);
- if (WAL && !os::fs::can_create_file(concat(Path, L'.', uuid::str(os::uuid::generate())))) // can't open db -- copy to %TEMP%
+ if (WAL && !os::fs::can_create_file_in(path::parent_path(Path))) // can't open db -- copy to %TEMP%
{
const auto TmpDbPath = concat(MakeTemp(), str(GetCurrentProcessId()), L'-', PointToName(Path));
if (!os::fs::copy_file(Path, TmpDbPath, nullptr, nullptr, 0))
diff --git a/far/usermenu.cpp b/far/usermenu.cpp
index 5c71932ab..8b1fba21f 100644
--- a/far/usermenu.cpp
+++ b/far/usermenu.cpp
@@ -388,10 +388,7 @@ void UserMenu::ProcessUserMenu(bool ChooseMenuType, string_view MenuFileName)
else
{
m_MenuMode = menu_mode::custom;
-
- auto ParentDir = MenuFileName;
- CutToParent(ParentDir);
- strMenuFilePath = ParentDir;
+ strMenuFilePath = path::parent_path(MenuFileName);
}
}
diff --git a/far/vbuild.m4 b/far/vbuild.m4
index 09596ee1f..f42a79915 100644
--- a/far/vbuild.m4
+++ b/far/vbuild.m4
@@ -1 +1 @@
-6612
+6613