F
Fao, Sean
Hello all,
As stated in another message, it's been a long time since I've done any
C coding and I'm not feeling comfortable that I'm doing this correctly.
Basically, I'd like to verify that my supplied input is alphanumeric.
White space and opening/closing parenthesis are also permitted.
Nothing more, nothing less.
Additionally, despite having read my documentation for scanf(), I'm
uncertain how --if it's even possible-- to have a variable input width.
This isn't so much a big deal; but, it would be nice to only have to
modify my symbolic constant rather than keeping track of two maximum
buffer lengths.
Lastly, the code needs to be portable. I understand that there are
probably better platform-specific methods for flushing the input buffer;
however, I'll have to implement these on an individual basis. Assuming
I have no disguised bugs in the FLUSH_IN() macro that you believe I'm
missing, I'm perfectly content with its current implementation.
On a side note, the input will rarely, if ever, actually be supplied by
a user as this is actually planned to be part of an embedded system
project (robot) I'm building in my free time. Ideally, I'll have some
form of GUI or whatever that automatically sends the proper commands
through the serial port. But this is still a good test to get myself
back in the swing of things.
--- Start Code ---
#include <stdio.h>
#define LINE_LIMIT 80
#define MAX_INPUT_SIZE 60
#define PROMPT "%> "
#define FLUSH_IN(fp) do { \
int ch; \
while((ch = fgetc(fp)) != EOF && ch != '\n'); \
} while(0)
void get_command(void);
int main(void)
{
get_command();
return 0;
}
void get_command(void)
{
int ret;
char input[MAX_INPUT_SIZE];
printf("%s", PROMPT);
while ((ret = scanf("%60[a-z[A-Z[0-9[()[ ]", input)) == EOF || ret == 0)
{
printf("Invalid input...\n");
FLUSH_IN(stdin);
}
printf("%s\n", input);
}
--- End Code ---
Any comments are welcome.
Thank you in advance,
As stated in another message, it's been a long time since I've done any
C coding and I'm not feeling comfortable that I'm doing this correctly.
Basically, I'd like to verify that my supplied input is alphanumeric.
White space and opening/closing parenthesis are also permitted.
Nothing more, nothing less.
Additionally, despite having read my documentation for scanf(), I'm
uncertain how --if it's even possible-- to have a variable input width.
This isn't so much a big deal; but, it would be nice to only have to
modify my symbolic constant rather than keeping track of two maximum
buffer lengths.
Lastly, the code needs to be portable. I understand that there are
probably better platform-specific methods for flushing the input buffer;
however, I'll have to implement these on an individual basis. Assuming
I have no disguised bugs in the FLUSH_IN() macro that you believe I'm
missing, I'm perfectly content with its current implementation.
On a side note, the input will rarely, if ever, actually be supplied by
a user as this is actually planned to be part of an embedded system
project (robot) I'm building in my free time. Ideally, I'll have some
form of GUI or whatever that automatically sends the proper commands
through the serial port. But this is still a good test to get myself
back in the swing of things.
--- Start Code ---
#include <stdio.h>
#define LINE_LIMIT 80
#define MAX_INPUT_SIZE 60
#define PROMPT "%> "
#define FLUSH_IN(fp) do { \
int ch; \
while((ch = fgetc(fp)) != EOF && ch != '\n'); \
} while(0)
void get_command(void);
int main(void)
{
get_command();
return 0;
}
void get_command(void)
{
int ret;
char input[MAX_INPUT_SIZE];
printf("%s", PROMPT);
while ((ret = scanf("%60[a-z[A-Z[0-9[()[ ]", input)) == EOF || ret == 0)
{
printf("Invalid input...\n");
FLUSH_IN(stdin);
}
printf("%s\n", input);
}
--- End Code ---
Any comments are welcome.
Thank you in advance,