mirror of https://github.com/aria2/aria2
2008-03-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten getBasename() and getDirname(), without standard library basename(), dirname(). * src/File.cc * test/FileTest.ccpull/1/head
parent
5cc4ca5887
commit
d034c4d773
|
@ -1,3 +1,10 @@
|
|||
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
|
||||
|
||||
2008-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||
|
||||
Added Simplified Chinese translation. Also updated the following
|
||||
|
|
30
src/File.cc
30
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)
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue