Skip to content

Commit af35acc

Browse files
committed
main.cpp: added option -input=file|fstream|fstream|buffer / removed -is
1 parent ea9d3cb commit af35acc

1 file changed

Lines changed: 47 additions & 8 deletions

File tree

main.cpp

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
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,28 @@ 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) {
96+
}
97+
else if (std::strncmp(arg, "-input=",7)==0) {
9098
found = true;
91-
use_istream = true;
99+
const std::string input = arg + 7;
100+
if (input.empty()) {
101+
std::cout << "error: option -inout with no value." << std::endl;
102+
error = true;
103+
break;
104+
}
105+
if (input == "file") {
106+
toklist_inf = File;
107+
} else if (input == "fstream") {
108+
toklist_inf = Fstream;
109+
} else if (input == "sstream") {
110+
toklist_inf = Sstream;
111+
} else if (input == "buffer") {
112+
toklist_inf = CharBuffer;
113+
} else {
114+
std::cout << "error: unknown input type '" << input << "'." << std::endl;
115+
error = true;
116+
break;
117+
}
92118
}
93119
break;
94120
case 's':
@@ -117,8 +143,8 @@ int main(int argc, char **argv)
117143
break;
118144
case 'f':
119145
if (std::strcmp(arg, "-f")==0) {
120-
found = true;
121146
fail_on_error = true;
147+
found = true;
122148
}
123149
break;
124150
case 'l':
@@ -157,7 +183,7 @@ int main(int argc, char **argv)
157183
std::cout << " -UNAME Undefine NAME." << std::endl;
158184
std::cout << " -std=STD Specify standard." << std::endl;
159185
std::cout << " -q Quiet mode (no output)." << std::endl;
160-
std::cout << " -is Use std::istream interface." << std::endl;
186+
std::cout << " -input=INPUT Specify input type - file (default), fstream, sstream, buffer." << std::endl;
161187
std::cout << " -e Output errors only." << std::endl;
162188
std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
163189
std::cout << " -l Print lines numbers." << std::endl;
@@ -197,8 +223,21 @@ int main(int argc, char **argv)
197223
simplecpp::TokenList outputTokens(files);
198224
{
199225
simplecpp::TokenList *rawtokens;
200-
if (use_istream) {
201-
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
226+
if (toklist_inf == Fstream) {
227+
rawtokens = new simplecpp::TokenList(f,files,filename,&outputList);
228+
}
229+
else if (toklist_inf == Sstream || toklist_inf == CharBuffer) {
230+
std::ostringstream oss;
231+
oss << f.rdbuf();
232+
f.close();
233+
const std::string s = oss.str();
234+
if (toklist_inf == Sstream) {
235+
std::istringstream iss(s);
236+
rawtokens = new simplecpp::TokenList(iss,files,filename,&outputList);
237+
}
238+
else {
239+
rawtokens = new simplecpp::TokenList({s.data(),s.size()},files,filename,&outputList);
240+
}
202241
} else {
203242
f.close();
204243
rawtokens = new simplecpp::TokenList(filename,files,&outputList);

0 commit comments

Comments
 (0)