CPN
Computational Process Networks
Directory.cc
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #include "common_priv.h"
24 #include <cpn/utils/Directory.h>
26 
27 Directory::Directory(const std::string &dirname_)
28  : dir(0),dirname(dirname_), entry(0), stated(false)
29 {
30  dir = opendir(dirname.c_str());
31  if (!dir) {
32  throw ErrnoException();
33  }
34  Next();
35 }
36 
38  closedir(dir);
39 }
40 
42  rewinddir(dir);
43 }
44 
46  entry = readdir(dir);
47  stated = false;
48 }
49 
50 std::string Directory::BaseName() const {
51  return std::string(entry->d_name);
52 }
53 
54 bool Directory::IsDirectory() const {
55  Stat();
56  return S_ISDIR(status.st_mode);
57 }
58 
60  Stat();
61  return S_ISREG(status.st_mode);
62 }
63 
64 std::size_t Directory::Size() const {
65  Stat();
66  return status.st_size;
67 }
68 
69 void Directory::Stat() const {
70  if (!stated) {
71  std::string fpath = dirname + "/" + entry->d_name;
72  if (stat(fpath.c_str(), &status) != 0) {
73  throw ErrnoException();
74  }
75  stated = true;
76  }
77 }
78 
std::string dirname
Definition: Directory.h:68
std::size_t Size() const
Definition: Directory.cc:64
void Next()
Definition: Directory.cc:45
struct stat status
Definition: Directory.h:70
void Stat() const
Definition: Directory.cc:69
std::string BaseName() const
Definition: Directory.cc:50
bool IsDirectory() const
Definition: Directory.cc:54
dirent * entry
Definition: Directory.h:69
bool stated
Definition: Directory.h:71
DIR * dir
Definition: Directory.h:67
void Rewind()
Definition: Directory.cc:41
Directory(const std::string &dirname_)
Definition: Directory.cc:27
~Directory()
Definition: Directory.cc:37
bool IsRegularFile() const
Definition: Directory.cc:59