Skip to content

Commit 86c5c05

Browse files
Update input.c
1 parent b598212 commit 86c5c05

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

code/logic/input.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,35 @@ static int long_base64_run(const char *s, size_t len, size_t threshold) {
8282
return 0;
8383
}
8484

85+
char *fossil_io_gets_from_stream_ex(char *buf, size_t size, fossil_fstream_t *input_stream, int *error_code) {
86+
if (buf == NULL || size == 0 || input_stream == NULL || error_code == NULL) {
87+
fossil_io_fprintf(FOSSIL_STDERR, "Error: Invalid buffer, stream, or error code.\n");
88+
return NULL;
89+
}
90+
91+
// Use fgets to get the input from the stream
92+
if (fgets(buf, size, input_stream->file) == NULL) {
93+
if (feof(input_stream->file)) {
94+
*error_code = EOF;
95+
return NULL; // End of file reached
96+
}
97+
*error_code = ferror(input_stream->file);
98+
fossil_io_fprintf(FOSSIL_STDERR, "Error: Failed to read from input stream.\n");
99+
return NULL;
100+
}
101+
102+
// Ensure the string is null-terminated
103+
size_t len = strlen(buf);
104+
if (len > 0 && buf[len - 1] == '\n') {
105+
buf[len - 1] = '\0'; // Remove the newline character
106+
}
107+
108+
// Trim any leading or trailing whitespace
109+
fossil_io_trim(buf);
110+
111+
return buf;
112+
}
113+
85114
/* Case-insensitive contains */
86115
static int strncase_contains(const char *haystack, const char *needle, size_t len) {
87116
size_t nlen = strlen(needle);

0 commit comments

Comments
 (0)