From: Philippe E. <ph...@us...> - 2002-10-11 02:11:42
|
Update of /cvsroot/oprofile/oprofile/libutil++ In directory usw-pr-cvs1:/tmp/cvs-serv14373/libutil++ Modified Files: file_manip.cpp filename_match.cpp string_manip.cpp string_manip.h Log Message: fix dirname(), basename(), remove rtrim() regards, Phil Index: file_manip.cpp =================================================================== RCS file: /cvsroot/oprofile/oprofile/libutil++/file_manip.cpp,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- file_manip.cpp 1 Oct 2002 22:32:31 -0000 1.15 +++ file_manip.cpp 11 Oct 2002 02:11:39 -0000 1.16 @@ -180,6 +180,26 @@ return res; } + +/** + * @param path_name the path where we remove trailing '/' + * + * erase all trailing '/' in path_name except if the last '/' is at pos 0 + */ +static string erase_trailing_path_separator(string const & path_name) +{ + string result(path_name); + + while (result.length() > 1) { + if (result[result.length() - 1] != '/') + break; + result.erase(result.length() - 1, 1); + } + + return result; +} + + /** * dirname - get the path component of a filename * @param file_name filename @@ -188,9 +208,29 @@ */ string dirname(string const & file_name) { - return erase_from_last_of(file_name, '/'); + string result = erase_trailing_path_separator(file_name); + + if (result.find_first_of('/') == string::npos) + return "."; + + if (result.length() == 1) + // catch result == "/" + return result; + + size_t pos = result.find_last_of('/'); + if (pos == 0) + // "/usr" must return "/" + pos = 1; + + result.erase(pos, result.length() - pos); + + // "////usr" must return "/" + result = erase_trailing_path_separator(result); + + return result; } + /** * basename - get the basename of a path * @param path_name path @@ -199,7 +239,11 @@ */ string basename(string const & path_name) { - string result = rtrim(path_name, '/'); + string result = erase_trailing_path_separator(path_name); + + if (result.length() == 1) + // catch result == "/" + return result; return erase_to_last_of(result, '/'); } Index: filename_match.cpp =================================================================== RCS file: /cvsroot/oprofile/oprofile/libutil++/filename_match.cpp,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- filename_match.cpp 1 Oct 2002 22:32:31 -0000 1.6 +++ filename_match.cpp 11 Oct 2002 02:11:39 -0000 1.7 @@ -44,6 +44,8 @@ while (!comp.empty() && comp != "/") { if (match(exclude_pattern, basename(comp))) return false; + // FIXME: test uneccessary, wait a decent testsuite before + // removing if (comp == dirname(comp)) break; comp = dirname(comp); @@ -63,6 +65,7 @@ while (!compi.empty() && compi != "/") { if (match(include_pattern, basename(compi))) return true; + // FIXME see above. if (compi == dirname(compi)) break; compi = dirname(compi); Index: string_manip.cpp =================================================================== RCS file: /cvsroot/oprofile/oprofile/libutil++/string_manip.cpp,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- string_manip.cpp 1 Oct 2002 22:32:31 -0000 1.10 +++ string_manip.cpp 11 Oct 2002 02:11:39 -0000 1.11 @@ -36,18 +36,6 @@ return result; } -string rtrim(string const & str, char ch) -{ - string result = str; - - // a more efficient implementation is possible if we need it. - string::size_type slash = result.find_last_of(ch); - if (slash != string::npos) - result.erase(0, slash + 1); - - return result; -} - string tostr(unsigned int i) { string str; Index: string_manip.h =================================================================== RCS file: /cvsroot/oprofile/oprofile/libutil++/string_manip.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- string_manip.h 29 Sep 2002 01:32:39 -0000 1.9 +++ string_manip.h 11 Oct 2002 02:11:39 -0000 1.10 @@ -34,14 +34,6 @@ std::string erase_to_last_of(std::string const & str, char ch); -/** - * @param str the string - * @param ch the character to remove - * - * Returns str removed of its trailing ch - */ -std::string rtrim(std::string const & str, char ch); - /// conversion to std::string std::string tostr(unsigned int i); |