Skip to content

Commit bd4456e

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

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

main.cpp

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

@@ -21,6 +22,7 @@ int main(int argc, char **argv)
2122

2223
// Settings..
2324
simplecpp::DUI dui;
25+
dui.removeComments = true;
2426
bool quiet = false;
2527
bool error_only = false;
2628
for (int i = 1; i < argc; i++) {
@@ -95,6 +97,32 @@ int main(int argc, char **argv)
9597
return 1;
9698
}
9799

100+
// TODO: move this logic into simplecpp?
101+
{
102+
bool inc_missing = false;
103+
for (const std::string& inc : dui.includes) {
104+
std::fstream f(inc, std::ios::in|std::ios::out); // check if this is an existing file
105+
if (!f.is_open()) {
106+
inc_missing = true;
107+
std::cout << "error: could not open include '" << inc << "'" << std::endl;
108+
}
109+
}
110+
if (inc_missing)
111+
return 1;
112+
}
113+
{
114+
bool inc_missing = false;
115+
for (const std::string& inc : dui.includePaths) {
116+
struct stat file_stat;
117+
if ((stat(inc.c_str(), &file_stat) == -1) || ((file_stat.st_mode & S_IFMT) != S_IFDIR)) {
118+
inc_missing = true;
119+
std::cout << "error: could not find include path '" << inc << "'" << std::endl;
120+
}
121+
}
122+
if (inc_missing)
123+
return 1;
124+
}
125+
98126
if (!filename) {
99127
std::cout << "Syntax:" << std::endl;
100128
std::cout << "simplecpp [options] filename" << std::endl;
@@ -110,29 +138,31 @@ int main(int argc, char **argv)
110138
return 0;
111139
}
112140

113-
dui.removeComments = true;
141+
// TODO: move this logic into simplecpp
142+
std::ifstream f(filename, std::ios::in|std::ios::out);
143+
if (!f.is_open()) {
144+
std::cout << "error: could not open file '" << filename << "'" << std::endl;
145+
return 1;
146+
}
114147

115148
// Perform preprocessing
116149
simplecpp::OutputList outputList;
117150
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;
151+
simplecpp::TokenList outputTokens(files);
152+
{
153+
simplecpp::TokenList *rawtokens;
154+
if (use_istream) {
155+
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
156+
} else {
157+
f.close();
158+
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
124159
}
125-
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
126-
} else {
127-
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
160+
rawtokens->removeComments();
161+
simplecpp::FileDataCache filedata;
162+
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
163+
simplecpp::cleanup(filedata);
164+
delete rawtokens;
128165
}
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;
136166

137167
// Output
138168
if (!quiet) {

0 commit comments

Comments
 (0)