]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/OS/DirContents.cxx
9a801e8b398d14445483719faae8522ea7271e05
[cpPlugins.git] / lib / cpPlugins / OS / DirContents.cxx
1 #include <cpPlugins/OS/DirContents.h>
2 #include <cpPlugins/OS/tinydir.h>
3 #include <sstream>
4
5 // -------------------------------------------------------------------------
6 std::set< std::string > cpPlugins::OS::
7 LoadDirContents(
8   const std::string& path, bool recursive, const std::string& pattern
9   )
10 {
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;
16
17   // Process data
18   tinydir_dir dir;
19   if( tinydir_open_sorted( &dir, path.c_str( ) ) != -1 )
20   {
21     for( auto i = 0; i < dir.n_files; i++ )
22     {
23       tinydir_file file;
24       if( tinydir_readfile_n( &dir, &file , i ) != -1 )
25       {
26         std::string fname = path_str + file.name;
27         if( file.is_dir )
28         {
29           if( recursive )
30           {
31             if( fname != "." && fname != ".." )
32             {
33               auto tmp = cpPlugins::OS::LoadDirContents( fname, true );
34               output.insert( tmp.begin( ), tmp.end( ) );
35
36             } // fi
37
38           } // fi
39         }
40         else
41         {
42           if( pattern != "" )
43           {
44             if( cpPlugins::OS::Glob( fname, pattern ) )
45               output.insert( fname );
46           }
47           else
48             output.insert( fname );
49
50         } // fi
51
52       } // fi
53
54     } // rof
55
56   } // fi
57   tinydir_close( &dir );
58   return( output );
59 }
60
61 // ------------------------------------------------------------------------
62 void cpPlugins::OS::
63 SplitPath( std::string& dname, std::string& fname, const std::string& path )
64 {
65   auto found = path.find_last_of( "/\\" );
66   dname = path.substr( 0, found );
67   fname = path.substr( found + 1 );
68 }
69
70 // ------------------------------------------------------------------------
71 int cpPlugins::OS::
72 GlobMatch( const char* target, const char* pattern )
73 {
74   char* pat = const_cast< char* >( pattern );
75   char* end = strchr( pat, ']' );
76   char* ptr;
77   if( *pat == '[' && end != NULL )
78   {
79     if( end == pat + 1 )
80     {
81       end = strchr( pat + 2, ']' );
82       if( end == NULL )
83         return( 0 );
84
85     } // fi
86
87     if( end - pat == 4 && pat[ 2 ] == '-' && pat[ 1 ] <= pattern[ 3 ] )
88     {
89       if( *target >= pat[ 1 ] && *target <= pat[ 3 ] )
90         return( 5 );
91       else
92         return( 0 );
93
94     } // fi
95
96     ptr = strchr( pat + 1, *target );
97     if( ptr != NULL && ptr < end )
98       return( end - pat + 1 );
99     else
100       return( 0 );
101
102   } // fi
103
104   if( *pat == '?' && *target != 0 )
105     return( 1 );
106   if( *pat == '*' )
107     return( 0 );
108   if( *target == 0 || *pat == 0 )
109     return( 0 );
110   if( *target == *pat )
111     return( 1 );
112   return( 0 );
113 }
114
115 // ------------------------------------------------------------------------
116 bool cpPlugins::OS::
117 Glob( const std::string& line, const std::string& pattern )
118 {
119   const char* target = line.c_str( );
120   const char* pat = pattern.c_str( );
121   int gobble;
122
123   while( ( gobble = GlobMatch( target, pat ) ) )
124   {
125     target++;
126     pat += gobble;
127
128   } // elihw
129   if( *target == 0 && *pat == 0 )
130     return( true );
131   else if( *pat == '*' )
132   {
133     while( pat[ 1 ] == '*' )
134       pat++;
135     if( pat[ 1 ] == 0 )
136       return( true );
137     while( *target )
138       if( Glob( target++, pat + 1 ) )
139         return( 1 );
140
141   } // fi
142   return( false );
143 }
144
145 // eof - $RCSfile$