-#include <creaImageIOImageFinder.h>
-#include <creaWx.h>
-#include <wx/dir.h>
-#include <wx/filename.h>
-
-using namespace crea;
-
-namespace creaImageIO
-{
- //====================================================================
- // Ctor
- ImageFinder::ImageFinder(TreeHandler* tree)
- : mTreeHandler(tree)
- {
- }
- // Dtor
- ImageFinder::~ImageFinder()
- {
- }
- //====================================================================
-
- //=====================================================================
- bool ImageFinder::IsHandledFile( const std::string& filename)
- {
- return (mReader.CanRead(filename,""));
- }
- //=====================================================================
-
- //=====================================================================
- bool ImageFinder::AddFiles( const std::vector<std::string>& filenames)
- // wxProgressDialog* progress,
- // UpdateSummary& summary)
- {
-
- unsigned int nbf = filenames.size();
- std::vector<std::string>::const_iterator i;
- for (i=filenames.begin();i!=filenames.end();++i)
- {
- // summary.scanned_files++;
- if (IsHandledFile(*i))
- {
- // summary.handled_images++;
- AddFile(*i);//,summary);
- /*
- if (progress)
- {
- std::string mess("Adding ");
- mess += *i;
- if (!progress->Update( (int)(summary.added_images*999./nbf),
- std2wx(mess)))
- {
- // Some file was added hence we must return true !
- summary.cancelled_by_user = true;
- break;
- }
- }
- */
- }
- }
-
- return true;
- }
- //=====================================================================
-
- //=====================================================================
- bool ImageFinder::AddFile( const std::string& filename )
- // UpdateSummary& summary)
- {
-
- std::map< std::string, std::string> attr;
- mTreeHandler->GetTree().GetDescriptor().BuildAttributeMap(attr);
-
- mReader.ReadAttributes(filename,attr);
-
- // image->SetFieldValue("FullFileName",filename);
-
- int lev = mTreeHandler->AddBranch(attr);
-
- // TO DO : update the summary according to lev
-
- return true;
- }
- //=====================================================================
-
- //=====================================================================
- /**
- * \brief Explore a directory with possibility of recursion
- * return number of files read
- * @param dirpath directory to explore
- * @param recursive whether we want recursion or not
- */
- void ImageFinder::ParseDirectory( const std::string &dirpath,
- std::vector<std::string> &Filenames,
- bool recursive)
- // wxProgressDialog* progress,
- // UpdateSummary& summary)
-
- {
- /*
- if (progress)
- {
- std::string mess("Parsing ");
- mess += dirpath;
- progress->Pulse(std2wx(mess));
- }
- */
- std::string fileName;
- std::string dirName = dirpath;
-
- // summary.scanned_dirs++;
-
- wxDir dir( std2wx(dirpath) );
-
- if ( !dir.IsOpened() )
- {
- // deal with the error here - wxDir would already log an error message
- // explaining the exact reason of the failure
- return;
- }
-
- wxString filename;
-
- bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
- while ( cont )
- {
- /*
- if ((progress)&&( sw.Time() >= 250 ))
- {
- // std::cout << "PULSE"<<std::endl;
- sw.Start(0);
- if (!progress->Pulse())
- {
- summary.cancelled_by_user = true;
- break;
- }
- }
- */
- // summary.scanned_files++;
- wxFileName wxffn(dir.GetName(),filename);
- std::string ffn = wx2std(wxffn.GetFullPath());
- // std::cout << ffn << std::endl;
- if (mReader.CanRead(ffn,""))
- {
- Filenames.push_back( ffn );
- // summary.handled_images++;
- }
- cont = dir.GetNext(&filename);
- }
-
- // Recurse into subdirs
- if ( recursive )
- {
- cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN );
- while ( cont )
- {
-
- wxFileName wxffn(dir.GetName(),filename);
- std::string ffn = wx2std(wxffn.GetFullPath());
-
- // std::cout << "dir="<< ffn<< std::endl;
-
- ParseDirectory( ffn,
- Filenames,
- recursive);
- // progress,
- // summary);
- // if (summary.cancelled_by_user) break;
-
- cont = dir.GetNext(&filename);
- }
- }
-
- }
- //=======================================================================
-
-
- //=====================================================================
- bool ImageFinder::AddDirectory( const std::string& directory,
- bool recurse)
- // wxProgressDialog* progress,
- // UpdateSummary& summary
- // )
- {
- // std::cout << "** ImageFinder::AddDirectory"
- // << " '"<<directory<<"'"<<std::endl;
- // std::cout << "------ Parsing directory ------"<<std::endl;
- /*
- if (progress)
- {
- progress->Pulse();
- }
-
- for (int swi=0;swi<10;swi++)
- {
- msw[swi].Start(0);
- msw[swi].Pause();
- }
-
- // Parse directory
- wxStopWatch sw;
-
- bool was_canceled_by_user(false);
- */
- std::vector<std::string> filenames;
- ParseDirectory( directory,
- filenames,
- recurse );
- //,
- // progress,
- // summary);
- /*
- if ( summary.cancelled_by_user )
- {
- return false;
- }
-
- summary.parse_time = sw.Time();
-
-
- summary.added_images = 0;
- */
- unsigned int nbf = filenames.size(); // , nf = 0;
- std::vector<std::string>::iterator i;
- for (i=filenames.begin();i!=filenames.end();++i)
- {
- AddFile(*i); //,summary);
- /*
- if (progress)
- {
- std::string mess("Adding ");
- mess += *i;
- if (!progress->Update( (int)(summary.added_images*999./nbf),
- std2wx(mess)))
- {
- // Some file was added hence we must return true !
- summary.cancelled_by_user = true;
- break;
- }
- }
- */
- }
-
- /*
- sw.Pause();
- msw[0].Pause();
- msw[1].Pause();
- msw[2].Pause();
-
- summary.total_time = sw.Time();
- summary.file_scan_time = msw[1].Time();
- summary.update_database_time = msw[2].Time();
- summary.update_structs_time =
- summary.total_time -
- summary.parse_time -
- summary.file_scan_time -
- summary.update_database_time;
- */
- return true;
- }
- //=====================================================================
-
-
-
-}