From Sukender, "Here is my proposal. I fixed what Paul said, added some doxygen comments, added the function I told about, and removed the two "find('/')"-like calls to use only one."

This commit is contained in:
Robert Osfield
2010-03-10 11:40:17 +00:00
parent ea5af1d010
commit 6ae2f4c6c6
2 changed files with 25 additions and 4 deletions

View File

@@ -20,16 +20,27 @@
namespace osgDB {
/** Gets the parent path from full name (Ex: /a/b/c.Ext => /a/b). */
extern OSGDB_EXPORT std::string getFilePath(const std::string& filename);
/** Gets the extension without dot (Ex: /a/b/c.Ext => Ext). */
extern OSGDB_EXPORT std::string getFileExtension(const std::string& filename);
/** Gets the extension including dot (Ex: /a/b/c.Ext => .Ext). */
extern OSGDB_EXPORT std::string getFileExtensionIncludingDot(const std::string& filename);
/** Gets the lowercase extension without dot (Ex: /a/b/c.Ext => ext). */
extern OSGDB_EXPORT std::string getLowerCaseFileExtension(const std::string& filename);
/** Gets file name with extension (Ex: /a/b/c.Ext => c.Ext). */
extern OSGDB_EXPORT std::string getSimpleFileName(const std::string& fileName);
/** Gets file path without last extension (Ex: /a/b/c.Ext => /a/b/c ; file.ext1.ext2 => file.ext1). */
extern OSGDB_EXPORT std::string getNameLessExtension(const std::string& fileName);
/** Gets file path without \b all extensions (Ex: /a/b/c.Ext => /a/b/c ; file.ext1.ext2 => file). */
extern OSGDB_EXPORT std::string getNameLessAllExtensions(const std::string& fileName);
/** Gets file name without last extension (Ex: /a/b/c.Ext => c ; file.ext1.ext2 => file.ext1). */
extern OSGDB_EXPORT std::string getStrippedName(const std::string& fileName);
/** Converts forward slashes (/) to back slashes (\). */
extern OSGDB_EXPORT std::string convertFileNameToWindowsStyle(const std::string& fileName);
/** Converts back slashes (\) to forward slashes (/). */
extern OSGDB_EXPORT std::string convertFileNameToUnixStyle(const std::string& fileName);
extern OSGDB_EXPORT std::string convertToLowerCase(const std::string& fileName);

View File

@@ -138,14 +138,24 @@ std::string osgDB::convertToLowerCase(const std::string& str)
// strip one level of extension from the filename.
std::string osgDB::getNameLessExtension(const std::string& fileName)
{
std::string::size_type dot = fileName.find_last_of('.');
std::string::size_type back_slash = fileName.find_last_of('\\');
std::string::size_type slash = fileName.find_last_of('/');
if (dot==std::string::npos || (dot<back_slash && dot<slash)) return fileName;
std::string::size_type dot = fileName.find_last_of('.');
std::string::size_type slash = fileName.find_last_of("/\\"); // Finds forward slash *or* back slash
if (dot==std::string::npos || (slash!=std::string::npos && dot<slash)) return fileName;
return std::string(fileName.begin(),fileName.begin()+dot);
}
// strip all extensions from the filename.
std::string osgDB::getNameLessAllExtensions(const std::string& fileName)
{
// Finds start serach position: from last slash, or the begining of the string if none found
std::string::size_type startPos = fileName.find_last_of("/\\"); // Finds forward slash *or* back slash
if (startPos == std::string::npos) startPos = 0;
std::string::size_type dot = fileName.find_first_of('.', startPos); // Finds *FIRST* dot from start pos
if (dot==std::string::npos) return fileName;
return std::string(fileName.begin(),fileName.begin()+dot);
}
std::string osgDB::getStrippedName(const std::string& fileName)
{
std::string simpleName = getSimpleFileName(fileName);