aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
gum::Directory Class Reference

Cross-platform directory utility. More...

#include <agrum/tools/core/utils_dir.h>

Public Member Functions

 Directory ()
 Contructor. More...
 
 Directory (const std::string &directory)
 Contructor. More...
 
 Directory (const Directory &dir)
 Copy contructor. More...
 
 ~Directory ()
 Destructor. More...
 
bool isValid () const
 Returns true if directory has been opened, false otherwise. More...
 
std::vector< std::string > entries () const
 Return directory content. More...
 
Directory parent () const
 Returns directory parent. More...
 
std::string path () const
 Returns directory path. More...
 
std::string absolutePath () const
 Returns directory absolute path. More...
 
Directoryoperator= (const Directory &d)
 Copy operator. More...
 

Static Public Member Functions

static bool isDir (const std::string &path)
 &brief Return true if directory is a valid directory, false otherwise. More...
 

Detailed Description

Cross-platform directory utility.

Definition at line 62 of file utils_dir.h.

Constructor & Destructor Documentation

◆ Directory() [1/3]

gum::Directory::Directory ( )

Contructor.

Definition at line 40 of file utils_dir.cpp.

Referenced by isDir(), and parent().

40 : m_dirPtr(nullptr) { GUM_CONSTRUCTOR(Directory); }
Directory()
Contructor.
Definition: utils_dir.cpp:40
DIR * m_dirPtr
A pointer towards the Directory stream.
Definition: utils_dir.h:137
+ Here is the caller graph for this function:

◆ Directory() [2/3]

gum::Directory::Directory ( const std::string &  directory)

Contructor.

Parameters
directoryThe path to the directory.

Definition at line 43 of file utils_dir.cpp.

References m_dirName, and m_dirPtr.

43  : m_dirName(directory) {
44  GUM_CONSTRUCTOR(Directory);
45  m_dirPtr = opendir(m_dirName.c_str());
46  }
std::string m_dirName
The directory path.
Definition: utils_dir.h:134
Directory()
Contructor.
Definition: utils_dir.cpp:40
DIR * m_dirPtr
A pointer towards the Directory stream.
Definition: utils_dir.h:137

◆ Directory() [3/3]

gum::Directory::Directory ( const Directory dir)

Copy contructor.

Parameters
dirThe gum::Directory to copy.

Definition at line 49 of file utils_dir.cpp.

References m_dirName, and m_dirPtr.

49  : m_dirName(dir.m_dirName) {
50  GUM_CONSTRUCTOR(Directory);
51  m_dirPtr = opendir(m_dirName.c_str());
52  }
std::string m_dirName
The directory path.
Definition: utils_dir.h:134
Directory()
Contructor.
Definition: utils_dir.cpp:40
DIR * m_dirPtr
A pointer towards the Directory stream.
Definition: utils_dir.h:137

◆ ~Directory()

gum::Directory::~Directory ( )

Destructor.

Definition at line 55 of file utils_dir.cpp.

References m_dirPtr.

55  {
56  GUM_DESTRUCTOR(Directory);
57 
58  if (m_dirPtr != nullptr) closedir(m_dirPtr);
59  }
Directory()
Contructor.
Definition: utils_dir.cpp:40
DIR * m_dirPtr
A pointer towards the Directory stream.
Definition: utils_dir.h:137

Member Function Documentation

◆ absolutePath()

std::string gum::Directory::absolutePath ( ) const

Returns directory absolute path.

Returns
Returns directory absolute path.

Definition at line 91 of file utils_dir.cpp.

References isValid(), and m_dirName.

Referenced by gum::prm::o3prmr::O3prmrInterpreter::import(), and gum::prm::o3prm::O3prmReader< double >::readFile().

91  {
92  std::string result;
93 
94  if (!isValid()) return result;
95 
96  char oldWD[255];
97 
98  if (getcwd(oldWD, 255) == nullptr) return result;
99 
100  if (chdir(m_dirName.c_str()) != 0) return result;
101 
102  char absPath[255];
103 
104  if (getcwd(absPath, 254) != nullptr) result = std::string(absPath) + '/';
105 
106  if (chdir(oldWD) != 0)
107  std::cerr << "Warning : Could not go to previous working directory. ("
108  << __FILE__ << ":" << __LINE__ << ")" << std::endl;
109 
110  return result;
111  }
std::string m_dirName
The directory path.
Definition: utils_dir.h:134
bool isValid() const
Returns true if directory has been opened, false otherwise.
Definition: utils_dir.cpp:62
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ entries()

std::vector< std::string > gum::Directory::entries ( ) const

Return directory content.

Returns
Return directory content.

Definition at line 65 of file utils_dir.cpp.

References isValid(), and m_dirPtr.

65  {
66  std::vector< std::string > result;
67 
68  if (!isValid()) return result;
69 
70  rewinddir(m_dirPtr);
71 
72  dirent* entry;
73 
74  while ((entry = readdir(m_dirPtr)))
75  result.push_back(std::string(entry->d_name));
76 
77  return result;
78  }
DIR * m_dirPtr
A pointer towards the Directory stream.
Definition: utils_dir.h:137
bool isValid() const
Returns true if directory has been opened, false otherwise.
Definition: utils_dir.cpp:62
+ Here is the call graph for this function:

◆ isDir()

bool gum::Directory::isDir ( const std::string &  path)
static

&brief Return true if directory is a valid directory, false otherwise.

Parameters
pathThe path to test. &return Return true if directory is a valid directory, false otherwise.

Definition at line 35 of file utils_dir.cpp.

References Directory().

Referenced by gum::prm::o3prmr::O3prmrInterpreter::addPath().

35  {
36  return Directory(directory).isValid();
37  }
Directory()
Contructor.
Definition: utils_dir.cpp:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isValid()

bool gum::Directory::isValid ( ) const

Returns true if directory has been opened, false otherwise.

Returns
Returns true if directory has been opened, false otherwise.

Definition at line 62 of file utils_dir.cpp.

References m_dirPtr.

Referenced by absolutePath(), entries(), and parent().

62 { return m_dirPtr != nullptr; }
DIR * m_dirPtr
A pointer towards the Directory stream.
Definition: utils_dir.h:137
+ Here is the caller graph for this function:

◆ operator=()

Directory & gum::Directory::operator= ( const Directory d)

Copy operator.

Parameters
dThe gum::Directory to copy.
Returns
Returns this gum::Directory.

Definition at line 113 of file utils_dir.cpp.

References m_dirName, and m_dirPtr.

113  {
114  if (m_dirPtr != nullptr) closedir(m_dirPtr);
115 
116  m_dirName = d.m_dirName;
117 
118  m_dirPtr = opendir(m_dirName.c_str());
119 
120  return *this;
121  }
std::string m_dirName
The directory path.
Definition: utils_dir.h:134
DIR * m_dirPtr
A pointer towards the Directory stream.
Definition: utils_dir.h:137

◆ parent()

Directory gum::Directory::parent ( ) const

Returns directory parent.

Returns
Returns directory parent.

Definition at line 81 of file utils_dir.cpp.

References Directory(), isValid(), and m_dirName.

81  {
82  if (!isValid()) return Directory();
83 
84  return Directory(m_dirName + "../");
85  }
std::string m_dirName
The directory path.
Definition: utils_dir.h:134
Directory()
Contructor.
Definition: utils_dir.cpp:40
bool isValid() const
Returns true if directory has been opened, false otherwise.
Definition: utils_dir.cpp:62
+ Here is the call graph for this function:

◆ path()

std::string gum::Directory::path ( ) const

Returns directory path.

Returns
Returns directory path.

Definition at line 88 of file utils_dir.cpp.

References m_dirName.

88 { return m_dirName; }
std::string m_dirName
The directory path.
Definition: utils_dir.h:134

Member Data Documentation

◆ m_dirName

std::string gum::Directory::m_dirName
private

The directory path.

Definition at line 134 of file utils_dir.h.

Referenced by absolutePath(), Directory(), operator=(), parent(), and path().

◆ m_dirPtr

DIR* gum::Directory::m_dirPtr
mutableprivate

A pointer towards the Directory stream.

Definition at line 137 of file utils_dir.h.

Referenced by Directory(), entries(), isValid(), operator=(), and ~Directory().


The documentation for this class was generated from the following files: