BESFSDir.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <sys/types.h>
00034 #include <sys/stat.h>
00035 #include <dirent.h>
00036 #ifdef WIN32
00037 #include <config.h>
00038 #endif
00039 #include <stdio.h>
00040
00041 #include "BESFSDir.h"
00042 #include "BESRegex.h"
00043 #include "BESInternalError.h"
00044
00045 BESFSDir::BESFSDir(const string &dirName)
00046 : _dirName(dirName),
00047 _fileExpr(""),
00048 _dirLoaded(false)
00049 {}
00050
00051 BESFSDir::BESFSDir(const string &dirName, const string &fileExpr)
00052 : _dirName(dirName),
00053 _fileExpr(fileExpr),
00054 _dirLoaded(false)
00055 {}
00056
00057 BESFSDir::BESFSDir(const BESFSDir ©From)
00058 : _dirName(copyFrom._dirName),
00059 _fileExpr(copyFrom._fileExpr),
00060 _dirLoaded(false)
00061 {}
00062
00063 BESFSDir::~BESFSDir()
00064 {}
00065
00066 BESFSDir::dirIterator
00067 BESFSDir::beginOfDirList()
00068 {
00069 if (_dirLoaded == false) {
00070 loadDir() ;
00071 _dirLoaded = true ;
00072 }
00073 return _dirList.begin() ;
00074 }
00075
00076 BESFSDir::dirIterator
00077 BESFSDir::endOfDirList()
00078 {
00079 if (_dirLoaded == false) {
00080 loadDir() ;
00081 _dirLoaded = true ;
00082 }
00083 return _dirList.end() ;
00084 }
00085
00086 BESFSDir::fileIterator
00087 BESFSDir::beginOfFileList()
00088 {
00089 if (_dirLoaded == false) {
00090 loadDir() ;
00091 _dirLoaded = true ;
00092 }
00093 return _fileList.begin() ;
00094 }
00095
00096 BESFSDir::fileIterator
00097 BESFSDir::endOfFileList()
00098 {
00099 if (_dirLoaded == false) {
00100 loadDir() ;
00101 _dirLoaded = true ;
00102 }
00103 return _fileList.end() ;
00104 }
00105
00106 void
00107 BESFSDir::loadDir()
00108 {
00109 DIR * dip;
00110 struct dirent *dit;
00111
00112
00113
00114 if( ( dip = opendir( _dirName.c_str() ) ) == NULL )
00115 {
00116 string err_str = "ERROR: failed to open directory '" + _dirName + "'" ;
00117 throw BESInternalError( err_str, __FILE__, __LINE__ ) ;
00118 }
00119 else
00120 {
00121
00122
00123 while ((dit = readdir(dip)) != NULL)
00124 {
00125 struct stat buf;
00126 string dirEntry = dit->d_name ;
00127 if (dirEntry != "." && dirEntry != "..") {
00128 string fullPath = _dirName + "/" + dirEntry ;
00129 stat(fullPath.c_str(), &buf) ;
00130
00131
00132
00133 if (S_ISDIR(buf.st_mode)) {
00134 _dirList.push_back(BESFSDir(fullPath)) ;
00135 }
00136 else {
00137 if (_fileExpr != "") {
00138 BESRegex reg_expr(_fileExpr.c_str()) ;
00139 if (reg_expr.match(dirEntry.c_str(),
00140 dirEntry.length()) != -1) {
00141 _fileList.push_back(BESFSFile(_dirName, dirEntry));
00142 }
00143 }
00144 else {
00145 _fileList.push_back(BESFSFile(_dirName, dirEntry)) ;
00146 }
00147 }
00148 }
00149 }
00150 }
00151
00152
00153 closedir(dip) ;
00154 }
00155