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
@@ -21,6 +22,7 @@ int main(int argc, char **argv)
21
22
22
23
// Settings..
23
24
simplecpp::DUI dui;
25
+ dui.removeComments = true ;
24
26
bool quiet = false ;
25
27
bool error_only = false ;
26
28
for (int i = 1 ; i < argc; i++) {
@@ -95,6 +97,32 @@ int main(int argc, char **argv)
95
97
return 1 ;
96
98
}
97
99
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
+
98
126
if (!filename) {
99
127
std::cout << " Syntax:" << std::endl;
100
128
std::cout << " simplecpp [options] filename" << std::endl;
@@ -110,29 +138,31 @@ int main(int argc, char **argv)
110
138
return 0 ;
111
139
}
112
140
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
+ }
114
147
115
148
// Perform preprocessing
116
149
simplecpp::OutputList outputList;
117
150
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);
124
159
}
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;
128
165
}
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
166
137
167
// Output
138
168
if (!quiet) {
0 commit comments