• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kgpg

  • sources
  • kde-4.14
  • kdeutils
  • kgpg
klinebufferedprocess.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Rolf Eike Beer <kde@opensource.sf-tec.de>
3  */
4 
5 /***************************************************************************
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  ***************************************************************************/
13 
14 #include "klinebufferedprocess.h"
15 
16 class KLineBufferedProcessPrivate
17 {
18 public:
19  KLineBufferedProcessPrivate(KLineBufferedProcess *parent);
20 
21 //private slot implementations
22  void _k_receivedStdout();
23  void _k_receivedStderr();
24 
25  QByteArray m_stdoutBuffer;
26  QByteArray m_stderrBuffer;
27  int m_newlineInStdout;
28  int m_newlineInStderr;
29  KLineBufferedProcess * const m_parent;
30  const QByteArray m_lineEnd;
31 };
32 
33 KLineBufferedProcessPrivate::KLineBufferedProcessPrivate(KLineBufferedProcess *parent)
34  : m_newlineInStdout(-1),
35  m_newlineInStderr(-1),
36  m_parent(parent),
37 #ifdef Q_OS_WIN32 //krazy:exclude=cpp
38  m_lineEnd("\r\n")
39 #else
40  m_lineEnd("\n")
41 #endif
42 {
43 }
44 
45 KLineBufferedProcess::KLineBufferedProcess(QObject *parent)
46  : KProcess(parent),
47  d(new KLineBufferedProcessPrivate(this))
48 {
49  connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(_k_receivedStdout()));
50  connect(this, SIGNAL(readyReadStandardError()), this, SLOT(_k_receivedStderr()));
51 }
52 
53 KLineBufferedProcess::~KLineBufferedProcess()
54 {
55  delete d;
56 }
57 
58 void KLineBufferedProcessPrivate::_k_receivedStdout()
59 {
60  QByteArray ndata = m_parent->readAllStandardOutput();
61  int oldBufferSize = m_stdoutBuffer.size();
62  m_stdoutBuffer.append(ndata);
63 
64  if (m_newlineInStdout < 0) {
65  m_newlineInStdout = ndata.indexOf(m_lineEnd);
66  if (m_newlineInStdout >= 0) {
67  m_newlineInStdout += oldBufferSize;
68  emit m_parent->lineReadyStandardOutput();
69  }
70  }
71 }
72 
73 void KLineBufferedProcessPrivate::_k_receivedStderr()
74 {
75  QByteArray ndata = m_parent->readAllStandardError();
76  int oldBufferSize = m_stderrBuffer.size();
77  m_stderrBuffer.append(ndata);
78 
79  if (m_newlineInStderr < 0) {
80  m_newlineInStderr = ndata.indexOf(m_lineEnd);
81  if (m_newlineInStderr >= 0) {
82  m_newlineInStderr += oldBufferSize;
83  emit m_parent->lineReadyStandardError();
84  }
85  }
86 }
87 
88 bool KLineBufferedProcess::readLineStandardOutput(QByteArray *line)
89 {
90  if (d->m_newlineInStdout < 0) {
91  return false;
92  }
93 
94  // don't copy '\n'
95  *line = d->m_stdoutBuffer.left(d->m_newlineInStdout);
96  d->m_stdoutBuffer.remove(0, d->m_newlineInStdout + d->m_lineEnd.length());
97 
98  d->m_newlineInStdout = d->m_stdoutBuffer.indexOf(d->m_lineEnd);
99 
100  return true;
101 }
102 
103 bool KLineBufferedProcess::readLineStandardError(QByteArray *line)
104 {
105  if (d->m_newlineInStderr < 0) {
106  return false;
107  }
108 
109  // don't copy '\n'
110  *line = d->m_stderrBuffer.left(d->m_newlineInStderr);
111  d->m_stderrBuffer.remove(0, d->m_newlineInStderr + d->m_lineEnd.length());
112 
113  d->m_newlineInStderr = d->m_stderrBuffer.indexOf(d->m_lineEnd);
114 
115  return true;
116 }
117 
118 bool KLineBufferedProcess::hasLineStandardOutput() const
119 {
120  return d->m_newlineInStdout >= 0;
121 }
122 
123 bool KLineBufferedProcess::hasLineStandardError() const
124 {
125  return d->m_newlineInStderr >= 0;
126 }
127 
128 #include "klinebufferedprocess.moc"
KProcess
QByteArray
KLineBufferedProcess::~KLineBufferedProcess
~KLineBufferedProcess()
Destructor.
Definition: klinebufferedprocess.cpp:53
KLineBufferedProcess::hasLineStandardOutput
bool hasLineStandardOutput() const
Checks if a line is ready on stdout.
Definition: klinebufferedprocess.cpp:118
QByteArray::indexOf
int indexOf(char ch, int from) const
QObject
KLineBufferedProcess
Read output of a process split into lines.
Definition: klinebufferedprocess.h:56
KLineBufferedProcess::readLineStandardOutput
bool readLineStandardOutput(QByteArray *line)
Reads a line of text (excluding '\n') from stdout.
Definition: klinebufferedprocess.cpp:88
KLineBufferedProcess::KLineBufferedProcess
KLineBufferedProcess(QObject *parent=0)
Constructor.
Definition: klinebufferedprocess.cpp:45
klinebufferedprocess.h
KLineBufferedProcess::readLineStandardError
bool readLineStandardError(QByteArray *line)
Reads a line of text (excluding '\n') from stderr.
Definition: klinebufferedprocess.cpp:103
QByteArray::size
int size() const
KLineBufferedProcess::hasLineStandardError
bool hasLineStandardError() const
Checks if a line is ready on stdout.
Definition: klinebufferedprocess.cpp:123
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgpg

Skip menu "kgpg"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal