Skip to content

Commit b5c0365

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

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,10 +9,20 @@
99
#include <cstring>
1010
#include <fstream>
1111
#include <iostream>
12+
#include <sys/stat.h>
1213
#include <string>
1314
#include <utility>
1415
#include <vector>
1516

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

2434
// Settings..
2535
simplecpp::DUI dui;
36+
dui.removeComments = true;
2637
bool quiet = false;
2738
bool error_only = false;
2839
for (int i = 1; i < argc; i++) {
@@ -144,29 +155,51 @@ int main(int argc, char **argv)
144155
return 0;
145156
}
146157

147-
dui.removeComments = true;
158+
// TODO: move this logic into simplecpp
159+
bool inp_missing = false;
160+
161+
for (const std::string& inc : dui.includes) {
162+
std::ifstream f(inc);
163+
if (!f.is_open() || isDir(inc)) {
164+
inp_missing = true;
165+
std::cout << "error: could not open include '" << inc << "'" << std::endl;
166+
}
167+
}
168+
169+
for (const std::string& inc : dui.includePaths) {
170+
if (!isDir(inc)) {
171+
inp_missing = true;
172+
std::cout << "error: could not find include path '" << inc << "'" << std::endl;
173+
}
174+
}
175+
176+
std::ifstream f(filename);
177+
if (!f.is_open() || isDir(filename)) {
178+
inp_missing = true;
179+
std::cout << "error: could not open file '" << filename << "'" << std::endl;
180+
}
181+
182+
if (inp_missing)
183+
return 1;
148184

149185
// Perform preprocessing
150186
simplecpp::OutputList outputList;
151187
std::vector<std::string> files;
152-
simplecpp::TokenList *rawtokens;
153-
if (use_istream) {
154-
std::ifstream f(filename);
155-
if (!f.is_open()) {
156-
std::cout << "error: could not open file '" << filename << "'" << std::endl;
157-
return 1;
188+
simplecpp::TokenList outputTokens(files);
189+
{
190+
simplecpp::TokenList *rawtokens;
191+
if (use_istream) {
192+
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
193+
} else {
194+
f.close();
195+
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
158196
}
159-
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
160-
} else {
161-
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
197+
rawtokens->removeComments();
198+
simplecpp::FileDataCache filedata;
199+
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
200+
simplecpp::cleanup(filedata);
201+
delete rawtokens;
162202
}
163-
rawtokens->removeComments();
164-
simplecpp::TokenList outputTokens(files);
165-
simplecpp::FileDataCache filedata;
166-
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
167-
simplecpp::cleanup(filedata);
168-
delete rawtokens;
169-
rawtokens = nullptr;
170203

171204
// Output
172205
if (!quiet) {

0 commit comments

Comments
 (0)