-
Notifications
You must be signed in to change notification settings - Fork 0
/
safeinput.c
122 lines (102 loc) · 2.85 KB
/
safeinput.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include "safeinput.h"
// Functions to help with handling user inputs, throughout the program.
bool parseLong(const char *str, long *val)
{
char *temp;
bool rc = true;
errno = 0;
*val = strtol(str, &temp, 0);
if (temp == str || *temp != '\0' ||
((*val == LONG_MIN || *val == LONG_MAX) && errno == ERANGE))
rc = false;
return rc;
}
bool parseFloat(const char *str, float *val)
{
char *temp;
bool rc = true;
errno = 0;
*val = strtof(str, &temp);
if (temp == str || *temp != '\0' )
rc = false;
return rc;
}
bool GetInputInt(char* prompt, int* value)
{
char buff[255];
if (GetInput(prompt, buff, sizeof(buff)) != INPUT_RESULT_OK)
return false;
long l = LONG_MIN;
if(!parseLong(buff,&l)) return false;
*value = l;
return true;
}
bool GetInputFloat(char* prompt, float* value)
{
char buff[255];
if (GetInput(prompt, buff, sizeof(buff)) != INPUT_RESULT_OK)
return false;
float l = -100000000.0f;
if(!parseFloat(buff,&l)) return false;
*value = l;
return true;
}
bool GetInputChar(char* prompt, char* value)
{
char buff[255];
if (GetInput(prompt, buff, sizeof(buff)) != INPUT_RESULT_OK)
return false;
*value = buff[0];
return true;
}
INPUT_RESULT GetInput(char* prompt, char* buff, int maxSize)
{
if (prompt != NULL && strlen(prompt) > 0)
{
printf("%s", prompt);
}
if (fgets(buff, maxSize, stdin) == NULL || (strlen(buff) == 1 && buff[0] == '\n'))
return INPUT_RESULT_NO_INPUT;
// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff) - 1] != '\n') {
int extra = 0;
char ch;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? INPUT_RESULT_TOO_LONG : INPUT_RESULT_OK;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff) - 1] = '\0';
return INPUT_RESULT_OK;
}
// Testing if the user wants an input to be true or false. Use case: accessGranted.
bool GetBooleanInput(const char* prompt) {
char input[10]; // Adjusted size for "yes" or "no" input
while (1) {
printf("%s", prompt);
if (fgets(input, sizeof(input), stdin) == NULL) {
// Handle error or "end of life".
exit(EXIT_FAILURE);
}
// Remove newline character from input
input[strcspn(input, "\n")] = '\0';
// Convert the input to lowercase
for (int i = 0; input[i]; i++) {
input[i] = tolower(input[i]);
}
if (strcmp(input, "yes") == 0) {
return true;
} else if (strcmp(input, "no") == 0) {
return false;
} else {
printf("Invalid input. Please enter 'yes' or 'no'.\n");
}
}
}