Skip to content

Commit 5f84ed8

Browse files
committed
fix: resolve minor bugs in Exporter classes and headers
1 parent 0f7c393 commit 5f84ed8

File tree

6 files changed

+43
-72
lines changed

6 files changed

+43
-72
lines changed

src/ConsoleExporter.cpp

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
11
#include "ConsoleExporter.h"
2+
#include "FileExporter.h"
23

34
#include <iostream>
4-
#include <sstream>
55

6-
ConsoleExporter::ConsoleExporter(const Options& options) {
7-
std::streambuf* buf;
8-
std::streambuf* logbuf;
9-
if (options.GetOutputFilename() == "-") {
10-
buf = std::cout.rdbuf();
11-
if (options.GetOutputXml() || options.GetOutputJSON()) {
12-
logbuf = 0;
13-
} else {
14-
logbuf = std::cout.rdbuf();
15-
}
16-
} else {
17-
m_of.open(options.GetOutputFilename().c_str(), std::ios::out | std::ios::binary);
18-
buf = m_of.rdbuf();
19-
logbuf = std::cout.rdbuf();
20-
}
21-
22-
m_out = std::make_shared<std::ostream>(buf);
23-
m_log = std::make_shared<std::ostream>(logbuf);
24-
std::ostream out(buf);
25-
std::ostream log(logbuf);
26-
if (!out) {
27-
std::ostringstream stream;
28-
stream
29-
<< "Error: Can't open file: "
30-
<< options.GetOutputFilename()
31-
<< std::endl;
32-
throw std::runtime_error(stream.str().c_str());
33-
}
6+
ConsoleExporter::ConsoleExporter(const Options& options)
7+
: FileExporter(options, true) {
348
}
359

3610
void ConsoleExporter::LogMessage(const std::string& message) {
37-
(*m_log) << message << std::flush;
11+
Log() << message << std::flush;
3812
}
3913

4014
void ConsoleExporter::WriteHeader() {}
@@ -43,7 +17,7 @@ void ConsoleExporter::WriteFooter(
4317
int files,
4418
long locsTotal,
4519
const ProcessResult& processResult) {
46-
(*m_out)
20+
Out()
4721
<< "Configuration:"
4822
<< std::endl
4923
<< " Number of files: "
@@ -83,17 +57,17 @@ void ConsoleExporter::ReportSeq(
8357
int count,
8458
const SourceFile& source1,
8559
const SourceFile& source2) {
86-
(*m_out)
60+
Out()
8761
<< source1.GetFilename()
8862
<< "(" << source1.GetLine(line1).GetLineNumber() << ")"
8963
<< std::endl;
90-
(*m_out)
64+
Out()
9165
<< source2.GetFilename()
9266
<< "(" << source2.GetLine(line2).GetLineNumber() << ")"
9367
<< std::endl;
9468
for (int j = 0; j < count; j++) {
95-
(*m_out) << source1.GetLine(j + line1).GetLine() << std::endl;
69+
Out() << source1.GetLine(j + line1).GetLine() << std::endl;
9670
}
9771

98-
(*m_out) << std::endl;
72+
Out() << std::endl;
9973
}

src/FileExporter.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,40 @@
33
#include <iostream>
44
#include <sstream>
55

6-
FileExporter::FileExporter(const Options& options) {
7-
std::streambuf* buf;
8-
std::streambuf* logbuf;
9-
if (options.GetOutputFilename() == "-") {
10-
buf = std::cout.rdbuf();
11-
logbuf = 0;
12-
} else {
13-
m_of.open(options.GetOutputFilename().c_str(), std::ios::out | std::ios::binary);
14-
buf = m_of.rdbuf();
15-
logbuf = std::cout.rdbuf();
16-
}
6+
FileExporter::FileExporter(const Options& options, bool verbose) {
7+
std::streambuf* buf;
8+
std::streambuf* logbuf;
9+
if (options.GetOutputFilename() == "-") {
10+
buf = std::cout.rdbuf();
11+
if (verbose) {
12+
logbuf = std::cout.rdbuf();
13+
} else {
14+
logbuf = 0;
15+
}
16+
} else {
17+
m_of.open(options.GetOutputFilename().c_str(), std::ios::out | std::ios::binary);
18+
buf = m_of.rdbuf();
19+
logbuf = std::cout.rdbuf();
20+
}
1721

18-
m_out = std::make_shared<std::ostream>(buf);
19-
m_log = std::make_shared<std::ostream>(logbuf);
20-
std::ostream out(buf);
21-
std::ostream log(logbuf);
22-
if (!out) {
23-
std::ostringstream stream;
24-
stream
25-
<< "Error: Can't open file: "
26-
<< options.GetOutputFilename()
27-
<< std::endl;
28-
throw std::runtime_error(stream.str().c_str());
29-
}
22+
m_out = std::make_shared<std::ostream>(buf);
23+
m_log = std::make_shared<std::ostream>(logbuf);
24+
std::ostream out(buf);
25+
std::ostream log(logbuf);
26+
if (!out) {
27+
std::ostringstream stream;
28+
stream
29+
<< "Error: Can't open file: "
30+
<< options.GetOutputFilename()
31+
<< std::endl;
32+
throw std::runtime_error(stream.str().c_str());
33+
}
3034
}
3135

3236
std::ostream& FileExporter::Log() const {
33-
return *m_log;
37+
return *m_log;
3438
}
3539

3640
std::ostream& FileExporter::Out() const {
37-
return *m_out;
41+
return *m_out;
3842
}

src/JsonExporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <iostream>
55

66
JsonExporter::JsonExporter(const Options& options)
7-
: FileExporter(options) {
7+
: FileExporter(options, false) {
88
}
99

1010
void JsonExporter::LogMessage(const std::string& message) {

src/XmlExporter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
#include "Utils.h"
33

44
#include <iostream>
5-
#include <sstream>
65

76
XmlExporter::XmlExporter(const Options& options)
8-
: FileExporter(options) {
7+
: FileExporter(options, false) {
98

109
}
1110

src/include/ConsoleExporter.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#ifndef _CONSOLEEXPORTER_H_
22
#define _CONSOLEEXPORTER_H_
33

4-
#include "IExporter.h"
5-
6-
#include <fstream>
7-
8-
class ConsoleExporter : public IExporter {
9-
std::ofstream m_of;
10-
std::shared_ptr<std::ostream> m_out;
11-
std::shared_ptr<std::ostream> m_log;
4+
#include "FileExporter.h"
125

6+
class ConsoleExporter : public FileExporter {
137
public:
148
ConsoleExporter(const Options& options);
159
void LogMessage(const std::string& message) override;

src/include/FileExporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class FileExporter : public IExporter {
1515
std::ostream& Out() const;
1616

1717
public:
18-
FileExporter(const Options&);
18+
FileExporter(const Options&, bool);
1919
};
2020

2121
#endif

0 commit comments

Comments
 (0)