1 #include "libplatform/impl.h"
2 
3 namespace mp4v2 { namespace platform { namespace io {
4 
5 ///////////////////////////////////////////////////////////////////////////////
6 
7 void
pathnameCleanup(string & name)8 FileSystem::pathnameCleanup( string& name )
9 {
10     string bad;
11 
12     // fold repeating directory separators
13     bad = DIR_SEPARATOR;
14     bad += DIR_SEPARATOR;
15     for( string::size_type pos = name.find( bad );
16          pos != string::npos;
17          pos = name.find( bad, pos ) )
18     {
19         name.replace( pos, bad.length(), DIR_SEPARATOR );
20     }
21 
22     // replace occurances of /./ with /
23     bad = DIR_SEPARATOR;
24     bad += '.';
25     bad += DIR_SEPARATOR;
26     for( string::size_type pos = name.find( bad );
27          pos != string::npos;
28          pos = name.find( bad, pos ) )
29     {
30         name.replace( pos, bad.length(), DIR_SEPARATOR );
31     }
32 }
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 void
pathnameOnlyExtension(string & name)37 FileSystem::pathnameOnlyExtension( string& name )
38 {
39     // compute basename
40     string::size_type dot_pos = name.rfind( '.' );
41     string::size_type slash_pos = name.rfind( DIR_SEPARATOR );
42 
43     // dot_pos must be after slash_pos
44     if( slash_pos != string::npos && dot_pos < slash_pos )
45         dot_pos = string::npos;
46 
47     // return empty-string if no dot
48     if( dot_pos == string::npos ) {
49         name.resize( 0 );
50         return;
51     }
52 
53     name = name.substr( dot_pos + 1 );
54     pathnameCleanup( name );
55 }
56 
57 ///////////////////////////////////////////////////////////////////////////////
58 
59 void
pathnameStripExtension(string & name)60 FileSystem::pathnameStripExtension( string& name )
61 {
62     pathnameCleanup( name );
63 
64     // compute basename
65     string::size_type dot_pos = name.rfind( '.' );
66     string::size_type slash_pos = name.rfind( DIR_SEPARATOR );
67 
68     // dot_pos must be after slash_pos
69     if( slash_pos != string::npos && dot_pos < slash_pos )
70         dot_pos = string::npos;
71 
72     // chop extension
73     if( dot_pos != string::npos )
74         name.resize( dot_pos );
75 }
76 
77 ///////////////////////////////////////////////////////////////////////////////
78 
79 void
pathnameTemp(string & name,string dir,string prefix,string suffix)80 FileSystem::pathnameTemp( string& name, string dir, string prefix, string suffix )
81 {
82     ostringstream buf;
83 
84     if( !dir.empty() ) {
85         buf << dir;
86 
87         // add dir separator if needed
88         // TODO there's a platform specific bug here, if someone passes in a pathname ending
89         // in '\', which would be legitimate on Windows.
90         if( dir[dir.length()-1] != '/' )
91             buf << '/';
92     }
93 
94     buf << prefix;
95     buf << setfill('0') << setw(8) << number::random32();
96     buf << suffix;
97 
98     name = buf.str();
99 }
100 
101 ///////////////////////////////////////////////////////////////////////////////
102 
103 }}} // namespace mp4v2::platform::io
104