Skip to content

Commit 0cf6967

Browse files
committed
Add -o stdout option, closes hsutter#183
1 parent 9ac6fe2 commit 0cf6967

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

source/cppfront.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static auto flag_cpp1_filename = std::string{};
110110
static cmdline_processor::register_flag cmd_cpp1_filename(
111111
2,
112112
"output",
113-
"Output filename (default is *.cpp)",
113+
"Output filename, or 'stdout' (default is *.cpp)",
114114
nullptr,
115115
[](std::string const& name) { flag_cpp1_filename = name; }
116116
);
@@ -126,7 +126,8 @@ struct text_with_pos{
126126
class positional_printer
127127
{
128128
// Core information
129-
std::ofstream out = {}; // Cpp1 syntax output file
129+
std::ofstream out_file = {}; // Cpp1 syntax output file
130+
std::ostream* out = {}; // will point to out_file or cout
130131
std::string cpp2_filename = {};
131132
std::string cpp1_filename = {};
132133
std::vector<comment> const* pcomments = {}; // Cpp2 comments data
@@ -193,7 +194,8 @@ class positional_printer
193194
// and update our curr_pos position
194195

195196
// Output the string
196-
out << s;
197+
assert (out);
198+
*out << s;
197199

198200
// Update curr_pos by finding how many line breaks s contained,
199201
// and where the last one was which determines our current colno
@@ -244,7 +246,8 @@ class positional_printer
244246

245247
// Not using print() here because this is transparent to the curr_pos
246248
if (!flag_clean_cpp1) {
247-
out << "#line " << line << " " << std::quoted(cpp2_filename) << "\n";
249+
assert (out);
250+
*out << "#line " << line << " " << std::quoted(cpp2_filename) << "\n";
248251
}
249252
}
250253

@@ -361,17 +364,23 @@ class positional_printer
361364
{
362365
source_has_cpp2 = has_cpp2;
363366
cpp2_filename = cpp2_filename_;
364-
assert (!out.is_open() && !pcomments && "ICE: tried to call .open twice");
367+
assert (!is_open() && !pcomments && "ICE: tried to call .open twice");
365368
cpp1_filename = cpp1_filename_;
366-
out.open(cpp1_filename);
369+
if (cpp1_filename == "stdout") {
370+
out = &std::cout;
371+
}
372+
else {
373+
out_file.open(cpp1_filename);
374+
out = &out_file;
375+
}
367376
pcomments = &comments;
368377
}
369378

370379
auto is_open() -> bool {
371-
if (out.is_open()) {
372-
assert (pcomments && "ICE: if out.is_open, pcomments should also be set");
380+
if (out) {
381+
assert (pcomments && "ICE: if is_open, pcomments should also be set");
373382
}
374-
return out.is_open();
383+
return out;
375384
}
376385

377386

@@ -380,11 +389,13 @@ class positional_printer
380389
//
381390
auto abandon() -> void
382391
{
383-
if (!out.is_open()) {
392+
if (!is_open()) {
384393
return;
385394
}
386-
out.close();
387-
std::remove(cpp1_filename.c_str());
395+
if (out_file.is_open()) {
396+
out_file.close();
397+
std::remove(cpp1_filename.c_str());
398+
}
388399
}
389400

390401

@@ -2863,7 +2874,7 @@ auto main(int argc, char* argv[]) -> int
28632874
}
28642875

28652876
if (cmdline.arguments().empty()) {
2866-
std::cout << "cppfront: error: no input files\n";
2877+
std::cerr << "cppfront: error: no input files\n";
28672878
return EXIT_FAILURE;
28682879
}
28692880

@@ -2893,9 +2904,9 @@ auto main(int argc, char* argv[]) -> int
28932904
}
28942905
// Otherwise, print the errors
28952906
else {
2896-
std::cout << "\n";
2907+
std::cerr << "\n";
28972908
c.print_errors();
2898-
std::cout << "\n";
2909+
std::cerr << "\n";
28992910
exit_status = EXIT_FAILURE;
29002911
}
29012912

0 commit comments

Comments
 (0)