-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (146 loc) · 3.53 KB
/
main.cpp
File metadata and controls
189 lines (146 loc) · 3.53 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include "parser.h"
#include "file-utils.h"
#include <fstream>
#include <sstream>
#include <unordered_set>
struct FileList
{
bool has_file(const std::string& file)
{
return files.count(file);
}
void add_file(const std::string& file)
{
files.insert(file);
orderedFiles.push_back(file);
}
std::unordered_set<std::string> files;
std::vector<std::string> orderedFiles;
};
struct Config
{
std::vector<std::string> externalPaths;
bool addExternalFiles = false;
bool addMissingFiles = false;
};
template<typename ActionFunc>
void handle_fileexpr(FileList& filelist, const std::string& wd, const std::string& fileexpr, const Config& config, ActionFunc func)
{
// Check neighbor file
auto locfile = combine_path(wd, fileexpr);
resolve_dotdots(locfile);
if (filelist.has_file(locfile))
return;
if (file_exists(locfile))
{
filelist.add_file(locfile);
func(locfile);
return;
}
// End here if neither option is set (small optimization)
if (!config.addMissingFiles && !config.addExternalFiles)
return;
// Check external files
for (auto& path : config.externalPaths)
{
const auto extfile = combine_path(path, fileexpr);
if (filelist.has_file(extfile))
return;
if (file_exists(extfile))
{
if (config.addExternalFiles)
{
filelist.add_file(extfile);
func(extfile);
}
return;
}
}
// Add missing neighbor file to list if addMissingFiles enabled
if (config.addMissingFiles)
filelist.add_file(locfile);
}
void list_depends(FileList& filelist, const std::string& filename, const Config& config)
{
// List includes
const auto includes = parse_includes(file_read_content(filename));
const auto wd = get_directory(filename);
// Add incbin files and do nothing with them
for (auto& fileexpr : includes.incbinFiles)
handle_fileexpr(filelist, wd, normalized(fileexpr), config, [&] (const auto&) {});
// Add include files and recursively add the files that they are including
for (auto& fileexpr : includes.includeFiles)
{
handle_fileexpr(filelist, wd, normalized(fileexpr), config, [&] (const auto& fullpath)
{
list_depends(filelist, fullpath, config);
});
}
}
FileList list_depends(const std::string& filename, const Config& config)
{
FileList list;
list_depends(list, filename, config);
return list;
}
int main(int argc, char** argv)
{
if (argc == 1)
{
std::cerr << "ea-dep 1.2 - include dependency analyzer for EA scripts" << std::endl;
std::cerr << "Usage: " << argv[0] << " INPUT [-I PATH]... [--add-missings] [--add-externals]" << std::endl;
return 0;
}
auto it = argv + 1;
const auto end = argv + argc;
Config config;
std::string input;
while (it != end)
{
std::string str(*it);
if (str == "-I")
{
if ((it + 1) == end)
{
std::cerr << "ERROR: -I without parameter!" << std::endl;
return 1;
}
config.externalPaths.emplace_back(it[1]);
it += 2;
}
else if (str == "--add-missings")
{
config.addMissingFiles = true;
++it;
}
else if (str == "--add-externals")
{
config.addExternalFiles = true;
++it;
}
else
{
if (!input.empty())
{
std::cerr << "ERROR: multiple input files!" << std::endl;
return 2;
}
input = std::move(normalize(str));
it++;
}
}
if (input.empty())
{
std::cerr << "ERROR: no input file!" << std::endl;
return 3;
}
if (!file_exists(input))
{
std::cerr << "ERROR: file `" << input << "` doesn't seem to exist!" << std::endl;
return 4;
}
for (auto& inc : list_depends(normalized(input), config).orderedFiles)
std::cout << inc << std::endl;
return 0;
}