Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Engine programming - standard input

Author: Vladimir Medvedev

Date: 04:18:03 12/03/03

Go up one level in this thread


1. Creating separate thread for input is one possible way. I had implemented it
in some earlier versions of GreKo, and I can send you samples of code.

2. Another popular way is to use stdin polling, something like that:

// GreKo source code sample, file Win32.cpp from version 2.53
// You can download it at http://bearlodge.webservis.ru/chess/greko.html

static int is_pipe;
static HANDLE input_handle;

void InitInput()
{
      //  Call this function at the initialization stage of your program

	DWORD dw;
	input_handle = GetStdHandle(STD_INPUT_HANDLE);
	is_pipe = !GetConsoleMode(input_handle, &dw);
}

int ReadInput (char * s, size_t sz)
{
           if (fgets (s, sz, stdin))
                 return 1;
           return 0;
}

int InputAvailable()
{
	DWORD nchars;

	/* When using Standard C input functions, also check if there
	is anything in the buffer. After a call to such functions,
	the input waiting in the pipe will be copied to the buffer,
	and the call to PeekNamedPipe can indicate no input available.
	Setting stdin to unbuffered was not enough, IIRC */

	if (stdin->_cnt > 0)
		return 1;

	if (is_pipe) {

		/* When running under a GUI, you will end here. */

		if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL))

		/* Something went wrong. Probably the parent program exited.
			Could call exit() here. Returning 1 will make the next call
			to the input function return EOF, where this should be
			catched then. */

			return 1;

		return nchars != 0;
	} else
		return _kbhit(); /* In "text-mode" without GUI */
}



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.