File: SystemCall.cpp

package info (click to toggle)
bsc 2.27-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,640 kB
  • ctags: 2,610
  • sloc: cpp: 14,100; perl: 114; makefile: 46
file content (173 lines) | stat: -rw-r--r-- 5,883 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/********************************************************************
 * Copyright (C) 2005, 2006 Piotr Pszczolkowski
 *-------------------------------------------------------------------
 * This file is part of BSCommander (Beesoft Commander).
 *
 * BsC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * BsC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BsC; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *******************************************************************/
 
/*------- include files:
-------------------------------------------------------------------*/
#include "SystemCall.h"
#include <qprocess.h>
#include <qtimer.h>
using namespace std;

//*******************************************************************
// SystemCall                                            CONSTRUCTOR
//********************************************************************
SystemCall::SystemCall( QWidget* const in_parent )
: QDialog( in_parent )
, d_process( 0 )
, d_mode( OFF_LINE )
, d_stdout_buffer( "" )
, d_stderr_buffer( "" )
{
	setHidden( TRUE );
}
// end of SystemCall

//*******************************************************************
// ~SystemCall                                            DESTRUCTOR
//*******************************************************************
SystemCall::~SystemCall()
{
	if( d_process ) {
		d_process->kill();
		delproc();
	}
}
// end of ~SystemCall

//*******************************************************************
// run                                                        PUBLIC
//*******************************************************************
bool SystemCall::run( const vector<QString>& in_parameters, const Mode in_mode )
{
	bool retval = FALSE;
	
	d_mode = in_mode;
	d_cout_strings.clear();
	d_cerr_strings.clear();

	d_process = new QProcess( this );
	if( d_process ) {
		connect( d_process, SIGNAL( readyReadStdout() ), this, SLOT( read_stdout()    ));
		connect( d_process, SIGNAL( readyReadStderr() ), this, SLOT( read_stderr()    ));
		connect( d_process, SIGNAL( processExited()   ), this, SLOT( process_exited() ));
		
		d_process->clearArguments();
		
		vector<QString>::const_iterator it = in_parameters.begin();
		while( it != in_parameters.end() ) {
			d_process->addArgument( *it );
			++it;
		}

		retval = d_process->start();
	}
	
	return retval;
}
// end of run

//*******************************************************************
// keyPressEvent                                   PRIVATE inherited
//*******************************************************************
void SystemCall::keyPressEvent( QKeyEvent* e )
{
	if( Qt::Key_Escape == e->key() ) {
		d_process->tryTerminate();
		QTimer::singleShot( 500, d_process, SLOT( kill() ) );
	}
	QDialog::keyPressEvent( e );
}
// end of keyPressEvent

//*******************************************************************
// result                                                     PUBLIC
//-------------------------------------------------------------------
// Za pomoca tej funkcji mozna pobrac wyniki operacji.
// Wyniki sa dwojakiego rodzaju: cout i cerr.
// Funkcje nalezy uzywac dopiero po zakonczeniu calej operacji.
//*******************************************************************
void SystemCall::result( vector<QString>& out_cout_strings, vector<QString>& out_cerr_strings )
{
	out_cout_strings = d_cout_strings;
	out_cerr_strings = d_cerr_strings;
}
// end of result

//*******************************************************************
// read_stdout                                          PRIVATE slot
//*******************************************************************
void SystemCall::read_stdout()
{
	d_stdout_buffer += d_process->readStdout();
	
	if( d_stdout_buffer.endsWith( "\n" ) ) {
		if( ON_LINE == d_mode ) {
			emit stdout_line( d_stdout_buffer );
		}
		else {
			d_cout_strings.push_back( d_stdout_buffer );
		}
		d_stdout_buffer = "";
	}
}
// end of read_stdout

//*******************************************************************
// read_stderr                                          PRIVATE slot
//*******************************************************************
void SystemCall::read_stderr()
{
	d_stderr_buffer += d_process->readStderr();
	
	if( d_stderr_buffer.endsWith( "\n" ) ) {
		if( ON_LINE == d_mode ) {
			emit stderr_line( d_stderr_buffer );
		}
		else {
			d_cerr_strings.push_back( d_stderr_buffer );
		}
		d_stderr_buffer = "";
	}
}
// end of read_stderr

//*******************************************************************
// process_exited                                       PRIVATE slot
//*******************************************************************
void SystemCall::process_exited()
{
	const int status = d_process->exitStatus();
	delproc();
	emit finished( status );
}
// end of process_exited

//*******************************************************************
// delproc                                                   PRIVATE
//*******************************************************************
void SystemCall::delproc()
{
	disconnect( d_process, SIGNAL( readyReadStdout() ), this, SLOT( read_stdout()    ));
	disconnect( d_process, SIGNAL( readyReadStderr() ), this, SLOT( read_stderr()    ));
	disconnect( d_process, SIGNAL( processExited()   ), this, SLOT( process_exited() ));
	delete d_process;
	d_process = 0;
}
// end of delproc