From d034c4d773141a0f26fef279cf6460445d5c111f Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 16 Mar 2008 09:04:21 +0000 Subject: 2008-03-16 Tatsuhiro Tsujikawa Rewritten getBasename() and getDirname(), without standard library basename(), dirname(). * src/File.cc * test/FileTest.cc --- src/File.cc | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'src/File.cc') 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) -- cgit