public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Newbie:getch??
@ 2000-03-16  7:45 Jose Camargo
  2000-03-16 10:17 ` Newbie:getch?? Mo McKinlay
  0 siblings, 1 reply; 4+ messages in thread
From: Jose Camargo @ 2000-03-16  7:45 UTC (permalink / raw)
  To: Joachim; +Cc: gcc

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 5304 bytes --]

Ok, depending on the plataform where you are working
on, you should use the ncurses library or something
like that.  At least, that's the only way I know.

--- Joachim <nuklear@singnet.com.sg> escribió:
> Hi there!
> I'm very new with gcc so please forgive me my stupid
> question!
> 
> I was wondering if there would be a function
> available that reads only
> one character from stdin.
> To be more specific I need something that works the
> same way as:
> kbhit() or...
> getch() or...
> getche() alias Borlands <conio.h>
> 
> I need it to write an input function to handle my
> own keyboard
> definitions. 
> Unfortunatly getchar() is unsuitablee for what I
> want to do. 
> Any glues on what to use?
> 
> Thank's  
> 
> 	Joachim!> /*
> 19. Feb. 2000
> Joachim Bauernberger
> This is a list of features to be implemented for the
> Gas Tracker project.
> 
> Here are a couple of ideas for Gas Tracker that are
> floating around in my head
> and scream to be put to paper 8-)
> 
> 1)	Rewrite the input function in the following way:
> 
> -->	check for keys pressed and accept the following:
>     a) digits
>     b) . use with float
>     c) Y = YES
>     d) N = NO
>     e) H = HELP
>     f) S = SAVE
>     g) Q = QUIT
>     h) C = Calculate
>     i) arrow keys
> 
> Note:
> * In case of a yes or no answer we have to be able
> to accept toupper(y/n)
> * The H key enables us to give guidance for lost
> souls 8-)
> * The S key saves everything to a file.
> * The Q key quits the program.
> 
> * The arrow keys help us in navigating through the
> different locations of the
>   program. This way we can change some of the values
> that we don't like but
>   have already entered earlier.
>   We have to make sure that all calculation will
> start after the C butten is
>   pusehed.
> 
> * This will make extensive use of pointers YIKES!
> 
> 
> 
> */
> 
> #include <stdio.h>
> #include <ctype.h>
> 
> #define CR 0x0d		           //carriage return (enter
> key)
> #define ESC 0x1b               //Escape key
> #define TAB 0x09               //Tab key
> #define LF 0x0a                //Line feed
> #define BACKSPACE 0x08         //Backspace
> #define SAVE 0x13              //Ctrl-S for Save
> #define HELP 0x08              //Ctrl-H for Help
> #define QUIT 0x11              //Ctrl-Q for Quit
> #define NULL 0		           //Empty character
> #define TRUE 1
> #define FALSE 0
> #define LENGTH 10	           //size of the string
> 
> int input(char *string, int length);
> 
> int main()
> {
> 
> 	char string[LENGTH];
> 
>         printf("Enter the text:\n");
>         if (!input(string,LENGTH))
>         printf("You entered: %s!\n",string);
>         else
>         printf("\n***** CANCELED. *****");
>         return 0;
> }
> 
> int input(char *string, int length)
> {
>     	int done = FALSE;
>         int cancel = FALSE;
>         int index = 0;
>         char ch;
> 
>         	string[0] = NULL;	//initialize the buffer
> 
>                 do {
>                 	ch = getc(stdin);      /*we need to
> change this to something that accepts only one
> char!*/
> 
>                         /*check to see whether the
> buffer is full*/
>                         if (index == length) {
>                         	switch (ch) {
>                                 	case CR:
>                                         	break;
>                                         default:
>                                         
> putchar('\a');
>                                         	ch = NULL;
>                                                
> break;
>                                         } //end
> switch
>                         }  //end if
> 
>                 /*process the keyboard input*/
> 
>                 switch(ch) {
>                 	case CR:  //enter key
>                         	putchar(ch);
>                                 putchar(LF);
>                         	string[index] = NULL;
>                                 done = TRUE; //set
> variable to exit while after break
>                                 break; //break out
> of switch
> 
>                         case NULL:
>                         	break;
> 
>                         case BACKSPACE:
>                             if (index == 0) {
> //bounds checking if the index is 0
> 
>                             	break;
>                             }
>                             else
>                         	putchar(ch);
>                                 putchar(' ');
>                                 putchar(ch);
>                                 index--;
> 
>                                 break;
> 
>                         case ESC:
>                                 done = TRUE;
>                                 cancel = TRUE;
>                                 break;
> 
>                         default:
>                         	putchar(ch);
>                                 string[index] = ch;
>                                 index++;
> 
>                                 break;
>                         }  //end switch
>                 } while (!done);   //end do-while
>                 return(cancel);
> } //end
> 

_________________________________________________________
Do You Yahoo!?
Obtenga su dirección de correo-e gratis @yahoo.com
en http://correo.espanol.yahoo.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Newbie:getch??
  2000-03-16  7:45 Newbie:getch?? Jose Camargo
@ 2000-03-16 10:17 ` Mo McKinlay
  0 siblings, 0 replies; 4+ messages in thread
