diff options
Diffstat (limited to 'libplatform/io/FileSystem.cpp')
-rw-r--r-- | libplatform/io/FileSystem.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/libplatform/io/FileSystem.cpp b/libplatform/io/FileSystem.cpp new file mode 100644 index 0000000..762511f --- /dev/null +++ b/libplatform/io/FileSystem.cpp @@ -0,0 +1,101 @@ +#include "libplatform/impl.h" + +namespace mp4v2 { namespace platform { namespace io { + +/////////////////////////////////////////////////////////////////////////////// + +void +FileSystem::pathnameCleanup( string& name ) +{ + string bad; + + // fold repeating directory separators + bad = DIR_SEPARATOR; + bad += DIR_SEPARATOR; + for( string::size_type pos = name.find( bad ); + pos != string::npos; + pos = name.find( bad, pos ) ) + { + name.replace( pos, bad.length(), DIR_SEPARATOR ); + } + + // replace occurances of /./ with / + bad = DIR_SEPARATOR; + bad += '.'; + bad += DIR_SEPARATOR; + for( string::size_type pos = name.find( bad ); + pos != string::npos; + pos = name.find( bad, pos ) ) + { + name.replace( pos, bad.length(), DIR_SEPARATOR ); + } +} + +/////////////////////////////////////////////////////////////////////////////// + +void +FileSystem::pathnameOnlyExtension( string& name ) +{ + // compute basename + string::size_type dot_pos = name.rfind( '.' ); + string::size_type slash_pos = name.rfind( DIR_SEPARATOR ); + + // dot_pos must be after slash_pos + if( slash_pos != string::npos && dot_pos < slash_pos ) + dot_pos = string::npos; + + // return empty-string if no dot + if( dot_pos == string::npos ) { + name.resize( 0 ); + return; + } + + name = name.substr( dot_pos + 1 ); + pathnameCleanup( name ); +} + +/////////////////////////////////////////////////////////////////////////////// + +void +FileSystem::pathnameStripExtension( string& name ) +{ + pathnameCleanup( name ); + + // compute basename + string::size_type dot_pos = name.rfind( '.' ); + string::size_type slash_pos = name.rfind( DIR_SEPARATOR ); + + // dot_pos must be after slash_pos + if( slash_pos != string::npos && dot_pos < slash_pos ) + dot_pos = string::npos; + + // chop extension + if( dot_pos != string::npos ) + name.resize( dot_pos ); +} + +/////////////////////////////////////////////////////////////////////////////// + +void +FileSystem::pathnameTemp( string& name, string dir, string prefix, string suffix ) +{ + ostringstream buf; + buf << dir; + + // add dir separator if needed + if( !dir.empty() ) { + buf << dir; + if( dir[dir.length()-1] != '/' ) + buf << '/'; + } + + buf << prefix; + buf << setfill('0') << setw(8) << number::random32(); + buf << suffix; + + name = buf.str(); +} + +/////////////////////////////////////////////////////////////////////////////// + +}}} // namespace mp4v2::platform::io |