Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Directory.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 #include <string>
24 #include <vector>
25 #include <iostream>
26 #include <sys/types.h>
27 #include <dirent.h>
28 
29 #include "Directory.H"
30 
31 Directory::Directory(const std::string &path)
32 {
33  _good = false;
34  _dir = NULL;
35  _path.assign(path);
36  if(open(path)){
37  std::cerr << "Directory::Error: Could not open " << path
38  << " as a directory." << std::endl;
39  _good = false;
40  }
41  else
42  close();
43 }
44 
46 {
47  // Take the default
48 }
49 
50 Directory::operator void* ()
51 {
52  return(_good ? this : NULL);
53 }
54 
55 bool
57 {
58  return(!_good);
59 }
60 
61 void
63 {
64  if(_good)
65  closedir(_dir);
66 }
67 
68 int
69 Directory::open(const std::string &path)
70 {
71  // if(_good){
72  // this->close();
73  _path = path;
74  // }
75  if(path.empty())
76  return(1);
77  if(!(_dir = opendir(path.c_str())))
78  return(1);
79  _path = path;
80  _good = true;
81  struct dirent *entry;
82  // Skip . and ..
83  entry = readdir(_dir);
84  entry = readdir(_dir);
85  while((entry = readdir(_dir)) != NULL)
86  this->push_back(entry->d_name);
87  return(0);
88 }
89 
90 
91 
92 
93 
94 
95 
DIR * _dir
Definition: Directory.H:31
bool operator!()
Definition: Directory.C:56
~Directory()
Definition: Directory.C:45
int open(const std::string &s="")
Definition: Directory.C:69
void close()
Definition: Directory.C:62
Directory(const std::string &s="")
Definition: Directory.C:31
std::string _path
Definition: Directory.H:29
bool _good
Definition: Directory.H:30