1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
string ExtractDirectory( const string& path, char delimiter = '/' )
//
// Returns everything, including the trailing path separator, except the filename
// part of the path.
//
// "/foo/bar/baz.txt" --> "/foo/bar/"
{
return path.substr( 0, path.find_last_of( delimiter ) + 1 );
}
string ExtractFilename( const string& path, char delimiter = '/' )
//
// Returns only the filename part of the path.
//
// "/foo/bar/baz.txt" --> "baz.txt"
{
return path.substr( path.find_last_of( delimiter ) + 1 );
}
string ExtractFileExtension( const string& path, char delimiter = '/' )
//
// Returns the file's extension, if any. The period is considered part
// of the extension.
//
// "/foo/bar/baz.txt" --> ".txt"
// "/foo/bar/baz" --> ""
{
string filename = ExtractFilename( path, delimiter );
string::size_type n = filename.find_last_of( '.' );
if (n != string::npos)
return filename.substr( n );
return string();
}
string ChangeFileExtension( const string& path, const string& ext, char delimiter = '/' )
//
// Modifies the filename's extension. The period is considered part
// of the extension.
//
// "/foo/bar/baz.txt", ".dat" --> "/foo/bar/baz.dat"
// "/foo/bar/baz.txt", "" --> "/foo/bar/baz"
// "/foo/bar/baz", ".txt" --> "/foo/bar/baz.txt"
//
{
string filename = ExtractFilename( path, delimiter );
return ExtractDirectory( path, delimiter )
+ filename.substr( 0, filename.find_last_of( '.' ) )
+ ext;
}
| |