OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESFSDir.cc
Go to the documentation of this file.
1 // BESFSDir.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <dirent.h>
36 #ifdef WIN32
37 #include <config.h> // for S_ISDIR macro
38 #endif
39 #include <stdio.h>
40 
41 #include "BESFSDir.h"
42 #include "BESRegex.h"
43 #include "BESInternalError.h"
44 
45 BESFSDir::BESFSDir(const string &dirName)
46  : _dirName(dirName),
47  _fileExpr(""),
48  _dirLoaded(false)
49 {}
50 
51 BESFSDir::BESFSDir(const string &dirName, const string &fileExpr)
52  : _dirName(dirName),
53  _fileExpr(fileExpr),
54  _dirLoaded(false)
55 {}
56 
57 BESFSDir::BESFSDir(const BESFSDir &copyFrom)
58  : _dirName(copyFrom._dirName),
59  _fileExpr(copyFrom._fileExpr),
60  _dirLoaded(false)
61 {}
62 
64 {}
65 
68 {
69  if (_dirLoaded == false) {
70  loadDir() ;
71  _dirLoaded = true ;
72  }
73  return _dirList.begin() ;
74 }
75 
78 {
79  if (_dirLoaded == false) {
80  loadDir() ;
81  _dirLoaded = true ;
82  }
83  return _dirList.end() ;
84 }
85 
88 {
89  if (_dirLoaded == false) {
90  loadDir() ;
91  _dirLoaded = true ;
92  }
93  return _fileList.begin() ;
94 }
95 
98 {
99  if (_dirLoaded == false) {
100  loadDir() ;
101  _dirLoaded = true ;
102  }
103  return _fileList.end() ;
104 }
105 
106 void
107 BESFSDir::loadDir()
108 {
109  DIR * dip;
110  struct dirent *dit;
111 
112  // open a directory stream
113  // make sure the directory is valid and readable
114  if( ( dip = opendir( _dirName.c_str() ) ) == NULL )
115  {
116  string err_str = "ERROR: failed to open directory '" + _dirName + "'" ;
117  throw BESInternalError( err_str, __FILE__, __LINE__ ) ;
118  }
119  else
120  {
121  // read in the files in this directory
122  // add each filename to the list of filenames
123  while ((dit = readdir(dip)) != NULL)
124  {
125  struct stat buf;
126  string dirEntry = dit->d_name ;
127  if (dirEntry != "." && dirEntry != "..") {
128  string fullPath = _dirName + "/" + dirEntry ;
129  stat(fullPath.c_str(), &buf) ;
130 
131  // look at the mode and determine if this is a filename
132  // or a directory name
133  if (S_ISDIR(buf.st_mode)) {
134  _dirList.push_back(BESFSDir(fullPath)) ;
135  }
136  else {
137  if (_fileExpr != "") {
138  BESRegex reg_expr(_fileExpr.c_str()) ;
139  int match_ret = reg_expr.match( dirEntry.c_str(),
140  dirEntry.length() ) ;
141  if( match_ret == static_cast<int>(dirEntry.length()) )
142  {
143  _fileList.push_back(BESFSFile(_dirName, dirEntry));
144  }
145  }
146  else
147  {
148  _fileList.push_back(BESFSFile(_dirName, dirEntry)) ;
149  }
150  }
151  }
152  }
153  }
154 
155  // close the directory
156  closedir(dip) ;
157 }
158 
exception thrown if inernal error encountered
int match(const char *s, int len, int pos=0)
Does the pattern match.
Definition: BESRegex.cc:103
a C++ interface to POSIX regular expression functions.
Definition: BESRegex.h:41
virtual BESFSDir::dirIterator endOfDirList()
Definition: BESFSDir.cc:77
list< BESFSDir >::iterator dirIterator
Definition: BESFSDir.h:61
BESFSDir(const string &dirName)
Definition: BESFSDir.cc:45
list< BESFSFile >::iterator fileIterator
Definition: BESFSDir.h:65
virtual BESFSDir::fileIterator endOfFileList()
Definition: BESFSDir.cc:97
virtual BESFSDir::dirIterator beginOfDirList()
Definition: BESFSDir.cc:67
virtual ~BESFSDir()
Definition: BESFSDir.cc:63
virtual BESFSDir::fileIterator beginOfFileList()
Definition: BESFSDir.cc:87