1 #include <cpPlugins/OS/FileSystem.h>
2 #include <cpPlugins/OS/tinydir.h>
6 // -------------------------------------------------------------------------
7 std::string cpPlugins::OS::FileSystem::
8 CanonicalPath( const std::string& path )
11 #ifdef cpExtensions_OS_Windows
12 TCHAR buffer[ 4096 ] = TEXT( "" );
13 TCHAR** lppPart = { NULL };
14 GetFullPathName( path.c_str( ), 4096, buffer, lppPart );
15 ret = std::string( buffer );
16 #else // cpExtensions_OS_Windows
17 char* canonical_path = realpath( path.c_str( ), NULL );
18 if( canonical_path != NULL )
21 free( canonical_path );
24 #endif // cpExtensions_OS_Windows
28 // -------------------------------------------------------------------------
29 std::pair< std::string, std::string > cpPlugins::OS::FileSystem::
30 SplitPath( const std::string& path )
32 size_t found = path.find_last_of( "/\\" );
34 std::pair< std::string, std::string >(
35 path.substr( 0, found + 1 ), path.substr( found + 1 )
40 // -------------------------------------------------------------------------
41 std::set< std::string > cpPlugins::OS::FileSystem::
43 const std::string& path, bool recursive,
44 const std::string& pattern
47 // Prepare inputs and outputs
48 std::stringstream path_stream;
49 path_stream << path << cpPlugins_PATH_SEPARATOR;
50 std::string path_str = path_stream.str( );
51 std::set< std::string > output;
55 if( tinydir_open_sorted( &dir, path.c_str( ) ) != -1 )
57 for( auto i = 0; i < dir.n_files; i++ )
60 if( tinydir_readfile_n( &dir, &file , i ) != -1 )
62 std::string fname = path_str + file.name;
67 if( fname != "." && fname != ".." )
69 auto tmp = Self::LoadDirContents( fname, true );
70 output.insert( tmp.begin( ), tmp.end( ) );
80 if( Self::Glob( fname, path_str + pattern ) )
81 output.insert( fname );
84 output.insert( fname );
93 tinydir_close( &dir );
97 // -------------------------------------------------------------------------
98 int cpPlugins::OS::FileSystem::
99 GlobMatch( const char* target, const char* pattern )
101 char* pat = const_cast< char* >( pattern );
102 char* end = strchr( pat, ']' );
104 if( *pat == '[' && end != NULL )
108 end = strchr( pat + 2, ']' );
114 if( end - pat == 4 && pat[ 2 ] == '-' && pat[ 1 ] <= pattern[ 3 ] )
116 if( *target >= pat[ 1 ] && *target <= pat[ 3 ] )
123 ptr = strchr( pat + 1, *target );
124 if( ptr != NULL && ptr < end )
125 return( end - pat + 1 );
131 if( *pat == '?' && *target != 0 )
135 if( *target == 0 || *pat == 0 )
137 if( *target == *pat )
142 // -------------------------------------------------------------------------
143 bool cpPlugins::OS::FileSystem::
144 Glob( const std::string& line, const std::string& pattern )
146 const char* target = line.c_str( );
147 const char* pat = pattern.c_str( );
150 while( ( gobble = GlobMatch( target, pat ) ) )
156 if( *target == 0 && *pat == 0 )
158 else if( *pat == '*' )
160 while( pat[ 1 ] == '*' )
165 if( Glob( target++, pat + 1 ) )
172 // -------------------------------------------------------------------------
173 bool cpPlugins::OS::FileSystem::
174 Read( std::string& buffer, const std::string& fname )
177 std::ifstream file_stream( fname.c_str( ) );
180 file_stream.seekg( 0, std::ios::end );
181 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
182 file_stream.seekg( 0, std::ios::beg );
184 ( std::istreambuf_iterator< char >( file_stream ) ),
185 std::istreambuf_iterator< char >( )
187 file_stream.close( );
191 // -------------------------------------------------------------------------
192 bool cpPlugins::OS::FileSystem::
193 Write( const std::string& buffer, const std::string& fname )
195 std::ofstream file_stream( fname.c_str( ), std::ofstream::binary );
198 file_stream.write( buffer.c_str( ), buffer.size( ) );