1010#include < cstring>
1111#include < fstream>
1212#include < iostream>
13- #include < sys/stat.h >
13+ #include < sstream >
1414#include < string>
15+ #include < sys/stat.h>
1516#include < utility>
1617#include < vector>
1718
@@ -28,7 +29,12 @@ int main(int argc, char **argv)
2829{
2930 bool error = false ;
3031 const char *filename = nullptr ;
31- bool use_istream = false ;
32+ enum {
33+ File,
34+ Fstream,
35+ Sstream,
36+ CharBuffer
37+ } toklist_inf = File;
3238 bool fail_on_error = false ;
3339 bool linenrs = false ;
3440
@@ -86,9 +92,6 @@ int main(int argc, char **argv)
8692 break ;
8793 }
8894 dui.includes .emplace_back (std::move (value));
89- } else if (std::strncmp (arg, " -is" ,3 )==0 ) {
90- found = true ;
91- use_istream = true ;
9295 }
9396 break ;
9497 case ' s' :
@@ -102,6 +105,10 @@ int main(int argc, char **argv)
102105 }
103106 dui.std = std::move (value);
104107 }
108+ else if (std::strncmp (arg, " -ss" ,3 )==0 ) {
109+ toklist_inf = Sstream;
110+ found = true ;
111+ }
105112 break ;
106113 case ' q' :
107114 found = true ;
@@ -112,13 +119,29 @@ int main(int argc, char **argv)
112119 error_only = true ;
113120 break ;
114121 case ' f' :
115- found = true ;
116- fail_on_error = true ;
122+ if (std::strncmp (arg, " -file" ,5 )==0 ) {
123+ toklist_inf = File;
124+ found = true ;
125+ }
126+ else if (std::strncmp (arg, " -fs" ,3 )==0 ) {
127+ toklist_inf = Fstream;
128+ found = true ;
129+ }
130+ else {
131+ fail_on_error = true ;
132+ found = true ;
133+ }
117134 break ;
118135 case ' l' :
119136 linenrs = true ;
120137 found = true ;
121138 break ;
139+ case ' b' :
140+ if (std::strncmp (arg, " -buf" ,4 )==0 ) {
141+ toklist_inf = CharBuffer;
142+ found = true ;
143+ }
144+ break ;
122145 }
123146 if (!found) {
124147 std::cout << " error: option '" << arg << " ' is unknown." << std::endl;
@@ -149,7 +172,10 @@ int main(int argc, char **argv)
149172 std::cout << " -UNAME Undefine NAME." << std::endl;
150173 std::cout << " -std=STD Specify standard." << std::endl;
151174 std::cout << " -q Quiet mode (no output)." << std::endl;
152- std::cout << " -is Use std::istream interface." << std::endl;
175+ std::cout << " -file Consume input as file pointer (default)." << std::endl;
176+ std::cout << " -fs Consume input as file stream." << std::endl;
177+ std::cout << " -ss Consume input as string stream." << std::endl;
178+ std::cout << " -buf Consume input as char buffer." << std::endl;
153179 std::cout << " -e Output errors only." << std::endl;
154180 std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
155181 std::cout << " -l Print lines numbers." << std::endl;
@@ -189,8 +215,21 @@ int main(int argc, char **argv)
189215 simplecpp::TokenList outputTokens (files);
190216 {
191217 simplecpp::TokenList *rawtokens;
192- if (use_istream) {
193- rawtokens = new simplecpp::TokenList (f, files,filename,&outputList);
218+ if (toklist_inf == Fstream) {
219+ rawtokens = new simplecpp::TokenList (f,files,filename,&outputList);
220+ }
221+ else if (toklist_inf == Sstream || toklist_inf == CharBuffer) {
222+ std::ostringstream oss;
223+ oss << f.rdbuf ();
224+ f.close ();
225+ const std::string s = oss.str ();
226+ if (toklist_inf == Sstream) {
227+ std::istringstream iss (s);
228+ rawtokens = new simplecpp::TokenList (iss,files,filename,&outputList);
229+ }
230+ else {
231+ rawtokens = new simplecpp::TokenList ({s.data (),s.size ()},files,filename,&outputList);
232+ }
194233 } else {
195234 f.close ();
196235 rawtokens = new simplecpp::TokenList (filename,files,&outputList);
0 commit comments