New functions used to extract the components of a full filename.

This commit is contained in:
Sebastien Barre 2001-05-24 21:17:02 -04:00
parent 2533d55099
commit 3546e5611c
2 changed files with 101 additions and 5 deletions

View File

@ -314,7 +314,7 @@ bool cmSystemTools::FileExists(const char* filename)
// Return a capitalized string (i.e the first letter is uppercased, all other // Return a capitalized string (i.e the first letter is uppercased, all other
// are lowercased) // are lowercased)
std::string cmSystemTools::Capitalized(std::string& s) std::string cmSystemTools::Capitalized(const std::string& s)
{ {
std::string n; std::string n;
n.resize(s.size()); n.resize(s.size());
@ -874,8 +874,9 @@ std::string cmSystemTools::GetProgramPath(const char* in_name)
} }
/** /**
* Given the path to a program executable, get the directory part of the path with the * Given the path to a program executable, get the directory part of the path
* file stripped off. If there is no directory part, the empty string is returned. * with the file stripped off. If there is no directory part, the empty
* string is returned.
*/ */
void cmSystemTools::SplitProgramPath(const char* in_name, void cmSystemTools::SplitProgramPath(const char* in_name,
std::string& dir, std::string& dir,
@ -934,3 +935,81 @@ std::string cmSystemTools::CollapseFullPath(const char* in_name)
return newPath; return newPath;
} }
/**
* Return path of a full filename (no trailing slashes).
* Warning: returned path is converted to Unix slashes format.
*/
std::string cmSystemTools::GetFilenamePath(const std::string& filename)
{
std::string fn = filename;
cmSystemTools::ConvertToUnixSlashes(fn);
std::string::size_type slash_pos = fn.rfind("/");
if(slash_pos != std::string::npos)
{
return fn.substr(0, slash_pos);
}
else
{
return "";
}
}
/**
* Return file name of a full filename (i.e. file name without path).
*/
std::string cmSystemTools::GetFilenameName(const std::string& filename)
{
std::string fn = filename;
cmSystemTools::ConvertToUnixSlashes(fn);
std::string::size_type slash_pos = fn.rfind("/");
if(slash_pos != std::string::npos)
{
return fn.substr(slash_pos + 1);
}
else
{
return filename;
}
}
/**
* Return file extension of a full filename (dot included).
* Warning: this is the longest extension (for example: .tar.gz)
*/
std::string cmSystemTools::GetFilenameExtension(const std::string& filename)
{
std::string name = cmSystemTools::GetFilenameName(filename);
std::string::size_type dot_pos = name.find(".");
if(dot_pos != std::string::npos)
{
return name.substr(dot_pos);
}
else
{
return "";
}
}
/**
* Return file name without extension of a full filename (i.e. without path).
* Warning: it considers the longest extension (for example: .tar.gz)
*/
std::string cmSystemTools::GetFilenameNameWithoutExtension(const std::string& filename)
{
std::string name = cmSystemTools::GetFilenameName(filename);
std::string::size_type dot_pos = name.find(".");
if(dot_pos != std::string::npos)
{
return name.substr(0, dot_pos);
}
else
{
return name;
}
}

View File

@ -82,7 +82,7 @@ public:
* Return a capitalized string (i.e the first letter is uppercased, all other * Return a capitalized string (i.e the first letter is uppercased, all other
* are lowercased). * are lowercased).
*/ */
static std::string Capitalized(std::string&); static std::string Capitalized(const std::string&);
/** /**
* Replace Windows file system slashes with Unix-style slashes. * Replace Windows file system slashes with Unix-style slashes.
@ -180,16 +180,33 @@ public:
///! Find an executable in the system PATH, with optional extra paths. ///! Find an executable in the system PATH, with optional extra paths.
static std::string FindProgram(const char* name, static std::string FindProgram(const char* name,
const std::vector<std::string>& path= std::vector<std::string>()); const std::vector<std::string>& path= std::vector<std::string>());
///! Find a library in the system PATH, with optional extra paths. ///! Find a library in the system PATH, with optional extra paths.
static std::string FindLibrary(const char* name, static std::string FindLibrary(const char* name,
const std::vector<std::string>& path); const std::vector<std::string>& path);
///! return true if the file is a directory. ///! return true if the file is a directory.
static bool FileIsDirectory(const char* name); static bool FileIsDirectory(const char* name);
static std::string GetCurrentWorkingDirectory(); static std::string GetCurrentWorkingDirectory();
static std::string GetProgramPath(const char*); static std::string GetProgramPath(const char*);
static void SplitProgramPath(const char* in_name, std::string& dir, std::string& file); static void SplitProgramPath(const char* in_name,
std::string& dir,
std::string& file);
static std::string CollapseFullPath(const char*); static std::string CollapseFullPath(const char*);
///! return path of a full filename (no trailing slashes).
static std::string GetFilenamePath(const std::string&);
///! return file name of a full filename (i.e. file name without path).
static std::string GetFilenameName(const std::string&);
///! return file extension of a full filename (dot included).
static std::string GetFilenameExtension(const std::string&);
///! return file name without extension of a full filename.
static std::string GetFilenameNameWithoutExtension(const std::string&);
static long int ModifiedTime(const char* filename); static long int ModifiedTime(const char* filename);
/** /**