Skip to content

Commit 21054a2

Browse files
committed
main.cpp: error out when file/path provided by -I or -include=, or the input do not exist
1 parent 15e8427 commit 21054a2

File tree

1 file changed

+50
-17
lines changed

1 file changed

+50
-17
lines changed

main.cpp

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@
99
#include <cstring>
1010
#include <fstream>
1111
#include <iostream>
12+
#include <sys/stat.h>
1213
#include <string>
1314
#include <vector>
1415

16+
static bool isDir(const std::string& path)
17+
{
18+
struct stat file_stat;
19+
if (stat(path.c_str(), &file_stat) == -1)
20+
return false;
21+
22+
return (file_stat.st_mode & S_IFMT) == S_IFDIR;
23+
}
24+
1525
int main(int argc, char **argv)
1626
{
1727
bool error = false;
@@ -21,6 +31,7 @@ int main(int argc, char **argv)
2131

2232
// Settings..
2333
simplecpp::DUI dui;
34+
dui.removeComments = true;
2435
bool quiet = false;
2536
bool error_only = false;
2637
for (int i = 1; i < argc; i++) {
@@ -110,29 +121,51 @@ int main(int argc, char **argv)
110121
return 0;
111122
}
112123

113-
dui.removeComments = true;
124+
// TODO: move this logic into simplecpp
125+
bool inp_missing = false;
126+
127+
for (const std::string& inc : dui.includes) {
128+
std::ifstream f(inc);
129+
if (!f.is_open() || isDir(inc)) {
130+
inp_missing = true;
131+
std::cout << "error: could not open include '" << inc << "'" << std::endl;
132+
}
133+
}
134+
135+
for (const std::string& inc : dui.includePaths) {
136+
if (!isDir(inc)) {
137+
inp_missing = true;
138+
std::cout << "error: could not find include path '" << inc << "'" << std::endl;
139+
}
140+
}
141+
142+
std::ifstream f(filename);
143+
if (!f.is_open() || isDir(filename)) {
144+
inp_missing = true;
145+
std::cout << "error: could not open file '" << filename << "'" << std::endl;
146+
}
147+
148+
if (inp_missing)
149+
return 1;
114150

115151
// Perform preprocessing
116152
simplecpp::OutputList outputList;
117153
std::vector<std::string> files;
118-
simplecpp::TokenList *rawtokens;
119-
if (use_istream) {
120-
std::ifstream f(filename);
121-
if (!f.is_open()) {
122-
std::cout << "error: could not open file '" << filename << "'" << std::endl;
123-
return 1;
154+
simplecpp::TokenList outputTokens(files);
155+
{
156+
simplecpp::TokenList *rawtokens;
157+
if (use_istream) {
158+
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
159+
} else {
160+
f.close();
161+
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
124162
}
125-
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
126-
} else {
127-
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
163+
rawtokens->removeComments();
164+
simplecpp::FileDataCache filedata;
165+
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
166+
simplecpp::cleanup(filedata);
167+
delete rawtokens;
128168
}
129-
rawtokens->removeComments();
130-
simplecpp::TokenList outputTokens(files);
131-
simplecpp::FileDataCache filedata;
132-
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
133-
simplecpp::cleanup(filedata);
134-
delete rawtokens;
135-
rawtokens = nullptr;
136169

137170
// Output
138171
if (!quiet) {

0 commit comments

Comments
 (0)