| /////////////////////////////////////////////////////////////////////////////// |
| // |
| // 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( std::string& name ) |
| { |
| std::string bad; |
| |
| // fold repeating directory separators |
| bad = DIR_SEPARATOR; |
| bad += DIR_SEPARATOR; |
| for( std::string::size_type pos = name.find( bad ); |
| pos != std::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( std::string::size_type pos = name.find( bad ); |
| pos != std::string::npos; |
| pos = name.find( bad, pos ) ) |
| { |
| name.replace( pos, bad.length(), DIR_SEPARATOR ); |
| } |
| } |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| |
| void |
| FileSystem::pathnameOnlyExtension( std::string& name ) |
| { |
| // compute basename |
| std::string::size_type dot_pos = name.rfind( '.' ); |
| std::string::size_type slash_pos = name.rfind( DIR_SEPARATOR ); |
| |
| // dot_pos must be after slash_pos |
| if( slash_pos != std::string::npos && dot_pos < slash_pos ) |
| dot_pos = std::string::npos; |
| |
| // return empty-string if no dot |
| if( dot_pos == std::string::npos ) { |
| name.resize( 0 ); |
| return; |
| } |
| |
| name = name.substr( dot_pos + 1 ); |
| pathnameCleanup( name ); |
| } |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| |
| void |
| FileSystem::pathnameStripExtension( std::string& name ) |
| { |
| pathnameCleanup( name ); |
| |
| // compute basename |
| std::string::size_type dot_pos = name.rfind( '.' ); |
| std::string::size_type slash_pos = name.rfind( DIR_SEPARATOR ); |
| |
| // dot_pos must be after slash_pos |
| if( slash_pos != std::string::npos && dot_pos < slash_pos ) |
| dot_pos = std::string::npos; |
| |
| // chop extension |
| if( dot_pos != std::string::npos ) |
| name.resize( dot_pos ); |
| } |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| |
| void |
| FileSystem::pathnameTemp( std::string& name, std::string dir, std::string prefix, std::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 |