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
49 changes: 22 additions & 27 deletions code/logic/fossil/io/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
#ifndef FOSSIL_IO_INPUT_H
#define FOSSIL_IO_INPUT_H

#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include "stream.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -28,7 +27,7 @@ extern "C" {
* @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);
int fossil_io_getc(fossil_fstream_t *input_stream);

/**
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
Expand All @@ -38,7 +37,7 @@ int fossil_io_getc(FILE *input_stream);
* @param input_stream Pointer to the input stream to read from.
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
*/
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream);
char *fossil_io_gets_from_stream(char *buf, size_t size, fossil_fstream_t *input_stream);

/**
* Reads a line from the input stream with error reporting.
Expand All @@ -49,7 +48,7 @@ char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream);
* @param error_code Pointer to an integer to store the error code (e.g., EOF, input error).
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
*/
char *fossil_io_gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code);
char *fossil_io_gets_from_stream_ex(char *buf, size_t size, fossil_fstream_t *input_stream, int *error_code);

/**
* Reads formatted input from the standard input stream.
Expand All @@ -70,7 +69,7 @@ int fossil_io_scanf(const char *format, ...);
* @return On success, the number of input items successfully matched and assigned is returned.
* On failure, EOF is returned.
*/
int fossil_io_fscanf(FILE *input_stream, const char *format, ...);
int fossil_io_fscanf(fossil_fstream_t *input_stream, const char *format, ...);

/**
* Validates the input buffer and size before reading.
Expand All @@ -89,7 +88,7 @@ int fossil_io_validate_input_buffer(const char *buf, size_t size);
* @param input_stream Pointer to the input stream to read from.
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
*/
char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream);
char *fossil_io_gets_utf8(char *buf, size_t size, fossil_fstream_t *input_stream);

