Edits to more closely match standard cgets
This commit is contained in:
@@ -118,10 +118,11 @@ char *cgets (char *buffer);
|
|||||||
** - call cgets
|
** - call cgets
|
||||||
** - buffer[1] will have the number of characters read
|
** - buffer[1] will have the number of characters read
|
||||||
** - the actual string starts at buffer + 2
|
** - the actual string starts at buffer + 2
|
||||||
** - trailing CRLF are removed
|
|
||||||
** - terminating \0 is appended
|
** - terminating \0 is appended
|
||||||
** - therefore the maximum number of characters which can be read is the size
|
** - therefore the maximum number of characters which can be read is the size
|
||||||
** of the buffer - 3!
|
** of the buffer - 3!
|
||||||
|
** - note: CR/LF are NOT echoed, typically a following call to cputs or
|
||||||
|
** cprintf will need "\r\n" prepended - "standard" behavior
|
||||||
**
|
**
|
||||||
** param: buffer - where to save the input
|
** param: buffer - where to save the input
|
||||||
** return: buffer + 2 (i.e. start of the string) if successful, NULL otherwise
|
** return: buffer + 2 (i.e. start of the string) if successful, NULL otherwise
|
||||||
|
|||||||
@@ -6,12 +6,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
|
||||||
#include <conio.h>
|
#include <conio.h>
|
||||||
|
#include <string.h>
|
||||||
#ifndef CRLF
|
#include <ctype.h>
|
||||||
#define CRLF "\r\n"
|
|
||||||
#endif /* CRLF */
|
|
||||||
|
|
||||||
enum {CGETS_SIZE, CGETS_READ, CGETS_DATA, CGETS_HDR_LEN = CGETS_DATA};
|
enum {CGETS_SIZE, CGETS_READ, CGETS_DATA, CGETS_HDR_LEN = CGETS_DATA};
|
||||||
|
|
||||||
@@ -25,32 +22,29 @@ char *cgets (char *buffer)
|
|||||||
** - call cgets
|
** - call cgets
|
||||||
** - buffer[1] will have the number of characters read
|
** - buffer[1] will have the number of characters read
|
||||||
** - the actual string starts at buffer + 2
|
** - the actual string starts at buffer + 2
|
||||||
** - trailing CRLF are removed
|
|
||||||
** - terminating \0 is appended
|
** - terminating \0 is appended
|
||||||
** - therefore the maximum number of characters which can be read is the size
|
** - therefore the maximum number of characters which can be read is the size
|
||||||
** of the buffer - 3!
|
** of the buffer - 3!
|
||||||
|
** - note: CR/LF are NOT echoed, typically a following call to cputs or
|
||||||
|
** cprintf will need "\r\n" prepended - this is standard behavior!
|
||||||
**
|
**
|
||||||
** param: buffer - where to save the input
|
** param: buffer - where to save the input
|
||||||
** return: buffer + 2 (or start of the string) if successful, NULL otherwise
|
** return: buffer + 2 (i.e. start of the string) or NULL if buffer is NULL
|
||||||
** see: cgetsx for equivalent functionality but with a saner interface!
|
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
/* Default to NULL */
|
/* Return buffer + 2 or NULL if buffer is NULL */
|
||||||
char *result = NULL;
|
char *result = buffer? buffer + CGETS_HDR_LEN: NULL;
|
||||||
|
|
||||||
if (buffer && buffer[CGETS_SIZE]) {
|
if (result) {
|
||||||
/* Initialize just in case the caller didn't! */
|
/* Initialize just in case the caller didn't! */
|
||||||
buffer[CGETS_READ] = 0;
|
buffer[CGETS_READ] = 0;
|
||||||
buffer[CGETS_DATA] = '\0';
|
buffer[CGETS_DATA] = '\0';
|
||||||
|
|
||||||
/* Call cgetsx to do the real work */
|
/* Call cgetsx to do the real work, ignore the result! */
|
||||||
result = cgetsx (buffer + CGETS_HDR_LEN, (unsigned char)buffer[CGETS_SIZE]);
|
cgetsx (result, (unsigned char)buffer[CGETS_SIZE]);
|
||||||
|
|
||||||
/* Trim trailing CRLF and set how many characters were read */
|
/* Set how many characters were read */
|
||||||
if (result) {
|
buffer[CGETS_READ] = (unsigned char)strlen (result);
|
||||||
result[strcspn (result, CRLF)] = '\0';
|
|
||||||
buffer[CGETS_READ] = (unsigned char)strlen (result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Done */
|
/* Done */
|
||||||
@@ -61,8 +55,8 @@ static char *cgetsx (char *buffer, int size)
|
|||||||
/* Like fgets but specifically for the console. Stops when CR/LF or size - 1
|
/* Like fgets but specifically for the console. Stops when CR/LF or size - 1
|
||||||
** characters are read. Will append a terminating \0, so at most size - 1
|
** characters are read. Will append a terminating \0, so at most size - 1
|
||||||
** characters can be read. Note that this function could be made public and
|
** characters can be read. Note that this function could be made public and
|
||||||
** have features like cursor vs no-cursor, and/or echo vs echo-pwd vs no-echo
|
** have features added like cursor vs no-cursor, echo vs echo-pwd vs no-echo,
|
||||||
** added to extend the functionality.
|
** or CR/LF handling to extend the functionality.
|
||||||
**
|
**
|
||||||
** param: buffer - where to save the input
|
** param: buffer - where to save the input
|
||||||
** param: size - the size of buffer
|
** param: size - the size of buffer
|
||||||
@@ -91,27 +85,25 @@ static char *cgetsx (char *buffer, int size)
|
|||||||
x = wherex ();
|
x = wherex ();
|
||||||
y = x? y: y - 1;
|
y = x? y: y - 1;
|
||||||
x = x? x - 1: w;
|
x = x? x - 1: w;
|
||||||
/* Clear the character and the cursor */
|
/* Clear the character */
|
||||||
gotoxy (x, y);
|
gotoxy (x, y);
|
||||||
cputs (" ");
|
cputc (' ');
|
||||||
gotoxy (x, y);
|
gotoxy (x, y);
|
||||||
}
|
}
|
||||||
/* Handle CRLF */
|
|
||||||
} else if (strchr (CRLF, c)) {
|
|
||||||
/* Clear the cursor and advance to the next line */
|
|
||||||
cputs (" " CRLF);
|
|
||||||
buffer[i] = c;
|
|
||||||
buffer[++i] = '\0';
|
|
||||||
break;
|
|
||||||
/* Handle regular characters */
|
/* Handle regular characters */
|
||||||
} else {
|
} else if (isprint (c)) {
|
||||||
cputc (c);
|
cputc (c);
|
||||||
buffer[i] = c;
|
buffer[i] = c;
|
||||||
buffer[++i] = '\0';
|
buffer[++i] = '\0';
|
||||||
|
/* Handle CR/LF */
|
||||||
|
} else if (c == '\r' || c == '\n') {
|
||||||
|
buffer[i] = '\0';
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cursor (0);
|
cursor (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Done */
|
||||||
return (i > 0)? buffer: NULL;
|
return (i > 0)? buffer: NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user