aGrUM  0.14.2
utils_dir.cpp
Go to the documentation of this file.
1 /*e**************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program 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 *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #include <agrum/core/utils_dir.h>
28 
29 namespace gum {
30 
31  // Return true if \a directory is a valid directory, false otherwise.
32  bool Directory::isDir(const std::string& directory) {
33  return Directory(directory).isValid();
34  }
35 
36  // Contructor
37  Directory::Directory() : m_dirPtr(nullptr) { GUM_CONSTRUCTOR(Directory); }
38 
39  // Contructor
40  Directory::Directory(const std::string& directory) : m_dirName(directory) {
41  GUM_CONSTRUCTOR(Directory);
42  m_dirPtr = opendir(m_dirName.c_str());
43  }
44 
45  // Contructor
47  GUM_CONSTRUCTOR(Directory);
48  m_dirPtr = opendir(m_dirName.c_str());
49  }
50 
51  // Destructor
53  GUM_DESTRUCTOR(Directory);
54 
55  if (m_dirPtr != nullptr) closedir(m_dirPtr);
56  }
57 
58  // Return true if directory has been opened, false otherwise.
59  bool Directory::isValid() const { return m_dirPtr != nullptr; }
60 
61  // Return directory content.
62  std::vector< std::string > Directory::entries() const {
63  std::vector< std::string > result;
64 
65  if (!isValid()) return result;
66 
67  rewinddir(m_dirPtr);
68 
69  dirent* entry;
70 
71  while ((entry = readdir(m_dirPtr)))
72  result.push_back(std::string(entry->d_name));
73 
74  return result;
75  }
76 
77  // Return directory parent.
79  if (!isValid()) return Directory();
80 
81  return Directory(m_dirName + "../");
82  }
83 
84  // Return directory path.
85  std::string Directory::path() const { return m_dirName; }
86 
87  // Return directory absolute path.
88  std::string Directory::absolutePath() const {
89  std::string result;
90 
91  if (!isValid()) return result;
92 
93  char oldWD[255];
94 
95  if (getcwd(oldWD, 255) == nullptr) return result;
96 
97  if (chdir(m_dirName.c_str()) != 0) return result;
98 
99  char absPath[255];
100 
101  if (getcwd(absPath, 254) != nullptr) result = std::string(absPath) + '/';
102 
103  if (chdir(oldWD) != 0)
104  std::cerr << "Warning : Could not go to previous working directory. ("
105  << __FILE__ << ":" << __LINE__ << ")" << std::endl;
106 
107  return result;
108  }
109 
111  if (m_dirPtr != nullptr) closedir(m_dirPtr);
112 
113  m_dirName = d.m_dirName;
114 
115  m_dirPtr = opendir(m_dirName.c_str());
116 
117  return *this;
118  }
119 
120 } // namespace gum
Cross-platform directory utility.
Definition: utils_dir.h:59
std::string m_dirName
The directory path.
Definition: utils_dir.h:131
Directory()
Contructor.
Definition: utils_dir.cpp:37
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
DIR * m_dirPtr
A pointer towards the Directory stream.
Definition: utils_dir.h:134
bool isValid() const
Returns true if directory has been opened, false otherwise.
Definition: utils_dir.cpp:59
Directory & operator=(const Directory &d)
Copy operator.
Definition: utils_dir.cpp:110
~Directory()
Destructor.
Definition: utils_dir.cpp:52
std::string absolutePath() const
Returns directory absolute path.
Definition: utils_dir.cpp:88
std::vector< std::string > entries() const
Return directory content.
Definition: utils_dir.cpp:62
Directory parent() const
Returns directory parent.
Definition: utils_dir.cpp:78
Contains usefull methods to work with files and directories.
static bool isDir(const std::string &path)
&brief Return true if directory is a valid directory, false otherwise.
Definition: utils_dir.cpp:32
std::string path() const
Returns directory path.
Definition: utils_dir.cpp:85