-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdirectories_posix.cpp
151 lines (115 loc) · 3.72 KB
/
directories_posix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "directories.h"
#include <dirent.h>
#include <errno.h>
#include <stdexcept>
#include <cstring>
namespace test262_harness_cpp
{
bool directories::is_directory(std::string& path)
{
auto directory_entry = opendir(path.c_str());
if (directory_entry == nullptr)
{
return false;
}
closedir(directory_entry);
return true;
}
std::string directories::get_directory(std::string& path)
{
auto location = path.find_last_of(path_separator());
if (location == std::string::npos)
{
return "";
}
return path.substr(0, location);
}
std::string directories::path_separator()
{
return "/";
}
std::string directories::combine(std::string path, std::string file)
{
auto location = path.find_last_of(path_separator());
if (location == path.length() - 1)
{
path.pop_back();
}
do
{
location = file.find_first_of(path_separator());
if (location == std::string::npos)
{
return path + path_separator() + file;
}
auto component = file.substr(0, location);
file.erase(0, location + 1);
if (component == "..")
{
location = path.find_last_of(path_separator());
if (location == std::string::npos)
{
throw std::runtime_error("Directory combination moved beyond original path.");
}
path.erase(0, location);
}
else if (component != ".")
{
path += path_separator();
path += component;
}
}
while (true);
return path;
}
void directories::get_test_files(std::string& directory, std::vector<std::string>& files)
{
const std::string to_exclude("_FIXTURE.js");
const std::string to_include(".js");
auto directory_entry = opendir(directory.c_str());
if (directory_entry == nullptr)
{
auto error_text = strerror(errno);
throw std::runtime_error("Could not open directory " + directory + ": " + std::string(error_text));
}
do
{
auto entry = readdir(directory_entry);
if (entry == nullptr)
{
break;
}
std::string filename(entry->d_name);
if (filename == "." || filename == "..")
{
continue;
}
auto to_add = combine(directory, filename);
if ((entry->d_type & DT_DIR) == DT_DIR)
{
get_test_files(to_add, files);
continue;
}
if (to_add.length() >= to_exclude.length())
{
auto ending = to_add.substr(to_add.length() - to_exclude.length());
if (ending == to_exclude)
{
continue;
}
}
if (to_add.length() <= to_include.length())
{
continue;
}
auto ending = to_add.substr(to_add.length() - to_include.length());
if (ending != to_include)
{
continue;
}
files.push_back(to_add);
}
while (true);
closedir(directory_entry);
}
}