fileSearch.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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
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;
00083
00084 const size_t end = candidate.rfind( second );
00085 if( end == std::string::npos ||
00086 end + second.size() < candidate.size( ))
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