Skip to content

Commit 94cf253

Browse files
committed
main.cpp: added options -file, -buf and -ss to specify the TokenList interface [skip ci]
1 parent 10c9681 commit 94cf253

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

main.cpp

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
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+
Istream,
34+
Sstream,
35+
CharBuffer
36+
} toklist_inf = File;
3137
bool fail_on_error = false;
3238
bool linenrs = false;
3339

@@ -86,8 +92,8 @@ int main(int argc, char **argv)
8692
}
8793
dui.includes.emplace_back(std::move(value));
8894
} else if (std::strncmp(arg, "-is",3)==0) {
95+
toklist_inf = Istream;
8996
found = true;
90-
use_istream = true;
9197
}
9298
break;
9399
case 's':
@@ -101,6 +107,10 @@ int main(int argc, char **argv)
101107
}
102108
dui.std = std::move(value);
103109
}
110+
else if (std::strncmp(arg, "-ss",3)==0) {
111+
toklist_inf = Sstream;
112+
found = true;
113+
}
104114
break;
105115
case 'q':
106116
found = true;
@@ -111,13 +121,25 @@ int main(int argc, char **argv)
111121
error_only = true;
112122
break;
113123
case 'f':
114-
found = true;
115-
fail_on_error = true;
124+
if (std::strncmp(arg, "-file",5)==0) {
125+
toklist_inf = File;
126+
found = true;
127+
}
128+
else {
129+
fail_on_error = true;
130+
found = true;
131+
}
116132
break;
117133
case 'l':
118134
linenrs = true;
119135
found = true;
120136
break;
137+
case 'b':
138+
if (std::strncmp(arg, "-buf",4)==0) {
139+
toklist_inf = CharBuffer;
140+
found = true;
141+
}
142+
break;
121143
}
122144
if (!found) {
123145
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
@@ -148,7 +170,10 @@ int main(int argc, char **argv)
148170
std::cout << " -UNAME Undefine NAME." << std::endl;
149171
std::cout << " -std=STD Specify standard." << std::endl;
150172
std::cout << " -q Quiet mode (no output)." << std::endl;
151-
std::cout << " -is Use std::istream interface." << std::endl;
173+
std::cout << " -file Use file TokenList interface (default)." << std::endl;
174+
std::cout << " -is Use std::istream TokenList interface." << std::endl;
175+
std::cout << " -ss Use std::stringstream interface." << std::endl;
176+
std::cout << " -buf Use char buffer TokenList interface." << std::endl;
152177
std::cout << " -e Output errors only." << std::endl;
153178
std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
154179
std::cout << " -l Print lines numbers." << std::endl;
@@ -188,8 +213,21 @@ int main(int argc, char **argv)
188213
simplecpp::TokenList outputTokens(files);
189214
{
190215
simplecpp::TokenList *rawtokens;
191-
if (use_istream) {
192-
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
216+
if (toklist_inf == Istream) {
217+
rawtokens = new simplecpp::TokenList(f,files,filename,&outputList);
218+
}
219+
else if (toklist_inf == Sstream || toklist_inf == CharBuffer) {
220+
std::ostringstream oss;
221+
oss << f.rdbuf();
222+
f.close();
223+
const std::string s = oss.str();
224+
if (toklist_inf == Sstream) {
225+
std::istringstream iss(s);
226+
rawtokens = new simplecpp::TokenList(iss,files,filename,&outputList);
227+
}
228+
else {
229+
rawtokens = new simplecpp::TokenList(s.data(),s.size(),files,filename,&outputList);
230+
}
193231
} else {
194232
f.close();
195233
rawtokens = new simplecpp::TokenList(filename,files,&outputList);

0 commit comments

Comments
 (0)