Demo: input5 from "C from Dummies" (2007)

Something like this exists in the Windows Kernel I guarantee it
This commit is contained in:
kougyokugentou 2021-10-24 16:16:42 -07:00
parent 587feda020
commit 6953a62612
1 changed files with 99 additions and 0 deletions

99
demos/input5.c Normal file
View File

@ -0,0 +1,99 @@
#include <stdio.h>
#include <conio.h>
#define CR 0x0d //carriage return
#define ESC 0x1b //Escape key
#define TAB 0x09
#define LF 0x0a //line feed
#define BACKSPACE 0x08
#define NULL 0 //Empty character
#define TRUE 1
#define FALSE 0
#define LENGTH 15 //size of the string
int input(char *string, int length);
void main()
{
char string[LENGTH];
printf("What is your name?");
if(!input(string,LENGTH))
printf("Darn glad to meet you, %s!\n",string);
else
printf("\n*** Canceled");
}
int input(char *string, int length)
{
int done = FALSE;
int cancel = FALSE;
int index = 0;
char ch;
string[0] = NULL; //init the buffer
do
{
ch = getch();
/* Check to see whether the buffer is full */
if (index == length)
{
switch(ch)
{
case ESC:
done = TRUE;
cancel = TRUE;
break;
case CR:
break;
case BACKSPACE:
break;
default:
ch = NULL;
break;
}
}
/* Process the keyboard input */
switch(ch)
{
case ESC: //Escape key
done = TRUE;
cancel = TRUE;
break;
case CR: //Enter key
putchar(ch);
putchar(LF);
string[index] = NULL;
done = TRUE;
break;
case BACKSPACE:
if(index==0)
{
break;
}
else
{
putchar(ch);
putchar(' ');
putchar(ch);
index--;
break;
}
case NULL:
break;
default: //display & store
putchar(ch);
index[string] = ch;
index++;
break;
}
}
while(!done);
return(cancel);
}