/home/fwph/code/wurde/rde/utils/DirectoryLoader.H

Go to the documentation of this file.
00001 #ifndef DIRECTORY_LOADER_H
00002 #define DIRECTORY_LOADER_H
00003 
00014 #include <Time.H>
00015 #include <Logger.H>
00016 #include <dirent.h>
00017 
00018 namespace WURDE {
00023         class FileEntry {
00024                 
00025         public:
00026                 std::string filename;
00027                 int index;      
00028                 Time timestamp;
00029                 
00033                 bool operator<(const FileEntry &right) const{
00034                         if(timestamp.getSeconds()<right.timestamp.getSeconds()){
00035                                 return true;
00036                         }else if(timestamp.getSeconds()==right.timestamp.getSeconds()){
00037                                 if(timestamp.getUSeconds()<right.timestamp.getUSeconds()){
00038                                         return true;
00039                                 }
00040                         }
00041                         return false;
00042                 }
00043         };
00044         
00055         class DirectoryScanner{
00056         public:
00060                 DirectoryScanner(std::string path,std::string pattern1,std::string pattern2){
00061                         m_path=path;
00062                         m_patterns.push_back(pattern1);
00063                         m_patterns.push_back(pattern2);
00064                 }
00065                 
00066                 explicit DirectoryScanner(std::string path){
00067                         m_path=path;
00068                 }
00069                 
00070                 DirectoryScanner(){};
00071 
00072                 void setPath(std::string path){m_path=path;}
00073                 
00077                 std::vector<FileEntry> getSortedFileList(){
00078                         return m_files;
00079                 }
00080                 
00085                 void addPattern(const std::string &val){
00086                         //                      g_logdebug << "Adding pattern " << val << std::endl;
00087                         m_patterns.push_back(val);
00088                 }
00089                 
00093                 void openDirectory(){
00094                         FileEntry temp;
00095                         struct dirent *current=NULL;
00096                         bool member=false;
00097                         char *marker=NULL;
00098                         unsigned long frameno=0;
00099                         unsigned long seconds=0;
00100                         unsigned long useconds=0;                      
00101                         DIR *directory=NULL;
00102                         long index=0;
00103                         directory=opendir(m_path.c_str());
00104                         if(!directory){
00105                                 g_logerror << "Couldn't open directory " << m_path << std::endl;
00106                                 return;
00107                         }
00108                         
00109                         while(true){
00110                                 member=true;
00111 
00112                                 current=readdir(directory);
00113                                 if(!current){
00114                                         break;
00115                                 }
00116                                 //                              g_logdebug << "Reading directory entry " << index << " name " << current->d_name << std::endl;
00117                                 index++;
00118                                 for( int i=0;i<m_patterns.size();i++){
00119                                         if(!strstr(current->d_name,m_patterns[i].c_str())){
00120                                                 member=false;
00121                                         }
00122                                 }
00123                                 if(member){                                    
00124                                 
00125                                         marker=strstr(current->d_name,"-");
00126                                         if(marker){
00127                                                 marker++;
00128                                                 frameno=strtol(marker,&marker,10);              
00129                                                 if(*marker='-'){
00130                                                         marker++;
00131                                                         marker=strstr(marker,"-");
00132                                                         if(marker){
00133                                                                 marker++;
00134                                                                 seconds=strtol(marker,&marker,10);
00135                                                                 if(*marker='.'){
00136                                                                         marker++;
00137                                                                         useconds=strtol(marker,&marker,10);
00138                                                                         //g_logdebug << "Adding file " << current->d_name << std::endl;
00139                                                                         temp.filename=m_path+"/"+current->d_name;
00140                                                                         temp.index=frameno;
00141                                                                         temp.timestamp.setSeconds(seconds);
00142                                                                         temp.timestamp.setUSeconds(useconds);
00143                                                                         m_files.push_back(temp);
00144                                                                 }else{
00145                                                                         g_logdebug << "Unexpected " << *marker << std::endl;
00146                                                                         g_logwarn << "Filename " << current->d_name << " doesn't conform."<<std::endl;
00147                                                                 }
00148                                                         }else{
00149                                                                 g_logdebug << "Unexpected " << *marker << std::endl;
00150                                                                 g_logwarn << "Filename " << current->d_name << " doesn't conform."<<std::endl;
00151                                                         }
00152                                                 }else{
00153                                                         g_logdebug << "Unexpected " << *marker << std::endl;
00154                                                         g_logwarn << "Filename " << current->d_name << " doesn't conform."<<std::endl;
00155                                                 }
00156                                         }else{
00157                                                 g_logwarn << "Filename " << current->d_name << " doesn't conform."<<std::endl;
00158                                         }
00159                                 }
00160 
00161                         }
00162                         
00163                         closedir(directory);
00164                         
00165                         sort(m_files.begin(),m_files.end());
00166                         
00167                 }
00168                 
00169         private:
00170                 std::string m_path;
00174                 int filterfunc(const struct dirent *entry){
00175                         for(int i=0;i<m_patterns.size();i++){
00176                                 if(strstr(entry->d_name,m_patterns[i].c_str())){
00177                                         return 0;
00178                                 }
00179                         }
00180                         return 1;
00181                 }
00182                 std::vector<std::string> m_patterns;
00183                 std::vector<FileEntry> m_files;
00184                 
00185         };
00186 
00187 }
00188 
00189 #endif

Generated on Thu Feb 1 15:31:54 2007 for WURDE by  doxygen 1.5.1