]> Creatis software - creaImageIO.git/blob - src2/creaImageIOImageFinder.cpp
Corrected ggo to work in win32.
[creaImageIO.git] / src2 / creaImageIOImageFinder.cpp
1 #include <creaImageIOImageFinder.h>
2 #include <creaWx.h>
3 #include <wx/dir.h>
4 #include <wx/filename.h>
5
6 using namespace crea;
7
8 namespace creaImageIO
9 {
10   //====================================================================
11   // Ctor
12   ImageFinder::ImageFinder(TreeHandler* tree)
13     : mTreeHandler(tree)
14   {
15   }
16   // Dtor
17   ImageFinder::~ImageFinder()
18   {
19   }
20   //====================================================================
21
22   //=====================================================================
23   bool ImageFinder::IsHandledFile( const std::string& filename)
24   {
25     return (mReader.CanRead(filename,""));
26   }
27   //=====================================================================
28
29   //=====================================================================
30   bool ImageFinder::AddFiles( const std::vector<std::string>& filenames)
31   //                          wxProgressDialog* progress, 
32   //                          UpdateSummary& summary)
33   {
34  
35     unsigned int nbf = filenames.size(); 
36     std::vector<std::string>::const_iterator i;
37     for (i=filenames.begin();i!=filenames.end();++i)
38       {
39         //      summary.scanned_files++;
40         if (IsHandledFile(*i)) 
41           {
42             //      summary.handled_images++;
43             AddFile(*i);//,summary);
44             /*
45             if (progress)
46               {
47                 std::string mess("Adding ");
48                 mess += *i;
49                 if (!progress->Update( (int)(summary.added_images*999./nbf),
50                                        std2wx(mess)))
51                   {
52                     // Some file was added hence we must return true !
53                     summary.cancelled_by_user = true;
54                     break;
55                   }
56               }
57             */
58           }
59       }
60
61     return true;
62   }
63   //=====================================================================
64
65   //=====================================================================
66   bool ImageFinder::AddFile( const std::string& filename )
67   //                         UpdateSummary& summary)
68   {
69     
70     std::map< std::string, std::string>  attr;
71     mTreeHandler->GetTree().GetDescriptor().BuildAttributeMap(attr);
72
73     mReader.ReadAttributes(filename,attr);
74
75     //     image->SetFieldValue("FullFileName",filename);
76      
77     int lev = mTreeHandler->AddBranch(attr);
78
79     // TO DO : update the summary according to lev
80
81     return true;
82   }
83   //=====================================================================
84
85   //=====================================================================
86   /**
87    * \brief   Explore a directory with possibility of recursion
88    *          return number of files read
89    * @param  dirpath   directory to explore
90    * @param  recursive whether we want recursion or not
91    */
92   void ImageFinder::ParseDirectory( const std::string &dirpath, 
93                                     std::vector<std::string> &Filenames,
94                                     bool recursive)
95   //                                wxProgressDialog* progress, 
96   //                                UpdateSummary& summary)
97     
98   {
99     /*
100     if (progress) 
101       {
102         std::string mess("Parsing ");
103         mess += dirpath;
104         progress->Pulse(std2wx(mess));
105       }
106     */    
107     std::string fileName;
108     std::string dirName = dirpath;
109
110     //    summary.scanned_dirs++;
111
112     wxDir dir( std2wx(dirpath) );
113
114     if ( !dir.IsOpened() )
115       {
116         // deal with the error here - wxDir would already log an error message
117         // explaining the exact reason of the failure
118         return;
119       }
120
121     wxString filename;
122
123     bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
124     while ( cont )
125       {
126         /*
127         if ((progress)&&( sw.Time() >= 250 )) 
128           {
129             //                  std::cout << "PULSE"<<std::endl;
130             sw.Start(0);
131             if (!progress->Pulse()) 
132               {
133                 summary.cancelled_by_user = true;
134                 break;
135               }
136           }
137         */
138         //      summary.scanned_files++;
139         wxFileName wxffn(dir.GetName(),filename);
140         std::string ffn = wx2std(wxffn.GetFullPath());
141         //              std::cout << ffn << std::endl;
142         if (mReader.CanRead(ffn,""))
143           {
144             Filenames.push_back( ffn );
145             //      summary.handled_images++;
146           }
147         cont = dir.GetNext(&filename);
148       }
149     
150     // Recurse into subdirs
151     if ( recursive )
152       {
153         cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN );
154         while ( cont )
155           {
156             
157             wxFileName wxffn(dir.GetName(),filename);
158             std::string ffn = wx2std(wxffn.GetFullPath());
159             
160             //                          std::cout << "dir="<< ffn<< std::endl;
161             
162             ParseDirectory( ffn, 
163                             Filenames,
164                             recursive);
165             //                      progress,
166             //                      summary);
167             //      if (summary.cancelled_by_user) break;
168             
169             cont = dir.GetNext(&filename);
170           }
171       }
172     
173   }
174   //=======================================================================
175
176
177   //=====================================================================
178   bool ImageFinder::AddDirectory( const std::string& directory,
179                                   bool recurse)
180   //                              wxProgressDialog* progress, 
181   //                              UpdateSummary& summary
182   //                              )
183   {
184     //    std::cout << "** ImageFinder::AddDirectory"
185     //        << " '"<<directory<<"'"<<std::endl;
186     //    std::cout << "------ Parsing directory ------"<<std::endl;
187     /*
188     if (progress)
189       {
190         progress->Pulse();
191       }
192
193     for (int swi=0;swi<10;swi++) 
194       {
195         msw[swi].Start(0);
196         msw[swi].Pause();
197       }
198     
199     // Parse directory
200     wxStopWatch sw; 
201     
202     bool was_canceled_by_user(false);
203     */
204     std::vector<std::string> filenames;
205     ParseDirectory( directory, 
206                     filenames,
207                     recurse );
208     //, 
209     //              progress,
210     //              summary);
211     /*
212     if ( summary.cancelled_by_user ) 
213       {
214         return false;
215       }
216     
217     summary.parse_time = sw.Time();
218
219
220     summary.added_images = 0;
221     */
222     unsigned int nbf = filenames.size(); // , nf = 0;
223     std::vector<std::string>::iterator i;
224     for (i=filenames.begin();i!=filenames.end();++i)
225       {
226         AddFile(*i); //,summary);
227         /*
228         if (progress)
229           {
230             std::string mess("Adding ");
231             mess += *i;
232             if (!progress->Update( (int)(summary.added_images*999./nbf),
233                                    std2wx(mess)))
234               {
235                 // Some file was added hence we must return true !
236                 summary.cancelled_by_user = true;
237                 break;
238               }
239           }
240         */
241       }
242
243     /*
244     sw.Pause();
245     msw[0].Pause();
246     msw[1].Pause();
247     msw[2].Pause();
248
249     summary.total_time = sw.Time();
250     summary.file_scan_time = msw[1].Time();
251     summary.update_database_time = msw[2].Time();
252     summary.update_structs_time = 
253       summary.total_time - 
254       summary.parse_time - 
255       summary.file_scan_time - 
256       summary.update_database_time;
257     */
258     return true;
259   }
260   //=====================================================================
261
262
263
264 }