66#define SIMPLECPP_TOKENLIST_ALLOW_PTR 0
77#include " simplecpp.h"
88
9+ #include < cstdint>
910#include < cstdlib>
1011#include < cstring>
1112#include < fstream>
1213#include < iostream>
13- #include < sys/stat.h >
14+ #include < sstream >
1415#include < string>
16+ #include < sys/stat.h>
1517#include < utility>
1618#include < vector>
1719
@@ -28,7 +30,12 @@ int main(int argc, char **argv)
2830{
2931 bool error = false ;
3032 const char *filename = nullptr ;
31- bool use_istream = false ;
33+ enum : std::uint8_t {
34+ File,
35+ Fstream,
36+ Sstream,
37+ CharBuffer
38+ } toklist_inf = File;
3239 bool fail_on_error = false ;
3340 bool linenrs = false ;
3441
@@ -86,9 +93,6 @@ int main(int argc, char **argv)
8693 break ;
8794 }
8895 dui.includes .emplace_back (std::move (value));
89- } else if (std::strcmp (arg, " -is" )==0 ) {
90- found = true ;
91- use_istream = true ;
9296 }
9397 break ;
9498 case ' s' :
@@ -102,6 +106,10 @@ int main(int argc, char **argv)
102106 }
103107 dui.std = std::move (value);
104108 }
109+ else if (std::strcmp (arg, " -ss" )==0 ) {
110+ toklist_inf = Sstream;
111+ found = true ;
112+ }
105113 break ;
106114 case ' q' :
107115 if (std::strcmp (arg, " -q" )==0 ) {
@@ -116,9 +124,17 @@ int main(int argc, char **argv)
116124 }
117125 break ;
118126 case ' f' :
119- if (std::strcmp (arg, " -f" )==0 ) {
127+ if (std::strcmp (arg, " -file" )==0 ) {
128+ toklist_inf = File;
129+ found = true ;
130+ }
131+ else if (std::strcmp (arg, " -fs" )==0 ) {
132+ toklist_inf = Fstream;
120133 found = true ;
134+ }
135+ else if (std::strcmp (arg, " -f" )==0 ) {
121136 fail_on_error = true ;
137+ found = true ;
122138 }
123139 break ;
124140 case ' l' :
@@ -127,6 +143,12 @@ int main(int argc, char **argv)
127143 found = true ;
128144 }
129145 break ;
146+ case ' b' :
147+ if (std::strcmp (arg, " -buf" )==0 ) {
148+ toklist_inf = CharBuffer;
149+ found = true ;
150+ }
151+ break ;
130152 }
131153 if (!found) {
132154 std::cout << " error: option '" << arg << " ' is unknown." << std::endl;
@@ -157,7 +179,10 @@ int main(int argc, char **argv)
157179 std::cout << " -UNAME Undefine NAME." << std::endl;
158180 std::cout << " -std=STD Specify standard." << std::endl;
159181 std::cout << " -q Quiet mode (no output)." << std::endl;
160- std::cout << " -is Use std::istream interface." << std::endl;
182+ std::cout << " -file Consume input as file pointer (default)." << std::endl;
183+ std::cout << " -fs Consume input as file stream." << std::endl;
184+ std::cout << " -ss Consume input as string stream." << std::endl;
185+ std::cout << " -buf Consume input as char buffer." << std::endl;
161186 std::cout << " -e Output errors only." << std::endl;
162187 std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
163188 std::cout << " -l Print lines numbers." << std::endl;
@@ -197,8 +222,21 @@ int main(int argc, char **argv)
197222 simplecpp::TokenList outputTokens (files);
198223 {
199224 simplecpp::TokenList *rawtokens;
200- if (use_istream) {
201- rawtokens = new simplecpp::TokenList (f, files,filename,&outputList);
225+ if (toklist_inf == Fstream) {
226+ rawtokens = new simplecpp::TokenList (f,files,filename,&outputList);
227+ }
228+ else if (toklist_inf == Sstream || toklist_inf == CharBuffer) {
229+ std::ostringstream oss;
230+ oss << f.rdbuf ();
231+ f.close ();
232+ const std::string s = oss.str ();
233+ if (toklist_inf == Sstream) {
234+ std::istringstream iss (s);
235+ rawtokens = new simplecpp::TokenList (iss,files,filename,&outputList);
236+ }
237+ else {
238+ rawtokens = new simplecpp::TokenList ({s.data (),s.size ()},files,filename,&outputList);
239+ }
202240 } else {
203241 f.close ();
204242 rawtokens = new simplecpp::TokenList (filename,files,&outputList);
0 commit comments