Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions code/logic/fossil/io/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
extern "C" {
#endif

/**
* Reads a single character from the input stream.
*
* @param input_stream Pointer to the input stream to read from.
* @return The character read as an unsigned char cast to an int, or EOF on end-of-file or error.
*/
int fossil_io_getc(FILE *input_stream);

/**
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
*
Expand Down Expand Up @@ -171,6 +179,16 @@ namespace fossil {
*/
class Input {
public:
/**
* Reads a single character from the input stream.
*
* @param input_stream Pointer to the input stream to read from.
* @return The character read as an unsigned char cast to an int, or EOF on end-of-file or error.
*/
static int getc(FILE *input_stream) {
return fossil_io_getc(input_stream);
}

/**
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
*
Expand Down
14 changes: 14 additions & 0 deletions code/logic/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ void fossil_io_show_progress(int progress) {
fflush(stdout);
}

int fossil_io_getc(FILE *input_stream) {
if (input_stream == NULL) {
fprintf(stderr, "Error: Invalid input stream.\n");
return EOF;
}

int c = fgetc(input_stream);
if (c == EOF && ferror(input_stream)) {
fprintf(stderr, "Error: Failed to read from input stream.\n");
}

return c;
}

// Function to get a sanitized line of input from a provided stream (or stdin by default)
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
if (buf == NULL || size == 0 || input_stream == NULL) {
Expand Down
17 changes: 9 additions & 8 deletions code/tests/cases/test_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,15 @@ FOSSIL_TEST_CASE(c_test_io_validate_is_length_invalid) {
ASSUME_ITS_FALSE(result);
}

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

// Test with various progress values (0%, 50%, 100%)
fossil_io_show_progress(0);
fossil_io_show_progress(50);
fossil_io_show_progress(100);
int ch = fossil_io_getc(input_stream);
ASSUME_ITS_EQUAL_I32('t', ch);
fclose(input_stream);
}

// * * * * * * * * * * * * * * * * * * * * * * * *
Expand All @@ -240,7 +241,7 @@ FOSSIL_TEST_GROUP(c_input_tests) {
FOSSIL_TEST_ADD(c_input_suite, c_test_io_validate_is_email_invalid);
FOSSIL_TEST_ADD(c_input_suite, c_test_io_validate_is_length_valid);
FOSSIL_TEST_ADD(c_input_suite, c_test_io_validate_is_length_invalid);
FOSSIL_TEST_ADD(c_input_suite, c_test_io_show_progress);
FOSSIL_TEST_ADD(c_input_suite, c_test_io_getc);

FOSSIL_TEST_REGISTER(c_input_suite);
}
12 changes: 12 additions & 0 deletions code/tests/cases/test_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,17 @@ FOSSIL_TEST_CASE(cpp_test_io_input_class_gets_from_stream_whitespace_only) {
fclose(input_stream);
}

FOSSIL_TEST_CASE(cpp_test_io_getc) {
const char *input_data = "test input\n";
FILE *input_stream = tmpfile();
fwrite(input_data, 1, strlen(input_data), input_stream);
rewind(input_stream);

int ch = fossil::io::Input::getc(input_stream);
ASSUME_ITS_EQUAL_I32('t', ch);
fclose(input_stream);
}

// * * * * * * * * * * * * * * * * * * * * * * * *
// * Fossil Logic Test Pool
// * * * * * * * * * * * * * * * * * * * * * * * *
Expand Down Expand Up @@ -369,6 +380,7 @@ FOSSIL_TEST_GROUP(cpp_input_tests) {
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_input_class_gets_utf8_valid);
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_input_class_gets_from_stream_empty);
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_input_class_gets_from_stream_whitespace_only);
FOSSIL_TEST_ADD(cpp_input_suite, cpp_test_io_getc);

FOSSIL_TEST_REGISTER(cpp_input_suite);
}
Loading