]> Creatis software - bbtk.git/commitdiff
Box to return file names from a directory (recursive expl or not)
authorjean-pierre roux <jean-pierre.roux@creatis.insa-lyon.fr>
Tue, 25 Nov 2008 13:03:26 +0000 (13:03 +0000)
committerjean-pierre roux <jean-pierre.roux@creatis.insa-lyon.fr>
Tue, 25 Nov 2008 13:03:26 +0000 (13:03 +0000)
packages/std/src/bbstdFilesFromDirectory.cxx [new file with mode: 0644]
packages/std/src/bbstdFilesFromDirectory.h [new file with mode: 0644]

diff --git a/packages/std/src/bbstdFilesFromDirectory.cxx b/packages/std/src/bbstdFilesFromDirectory.cxx
new file mode 100644 (file)
index 0000000..d7c747e
--- /dev/null
@@ -0,0 +1,177 @@
+#include "bbstdFilesFromDirectory.h"
+#include "bbstdPackage.h"
+
+#ifdef _MSC_VER
+   #include <windows.h>
+   #include <direct.h>
+#else
+   #include <dirent.h>   
+   #include <sys/types.h>
+#endif
+
+#include <sys/stat.h>  //stat function
+
+namespace bbstd
+{
+
+BBTK_ADD_BLACK_BOX_TO_PACKAGE(std,FilesFromDirectory)
+BBTK_BLACK_BOX_IMPLEMENTATION(FilesFromDirectory,bbtk::AtomicBlackBox);
+
+void FilesFromDirectory::Process()
+{
+
+   DirName = bbGetInputIn();
+   bool rec = bbGetInputRecursive();
+   /*int nbFiles = */ Explore(DirName, rec);
+   bbSetOutputOut(Filenames);
+   
+  
+}
+void FilesFromDirectory::bbUserConstructor()
+{
+
+    bbSetInputIn(".");
+    bbSetInputRecursive(false);
+  
+}
+void FilesFromDirectory::bbUserCopyConstructor(bbtk::BlackBox::Pointer)
+{
+
+  
+}
+void FilesFromDirectory::bbUserDestructor()
+{
+
+  
+}
+
+/**
+ * \brief   Add a SEPARATOR to the end of the name if necessary
+ * @param   pathname file/directory name to normalize 
+ */
+std::string FilesFromDirectory::NormalizePath(std::string const &pathname)
+{
+#ifdef _WIN32
+   const char FILESEPARATOR = '\\;
+#else
+   const char FILESEPARATOR = '/';
+#endif
+
+   std::string name = pathname;
+   int size = name.size();
+
+   if ( name[size-1] != FILESEPARATOR )
+   {
+      name += FILESEPARATOR;
+   }
+   return name;
+} 
+
+/**
+ * \brief   Explores a directory with possibility of recursion
+ *          return number of files read
+ * @param  dirpath   directory to explore
+ * @param  recursive whether we want recursion or not
+ */
+int FilesFromDirectory::Explore(std::string const &dirpath, bool recursive)
+{
+   int numberOfFiles = 0;
+   std::string fileName;
+   std::string dirName = NormalizePath(dirpath);
+#ifdef _MSC_VER
+   WIN32_FIND_DATA fileData;
+   //assert( dirName[dirName.size()-1] == '' );
+   HANDLE hFile = FindFirstFile((dirName+"*").c_str(), &fileData);
+
+   for(BOOL b = (hFile != INVALID_HANDLE_VALUE); b;
+       b = FindNextFile(hFile, &fileData))
+   {
+      fileName = fileData.cFileName;
+      if ( fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
+      {
+         // Need to check for . and .. to avoid infinite loop
+         if ( fileName != "." && fileName != ".." && recursive )
+         {
+            numberOfFiles += Explore(dirName+fileName,recursive);
+         }
+      }
+      else
+      {
+         Filenames.push_back(dirName+fileName);
+         numberOfFiles++;
+      }
+   }
+   DWORD dwError = GetLastError();
+   if (hFile != INVALID_HANDLE_VALUE) 
+      FindClose(hFile);
+   if (dwError != ERROR_NO_MORE_FILES) 
+   {
+      LPVOID lpMsgBuf;
+      FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
+                    FORMAT_MESSAGE_FROM_SYSTEM|
+                    FORMAT_MESSAGE_IGNORE_INSERTS,
+                    NULL,GetLastError(),
+                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+                    (LPTSTR) &lpMsgBuf,0,NULL);
+
+      //gdcmErrorMacro("FindNextFile error. Error is " << (char *)lpMsgBuf
+      //             <<" for the directory : "<<dirName);
+      return -1;
+   }
+
+#else
+  // Real POSIX implementation: scandir is a BSD extension only, and doesn't 
+  // work on debian for example
+
+   DIR* dir = opendir(dirName.c_str());
+   if (!dir)
+   {
+      return 0;
+   }
+
+   // According to POSIX, the dirent structure contains a field char d_name[]
+   // of unspecified size, with at most NAME_MAX characters preceeding the
+   // terminating null character. Use of other fields will harm the  porta-
+   // bility of your programs.
+
+   struct stat buf;
+   dirent *d;
+   for (d = readdir(dir); d; d = readdir(dir))
+   {
+      fileName = dirName + d->d_name;
+      if( stat(fileName.c_str(), &buf) != 0 )
+      {
+         //gdcmErrorMacro( strerror(errno) );
+      }
+      if ( S_ISREG(buf.st_mode) )    //is it a regular file?
+      {
+         Filenames.push_back( fileName );
+         numberOfFiles++;
+      }
+      else if ( S_ISDIR(buf.st_mode) ) //directory?
+      {
+         if ( d->d_name[0] != '.' && recursive ) //we also skip hidden files
+         {
+            numberOfFiles += Explore( fileName, recursive);
+         }
+      }
+      else
+      {
+         //gdcmErrorMacro( "Unexpected error" );
+         return -1;
+      }
+   }
+   if( closedir(dir) != 0 )
+   {
+      //gdcmErrorMacro( strerror(errno) );
+   }
+#endif
+
+  return numberOfFiles;
+}
+
+
+}
+// EO namespace bbstd
+
+
diff --git a/packages/std/src/bbstdFilesFromDirectory.h b/packages/std/src/bbstdFilesFromDirectory.h
new file mode 100644 (file)
index 0000000..10ec9b2
--- /dev/null
@@ -0,0 +1,54 @@
+#ifndef __bbstdFilesFromDirectory_h_INCLUDED__
+#define __bbstdFilesFromDirectory_h_INCLUDED__
+#include "bbtkAtomicBlackBox.h"
+#include "iostream"
+
+namespace bbstd
+{
+
+class /*BBTK_EXPORT*/ FilesFromDirectory
+ : 
+   public bbtk::AtomicBlackBox
+{
+  BBTK_BLACK_BOX_INTERFACE(FilesFromDirectory,bbtk::AtomicBlackBox);
+//==================================================================
+/// User callback called in the box contructor
+virtual void bbUserConstructor();
+/// User callback called in the box copy constructor
+virtual void bbUserCopyConstructor(bbtk::BlackBox::Pointer);
+/// User callback called in the box destructor
+virtual void bbUserDestructor();
+//==================================================================
+  BBTK_DECLARE_INPUT(In,std::string);
+  BBTK_DECLARE_INPUT(Recursive,bool);  
+  BBTK_DECLARE_OUTPUT(Out,std::vector<std::string>);
+  BBTK_PROCESS(Process);
+  void Process();
+  
+  private :
+  std::string NormalizePath(std::string const &pathname);
+  int Explore(std::string const &dirpath, bool recursive);
+  
+  /// List of file names
+  std::vector<std::string> Filenames;
+  /// name of the root directory to explore
+  std::string DirName;
+};
+
+//=================================================================
+// UserBlackBox description
+BBTK_BEGIN_DESCRIBE_BLACK_BOX(FilesFromDirectory,bbtk::AtomicBlackBox);
+BBTK_NAME("FilesFromDirectory");
+BBTK_AUTHOR("jpr@creatis.univ-lyon1.fr");
+BBTK_DESCRIPTION("returns the fullPathNames of the files in a Directory");
+BBTK_CATEGORY("");
+BBTK_INPUT(FilesFromDirectory,In,"Directory Name",std::string,"");
+BBTK_INPUT(FilesFromDirectory,Recursive,"Recursive directory exploration",bool,"");
+
+BBTK_OUTPUT(FilesFromDirectory,Out,"FullPathNames of the files",std::vector<std::string>,"");
+BBTK_END_DESCRIBE_BLACK_BOX(FilesFromDirectory);
+}
+// EO namespace bbstd
+
+#endif // __bbstdFilesFromDirectory_h_INCLUDED__
+