aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
utils_dir.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Contains usefull methods to work with files and directories.
25  *
26  * @author Vincent RENAUDINEAU and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_DIR_UTILS_H
30 #define GUM_DIR_UTILS_H
31 
32 #include <cstdio>
33 #include <cstdlib>
34 #include <iostream>
35 #include <string>
36 #include <vector>
37 
38 #include <agrum/agrum.h>
39 
40 #ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 #else
43 # include <agrum/tools/core/mvsc/unistd.h>
44 #endif
45 
46 #ifdef HAVE_DIRENT_H
47 # include <dirent.h>
48 #else
49 # include <agrum/tools/core/mvsc/dirent.h>
50 #endif
51 
52 
53 namespace gum {
54 
55  /**
56  * @class Directory
57  * @headerfile utils_dir.h <agrum/tools/core/utils_dir.h>
58  * @brief Cross-platform directory utility.
59  * @ingroup utilities_group
60  */
61  class Directory {
62  public:
63  /**
64  * &brief Return true if \a directory is a valid directory, false
65  * otherwise.
66  * @param path The path to test.
67  * &return Return true if \a directory is a valid directory, false
68  * otherwise.
69  */
70  static bool isDir(const std::string& path);
71 
72  /**
73  * @brief Contructor.
74  */
75  Directory();
76 
77  /**
78  * @brief Contructor.
79  * @param directory The path to the directory.
80  */
81  Directory(const std::string& directory);
82 
83  /**
84  * @brief Copy contructor.
85  * @param dir The gum::Directory to copy.
86  */
87  Directory(const Directory& dir);
88 
89  /**
90  * @brief Destructor.
91  */
92  ~Directory();
93 
94  /**
95  * @brief Returns true if directory has been opened, false otherwise.
96  * @return Returns true if directory has been opened, false otherwise.
97  */
98  bool isValid() const;
99 
100  /**
101  * @brief Return directory content.
102  * @return Return directory content.
103  */
104  std::vector< std::string > entries() const;
105 
106  /**
107  * @brief Returns directory parent.
108  * @return Returns directory parent.
109  */
110  Directory parent() const;
111 
112  /**
113  * @brief Returns directory path.
114  * @return Returns directory path.
115  */
116  std::string path() const;
117 
118  /**
119  * @brief Returns directory absolute path.
120  * @return Returns directory absolute path.
121  */
122  std::string absolutePath() const;
123 
124  /**
125  * @brief Copy operator.
126  * @param d The gum::Directory to copy.
127  * @return Returns this gum::Directory.
128  */
129  Directory& operator=(const Directory& d);
130 
131  private:
132  /// The directory path.
133  std::string m_dirName;
134 
135  /// A pointer towards the Directory stream
136  mutable DIR* m_dirPtr;
137 
138  }; // END CLASS DIRECTORY
139 
140 } // namespace gum
141 
142 #endif // GUM_DIR_UTILS_H