blob: 122af24a0809a0d700ff904f8792afe431c042eb [file] [log] [blame]
///////////////////////////////////////////////////////////////////////////////
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is MP4v2.
//
// The Initial Developer of the Original Code is Kona Blend.
// Portions created by Kona Blend are Copyright (C) 2008.
// All Rights Reserved.
//
// Contributors:
// Kona Blend, kona8lend@@gmail.com
//
///////////////////////////////////////////////////////////////////////////////
#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;
if( !dir.empty() ) {
buf << dir;
// add dir separator if needed
// TODO there's a platform specific bug here, if someone passes in a pathname ending
// in '\', which would be legitimate on Windows.
if( dir[dir.length()-1] != '/' )
buf << '/';
}
buf << prefix;
buf << std::setfill('0') << std::setw(8) << number::random32();
buf << suffix;
name = buf.str();
}
///////////////////////////////////////////////////////////////////////////////
}}} // namespace mp4v2::platform::io