|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
[C] svuotare stdin
Argomento già trattato sicuramente, ma non riesco a trovare un modo per svuotare lo stdin.
Ho fatto una funzione che non aspetta l'invio (come il getch della Borland) ma se premo per sbaglio 2 volte un tasto, alla prossima getch mi prende il carattere nel buffer. Io usavo fflush(stdin) che, giustamente, non funziona più sull'input (come da specifiche). Quindi ? che posso fare ?
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Che pirla! avevo già risolto il problema qualche anno fa ma mi ero dimenticato Vabbè, se a qualcuno serve posto la soluzione (che non è breve).
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Allora, visto che sto lavorando in C++ ho "tradotto" velocemente il codice in C++ (in pratica ho fatto una classe all static
Header file: Codice:
#ifndef KB_UTILS_H_
#define KB_UTILS_H_
namespace AutoTesterSpace
{
#define IOERROR (-1)
#define ESC 0X1B
#define TIMEOUT_SEC 0
#define TIMEOUT_USEC 0
class KB_utils
{
public:
static int getch ();
private:
static int KbdHit ();
static int TimeOut ();
static int no_block;
};
}
#endif /*KB_UTILS_H_*/
Codice:
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <sys/select.h>
#include "KB_utils.h"
using namespace AutoTesterSpace;
////////////////////////////////////////////////////////////////////
int KB_utils::no_block = 0;
/** \brief
*
* \post
*/
int KB_utils::getch()
{
struct termios org_opts,
new_opts;
int res = 0,
c = 0;
//----- store old settings -----------
res = tcgetattr(STDIN_FILENO, &org_opts);
assert(res == 0);
//---- set new terminal parms --------
memcpy( &new_opts, &org_opts, sizeof(new_opts) );
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
while( KbdHit() )
; // QUÌ SVUOTA LO STDIN !!!
c = getchar();
//------ restore old settings ---------
res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res == 0);
return(c);
}
////////////////////////////////////////////////////////////////////
/** \brief
*
* \post
*/
int KB_utils::KbdHit()
{
struct termios oattr,
attr ;
int n ,
retval;
unsigned char ch;
if ( (tcgetattr)(STDIN_FILENO, &oattr) < 0 )
return IOERROR;
attr = oattr;
attr.c_lflag &= ~(ICANON | ECHO);
attr.c_cc[ VMIN] = 1;
attr.c_cc[VTIME] = 0;
if ( (tcsetattr)(STDIN_FILENO, TCSAFLUSH, &attr) < 0 )
return IOERROR;
else if ( !(isatty)(STDIN_FILENO) )
return IOERROR;
no_block = 1;
if ( (n = TimeOut()) < 0 )
return IOERROR;
retval = n;
if (retval)
{
if ( (read)(STDIN_FILENO, &ch, 1) != 1 )
return IOERROR;
if ( ch == ESC )
{
if ( (n = TimeOut()) < 0 )
return IOERROR;
if (n)
{
do
{
if ( (read)(STDIN_FILENO, &ch, 1) != 1 )
return IOERROR;
if ( (n = TimeOut()) < 0 )
return IOERROR;
} while(n);
}
}
}
if ( (tcsetattr)(STDIN_FILENO, TCSAFLUSH, &oattr) < 0 )
return IOERROR;
return retval;
}
////////////////////////////////////////////////////////////////////
/** \brief
*
* \post
*/
int KB_utils::TimeOut()
{
struct timeval tv;
fd_set readfds;
int n;
if ( !(isatty)(STDIN_FILENO) )
return IOERROR;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO,&readfds);
tv.tv_sec = TIMEOUT_SEC;
tv.tv_usec = TIMEOUT_USEC + no_block;
if ( (n = (select)(1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv)) < 0 )
return IOERROR;
return n;
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
Codice:
int res = KB_utils::getch(); Codice:
while( KbdHit() ) ;
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
#5 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Appena ho tempo spulcio tutto con calma per capire bene come funziona, anche se ho appena dato una letta veloce. Grazie per lo spirito di condivisione
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
|
#6 | |
|
Senior Member
Iscritto dal: May 2006
Città: Wursteland
Messaggi: 1749
|
Quote:
A buon rendere ![]() PS: ma ci si può dimenticare una cosa del genere ??? dopo 2 ore di googlate ed un 3d aperto mi son ritrovato a parlarne con un collega e li mi è venuto in mente che lo avevo già risolto
__________________
Nintendo WIII 4d Turbo Intercooler - Sestium X 666 99,312 GHz - 6.984 Ram Σ(9999) MHz - HDD SATA 97e^(10) bytes 93³ rpm - ATI biberon X900z ∞Mb - Win Eight SP (1 > yours) 16 Valve |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:57.





















