summaryrefslogtreecommitdiff
path: root/devtools/syncfrommirror/Redirect.cpp
blob: 15ffa8bace42b752ea53d0bad48e7ccf4ec5d81b (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
//------------------------------------------------------------------------------
// Redirect.cpp : implementation file
//
// Creates a child process that runs a user-specified command and redirects its
// standard output and standard error to a CEdit control.
//
// Written by Matt Brunk ([email protected])
// Copyright (C) 1999 Matt Brunk
// All rights reserved.
//
// This code may be used in compiled form in any way. This file may be
// distributed by any means providing it is not sold for profit without
// the written consent of the author, and providing that this notice and the
// author's name is included in the distribution. If the compiled form of the
// source code in this file is used in a commercial application, an e-mail to
// the author would be appreciated.
//
// Thanks to Dima Shamroni ([email protected]) for providing the essential
// code for this class.
//
// Thanks to Chris Maunder ([email protected]) for the PeekAndPump()
// function (from his CProgressWnd class).
//
// Initial Release Feb 8, 1999
//------------------------------------------------------------------------------


#include "stdafx.h"
#include <string.h>
#include <string>
#include "Redirect.h"

const int BUF_SIZE = 8192;

CRedirect::~CRedirect()
{
}

void CRedirect::Run( LPCTSTR szCommand, CEdit *pEdit, LPCTSTR pszCurrentDirectory )
{
	m_pEdit = pEdit;
	m_bStopped = false;

	if ( !Open( szCommand, pszCurrentDirectory ) )
	{
		AppendText("\nFAILED TO SPAWN PROCESS\n");
		return;
	}

	for (;;)
	{ 
		//------------------------------------------------------------------
		//	If the child process has completed, break out.
		//------------------------------------------------------------------
		if ( !m_hThread || MsgWaitForMultipleObjects( 1, &m_hThread, FALSE, 20, QS_ALLINPUT) == WAIT_OBJECT_0 )
		{
			break;
		}

		//------------------------------------------------------------------
		//	Peek and pump messages.
		//------------------------------------------------------------------
		PeekAndPump();

		//------------------------------------------------------------------
		//	If the user cancelled the operation, terminate the process.
		//------------------------------------------------------------------
		if ( m_bStopped )
		{
			Close();
		}
	}
}


void CRedirect::PeekAndPump()
{
    MSG Msg;
    while (::PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE)) 
    {
        (void)AfxGetApp()->PumpMessage(); //lint !e1924 (warning about C-style cast)
    }
}

void CRedirect::Stop()
{
	m_bStopped = true;
}

void CRedirect::AppendText(LPCTSTR Text)
{
	char *pszCurrent = const_cast<char *>(Text);
	int Length;

	Length = m_pEdit->GetWindowTextLength();
	if ( Length + strlen(Text) > m_pEdit->GetLimitText() )
		m_pEdit->SetLimitText( m_pEdit->GetLimitText() * 2 );

	if ( *pszCurrent == '\n' )
		pszCurrent++;

	while ( pszCurrent && *pszCurrent )
	{
		char *pszLineEnd = strchr( pszCurrent, '\r' );
		if ( pszLineEnd )
			*pszLineEnd = 0;

		Length = m_pEdit->GetWindowTextLength();
		m_pEdit->SetSel(Length, Length);
		m_pEdit->ReplaceSel(pszCurrent);
		//OutputDebugString(pszCurrent);

		if ( pszLineEnd )
		{
			Length = m_pEdit->GetWindowTextLength();
			m_pEdit->SetSel(Length, Length);
			m_pEdit->ReplaceSel("\r\n");
			//OutputDebugString("\r\n");
		}

		m_pEdit->LineScroll( m_pEdit->GetLineCount() );
		PeekAndPump();

		if ( pszLineEnd )
		{
			*pszLineEnd = '\r';
			pszCurrent = pszLineEnd + 1;
			if ( *pszCurrent == '\n' )
				pszCurrent++;
		}
		else
			pszCurrent = NULL;
	};
}


void CRedirect::WriteStdOut(LPCSTR pszOutput)
{
	AppendText( pszOutput );
}

void CRedirect::WriteStdError(LPCSTR pszError)
{
	AppendText( CString(pszError) );
}