Skip to content

Commit b20ad74

Browse files
Merge pull request #33 from dreamer-coding/add_getc
2 parents 31e8148 + f732d4f commit b20ad74

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
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) {

code/tests/cases/test_input.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,15 @@ FOSSIL_TEST_CASE(c_test_io_validate_is_length_invalid) {
208208
ASSUME_ITS_FALSE(result);
209209
}
210210

211-
FOSSIL_TEST_CASE(c_test_io_show_progress) {
212-
// This test assumes the progress bar function doesn't return anything.
213-
// We'll just check that the progress is shown correctly.
211+
FOSSIL_TEST_CASE(c_test_io_getc) {
212+
const char *input_data = "test input\n";
213+
FILE *input_stream = tmpfile();
214+
fwrite(input_data, 1, strlen(input_data), input_stream);
215+
rewind(input_stream);
214216

215-
// Test with various progress values (0%, 50%, 100%)
216-
fossil_io_show_progress(0);
217-
fossil_io_show_progress(50);
218-
fossil_io_show_progress(100);
217+
int ch = fossil_io_getc(input_stream);
218+
ASSUME_ITS_EQUAL_I32('t', ch);
219+
fclose(input_stream);
219220
}
220221

221222
// * * * * * * * * * * * * * * * * * * * * * * * *
@@ -240,7 +241,7 @@ FOSSIL_TEST_GROUP(c_input_tests) {
240241
FOSSIL_TEST_ADD(c_input_suite, c_test_io_validate_is_email_invalid);
241242
FOSSIL_TEST_ADD(c_input_suite, c_test_io_validate_is_length_valid);
242243
FOSSIL_TEST_ADD(c_input_suite, c_test_io_validate_is_length_invalid);
243-
FOSSIL_TEST_ADD(c_input_suite, c_test_io_show_progress);
244+
FOSSIL_TEST_ADD(c_input_suite, c_test_io_getc);
244245

245246
FOSSIL_TEST_REGISTER(c_input_suite);
246247
}

code/tests/cases/test_input.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,17 @@ FOSSIL_TEST_CASE(cpp_test_io_input_class_gets_from_stream_whitespace_only) {
335335
fclose(input_stream);
336336
}
337337

338+
FOSSIL_TEST_CASE(cpp_test_io_getc) {
339+
const char *input_data = "test input\n";
340+
FILE *input_stream = tmpfile();
341+
fwrite(input_data, 1, strlen(input_data), input_stream);
342+
rewind(input_stream);
343+
344+
int ch = fossil::io::Input::getc(input_stream);
345+
ASSUME_ITS_EQUAL_I32('t', ch);
346+
fclose(input_stream);
347+
}
348+
338349
// * * * * * * * * * * * * * * * * * * * * * * * *
339350
// * Fossil Logic Test Pool
340351
// * * * * * * * * * * * * * * * * * * * * * * * *
@@ -369,6 +380,7 @@ FOSSIL_TEST_GROUP(cpp_input_tests) {
369380
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_input_class_gets_utf8_valid);
370381
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_input_class_gets_from_stream_empty);
371382
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_input_class_gets_from_stream_whitespace_only);
383+
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_getc);
372384

373385
FOSSIL_TEST_REGISTER(cpp_input_suite);
374386
}

0 commit comments

Comments
 (0)