Article View: microsoft.public.vc.language
Article #8Re: choiceyn: cut down choice command, use of sleep, kbhit and getch
From: Hal Styli
Date: Tue, 13 Jan 2004 04:18
Date: Tue, 13 Jan 2004 04:18
124 lines
4162 bytes
4162 bytes
"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:
<4003717f_5@127.0.0.1>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!east1.newsfeed.sprint-canada.net!news-out2.spamkiller.net!corp-news!not-for-mailReferences:
<4002d4eb_5@127.0.0.1> <uIWdnZPEO6ofaJ_dRVn-ig@comcast.com>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 20:18:21 PST From: "Hal Styli" <no_spam@all> Newsgroups: microsoft.public.vc.language,borland.public.cpp.language References: <4002d4eb_5@127.0.0.1> <uIWdnZPEO6ofaJ_dRVn-ig@comcast.com> Subject: Re: choiceyn: cut down choice command, use of sleep, kbhit and getch Date: Tue, 13 Jan 2004 04:18:01 -0000 Lines: 127 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2720.3000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2727.1300 Message-ID: <4003717f_5@127.0.0.1> X-Comments: This message was posted through Newsfeed.com X-Comments2: IMPORTANT: Newsfeeds.com does not condone, nor support, spam or any illegal or copyrighted postings. X-Comments3: IMPORTANT: Under NO circumstances will postings containing illegal or copyrighted material through this service be tolerated!! X-Report: Please report illegal or inappropriate use to <abuse@newsfeed.com> You may also use our online abuse reporting from: http://www.newsfeed.com/abuseform.htm X-Abuse-Info: Please be sure to forward a copy of ALL headers, INCLUDING the body (DO NOT SEND ATTACHMENTS) Organization: Newsfeed.com http://www.newsfeed.com 100,000+ UNCENSORED Newsgroups. Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!east1.newsfeed.sprint-canada.net!news-out2.spamkiller.net!corp-news!not-for-mail