fileSearch.h

00001 /* Copyright (c) 2009, Cedric Stalder <cedric.stalder@gmail.com> 
00002  *               2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef EQBASE_FILESEARCH_H
00019 #define EQBASE_FILESEARCH_H
00020 
00021 #ifndef WIN32_VC
00022 #  include <dirent.h>
00023 #endif
00024 
00025 namespace eq
00026 {
00027 namespace base
00028 {
00029 
00030 /* @return all file names matching the given pattern in the given directory*/
00031 inline StringVector fileSearch( const std::string directory,  
00032                                 const std::string pattern )
00033 {
00034     StringVector files;
00035    
00036 #ifdef WIN32_VC
00037 
00038     WIN32_FIND_DATA file;
00039     const std::string search = 
00040         directory.empty() ? pattern : directory + '\\' + pattern;
00041     HANDLE hSearch = FindFirstFile( search.c_str(), &file );
00042     
00043     if( hSearch == INVALID_HANDLE_VALUE )
00044     {
00045         EQVERB << "Error finding the first file to match " << pattern << " in "
00046                << directory << std::endl;
00047         FindClose( hSearch );
00048         return files;
00049     }
00050 
00051     files.push_back( file.cFileName );    
00052     while( FindNextFile( hSearch, &file ))
00053         files.push_back( file.cFileName );    
00054     
00055     FindClose( hSearch );
00056 #else
00057 
00058     // only foo*bar pattern are implemented
00059     const size_t findPos = pattern.find( '*' );
00060     if( findPos == std::string::npos )
00061     {
00062         EQASSERTINFO( 0, "Unimplemented" );
00063         return files;
00064     }
00065 
00066     const std::string first = pattern.substr( 0, findPos );
00067     const std::string second = pattern.substr( findPos + 1 );
00068   
00069     DIR* dir = opendir( directory.c_str() );
00070     if( dir == 0 )
00071     {
00072         EQVERB << "Can't open directory " << directory << std::endl;
00073         return files;
00074     }
00075 
00076     struct dirent* entry;
00077     
00078     while(( entry = readdir( dir )) != 0 )
00079     {
00080         const std::string candidate( entry->d_name );
00081         if( candidate.find( first ) != 0 )
00082             continue; // doesn't start with filename
00083 
00084         const size_t end = candidate.rfind( second );
00085         if( end == std::string::npos ||               // not found
00086             end + second.size() < candidate.size( ))  // not at the end
00087         {
00088             continue;
00089         }
00090 
00091         files.push_back( entry->d_name );  
00092     }
00093         
00094     closedir(dir);
00095 #endif
00096     return files;
00097 }
00098 
00100 inline std::string getFilename( const std::string& filename )
00101 {
00102     size_t lastSeparator = 0;
00103     const size_t length = filename.length();
00104 
00105     for( size_t i = 0; i < length; ++i )
00106         if( filename[ i ] == '/' || filename[i] == '\\' )
00107             lastSeparator = i+1;
00108 
00109     return filename.substr( lastSeparator, length );
00110 }
00111 
00113 inline std::string getDirname( const std::string& filename )
00114 {
00115     size_t lastSeparator = 0;
00116     const size_t length = filename.length();
00117 
00118     for( size_t i = 0; i < length; ++i )
00119         if( filename[ i ] == '/' || filename[i] == '\\' )
00120             lastSeparator = i+1;
00121 
00122     return filename.substr( 0, lastSeparator );
00123 }
00124 
00125 }
00126 }
00127 #endif //EQBASE_FILESEARCH_H
Generated on Mon Aug 10 18:58:32 2009 for Equalizer 0.9 by  doxygen 1.5.8