🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: microsoft.public.vc.language
Article #9

Re: choiceyn: cut down choice command, use of sleep, kbhit and getch

#9
From: Frank Hickman
Date: Tue, 13 Jan 2004 05:55
137 lines
4852 bytes
Coming along, one thing sticks out though...in your yn() function your still
not handling special keys.  Run it and try pressing F1, you should hear two
beeps.  This is because getch() will return zero or 0xE0 for special keys in
which case getch() must be called again to get the extended key code.

--
Frank

"Hal Styli" <no_spam@all> wrote in message news:4003717f_5@127.0.0.1...
> "Frank Hickman" <fhickman_nosp@m_noblesoft.com> wrote in message
> news:uIWdnZPEO6ofaJ_dRVn-ig@comcast.com...
> <<>>>
> >
> > Just my two cents, of course when asking a question like this your bound
> to
> > get several different variations :)
> >
> <<>>
> Thanks Frank, much more than 2 cents worth, very useful.
> I must admit I expected more takers wich such an inviting subject line!
> I came up with one more variation myself taking your comments into
> account...
> and then added a timeout.
>
> Any comments on the code below?
>
> Hal.
>
> /*
>  * choiceyn.c - heavily cut down clone of Win98's CHOICE.COM.
>  * The only parameter allowed is -t which works like '/t:y,05' does on
>  * CHOICE.COM, otherwise works like 'CHOICE.COM' (i.e. with no arguments)
>  *
>  * No warranty whatsoever given or implied.
>  *
>  * v1.1, Hal Styli, 2004.
>  */
>
> #if !defined( _MSC_VER ) && !defined( __BORLANDC__ )
>  #error This source has not otherkeyeen tested on this platform / with
this
> compiler
> #endif
>
> #include <stdio.h>
> #include <conio.h>   /* for getch() */
> #include <windows.h> /* for Sleep() */
>
> #define WAITTIME 200
> #define TIMEOUT (5*(1000/WAITTIME))
>
> #define PR(s)  fputs(s,stdout)
> #define PRE(s) fputs(s,stderr)
>
> void yn();
> int getargs(int argc, char **argv);
>
> /*---------------------------------------------------*/
> int main(int argc, char **argv )
> {
>     int t=0, otherkey=0;
>
>     t=getargs(argc,argv);
>
>     PR("[Y,N]?");
>
>     if(t>0)
>     {
>        while(t--)
>        {
>           if (kbhit())
>    {
>              yn();
>       otherkey=1;
>       break;
>    }
>
>           Sleep(WAITTIME);
>        }
>
>        if (otherkey)  /* then revert to no timeout.*/
>          for(;;)
>             yn();
>        {
>   PR("Y\n");
>   exit(1);
>        }
>     }
>     else
>        for(;;)
>    yn();
> }
> /*---------------------------------------------------*/
> void yn()
> {
>    switch(getch())
>    {
>       case 'y':  case 'Y':
>         PR("Y\n");
>         exit(1);
>         /*break;*/
>       case 'n': case 'N':
>         PR("N\n");
>         exit(2);
>         /*break;*/
>       default:
>         putchar('\a'); /*beep*/
>         break;
>    }
> }
> /*---------------------------------------------------*/
> int getargs(int argc, char **argv)
> {
>   int t=0;
>
>     if(argc==2)
>     {
>  if( (argv[1][0]=='-') && (argv[1][1]=='t'))
>            t= TIMEOUT;
>   else
>     t= -1;
>     }
>     else if(argc!=1)
>  t= -1;
>
>     if(t==-1)
>     {
>       PRE("\nInvalid argument in choiceyn command.\n");
>       exit(255);
>     }
>     else
>       return t;
> }
> /*---------------------------------------------------*/
>
>
>
Technical Details
Message-ID: <yJOdnaEvW6CsFZ7d4p2dnA@comcast.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newshosting.com!nx02.iad01.newshosting.com!216.196.98.140.MISMATCH!border1.nntp.ash.giganews.com!border2.nntp.sjc.giganews.com!border1.nntp.sjc.giganews.com!nntp.giganews.com!local1.nntp.sjc.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
References: <4002d4eb_5@127.0.0.1> <uIWdnZPEO6ofaJ_dRVn-ig@comcast.com> <4003717f_5@127.0.0.1>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f8d38,6ae23aa1a5869fb6
X-Google-Attributes: gidf8d38,public
X-Google-Thread: f7a51,6ae23aa1a5869fb6
X-Google-Attributes: gidf7a51,public
X-Google-ArrivalTime: 2004-01-12 21:55:11 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newshosting.com!nx02.iad01.newshosting.com!216.196.98.140.MISMATCH!border1.nntp.ash.giganews.com!border2.nntp.sjc.giganews.com!border1.nntp.sjc.giganews.com!nntp.giganews.com!local1.nntp.sjc.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail
NNTP-Posting-Date: Mon, 12 Jan 2004 23:54:57 -0600
From: "Frank Hickman" <fhickman_nosp@m_noblesoft.com>
Newsgroups: microsoft.public.vc.language,borland.public.cpp.language
References: <4002d4eb_5@127.0.0.1> <uIWdnZPEO6ofaJ_dRVn-ig@comcast.com> <4003717f_5@127.0.0.1>
Subject: Re: choiceyn: cut down choice command, use of sleep, kbhit and getch
Date: Tue, 13 Jan 2004 00:55:16 -0500
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <yJOdnaEvW6CsFZ7d4p2dnA@comcast.com>
Lines: 139
NNTP-Posting-Host: 68.55.155.67
X-Trace: sv3-2BtEIYcVFolUJQyM9v6IUsC+3BZubnCHt3/T65cPP9cDSyzkYiQ+WgcEcQ+cwWat6dx09FY1bNoA34V!FUXdv7iuN9BcWLsLFeXmWQfzsx59Y9dNFKLBNAJhZ3QP+i5ky+O7J8qewoHT
X-Complaints-To: abuse@comcast.net
X-DMCA-Complaints-To: dmca@comcast.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.1