Skip to content

Commit a51e4af

Browse files
swap old input functions with new input functions
1 parent a23c1fc commit a51e4af

File tree

1 file changed

+112
-10
lines changed

1 file changed

+112
-10
lines changed

code/logic/input.c

Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,140 @@
1818
#include <stdlib.h>
1919
#include <string.h>
2020

21+
// Function to trim leading and trailing spaces from a string
22+
void fossil_io_trim(char *str) {
23+
if (str == NULL) return;
24+
25+
// Trim leading spaces
26+
char *start = str;
27+
while (*start == ' ' || *start == '\t' || *start == '\n' || *start == '\r') {
28+
start++;
29+
}
30+
31+
// Move the trimmed string to the beginning
32+
if (start != str) {
33+
memmove(str, start, strlen(start) + 1);
34+
}
35+
36+
// Trim trailing spaces
37+
size_t len = strlen(str);
38+
while (len > 0 && (str[len - 1] == ' ' || str[len - 1] == '\t' || str[len - 1] == '\n' || str[len - 1] == '\r')) {
39+
str[--len] = '\0';
40+
}
41+
}
42+
2143
// Function to get a sanitized line of input from a provided stream (or stdin by default)
2244
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
2345
if (buf == NULL || size == 0 || input_stream == NULL) {
46+
fprintf(stderr, "Error: Invalid buffer or stream\n");
47+
return NULL;
48+
}
49+
50+
// Use fgets to get the input from the stream
51+
if (fgets(buf, size, input_stream) == NULL) {
52+
if (feof(input_stream)) {
53+
return NULL; // End of file reached
54+
}
55+
fprintf(stderr, "Error: Failed to read from input stream\n");
56+
return NULL;
57+
}
58+
59+
// Ensure the string is null-terminated
60+
size_t len = strlen(buf);
61+
if (len > 0 && buf[len - 1] == '\n') {
62+
buf[len - 1] = '\0'; // Remove the newline character
63+
}
64+
65+
// Sanitize the input buffer
66+
fossil_soap_sanitize(buf);
67+
68+
// Trim any leading or trailing whitespace
69+
fossil_io_trim(buf);
70+
71+
return buf;
72+
}
73+
74+
char *fossil_io_gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code) {
75+
if (buf == NULL || size == 0 || input_stream == NULL || error_code == NULL) {
76+
fprintf(stderr, "Error: Invalid buffer, stream, or error code\n");
2477
return NULL;
2578
}
2679

80+
// Use fgets to get the input from the stream
2781
if (fgets(buf, size, input_stream) == NULL) {
82+
if (feof(input_stream)) {
83+
*error_code = EOF;
84+
return NULL; // End of file reached
85+
}
86+
*error_code = ferror(input_stream);
87+
fprintf(stderr, "Error: Failed to read from input stream\n");
2888
return NULL;
2989
}
3090

91+
// Ensure the string is null-terminated
3192
size_t len = strlen(buf);
3293
if (len > 0 && buf[len - 1] == '\n') {
33-
buf[len - 1] = '\0';
94+
buf[len - 1] = '\0'; // Remove the newline character
3495
}
3596

3697
// Sanitize the input buffer
3798
fossil_soap_sanitize(buf);
3899

100+
// Trim any leading or trailing whitespace
101+
fossil_io_trim(buf);
102+
39103
return buf;
40104
}
41105

42-
// Default version that uses stdin
43-
char *fossil_io_gets(char *buf, size_t size) {
44-
return fossil_io_gets_from_stream(buf, size, stdin);
106+
int fossil_io_scanf(const char *format, ...) {
107+
va_list args;
108+
va_start(args, format);
109+
int result = vscanf(format, args);
110+
va_end(args);
111+
return result;
112+
}
113+
114+
int fossil_io_fscanf(FILE *input_stream, const char *format, ...) {
115+
va_list args;
116+
va_start(args, format);
117+
int result = vfscanf(input_stream, format, args);
118+
va_end(args);
119+
return result;
45120
}
46121

47-
// Function to get a sanitized line of input with a dialog prompt
48-
char *fossil_io_gets_with_dialog(char *buf, size_t size, const char *dialog)
49-
{
50-
if (dialog != NULL) {
51-
fossil_io_puts(dialog); // Print the dialog before input
122+
int fossil_io_validate_input_buffer(const char *buf, size_t size) {
123+
if (buf == NULL || size == 0) {
124+
fprintf(stderr, "Error: Invalid buffer or size\n");
125+
return 0;
126+
}
127+
return 1;
128+
}
129+
130+
char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream) {
131+
if (!fossil_io_validate_input_buffer(buf, size)) {
132+
return NULL;
52133
}
53134

54-
return fossil_io_gets(buf, size);
135+
// Use fgets to get the input from the stream
136+
if (fgets(buf, size, input_stream) == NULL) {
137+
if (feof(input_stream)) {
138+
return NULL; // End of file reached
139+
}
140+
fprintf(stderr, "Error: Failed to read from input stream\n");
141+
return NULL;
142+
}
143+
144+
// Ensure the string is null-terminated
145+
size_t len = strlen(buf);
146+
if (len > 0 && buf[len - 1] == '\n') {
147+
buf[len - 1] = '\0'; // Remove the newline character
148+
}
149+
150+
// Sanitize the input buffer
151+
fossil_soap_sanitize(buf);
152+
153+
// Trim any leading or trailing whitespace
154+
fossil_io_trim(buf);
155+
156+
return buf;
55157
}

0 commit comments

Comments
 (0)