1 #include <cpPlugins/OS/DirContents.h>
2 #include <cpPlugins/OS/tinydir.h>
5 // -------------------------------------------------------------------------
6 std::set< std::string > cpPlugins::OS::
8 const std::string& path, bool recursive, const std::string& pattern
11 // Prepare inputs and outputs
12 std::stringstream path_stream;
13 path_stream << path << cpPlugins_PATH_SEPARATOR;
14 std::string path_str = path_stream.str( );
15 std::set< std::string > output;
19 if( tinydir_open_sorted( &dir, path.c_str( ) ) != -1 )
21 for( auto i = 0; i < dir.n_files; i++ )
24 if( tinydir_readfile_n( &dir, &file , i ) != -1 )
26 std::string fname = path_str + file.name;
31 if( fname != "." && fname != ".." )
33 auto tmp = cpPlugins::OS::LoadDirContents( fname, true );
34 output.insert( tmp.begin( ), tmp.end( ) );
44 if( cpPlugins::OS::Glob( fname, pattern ) )
45 output.insert( fname );
48 output.insert( fname );
57 tinydir_close( &dir );
61 // ------------------------------------------------------------------------
63 SplitPath( std::string& dname, std::string& fname, const std::string& path )
65 auto found = path.find_last_of( "/\\" );
66 dname = path.substr( 0, found );
67 fname = path.substr( found + 1 );
70 // ------------------------------------------------------------------------
72 GlobMatch( const char* target, const char* pattern )
74 char* pat = const_cast< char* >( pattern );
75 char* end = strchr( pat, ']' );
77 if( *pat == '[' && end != NULL )
81 end = strchr( pat + 2, ']' );
87 if( end - pat == 4 && pat[ 2 ] == '-' && pat[ 1 ] <= pattern[ 3 ] )
89 if( *target >= pat[ 1 ] && *target <= pat[ 3 ] )
96 ptr = strchr( pat + 1, *target );
97 if( ptr != NULL && ptr < end )
98 return( end - pat + 1 );
104 if( *pat == '?' && *target != 0 )
108 if( *target == 0 || *pat == 0 )
110 if( *target == *pat )
115 // ------------------------------------------------------------------------
117 Glob( const std::string& line, const std::string& pattern )
119 const char* target = line.c_str( );
120 const char* pat = pattern.c_str( );
123 while( ( gobble = GlobMatch( target, pat ) ) )
129 if( *target == 0 && *pat == 0 )
131 else if( *pat == '*' )
133 while( pat[ 1 ] == '*' )
138 if( Glob( target++, pat + 1 ) )