/*
Copyright 1997 David Coppit.  Permission to use and modify this code for
research purposes is granted to provided that this copyright statement retained
in all derivative software.
*/

#ifdef WIN32
#include "resource.h"
#include "PromptDialog.h"
#include "StdAfx.h"

#include "winbuff.h"
#include "blah2Doc.h"
#endif // WIN32

void externalCode() {

#ifdef WIN32
	PromptDialog* md = new PromptDialog(AfxGetMainWnd());
	EnableWindow(AfxGetMainWnd()->GetSafeHwnd(),FALSE);
	md->m_displaytext = "Initial output string.\r\n";
	md->m_strEdit1 = ""; // No input default.

	md->Create();

	// Store the old stream handles
	istream *in = &cin;
	ostream *out = &cout;
	ostream *err = &cerr;

	// You can also specify the size of the input buffer you want. (No dire consequences for
	// a small buffer.)
	inWinStream win(md,3);
	cin = win;

	// You can also specify the size of the output buffer you want. (It will dump output as it
	// is filled.) The default is 256
	outWinStream wout(md);
	cout = wout;

	// You can also specify the size of the error buffer you want. (It will dynamically grow
	// as needed.)
	errWinStream werr(3);
	cerr = werr;

	// Tie the input to the output so that pending output will be flushed when input is
	// needed.
	cin.tie(&cout);


#endif // WIN32

/*
Here's where your UNIX-style code starts.
*/

	//output some stuff
	cout << "test" << endl << "one" << endl;
	cout << "two" << endl;

	// wait for input
	char w[256];
	cin >> w;

	// Pop up an error box. We could have also redirected the error to cout above...
	cerr << "Oh no!\n";

#ifdef WIN32
	// Here's how to wait for an OK.
	// It's an unfortunate fact of life that Microsoft's implementation
	// of cout doesn't automatically flush on newlines.
	cout.flush();

    md->WaitForOK();
#endif // WIN32

#ifdef WIN32

	// destroy our window
	md->DestroyWindow();
	EnableWindow(AfxGetMainWnd()->GetSafeHwnd(),TRUE);

	// restore the streams.
	cout = *out;
	cin = *in;
	cerr = *err;

#endif // WIN32

}