/**
* @brief Validates if the input string is a valid integer.
Expand Down Expand Up @@ -185,7 +184,7 @@ namespace fossil {
* @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) {
static int getc(fossil_fstream_t *input_stream) {
return fossil_io_getc(input_stream);
}

Expand All @@ -197,7 +196,7 @@ namespace fossil {
* @param input_stream Pointer to the input stream to read from.
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
*/
static char *gets_from_stream(char *buf, size_t size, FILE *input_stream) {
static char *gets_from_stream(char *buf, size_t size, fossil_fstream_t *input_stream) {
return fossil_io_gets_from_stream(buf, size, input_stream);
}

Expand All @@ -210,7 +209,7 @@ namespace fossil {
* @param error_code Pointer to an integer to store the error code (e.g., EOF, input error).
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
*/
static char *gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code) {
static char *gets_from_stream_ex(char *buf, size_t size, fossil_fstream_t *input_stream, int *error_code) {
return fossil_io_gets_from_stream_ex(buf, size, input_stream, error_code);
}

Expand All @@ -233,7 +232,7 @@ namespace fossil {
* @param input_stream Pointer to the input stream to read from.
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
*/
static char *gets_utf8(char *buf, size_t size, FILE *input_stream) {
static char *gets_utf8(char *buf, size_t size, fossil_fstream_t *input_stream) {
return fossil_io_gets_utf8(buf, size, input_stream);
}

Expand Down Expand Up @@ -262,7 +261,7 @@ namespace fossil {
* @return On success, the number of input items successfully matched and assigned is returned.
* On failure, EOF is returned.
*/
static int fscanf(FILE *input_stream, const char *format, ...) {
static int fscanf(fossil_fstream_t *input_stream, const char *format, ...) {
va_list args;
va_start(args, format);
int result = fossil_io_fscanf(input_stream, format, args);
Expand Down Expand Up @@ -357,25 +356,21 @@ namespace fossil {
}

/**
* @brief Override the output stream operator to display Input object details.
* @brief Overloads the input stream operator for the Input class.
*
* @param os The output stream where data will be printed.
* @param input The Input object to display.
* @return The modified output stream.
* @param input_stream The input stream to read from.
* @param input The Input object to populate.
* @return The input stream after reading.
*/
friend std::ostream& operator<<(std::ostream& os, const Input& input) {
// Example of what to output: printing the state or some meaningful data
os << "Input Stream Details:\n";
os << " - Max Buffer Size: " << input.max_buffer_size << "\n";
os << " - Input Stream: " << (input.stream ? "Valid Stream" : "Invalid Stream") << "\n";

// Return the output stream
return os;
friend std::istream &operator>>(std::istream &input_stream, Input & /*input*/) {
// Implement the logic for populating the Input object from the input stream.
// This is a placeholder implementation.
char buffer[256];
input_stream.getline(buffer, sizeof(buffer));
// Process the buffer as needed to populate the Input object.
return input_stream;
}

private:
size_t max_buffer_size; // Example private member
FILE* stream; // Example stream (pointer to the input stream, like stdin)
};

}
Expand Down
76 changes: 43 additions & 33 deletions code/logic/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ void fossil_io_trim(char *str) {

int fossil_io_display_menu(const char *prompt, const char *choices[], int num_choices) {
if (prompt != NULL) {
printf("%s\n", prompt);
fossil_io_printf("%s\n", prompt);
}

for (int i = 0; i < num_choices; i++) {
printf("%d. %s\n", i + 1, choices[i]);
fossil_io_printf("%d. %s\n", i + 1, choices[i]);
}

int choice;
do {
printf("Please choose an option (1-%d): ", num_choices);
fossil_io_printf("Please choose an option (1-%d): ", num_choices);
if (fossil_io_scanf("%d", &choice) != 1 || choice < 1 || choice > num_choices) {
printf("Invalid choice. Please try again.\n");
fossil_io_printf("Invalid choice. Please try again.\n");
}
} while (choice < 1 || choice > num_choices);

Expand All @@ -74,47 +74,47 @@ int fossil_io_display_menu(const char *prompt, const char *choices[], int num_ch
void fossil_io_show_progress(int progress) {
int width = 50; // Width of the progress bar
int pos = (progress * width) / 100;
printf("[");
fossil_io_printf("[");
for (int i = 0; i < width; i++) {
if (i < pos) {
printf("=");
fossil_io_printf("=");
} else if (i == pos) {
printf(">");
fossil_io_printf(">");
} else {
printf(" ");
fossil_io_printf(" ");
}
}
printf("] %d%%\r", progress);
fossil_io_printf("] %d%%\r", progress);
fflush(stdout);
}

int fossil_io_getc(FILE *input_stream) {
int fossil_io_getc(fossil_fstream_t *input_stream) {
if (input_stream == NULL) {
fprintf(stderr, "Error: Invalid input stream.\n");
fossil_io_fprintf(FOSSIL_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");
int c = fgetc(input_stream->file);
if (c == EOF && ferror(input_stream->file)) {
fossil_io_fprintf(FOSSIL_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) {
char *fossil_io_gets_from_stream(char *buf, size_t size, fossil_fstream_t *input_stream) {
if (buf == NULL || size == 0 || input_stream == NULL) {
fprintf(stderr, "Error: Invalid buffer or stream.\n");
fossil_io_fprintf(FOSSIL_STDERR, "Error: Invalid buffer or stream.\n");
return NULL;
}

// Use fgets to get the input from the stream
if (fgets(buf, size, input_stream) == NULL) {
if (feof(input_stream)) {
if (fgets(buf, size, input_stream->file) == NULL) {
if (feof(input_stream->file)) {
return NULL; // End of file reached
}
fprintf(stderr, "Error: Failed to read from input stream.\n");
fossil_io_fprintf(FOSSIL_STDERR, "Error: Failed to read from input stream.\n");
return NULL;
}

Expand All @@ -130,20 +130,20 @@ char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
return buf;
}

char *fossil_io_gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code) {
char *fossil_io_gets_from_stream_ex(char *buf, size_t size, fossil_fstream_t *input_stream, int *error_code) {
if (buf == NULL || size == 0 || input_stream == NULL || error_code == NULL) {
fprintf(stderr, "Error: Invalid buffer, stream, or error code.\n");
fossil_io_fprintf(FOSSIL_STDERR, "Error: Invalid buffer, stream, or error code.\n");
return NULL;
}

// Use fgets to get the input from the stream
if (fgets(buf, size, input_stream) == NULL) {
if (feof(input_stream)) {
if (fgets(buf, size, input_stream->file) == NULL) {
if (feof(input_stream->file)) {
*error_code = EOF;
return NULL; // End of file reached
}
*error_code = ferror(input_stream);
fprintf(stderr, "Error: Failed to read from input stream.\n");
*error_code = ferror(input_stream->file);
fossil_io_fprintf(FOSSIL_STDERR, "Error: Failed to read from input stream.\n");
return NULL;
}

Expand All @@ -167,33 +167,33 @@ int fossil_io_scanf(const char *format, ...) {
return result;
}

int fossil_io_fscanf(FILE *input_stream, const char *format, ...) {
int fossil_io_fscanf(fossil_fstream_t *input_stream, const char *format, ...) {
va_list args;
va_start(args, format);
int result = vfscanf(input_stream, format, args);
int result = vfscanf(input_stream->file, format, args);
va_end(args);
return result;
}

int fossil_io_validate_input_buffer(const char *buf, size_t size) {
if (buf == NULL || size == 0) {
fprintf(stderr, "Error: Invalid buffer or size.\n");
fossil_io_fprintf(FOSSIL_STDERR, "Error: Invalid buffer or size.\n");
return 0;
}
return 1;
}

char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream) {
char *fossil_io_gets_utf8(char *buf, size_t size, fossil_fstream_t *input_stream) {
if (!fossil_io_validate_input_buffer(buf, size)) {
return NULL;
}

// Use fgets to get the input from the stream
if (fgets(buf, size, input_stream) == NULL) {
if (feof(input_stream)) {
if (fgets(buf, size, input_stream->file) == NULL) {
if (feof(input_stream->file)) {
return NULL; // End of file reached
}
fprintf(stderr, "Error: Failed to read from input stream.\n");
fossil_io_fprintf(FOSSIL_STDERR, "Error: Failed to read from input stream.\n");
return NULL;
}

Expand Down Expand Up @@ -273,7 +273,17 @@ int fossil_io_validate_is_email(const char *input) {
return 0;
}

return 1;
// Validate the domain against a list of known mailing services
const char *valid_services[] = {"gmail.com", "yahoo.com", "outlook.com", "hotmail.com", "icloud.com"};
size_t num_services = sizeof(valid_services) / sizeof(valid_services[0]);

for (size_t i = 0; i < num_services; i++) {
if (strcmp(at + 1, valid_services[i]) == 0) {
return 1;
}
}

return 0;
}

int fossil_io_validate_is_length(const char *input, size_t max_length) {
Expand Down
Loading