aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
utils_dir.cpp
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 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 directories.
25
*
26
* @author Vincent RENAUDINEAU and Pierre-Henri WUILLEMIN(@LIP6)
27
*/
28
29
#
include
<
agrum
/
tools
/
core
/
utils_dir
.
h
>
30
31
namespace
gum
{
32
33
// Return true if \a directory is a valid directory, false otherwise.
34
bool
Directory
::
isDir
(
const
std
::
string
&
directory
) {
35
return
Directory
(
directory
).
isValid
();
36
}
37
38
// Contructor
39
Directory
::
Directory
() :
m_dirPtr
(
nullptr
) {
GUM_CONSTRUCTOR
(
Directory
); }
40
41
// Contructor
42
Directory
::
Directory
(
const
std
::
string
&
directory
) :
m_dirName
(
directory
) {
43
GUM_CONSTRUCTOR
(
Directory
);
44
m_dirPtr
=
opendir
(
m_dirName
.
c_str
());
45
}
46
47
// Contructor
48
Directory
::
Directory
(
const
Directory
&
dir
) :
m_dirName
(
dir
.
m_dirName
) {
49
GUM_CONSTRUCTOR
(
Directory
);
50
m_dirPtr
=
opendir
(
m_dirName
.
c_str
());
51
}
52
53
// Destructor
54
Directory
::~
Directory
() {
55
GUM_DESTRUCTOR
(
Directory
);
56
57
if
(
m_dirPtr
!=
nullptr
)
closedir
(
m_dirPtr
);
58
}
59
60
// Return true if directory has been opened, false otherwise.
61
bool
Directory
::
isValid
()
const
{
return
m_dirPtr
!=
nullptr
; }
62
63
// Return directory content.
64
std
::
vector
<
std
::
string
>
Directory
::
entries
()
const
{
65
std
::
vector
<
std
::
string
>
result
;
66
67
if
(!
isValid
())
return
result
;
68
69
rewinddir
(
m_dirPtr
);
70
71
dirent
*
entry
;
72
73
while
((
entry
=
readdir
(
m_dirPtr
)))
74
result
.
push_back
(
std
::
string
(
entry
->
d_name
));
75
76
return
result
;
77
}
78
79
// Return directory parent.
80
Directory
Directory
::
parent
()
const
{
81
if
(!
isValid
())
return
Directory
();
82
83
return
Directory
(
m_dirName
+
"../"
);
84
}
85
86
// Return directory path.
87
std
::
string
Directory
::
path
()
const
{
return
m_dirName
; }
88
89
// Return directory absolute path.
90
std
::
string
Directory
::
absolutePath
()
const
{
91
std
::
string
result
;
92
93
if
(!
isValid
())
return
result
;
94
95
char
oldWD
[255];
96
97
if
(
getcwd
(
oldWD
, 255) ==
nullptr
)
return
result
;
98
99
if
(
chdir
(
m_dirName
.
c_str
()) != 0)
return
result
;
100
101
char
absPath
[255];
102
103
if
(
getcwd
(
absPath
, 254) !=
nullptr
)
result
=
std
::
string
(
absPath
) +
'/'
;
104
105
if
(
chdir
(
oldWD
) != 0)
106
std
::
cerr
<<
"Warning : Could not go to previous working directory. ("
107
<< __FILE__ <<
":"
<< __LINE__ <<
")"
<<
std
::
endl
;
108
109
return
result
;
110
}
111
112
Directory
&
Directory
::
operator
=(
const
Directory
&
d
) {
113
if
(
m_dirPtr
!=
nullptr
)
closedir
(
m_dirPtr
);
114
115
m_dirName
=
d
.
m_dirName
;
116
117
m_dirPtr
=
opendir
(
m_dirName
.
c_str
());
118
119
return
*
this
;
120
}
121
122
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669