From 9ec0d27e7a99329b32311fa693dca4a389b89833 Mon Sep 17 00:00:00 2001 From: jean-pierre roux Date: Tue, 25 Nov 2008 13:03:26 +0000 Subject: [PATCH] Box to return file names from a directory (recursive expl or not) --- packages/std/src/bbstdFilesFromDirectory.cxx | 177 +++++++++++++++++++ packages/std/src/bbstdFilesFromDirectory.h | 54 ++++++ 2 files changed, 231 insertions(+) create mode 100644 packages/std/src/bbstdFilesFromDirectory.cxx create mode 100644 packages/std/src/bbstdFilesFromDirectory.h diff --git a/packages/std/src/bbstdFilesFromDirectory.cxx b/packages/std/src/bbstdFilesFromDirectory.cxx new file mode 100644 index 0000000..d7c747e --- /dev/null +++ b/packages/std/src/bbstdFilesFromDirectory.cxx @@ -0,0 +1,177 @@ +#include "bbstdFilesFromDirectory.h" +#include "bbstdPackage.h" + +#ifdef _MSC_VER + #include + #include +#else + #include + #include +#endif + +#include //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 : "<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 index 0000000..10ec9b2 --- /dev/null +++ b/packages/std/src/bbstdFilesFromDirectory.h @@ -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); + 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 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,""); +BBTK_END_DESCRIBE_BLACK_BOX(FilesFromDirectory); +} +// EO namespace bbstd + +#endif // __bbstdFilesFromDirectory_h_INCLUDED__ + -- 2.45.1