Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: polling stdin

Author: Tim Foden

Date: 04:35:44 07/27/03

Go up one level in this thread


On July 27, 2003 at 06:41:28, martin fierz wrote:

>hmm, i'm feeling a bit stupid, perhaps somebody can help me: i want to detect
>when there is input for my engine, but i'm doing something wrong. functions like
>getc() don't return if there is no input, and the function kbhit() which works
>fine in console mode doesn't seem to work when the engine is started by a GUI.
>what function can i use to check whether there is input at stdin?
>
>cheers
>  martin

Here the code I use from GLC:

CConsole::CConsole()
:   m_isatty(true),
    m_hIn(INVALID_HANDLE_VALUE),
    m_hOut(INVALID_HANDLE_VALUE)
{
    m_isatty = _isatty(0) != 0;

    if( !m_isatty )
    {
        m_hIn = GetStdHandle(STD_INPUT_HANDLE);
        m_hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    }
}

CConsole::~CConsole()
{
}

int     CConsole::ReadPolled( int max, char* pBuf )
{
    DWORD   nRead = 0;

    if( m_isatty )
    {
        while( nRead < DWORD(max) && _kbhit() )
            pBuf[nRead++] = _getch();
    }
    else
    {
        DWORD   nAvail = 0;
        if( PeekNamedPipe(m_hIn, 0, 0, 0, &nAvail, 0) == 0 || nAvail == 0 )
            return 0;

        if( nAvail > DWORD(max) )
            nAvail = max;
        ReadFile( m_hIn, pBuf, nAvail, &nRead, 0 );
    }

    return int(nRead);
}



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.