From d034c4d773141a0f26fef279cf6460445d5c111f Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 16 Mar 2008 09:04:21 +0000 Subject: [PATCH] 2008-03-16 Tatsuhiro Tsujikawa Rewritten getBasename() and getDirname(), without standard library basename(), dirname(). * src/File.cc * test/FileTest.cc --- ChangeLog | 7 ++++++ src/File.cc | 30 ++++++++++++++---------- test/FileTest.cc | 60 ++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 81 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3defd017..502fac17 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-03-16 Tatsuhiro Tsujikawa + + Rewritten getBasename() and getDirname(), without standard library + basename(), dirname(). + * src/File.cc + * test/FileTest.cc + 2008-03-15 Tatsuhiro Tsujikawa Added Simplified Chinese translation. Also updated the following diff --git a/src/File.cc b/src/File.cc index 6af28fd0..7d13b035 100644 --- a/src/File.cc +++ b/src/File.cc @@ -34,10 +34,6 @@ /* copyright --> */ #include "File.h" #include "Util.h" -#define basename posix_basename -#include -// use GNU version basename -#undef basename #include #include #include @@ -134,18 +130,28 @@ mode_t File::mode() std::string File::getBasename() const { - char* s = strdup(name.c_str()); - std::string bname = basename(s); - free(s); - return bname; + std::string::size_type lastSlashIndex = name.find_last_of("/"); + if(lastSlashIndex == std::string::npos) { + return name; + } else { + return name.substr(lastSlashIndex+1); + } } std::string File::getDirname() const { - char* s = strdup(name.c_str()); - std::string dname = dirname(s); - free(s); - return dname; + std::string::size_type lastSlashIndex = name.find_last_of("/"); + if(lastSlashIndex == std::string::npos) { + if(name == "") { + return ""; + } else { + return "."; + } + } else if(lastSlashIndex == 0) { + return "/"; + } else { + return name.substr(0, lastSlashIndex); + } } bool File::isDir(const std::string& filename) diff --git a/test/FileTest.cc b/test/FileTest.cc index 2bbd5177..56fdf7f4 100644 --- a/test/FileTest.cc +++ b/test/FileTest.cc @@ -126,14 +126,66 @@ void FileTest::testMkdir() { void FileTest::testGetDirname() { - File f("/tmp/dist/aria2.tar.bz2"); - CPPUNIT_ASSERT_EQUAL(std::string("/tmp/dist"), f.getDirname()); + { + File f("/usr/lib"); + CPPUNIT_ASSERT_EQUAL(std::string("/usr"), f.getDirname()); + } + { + File f("/usr/"); + CPPUNIT_ASSERT_EQUAL(std::string("/usr"), f.getDirname()); + } + { + File f("usr"); + CPPUNIT_ASSERT_EQUAL(std::string("."), f.getDirname()); + } + { + File f("/"); + CPPUNIT_ASSERT_EQUAL(std::string("/"), f.getDirname()); + } + { + File f("."); + CPPUNIT_ASSERT_EQUAL(std::string("."), f.getDirname()); + } + { + File f(".."); + CPPUNIT_ASSERT_EQUAL(std::string("."), f.getDirname()); + } + { + File f(""); + CPPUNIT_ASSERT_EQUAL(std::string(""), f.getDirname()); + } } void FileTest::testGetBasename() { - File f("/tmp/dist/aria2.tar.bz2"); - CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), f.getBasename()); + { + File f("/usr/lib"); + CPPUNIT_ASSERT_EQUAL(std::string("lib"), f.getBasename()); + } + { + File f("/usr/"); + CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename()); + } + { + File f("usr"); + CPPUNIT_ASSERT_EQUAL(std::string("usr"), f.getBasename()); + } + { + File f("/"); + CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename()); + } + { + File f("."); + CPPUNIT_ASSERT_EQUAL(std::string("."), f.getBasename()); + } + { + File f(".."); + CPPUNIT_ASSERT_EQUAL(std::string(".."), f.getBasename()); + } + { + File f(""); + CPPUNIT_ASSERT_EQUAL(std::string(""), f.getBasename()); + } } void FileTest::testRenameTo()