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