File tree 4 files changed +63
-0
lines changed
4 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -145,3 +145,28 @@ std::string concat_dir_file(
145
145
file_name : directory+" /" +file_name;
146
146
#endif
147
147
}
148
+
149
+ bool is_directory (const std::string &path)
150
+ {
151
+ if (path.empty ())
152
+ return false ;
153
+
154
+ #ifdef _WIN32
155
+
156
+ auto attributes = ::GetFileAttributesW (widen (path).c_str ());
157
+ if (attributes == INVALID_FILE_ATTRIBUTES)
158
+ return false ;
159
+ else
160
+ return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0 ;
161
+
162
+ #else
163
+
164
+ struct stat buf;
165
+
166
+ if (stat (path.c_str (), &buf)!=0 )
167
+ return false ;
168
+ else
169
+ return (buf.st_mode & S_IFDIR) != 0 ;
170
+
171
+ #endif
172
+ }
Original file line number Diff line number Diff line change @@ -19,4 +19,7 @@ std::string get_current_working_directory();
19
19
std::string concat_dir_file (const std::string &directory,
20
20
const std::string &file_name);
21
21
22
+ // C++17 will allow us to use std::filesystem::is_directory()
23
+ bool is_directory (const std::string &path);
24
+
22
25
#endif // CPROVER_UTIL_FILE_UTIL_H
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ SRC += unit_tests.cpp \
28
28
solvers/refinement/string_refinement/union_find_replace.cpp \
29
29
util/expr.cpp \
30
30
util/expr_cast/expr_cast.cpp \
31
+ util/file_util.cpp \
31
32
util/get_base_name.cpp \
32
33
util/graph.cpp \
33
34
util/irep.cpp \
Original file line number Diff line number Diff line change
1
+ /* ******************************************************************\
2
+
3
+ Module: Unit tests for file_util.h
4
+
5
+ Author: Daniel Kroening
6
+
7
+ \*******************************************************************/
8
+
9
+ #include < testing-utils/catch.hpp>
10
+
11
+ #include < util/file_util.h>
12
+ #include < util/tempdir.h>
13
+ #include < util/unicode.h>
14
+
15
+ #include < fstream>
16
+
17
+ TEST_CASE (" is_directory functionality" , " [core][util][file_util]" )
18
+ {
19
+ temp_dirt temp_dir (" testXXXXXX" );
20
+
21
+ #ifdef _WIN32
22
+ std::ofstream outfile (widen (temp_dir (" file" )));
23
+ #else
24
+ std::ofstream outfile (temp_dir (" file" ));
25
+ #endif
26
+
27
+ outfile.close ();
28
+
29
+ REQUIRE (is_directory (temp_dir.path ));
30
+ REQUIRE (is_directory (temp_dir.path +" /" ));
31
+ REQUIRE (!is_directory (temp_dir (" whatnot" )));
32
+ REQUIRE (!is_directory (temp_dir (" file" )));
33
+ REQUIRE (!is_directory (" " ));
34
+ }
You can’t perform that action at this time.
0 commit comments