Skip to content

Commit

Permalink
1. Уточнение 4280.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Mar 2, 2015
1 parent 02f72e3 commit b6d7c65
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
6 changes: 5 additions & 1 deletion far/changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
drkns 01.03.2015 16:17:34 +0200 - build 4284
drkns 02.03.2015 22:15:32 +0200 - build 4285

1. Уточнение 4280.1.

drkns 01.03.2015 16:17:34 +0200 - build 4284

1. При определённых манипуляциях с QuickView могло происходить странное.

Expand Down
5 changes: 1 addition & 4 deletions far/dirmix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,10 @@ bool CheckShortcutFolder(string& pTestPath, bool TryClosest, bool Silent)
{
if (Silent || !Message(MSG_WARNING | MSG_ERRORTYPE, 2, MSG(MError), strTarget.data(), MSG(MNeedNearPath), MSG(MHYes),MSG(MHNo)))
{
size_t RootLength = 0;
ParsePath(pTestPath, &RootLength);

string strTestPathTemp = pTestPath;
for (;;)
{
if (!CutToSlash(strTestPathTemp) || strTestPathTemp.size() < RootLength)
if (!CutToParent(strTestPathTemp))
break;

if (api::fs::exists(strTestPathTemp))
Expand Down
87 changes: 83 additions & 4 deletions far/pathmix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,27 @@ bool CutToSlash(string &strStr, bool bInclude)
return false;
}

bool CutToParent(string &strStr)
{
bool Result = false;
size_t RootLength = 0;
ParsePath(strStr, &RootLength);
const auto RootOnly = RootLength == strStr.size();
const auto EndPos = !strStr.empty() && IsSlash(strStr.back()) && !RootOnly? strStr.size() - 1 : strStr.size();
size_t pos;
if (FindLastSlash(pos, strStr, RootLength, EndPos))
{
strStr.resize(pos);
Result = true;
}
else if (RootLength && !RootOnly)
{
strStr.resize(RootLength);
Result = true;
}
return Result;
}

string &CutToNameUNC(string &strPath)
{
const wchar_t *lpwszPath = strPath.data();
Expand Down Expand Up @@ -455,9 +476,10 @@ const wchar_t *LastSlash(const wchar_t *String)
return IsSlash(*String)?String:nullptr;
}

bool FindSlash(size_t &Pos, const string &Str, size_t StartPos)
bool FindSlash(size_t &Pos, const string &Str, size_t StartPos, size_t EndPos)
{
for (size_t p = StartPos; p < Str.size(); p++)
EndPos = std::min(EndPos, Str.size());
for (size_t p = StartPos; p != EndPos; ++p)
{
if (IsSlash(Str[p]))
{
Expand All @@ -469,9 +491,10 @@ bool FindSlash(size_t &Pos, const string &Str, size_t StartPos)
return false;
}

bool FindLastSlash(size_t &Pos, const string &Str)
bool FindLastSlash(size_t &Pos, const string &Str, size_t StartPos, size_t EndPos)
{
for (size_t p = Str.size(); p > 0; p--)
EndPos = std::min(EndPos, Str.size());
for (size_t p = EndPos; p != StartPos; --p)
{
if (IsSlash(Str[p - 1]))
{
Expand Down Expand Up @@ -676,5 +699,61 @@ void TestPathParser()
assert(!PathStartsWith(L"C:\\path\\file", L"C:\\pat"));
assert(PathStartsWith(L"\\", L""));
assert(!PathStartsWith(L"C:\\path\\file", L""));

const wchar_t* TestRoots[] =
{
L"",
L"C:",
L"C:\\",
L"\\\\server\\share\\",
L"\\\\?\\C:\\",
L"\\\\?\\UNC\\server\\share\\",
L"\\\\?\\Volume{f26b206c-f912-11e1-b516-806e6f6e6963}\\",
L"\\\\?\\pipe\\",
};

const struct
{
const wchar_t* InputPath;
const wchar_t* ExpectedPath;
bool RootMustExist;
bool ExpectedReult;
}
TestCases[] =
{
// root directory, shall fail
{ L"[root]", L"[root]", false, false},

// one level, shall return root directory
{ L"[root]dir1", L"[root]", true, true },

// one level without root, shall fail
{ L"dir1", L"dir1", false, false },

// two levels, shall return first level
{ L"[root]dir1\\dir2", L"[root]dir1", false, true },

// two levels with trailing slash, shall return first level
{ L"[root]dir1\\dir2\\", L"[root]dir1", false, true },
};

string Path, Baseline;
FOR(const auto& Root, TestRoots)
{
FOR(const auto& Test, TestCases)
{
if (!*Root && Test.RootMustExist)
continue;

Path = Test.InputPath;
ReplaceStrings(Path, L"[root]", Root);
Baseline = Test.ExpectedPath;
ReplaceStrings(Baseline, L"[root]", Root);

const auto Result = CutToParent(Path);
assert(Result == Test.ExpectedReult);
assert(Path == Baseline);
}
}
#endif
}
8 changes: 4 additions & 4 deletions far/pathmix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ bool HasPathPrefix(const string &Path);
bool PathStartsWith(const string &Path, const string &Start);
bool PathCanHoldRegularFile(const string& Path);
bool IsPluginPrefixPath(const string &Path);

bool CutToSlash(string &strStr, bool bInclude = false);
bool CutToSlash(string &strStr, bool bInclude = false); // BUGBUG, deprecated. Use CutToParent.
bool CutToParent(string &strStr);
string &CutToNameUNC(string &strPath);
string &CutToFolderNameIfFolder(string &strPath);
const wchar_t* PointToName(const wchar_t *lpwszPath);
Expand All @@ -85,8 +85,8 @@ string &ReplaceSlashToBSlash(string &strStr);

const wchar_t *FirstSlash(const wchar_t *String);
const wchar_t *LastSlash(const wchar_t *String);
bool FindSlash(size_t &Pos, const string &Str, size_t StartPos = 0);
bool FindLastSlash(size_t &Pos, const string &Str);
bool FindSlash(size_t &Pos, const string &Str, size_t StartPos = 0, size_t EndPos = string::npos);
bool FindLastSlash(size_t &Pos, const string &Str, size_t StartPos = 0, size_t EndPos = string::npos);

bool TestParentFolderName(const string& Name);
bool TestCurrentDirectory(const string& TestDir);
Expand Down
2 changes: 1 addition & 1 deletion far/vbuild.m4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
m4_define(BUILD,4284)m4_dnl
m4_define(BUILD,4285)m4_dnl

0 comments on commit b6d7c65

Please sign in to comment.