Skip to content

Commit 09a2526

Browse files
committed
avoid direct usage of std::cout
1 parent 1678b7d commit 09a2526

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

simplecpp.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <limits>
2929
#include <list>
3030
#include <map>
31+
#include <ostream>
3132
#include <set>
3233
#include <sstream>
3334
#include <stack>
@@ -205,29 +206,29 @@ bool simplecpp::Token::endsWithOneOf(const char c[]) const
205206
return std::strchr(c, string[string.size() - 1U]) != nullptr;
206207
}
207208

208-
void simplecpp::Token::printAll() const
209+
void simplecpp::Token::printAll(std::ostream& ostr) const
209210
{
210211
const Token *tok = this;
211212
while (tok->previous)
212213
tok = tok->previous;
213214
for (; tok; tok = tok->next) {
214215
if (tok->previous) {
215-
std::cout << (sameline(tok, tok->previous) ? ' ' : '\n');
216+
ostr << (sameline(tok, tok->previous) ? ' ' : '\n');
216217
}
217-
std::cout << tok->str();
218+
ostr << tok->str();
218219
}
219-
std::cout << std::endl;
220+
ostr << std::endl;
220221
}
221222

222-
void simplecpp::Token::printOut() const
223+
void simplecpp::Token::printOut(std::ostream& ostr) const
223224
{
224225
for (const Token *tok = this; tok; tok = tok->next) {
225226
if (tok != this) {
226-
std::cout << (sameline(tok, tok->previous) ? ' ' : '\n');
227+
ostr << (sameline(tok, tok->previous) ? ' ' : '\n');
227228
}
228-
std::cout << tok->str();
229+
ostr << tok->str();
229230
}
230-
std::cout << std::endl;
231+
ostr << std::endl;
231232
}
232233

233234
// cppcheck-suppress noConstructor - we call init() in the inherited to initialize the private members
@@ -553,9 +554,9 @@ void simplecpp::TokenList::push_back(Token *tok)
553554
backToken = tok;
554555
}
555556

556-
void simplecpp::TokenList::dump() const
557+
void simplecpp::TokenList::dump(std::ostream& ostr) const
557558
{
558-
std::cout << stringify() << std::endl;
559+
ostr << stringify() << std::endl;
559560
}
560561

561562
std::string simplecpp::TokenList::stringify() const

simplecpp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ namespace simplecpp {
176176
return mExpandedFrom.find(m) != mExpandedFrom.end();
177177
}
178178

179-
void printAll() const;
180-
void printOut() const;
179+
void printAll(std::ostream& ostr) const;
180+
void printOut(std::ostream& ostr) const;
181181
private:
182182
TokenString string;
183183

@@ -235,7 +235,7 @@ namespace simplecpp {
235235
}
236236
void push_back(Token *tok);
237237

238-
void dump() const;
238+
void dump(std::ostream& ostr) const;
239239
std::string stringify() const;
240240

241241
void readfile(Stream &stream, const std::string &filename=std::string(), OutputList *outputList = nullptr);

0 commit comments

Comments
 (0)