]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/OS/DirContents.cxx
...
[cpPlugins.git] / lib / cpPlugins / OS / DirContents.cxx
diff --git a/lib/cpPlugins/OS/DirContents.cxx b/lib/cpPlugins/OS/DirContents.cxx
new file mode 100644 (file)
index 0000000..4bb759c
--- /dev/null
@@ -0,0 +1,145 @@
+#include <cpPlugins/OS/DirContents.h>
+#include <cpPlugins/OS/tinydir.h>
+#include <sstream>
+
+// -------------------------------------------------------------------------
+std::set< std::string > cpPlugins::OS::
+LoadDirContents(
+  const std::string& path, bool recursive, const std::string& pattern
+  )
+{
+  // Prepare inputs and outputs
+  std::stringstream path_stream;
+  path_stream << path << cpPlugins_PATH_SEPARATOR;
+  std::string path_str = path_stream.str( );
+  std::set< std::string > output;
+
+  // Process data
+  tinydir_dir dir;
+  if( tinydir_open_sorted( &dir, path.c_str( ) ) != -1 )
+  {
+    for( auto i = 0; i < dir.n_files; i++ )
+    {
+      tinydir_file file;
+      if( tinydir_readfile_n( &dir, &file , i ) != -1 )
+      {
+        std::string fname = path_str + file.name;
+        if( file.is_dir )
+        {
+          if( recursive )
+          {
+            if( fname != "." && fname != ".." )
+            {
+              auto tmp = cpPlugins::OS::LoadDirContents( fname, true );
+              output.insert( tmp.begin( ), tmp.end( ) );
+
+            } // fi
+
+          } // fi
+        }
+        else
+        {
+          if( pattern != "" )
+          {
+            if( cpPlugins::OS::Glob( fname, path_str + pattern ) )
+              output.insert( fname );
+          }
+          else
+            output.insert( fname );
+
+        } // fi
+
+      } // fi
+
+    } // rof
+
+  } // fi
+  tinydir_close( &dir );
+  return( output );
+}
+
+// ------------------------------------------------------------------------
+void cpPlugins::OS::
+SplitPath( std::string& dname, std::string& fname, const std::string& path )
+{
+  auto found = path.find_last_of( "/\\" );
+  dname = path.substr( 0, found );
+  fname = path.substr( found + 1 );
+}
+
+// ------------------------------------------------------------------------
+int cpPlugins::OS::
+GlobMatch( const char* target, const char* pattern )
+{
+  char* pat = const_cast< char* >( pattern );
+  char* end = strchr( pat, ']' );
+  char* ptr;
+  if( *pat == '[' && end != NULL )
+  {
+    if( end == pat + 1 )
+    {
+      end = strchr( pat + 2, ']' );
+      if( end == NULL )
+        return( 0 );
+
+    } // fi
+
+    if( end - pat == 4 && pat[ 2 ] == '-' && pat[ 1 ] <= pattern[ 3 ] )
+    {
+      if( *target >= pat[ 1 ] && *target <= pat[ 3 ] )
+        return( 5 );
+      else
+        return( 0 );
+
+    } // fi
+
+    ptr = strchr( pat + 1, *target );
+    if( ptr != NULL && ptr < end )
+      return( end - pat + 1 );
+    else
+      return( 0 );
+
+  } // fi
+
+  if( *pat == '?' && *target != 0 )
+    return( 1 );
+  if( *pat == '*' )
+    return( 0 );
+  if( *target == 0 || *pat == 0 )
+    return( 0 );
+  if( *target == *pat )
+    return( 1 );
+  return( 0 );
+}
+
+// ------------------------------------------------------------------------
+bool cpPlugins::OS::
+Glob( const std::string& line, const std::string& pattern )
+{
+  const char* target = line.c_str( );
+  const char* pat = pattern.c_str( );
+  int gobble;
+
+  while( ( gobble = GlobMatch( target, pat ) ) )
+  {
+    target++;
+    pat += gobble;
+
+  } // elihw
+  if( *target == 0 && *pat == 0 )
+    return( true );
+  else if( *pat == '*' )
+  {
+    while( pat[ 1 ] == '*' )
+      pat++;
+    if( pat[ 1 ] == 0 )
+      return( true );
+    while( *target )
+      if( Glob( target++, pat + 1 ) )
+        return( 1 );
+
+  } // fi
+  return( false );
+}
+
+// eof - $RCSfile$