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>
|
2008-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
Added Simplified Chinese translation. Also updated the following
|
Added Simplified Chinese translation. Also updated the following
|
||||||
|
|
30
src/File.cc
30
src/File.cc
|
@ -34,10 +34,6 @@
|
||||||
/* copyright --> */
|
/* copyright --> */
|
||||||
#include "File.h"
|
#include "File.h"
|
||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
#define basename posix_basename
|
|
||||||
#include <libgen.h>
|
|
||||||
// use GNU version basename
|
|
||||||
#undef basename
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
|
@ -134,18 +130,28 @@ mode_t File::mode()
|
||||||
|
|
||||||
std::string File::getBasename() const
|
std::string File::getBasename() const
|
||||||
{
|
{
|
||||||
char* s = strdup(name.c_str());
|
std::string::size_type lastSlashIndex = name.find_last_of("/");
|
||||||
std::string bname = basename(s);
|
if(lastSlashIndex == std::string::npos) {
|
||||||
free(s);
|
return name;
|
||||||
return bname;
|
} else {
|
||||||
|
return name.substr(lastSlashIndex+1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string File::getDirname() const
|
std::string File::getDirname() const
|
||||||
{
|
{
|
||||||
char* s = strdup(name.c_str());
|
std::string::size_type lastSlashIndex = name.find_last_of("/");
|
||||||
std::string dname = dirname(s);
|
if(lastSlashIndex == std::string::npos) {
|
||||||
free(s);
|
if(name == "") {
|
||||||
return dname;
|
return "";
|
||||||
|
} else {
|
||||||
|
return ".";
|
||||||
|
}
|
||||||
|
} else if(lastSlashIndex == 0) {
|
||||||
|
return "/";
|
||||||
|
} else {
|
||||||
|
return name.substr(0, lastSlashIndex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool File::isDir(const std::string& filename)
|
bool File::isDir(const std::string& filename)
|
||||||
|
|
|
@ -126,14 +126,66 @@ void FileTest::testMkdir() {
|
||||||
|
|
||||||
void FileTest::testGetDirname()
|
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()
|
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()
|
void FileTest::testRenameTo()
|
||||||
|
|
Loading…
Reference in New Issue