g2o
stream_redirect.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 //
4 // g2o is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published
6 // by the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // g2o is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #include "stream_redirect.h"
18 
19 #include <QPlainTextEdit>
20 #include <QString>
21 
22 #include <iostream>
23 using namespace std;
24 
25 StreamRedirect::StreamRedirect(std::ostream &stream, QPlainTextEdit* te):
26  _stream(stream), _te(te)
27 {
28  _old_buf = stream.rdbuf();
29  _stream.rdbuf(this);
30 }
31 
33 {
34  if (!_buffer.empty())
35  xsputn(_buffer.c_str(), _buffer.size());
36  _stream.rdbuf(_old_buf);
37 }
38 
39 std::char_traits<char>::int_type StreamRedirect::overflow(int_type v)
40 {
41  _mutex.lock();
42  if (v == '\n') {
43  _te->appendPlainText(QString::fromLatin1(_buffer.c_str(), _buffer.size()));
44  _buffer.clear();
45  }
46  else
47  _buffer.push_back(v);
48 
49  _mutex.unlock();
50  return v;
51 }
52 
53 std::streamsize StreamRedirect::xsputn(const char *p, std::streamsize n)
54 {
55  _mutex.lock();
56  _buffer.append(p, p + n);
57 
58  std::string::size_type pos = 0;
59  while (1) {
60  pos = _buffer.find('\n');
61  if (pos != std::string::npos) {
62  _te->appendPlainText(QString::fromLatin1(_buffer.c_str(), pos));
63  _buffer.erase(_buffer.begin(), _buffer.begin() + pos + 1);
64  } else
65  break;
66  }
67 
68  _mutex.unlock();
69  return n;
70 }
virtual std::streamsize xsputn(const char *p, std::streamsize n)
std::string _buffer
virtual std::char_traits< char >::int_type overflow(int_type v)
std::streambuf * _old_buf
StreamRedirect(std::ostream &stream, QPlainTextEdit *te)
QPlainTextEdit * _te
std::ostream & _stream
std::char_traits< char >::int_type int_type