99#include < cstring>
1010#include < fstream>
1111#include < iostream>
12- #include < sys/stat.h >
12+ #include < sstream >
1313#include < string>
14+ #include < sys/stat.h>
1415#include < utility>
1516#include < vector>
1617
@@ -27,7 +28,12 @@ int main(int argc, char **argv)
2728{
2829 bool error = false ;
2930 const char *filename = nullptr ;
30- bool use_istream = false ;
31+ enum {
32+ File,
33+ Fstream,
34+ Sstream,
35+ CharBuffer
36+ } toklist_inf = File;
3137 bool fail_on_error = false ;
3238 bool linenrs = false ;
3339
@@ -85,9 +91,6 @@ int main(int argc, char **argv)
8591 break ;
8692 }
8793 dui.includes .emplace_back (std::move (value));
88- } else if (std::strncmp (arg, " -is" ,3 )==0 ) {
89- found = true ;
90- use_istream = true ;
9194 }
9295 break ;
9396 case ' s' :
@@ -101,6 +104,10 @@ int main(int argc, char **argv)
101104 }
102105 dui.std = std::move (value);
103106 }
107+ else if (std::strncmp (arg, " -ss" ,3 )==0 ) {
108+ toklist_inf = Sstream;
109+ found = true ;
110+ }
104111 break ;
105112 case ' q' :
106113 found = true ;
@@ -111,13 +118,29 @@ int main(int argc, char **argv)
111118 error_only = true ;
112119 break ;
113120 case ' f' :
114- found = true ;
115- fail_on_error = true ;
121+ if (std::strncmp (arg, " -file" ,5 )==0 ) {
122+ toklist_inf = File;
123+ found = true ;
124+ }
125+ else if (std::strncmp (arg, " -fs" ,3 )==0 ) {
126+ toklist_inf = Fstream;
127+ found = true ;
128+ }
129+ else {
130+ fail_on_error = true ;
131+ found = true ;
132+ }
116133 break ;
117134 case ' l' :
118135 linenrs = true ;
119136 found = true ;
120137 break ;
138+ case ' b' :
139+ if (std::strncmp (arg, " -buf" ,4 )==0 ) {
140+ toklist_inf = CharBuffer;
141+ found = true ;
142+ }
143+ break ;
121144 }
122145 if (!found) {
123146 std::cout << " error: option '" << arg << " ' is unknown." << std::endl;
@@ -148,7 +171,10 @@ int main(int argc, char **argv)
148171 std::cout << " -UNAME Undefine NAME." << std::endl;
149172 std::cout << " -std=STD Specify standard." << std::endl;
150173 std::cout << " -q Quiet mode (no output)." << std::endl;
151- std::cout << " -is Use std::istream interface." << std::endl;
174+ std::cout << " -file Consume input as file pointer (default)." << std::endl;
175+ std::cout << " -fs Consume input as file stream." << std::endl;
176+ std::cout << " -ss Consume input as string string." << std::endl;
177+ std::cout << " -buf Consume input as char buffer." << std::endl;
152178 std::cout << " -e Output errors only." << std::endl;
153179 std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
154180 std::cout << " -l Print lines numbers." << std::endl;
@@ -188,8 +214,21 @@ int main(int argc, char **argv)
188214 simplecpp::TokenList outputTokens (files);
189215 {
190216 simplecpp::TokenList *rawtokens;
191- if (use_istream) {
192- rawtokens = new simplecpp::TokenList (f, files,filename,&outputList);
217+ if (toklist_inf == Fstream) {
218+ rawtokens = new simplecpp::TokenList (f,files,filename,&outputList);
219+ }
220+ else if (toklist_inf == Sstream || toklist_inf == CharBuffer) {
221+ std::ostringstream oss;
222+ oss << f.rdbuf ();
223+ f.close ();
224+ const std::string s = oss.str ();
225+ if (toklist_inf == Sstream) {
226+ std::istringstream iss (s);
227+ rawtokens = new simplecpp::TokenList (iss,files,filename,&outputList);
228+ }
229+ else {
230+ rawtokens = new simplecpp::TokenList (s.data (),s.size (),files,filename,&outputList);
231+ }
193232 } else {
194233 f.close ();
195234 rawtokens = new simplecpp::TokenList (filename,files,&outputList);
0 commit comments