KPath: Fixed Normalize() return code, changed Leaf().

* Normalize() now returns the error code that vfs_normalize_path()
  returns.
* Leaf() now returns "" instead of "/" for the root. It's not used
  outside of KPath.
* Adapted RemoveLeaf() to deal with this correctly.
* "KPath = string" no longer changes the buffer size.
* Added missing operator tests for =, ==, and !=.
This commit is contained in:
Axel Dörfler
2017-04-30 17:13:33 +02:00
parent ec407447c9
commit e1b4aed0cb
3 changed files with 76 additions and 17 deletions
+11 -12
View File
@@ -1,5 +1,6 @@
/*
* Copyright 2004-2008, Ingo Weinhold, [email protected].
* Copyright 2008-2017, Axel Dörfler, [email protected].
* Distributed under the terms of the MIT License.
*/
@@ -106,7 +107,7 @@ KPath::Adopt(KPath& other)
status_t
KPath::InitCheck() const
{
return fBuffer ? B_OK : B_NO_MEMORY;
return fBuffer != NULL ? B_OK : B_NO_MEMORY;
}
@@ -203,14 +204,11 @@ KPath::Leaf() const
if (fBuffer == NULL)
return NULL;
// only "/" has trailing slashes -- then we have to return the complete
// buffer, as we have to do in case there are no slashes at all
if (fPathLength != 1 || fBuffer[0] != '/') {
for (int32 i = fPathLength - 1; i >= 0; i--) {
if (fBuffer[i] == '/')
return fBuffer + i + 1;
}
for (int32 i = fPathLength - 1; i >= 0; i--) {
if (fBuffer[i] == '/')
return fBuffer + i + 1;
}
return fBuffer;
}
@@ -242,7 +240,7 @@ KPath::RemoveLeaf()
{
// get the leaf -- bail out, if not initialized or only the "/" is left
const char* leaf = Leaf();
if (leaf == NULL || leaf == fBuffer)
if (leaf == NULL || leaf == fBuffer || leaf[0] == '\0')
return false;
// chop off the leaf
@@ -305,6 +303,7 @@ KPath::Normalize(bool traverseLeafLink)
// it completely to avoid weird problems.
fBuffer[0] = '\0';
fPathLength = 0;
return error;
}
fPathLength = strlen(fBuffer);
@@ -323,7 +322,7 @@ KPath::operator=(const KPath& other)
KPath&
KPath::operator=(const char* path)
{
SetTo(path);
SetPath(path);
return *this;
}
@@ -331,7 +330,7 @@ KPath::operator=(const char* path)
bool
KPath::operator==(const KPath& other) const
{
if (!fBuffer)
if (fBuffer == NULL)
return !other.fBuffer;
return other.fBuffer
@@ -343,7 +342,7 @@ KPath::operator==(const KPath& other) const
bool
KPath::operator==(const char* path) const
{
if (!fBuffer)
if (fBuffer == NULL)
return (!path);
return path && strcmp(fBuffer, path) == 0;
+63 -5
View File
@@ -97,8 +97,7 @@ KPathTest::TestLeaf()
path.SetTo("/");
CPPUNIT_ASSERT(strcmp(path.Path(), "/") == 0);
// CPPUNIT_ASSERT(path.Leaf() == NULL);
// TODO: why '/'?
CPPUNIT_ASSERT(strcmp(path.Leaf(), "") == 0);
path.SetTo("a/b");
CPPUNIT_ASSERT(strcmp(path.Path(), "a/b") == 0);
@@ -150,19 +149,30 @@ KPathTest::TestRemoveLeaf()
path.SetTo("a//b/c");
removed = path.RemoveLeaf();
CPPUNIT_ASSERT(removed);
CPPUNIT_ASSERT_MESSAGE("1. removed", removed);
CPPUNIT_ASSERT(strcmp(path.Path(), "a//b") == 0);
CPPUNIT_ASSERT(path.Length() == 4);
removed = path.RemoveLeaf();
CPPUNIT_ASSERT(removed);
CPPUNIT_ASSERT_MESSAGE("2. removed", removed);
CPPUNIT_ASSERT(strcmp(path.Path(), "a") == 0);
CPPUNIT_ASSERT(path.Length() == 1);
removed = path.RemoveLeaf();
CPPUNIT_ASSERT(!removed);
CPPUNIT_ASSERT_MESSAGE("3. !removed", !removed);
CPPUNIT_ASSERT(strcmp(path.Path(), "a") == 0);
CPPUNIT_ASSERT(path.Length() == 1);
path.SetTo("/a");
removed = path.RemoveLeaf();
CPPUNIT_ASSERT_MESSAGE("4. removed", removed);
CPPUNIT_ASSERT(strcmp(path.Path(), "/") == 0);
CPPUNIT_ASSERT(path.Length() == 1);
removed = path.RemoveLeaf();
CPPUNIT_ASSERT_MESSAGE("5. !removed", !removed);
CPPUNIT_ASSERT(strcmp(path.Path(), "/") == 0);
CPPUNIT_ASSERT(path.Length() == 1);
}
@@ -218,9 +228,53 @@ KPathTest::TestNormalize()
}
void
KPathTest::TestAssign()
{
KPath one("first", false, 10);
CPPUNIT_ASSERT(one.Length() == 5);
KPath two("second", false, 20);
two = one;
CPPUNIT_ASSERT(strcmp(two.Path(), one.Path()) == 0);
CPPUNIT_ASSERT(two.Path() != one.Path());
CPPUNIT_ASSERT(two.BufferSize() == one.BufferSize());
CPPUNIT_ASSERT(two.Length() == one.Length());
one = "/whatever";
CPPUNIT_ASSERT(one.Length() == 9);
CPPUNIT_ASSERT(one.BufferSize() == two.BufferSize());
CPPUNIT_ASSERT(strcmp(one.Path(), "/whatever") == 0);
}
void
KPathTest::TestEquals()
{
KPath a("one");
KPath b("two");
CPPUNIT_ASSERT_MESSAGE("1.", !(a == b));
b = a;
CPPUNIT_ASSERT_MESSAGE("2.", a == b);
b = "ones";
CPPUNIT_ASSERT_MESSAGE("3.", !(a == b));
}
void
KPathTest::TestNotEquals()
{
KPath a("one");
KPath b("two");
CPPUNIT_ASSERT_MESSAGE("1.", a != b);
b = a;
CPPUNIT_ASSERT_MESSAGE("2.", !(a != b));
b = "ones";
CPPUNIT_ASSERT_MESSAGE("3.", a != b);
}
@@ -243,8 +297,12 @@ KPathTest::AddTests(BTestSuite& parent)
"KPathTest::TestDetachBuffer", &KPathTest::TestDetachBuffer));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestNormalize", &KPathTest::TestNormalize));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestAssign", &KPathTest::TestAssign));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestEquals", &KPathTest::TestEquals));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestNotEquals", &KPathTest::TestNotEquals));
parent.addTest("KPathTest", &suite);
}
+2
View File
@@ -22,7 +22,9 @@ public:
void TestAdopt();
void TestDetachBuffer();
void TestNormalize();
void TestAssign();
void TestEquals();
void TestNotEquals();
static void AddTests(BTestSuite& suite);
};