From: Mo McKinlay @ 2000-03-16 10:17 UTC (permalink / raw)
  To: Jose Camargo; +Cc: Joachim, gcc

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1154 bytes --]

# Ok, depending on the plataform where you are working
# on, you should use the ncurses library or something
# like that.  At least, that's the only way I know.
# 
# --- Joachim <nuklear@singnet.com.sg> escribió:
# > Hi there!
# > I'm very new with gcc so please forgive me my stupid
# > question!
# > 
# > I was wondering if there would be a function
# > available that reads only
# > one character from stdin.
# > To be more specific I need something that works the
# > same way as:
# > kbhit() or...
# > getch() or...
# > getche() alias Borlands <conio.h>

I'm currently coordinating development on a project to develop a
Borland C-style conio library, as part of our Synergist project. I can let
you have a copy if you like? It's not complete, but what's implemented
currently should be enough to get you started.

Let me know if this would be any help.

-- 
Mo McKinlay                                T: +44 (0) 709 22 55 05  x1
Chief Software Architect                   F: +44 (0) 709 22 55 05  x3
inter/open                                 E: mmckinlay@labs.interopen.org
A division of Bekon Marketing Limited      W: http://www.interopen.org

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Newbie:getch??
  2000-02-20  7:52 Newbie:getch?? Joachim
@ 2000-02-20 10:07 ` Erik Mouw
  0 siblings, 0 replies; 4+ messages in thread
From: Erik Mouw @ 2000-02-20 10:07 UTC (permalink / raw)
  To: nuklear; +Cc: gcc

On Sun, 20 Feb 2000 15:53:54 +0000, Joachim wrote:
> I'm very new with gcc so please forgive me my stupid question!
> 
> I was wondering if there would be a function available that reads only
> one character from stdin.
> To be more specific I need something that works the same way as:
> kbhit() or...
> getch() or...
> getche() alias Borlands <conio.h>
> 
> I need it to write an input function to handle my own keyboard
> definitions. 
> Unfortunatly getchar() is unsuitablee for what I want to do. 
> Any glues on what to use?

First of all, this is not the correct mailing list. This list about the
development of gcc, not the development with gcc. If you're working on
Linux, consider joining the Linux-C-Programming mailinglist:

  http://mail.i-docs.org/mailman/listinfo/linuxcprogramming

To answer your question: all functions in conio.h are DOS specific (as the
Borland manuals will point out). Your problem is that you are using the
stdin FILE pointer which is buffered, so all read functions won't return
until the complete line is read. Make it unbuffered (man setbuf). You can
also use the S-Lang library, which makes it as simple as using the
function SLang_getkey(). The book "Linux Application Development" by
Michael K. Johnson and Erik W. Troan has a complete chapter about S-Lang.


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2785859  Fax: +31-15-2781843  Email J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Newbie:getch??
@ 2000-02-20  7:52 Joachim
  2000-02-20 10:07 ` Newbie:getch?? Erik Mouw
  0 siblings, 1 reply; 4+ messages in thread
