diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-03-16 09:04:21 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2008-03-16 09:04:21 +0000 |
| commit | d034c4d773141a0f26fef279cf6460445d5c111f (patch) | |
| tree | 73e43eeec52758282f890d11e8e86b5b628bdc45 /src/File.cc | |
| parent | 5cc4ca588742de393cdc1b9e3b66d620043a2cb4 (diff) | |
2008-03-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten getBasename() and getDirname(), without standard
library
basename(), dirname().
* src/File.cc
* test/FileTest.cc
Diffstat (limited to 'src/File.cc')
| -rw-r--r-- | src/File.cc | 30 |
1 files changed, 18 insertions, 12 deletions
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 <libgen.h> -// use GNU version basename -#undef basename #include <cstring> #include <stdlib.h> #include <deque> @@ -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) |
