input polling question

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

input polling question

Postby Daniel Shawul » 08 Nov 2004, 14:40

i use the popular function in Beowuluf
for input polling during pondering. Problem is
after i check for input, how to read?
if(bioskey())
read();
my read fuction is a simple fgets function. It works
well in WinBoard GUI. but under a console GUI
it fails,i always get a wrong character[always "D"].
what is the problem here?
daniel
User avatar
Daniel Shawul
 
Posts: 366
Joined: 28 Sep 2004, 09:33
Location: Ethiopia

Re: input polling question

Postby Pallav Nawani » 08 Nov 2004, 18:22

Hi,

I use fgetc. Since I have no Idea what's wrong in your input reading, I'll just post here what I use.

Code: Select all
//==///////////////////////////////////////////////////////////////////
//
// getline()
///   Read a line from an input file
///
/// \param
///   fptr:   The file from which text is to be read.
/// \param
///   buf:   The buffer where input is stored
/// \param
///   bufsize:The size of the buffer.
/// \return
///   1 on success, 0 on failure
///
//==///////////////////////////////////////////////////////////////////
int getline(FILE *fptr, char *buf, int bufsize)
{
   int val;
   int count;

   count = 0;
   do {

      val = fgetc(fptr);
      if(val == '\n') break;
      if(val != EOF && count < bufsize-1) {
    buf[count++] = val;
      }

   }
   while(val != EOF && val != '\0');
   buf[count] = '\0';

   if(val == EOF)
      return 0;
   return 1;
     
}


Hth,
Pallav
[/url]
User avatar
Pallav Nawani
 
Posts: 147
Joined: 26 Sep 2004, 20:00
Location: Dehradun, India

Re: input polling question

Postby Anonymous » 08 Nov 2004, 20:53

Daniel, can you give a short complete program, that shows the failure? I am surprised, that the bioskey stuff works under WB ... The typical method is to use "PeekNamedPipe" when running under a GUI. I use _kbhit() when running in Console mode. For reading I use fgets(), too.

Regards,
Dieter
Anonymous
 

Re: input polling question

Postby Daniel Shawul » 09 Nov 2004, 06:33

i didn't explain the problem well. The BiosKey function uses
PeakNamedPipe for pipes (Winboard GUI) and GetNoofConsoleEvents
for conosol.

int Bioskey(void) {
static int init = 0, pipe;
static HANDLE inh;
DWORD dw;
if (XBoard) {
if (!init) {
init = 1;
inh = GetStdHandle(STD_INPUT_HANDLE);
pipe = !GetConsoleMode(inh, &dw);
if (!pipe) {
SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
FlushConsoleInputBuffer(inh);
}
}

if (pipe) {
if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) return 1;
return dw;
}
else {
GetNumberOfConsoleInputEvents(inh, &dw);
return dw <= 1 ? 0 : dw;
}
}
else return _kbhit();
}

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

the ReadInput funcion doesn't work for console mode.

daniel
User avatar
Daniel Shawul
 
Posts: 366
Joined: 28 Sep 2004, 09:33
Location: Ethiopia

Re: input polling question

Postby Anonymous » 10 Nov 2004, 10:23

Your code is not a complete program. If I make a complete program out of it, it works as expected - no problem at all with the input reading. I tried with XBoard 0 and 1, to test both paths. I suggest you add the stdin->_cnt thing. It is not important for console mode, but you can miss input without it when running under a GUI. I would rename the Bioskey - bioskey was/is a function available under many PC compilers in the library. I have no idea, if the SetConsoleMode stuff is necessary.

Regards,
Dieter

Code: Select all
#include <stdio.h>
#include <windows.h>
#include <conio.h>

int XBoard=1;

int Bioskey(void) {
  static int init = 0, pipe;
  static HANDLE inh;
  DWORD dw;
  if (XBoard) {
    if (!init) {
      init = 1;
      inh = GetStdHandle(STD_INPUT_HANDLE);
      pipe = !GetConsoleMode(inh, &dw);
      if (!pipe) {
        SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
        FlushConsoleInputBuffer(inh);
      }
    }
    if (stdin->_cnt > 0)
      return stdin->_cnt;
    if (pipe) {
      if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) return 1;
        return dw;
    }
    else {
      GetNumberOfConsoleInputEvents(inh, &dw);
      return dw <= 1 ? 0 : dw;
    }
  }
  else return _kbhit();
}

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

int main(void)
{
  char buf[1024];
  while(1)
  {
    if (Bioskey())
    {
      if (!ReadInput(buf, sizeof buf))
        return 0;
      printf("Input %s", buf); /* "\n" from fgets ... */
    }
    else
      printf("no input\n");
    Sleep(1000);
  }
}
Anonymous
 


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 21 guests