From: Joachim @ 2000-02-20  7:52 UTC (permalink / raw)
  To: gcc

Hi there!
I'm very new with gcc so please forgive me my stupid question!

I was wondering if there would be a function available that reads only
one character from stdin.
To be more specific I need something that works the same way as:
kbhit() or...
getch() or...
getche() alias Borlands <conio.h>

I need it to write an input function to handle my own keyboard
definitions. 
Unfortunatly getchar() is unsuitablee for what I want to do. 
Any glues on what to use?

Thank's  

	Joachim!
/*
19. Feb. 2000
Joachim Bauernberger
This is a list of features to be implemented for the Gas Tracker project.

Here are a couple of ideas for Gas Tracker that are floating around in my head
and scream to be put to paper 8-)

1)	Rewrite the input function in the following way:

-->	check for keys pressed and accept the following:
    a) digits
    b) . use with float
    c) Y = YES
    d) N = NO
    e) H = HELP
    f) S = SAVE
    g) Q = QUIT
    h) C = Calculate
    i) arrow keys

Note:
* In case of a yes or no answer we have to be able to accept toupper(y/n)
* The H key enables us to give guidance for lost souls 8-)
* The S key saves everything to a file.
* The Q key quits the program.

* The arrow keys help us in navigating through the different locations of the
  program. This way we can change some of the values that we don't like but
  have already entered earlier.
  We have to make sure that all calculation will start after the C butten is
  pusehed.

* This will make extensive use of pointers YIKES!



*/

#include <stdio.h>
#include <ctype.h>

#define CR 0x0d		           //carriage return (enter key)
#define ESC 0x1b               //Escape key
#define TAB 0x09               //Tab key
#define LF 0x0a                //Line feed
#define BACKSPACE 0x08         //Backspace
#define SAVE 0x13              //Ctrl-S for Save
#define HELP 0x08              //Ctrl-H for Help
#define QUIT 0x11              //Ctrl-Q for Quit
#define NULL 0		           //Empty character
#define TRUE 1
#define FALSE 0
#define LENGTH 10	           //size of the string

int input(char *string, int length);

int main()
{

	char string[LENGTH];

        printf("Enter the text:\n");
        if (!input(string,LENGTH))
        printf("You entered: %s!\n",string);
        else
        printf("\n***** CANCELED. *****");
        return 0;
}

int input(char *string, int length)
{
    	int done = FALSE;
        int cancel = FALSE;
        int index = 0;
        char ch;

        	string[0] = NULL;	//initialize the buffer

                do {
                	ch = getc(stdin);      /*we need to change this to something that accepts only one char!*/

                        /*check to see whether the buffer is full*/
                        if (index == length) {
                        	switch (ch) {
                                	case CR:
                                        	break;
                                        default:
                                        	putchar('\a');
                                        	ch = NULL;
                                                break;
                                        } //end switch
                        }  //end if

                /*process the keyboard input*/

                switch(ch) {
                	case CR:  //enter key
                        	putchar(ch);
                                putchar(LF);
                        	string[index] = NULL;
                                done = TRUE; //set variable to exit while after break
                                break; //break out of switch

                        case NULL:
                        	break;

                        case BACKSPACE:
                            if (index == 0) { //bounds checking if the index is 0

                            	break;
                            }
                            else
                        	putchar(ch);
                                putchar(' ');
                                putchar(ch);
                                index--;

                                break;

                        case ESC:
                                done = TRUE;
                                cancel = TRUE;
                                break;

                        default:
                        	putchar(ch);
                                string[index] = ch;
                                index++;

                                break;
                        }  //end switch
                } while (!done);   //end do-while
                return(cancel);
} //end

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2000-03-16 10:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-16  7:45 Newbie:getch?? Jose Camargo
2000-03-16 10:17 ` Newbie:getch?? Mo McKinlay
  -- strict thread matches above, loose matches on Subject: below --
2000-02-20  7:52 Newbie:getch?? Joachim
2000-02-20 10:07 ` Newbie:getch?? Erik Mouw

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).