9
9
#include < cstring>
10
10
#include < fstream>
11
11
#include < iostream>
12
+ #include < sys/stat.h>
12
13
#include < string>
13
14
#include < vector>
14
15
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
+
15
25
int main (int argc, char **argv)
16
26
{
17
27
bool error = false ;
@@ -21,6 +31,7 @@ int main(int argc, char **argv)
21
31
22
32
// Settings..
23
33
simplecpp::DUI dui;
34
+ dui.removeComments = true ;
24
35
bool quiet = false ;
25
36
bool error_only = false ;
26
37
for (int i = 1 ; i < argc; i++) {
@@ -110,29 +121,51 @@ int main(int argc, char **argv)
110
121
return 0 ;
111
122
}
112
123
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 ;
114
150
115
151
// Perform preprocessing
116
152
simplecpp::OutputList outputList;
117
153
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);
124
162
}
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;
128
168
}
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 ;
136
169
137
170
// Output
138
171
if (!quiet) {
0 commit comments