Skip to content

Commit f732d4f

Browse files
new getc function in input
1 parent 027275f commit f732d4f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

code/logic/fossil/io/input.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
extern "C" {
2323
#endif
2424

25+
/**
26+
* Reads a single character from the input stream.
27+
*
28+
* @param input_stream Pointer to the input stream to read from.
29+
* @return The character read as an unsigned char cast to an int, or EOF on end-of-file or error.
30+
*/
31+
int fossil_io_getc(FILE *input_stream);
32+
2533
/**
2634
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
2735
*
@@ -171,6 +179,16 @@ namespace fossil {
171179
*/
172180
class Input {
173181
public:
182+
/**
183+
* Reads a single character from the input stream.
184+
*
185+
* @param input_stream Pointer to the input stream to read from.
186+
* @return The character read as an unsigned char cast to an int, or EOF on end-of-file or error.
187+
*/
188+
static int getc(FILE *input_stream) {
189+
return fossil_io_getc(input_stream);
190+
}
191+
174192
/**
175193
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
176194
*

code/logic/input.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ void fossil_io_show_progress(int progress) {
8888
fflush(stdout);
8989
}
9090

91+
int fossil_io_getc(FILE *input_stream) {
92+
if (input_stream == NULL) {
93+
fprintf(stderr, "Error: Invalid input stream.\n");
94+
return EOF;
95+
}
96+
97+
int c = fgetc(input_stream);
98+
if (c == EOF && ferror(input_stream)) {
99+
fprintf(stderr, "Error: Failed to read from input stream.\n");
100+
}
101+
102+
return c;
103+
}
104+
91105
// Function to get a sanitized line of input from a provided stream (or stdin by default)
92106
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
93107
if (buf == NULL || size == 0 || input_stream == NULL) {

0 commit comments

Comments
 (0)