Commit 0e031774 authored by miller86's avatar miller86

adding optional extension to Basename method and adding tests for it

git-svn-id: http://visit.ilight.com/svn/visit/trunk/src@27203 18c085ea-50e0-402c-830e-de6fd14e8384
parent 52ca0b71
......@@ -568,9 +568,12 @@ FileFunctions::FilteredPath(const std::string &path)
// When searching the string, look for either type of slash char, but still
// use the sys-dependent VISIT_SLASH_STRING when setting in the empty buf.
//
// Mark C. Miller, Tue Sep 15 20:18:22 PDT 2015
// Added arg and logic to support an optional suffix string just like
// Unix basename command.
// ****************************************************************************
static const char *
basename(const char *path, int& start)
static char const *
basename(char const *path, int& start, char const *suffix=0)
{
start = 0;
......@@ -618,26 +621,36 @@ basename(const char *path, int& start)
i++;
start = i;
// build the return string
// build the candidate return string
int k;
for (k = 0; k < j - i + 1; k++)
StaticStringBuf[k] = path[i+k];
StaticStringBuf[k] = '\0';
// Handle optional suffix but only if its not equal to
// remaining string (as per man pages for dirname)
if (suffix)
{
int n = strlen(suffix);
if (n < k && !strncmp(&StaticStringBuf[k-n],suffix,n))
StaticStringBuf[k-n] = '\0';
}
return StaticStringBuf;
}
}
const char *
FileFunctions::Basename(const char *path)
char const *
FileFunctions::Basename(char const *path, char const *suffix)
{
int dummy1;
return basename(path, dummy1);
return basename(path, dummy1, suffix);
}
std::string
FileFunctions::Basename(const std::string &path)
FileFunctions::Basename(const std::string &path, const std::string &suffix)
{
return Basename(path.c_str());
return Basename(path.c_str(), suffix.c_str());
}
// ****************************************************************************
......
......@@ -100,8 +100,8 @@ std::string MISC_API ExpandPath(const std::string &path,
std::string MISC_API FilteredPath(const std::string &path);
// Filename manipulation
const char MISC_API *Basename(const char *path);
std::string MISC_API Basename(const std::string &path);
const char MISC_API *Basename(const char *path, const char *suffix=0);
std::string MISC_API Basename(const std::string &path, const std::string &suffix="");
const char MISC_API *Dirname(const char *path);
std::string MISC_API Dirname(const std::string &path);
const char MISC_API *Absname(const char *cwd_context,
......
......@@ -248,6 +248,20 @@ int main(int argc, char **argv)
//
// Test Basename and Dirname
//
#define CHECK_BASENAME_WITH_SUFFIX(path,suffix,expected) \
{ \
string _path = slash_swap_for_os(path); \
string _suffix = slash_swap_for_os(suffix); \
string _expected = slash_swap_for_os(expected); \
if (string(Basename(_path,_suffix)) != string(_expected)) \
{ \
cerr << "Basename(" << _path << "," << _suffix << ") = \""\
<< Basename(_path,_suffix) << "\", expected \"" \
<< _expected << "\"" << endl; \
pathname_errors++; \
} \
}
#define CHECK_PATHNAMES(path,dir,base) \
{ \
string _path = slash_swap_for_os(path); \
......@@ -268,6 +282,12 @@ int main(int argc, char **argv)
}
int pathname_errors = 0;
CHECK_BASENAME_WITH_SUFFIX("/foo/bar/gorfo.txt", ".txt", "gorfo");
CHECK_BASENAME_WITH_SUFFIX("../../gorfo.txt.zip", ".txt.zip", "gorfo");
CHECK_BASENAME_WITH_SUFFIX("/foo/bar/gorfo.txt", "gorfo.txt", "gorfo.txt");
CHECK_BASENAME_WITH_SUFFIX("/foo/bar/gorfo.txt", "", "gorfo.txt");
#ifdef WIN32
CHECK_PATHNAMES("C:/usr/lib", "C:/usr", "lib");
CHECK_PATHNAMES("D:/usr/", "D:/", "usr");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment