Skip to content

Commit 8df9bcd

Browse files
Merge pull request #34 from dreamer-coding/enforce_fstream_handle
2 parents 99a839d + c7045f3 commit 8df9bcd

File tree

4 files changed

+248
-192
lines changed

4 files changed

+248
-192
lines changed

code/logic/fossil/io/input.h

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
#ifndef FOSSIL_IO_INPUT_H
1515
#define FOSSIL_IO_INPUT_H
1616

17-
#include <stddef.h>
1817
#include <stdarg.h>
19-
#include <stdio.h>
18+
#include "stream.h"
2019

2120
#ifdef __cplusplus
2221
extern "C" {
@@ -28,7 +27,7 @@ extern "C" {
2827
* @param input_stream Pointer to the input stream to read from.
2928
* @return The character read as an unsigned char cast to an int, or EOF on end-of-file or error.
3029
*/
31-
int fossil_io_getc(FILE *input_stream);
30+
int fossil_io_getc(fossil_fstream_t *input_stream);
3231

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

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

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

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

9493
/**
9594
* @brief Validates if the input string is a valid integer.
@@ -185,7 +184,7 @@ namespace fossil {
185184
* @param input_stream Pointer to the input stream to read from.
186185
* @return The character read as an unsigned char cast to an int, or EOF on end-of-file or error.
187186
*/
188-
static int getc(FILE *input_stream) {
187+
static int getc(fossil_fstream_t *input_stream) {
189188
return fossil_io_getc(input_stream);
190189
}
191190

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

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

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

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

359358
/**
360-
* @brief Override the output stream operator to display Input object details.
359+
* @brief Overloads the input stream operator for the Input class.
361360
*
362-
* @param os The output stream where data will be printed.
363-
* @param input The Input object to display.
364-
* @return The modified output stream.
361+
* @param input_stream The input stream to read from.
362+
* @param input The Input object to populate.
363+
* @return The input stream after reading.
365364
*/
366-
friend std::ostream& operator<<(std::ostream& os, const Input& input) {
367-
// Example of what to output: printing the state or some meaningful data
368-
os << "Input Stream Details:\n";
369-
os << " - Max Buffer Size: " << input.max_buffer_size << "\n";
370-
os << " - Input Stream: " << (input.stream ? "Valid Stream" : "Invalid Stream") << "\n";
371-
372-
// Return the output stream
373-
return os;
365+
friend std::istream &operator>>(std::istream &input_stream, Input & /*input*/) {
366+
// Implement the logic for populating the Input object from the input stream.
367+
// This is a placeholder implementation.
368+
char buffer[256];
369+
input_stream.getline(buffer, sizeof(buffer));
370+
// Process the buffer as needed to populate the Input object.
371+
return input_stream;
374372
}
375373

376-
private:
377-
size_t max_buffer_size; // Example private member
378-
FILE* stream; // Example stream (pointer to the input stream, like stdin)
379374
};
380375

381376
}

code/logic/input.c

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ void fossil_io_trim(char *str) {
5353

5454
int fossil_io_display_menu(const char *prompt, const char *choices[], int num_choices) {
5555
if (prompt != NULL) {
56-
printf("%s\n", prompt);
56+
fossil_io_printf("%s\n", prompt);
5757
}
5858

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

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

@@ -74,47 +74,47 @@ int fossil_io_display_menu(const char *prompt, const char *choices[], int num_ch
7474
void fossil_io_show_progress(int progress) {
7575
int width = 50; // Width of the progress bar
7676
int pos = (progress * width) / 100;
77-
printf("[");
77+
fossil_io_printf("[");
7878
for (int i = 0; i < width; i++) {
7979
if (i < pos) {
80-
printf("=");
80+
fossil_io_printf("=");
8181
} else if (i == pos) {
82-
printf(">");
82+
fossil_io_printf(">");
8383
} else {
84-
printf(" ");
84+
fossil_io_printf(" ");
8585
}
8686
}
87-
printf("] %d%%\r", progress);
87+
fossil_io_printf("] %d%%\r", progress);
8888
fflush(stdout);
8989
}
9090

91-
int fossil_io_getc(FILE *input_stream) {
91+
int fossil_io_getc(fossil_fstream_t *input_stream) {
9292
if (input_stream == NULL) {
93-
fprintf(stderr, "Error: Invalid input stream.\n");
93+
fossil_io_fprintf(FOSSIL_STDERR, "Error: Invalid input stream.\n");
9494
return EOF;
9595
}
9696

97-
int c = fgetc(input_stream);
98-
if (c == EOF && ferror(input_stream)) {
99-
fprintf(stderr, "Error: Failed to read from input stream.\n");
97+
int c = fgetc(input_stream->file);
98+
if (c == EOF && ferror(input_stream->file)) {
99+
fossil_io_fprintf(FOSSIL_STDERR, "Error: Failed to read from input stream.\n");
100100
}
101101

102102
return c;
103103
}
104104

105105
// Function to get a sanitized line of input from a provided stream (or stdin by default)
106-
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
106+
char *fossil_io_gets_from_stream(char *buf, size_t size, fossil_fstream_t *input_stream) {
107107
if (buf == NULL || size == 0 || input_stream == NULL) {
108-
fprintf(stderr, "Error: Invalid buffer or stream.\n");
108+
fossil_io_fprintf(FOSSIL_STDERR, "Error: Invalid buffer or stream.\n");
109109
return NULL;
110110
}
111111

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

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

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

139139
// Use fgets to get the input from the stream
140-
if (fgets(buf, size, input_stream) == NULL) {
141-
if (feof(input_stream)) {
140+
if (fgets(buf, size, input_stream->file) == NULL) {
141+
if (feof(input_stream->file)) {
142142
*error_code = EOF;
143143
return NULL; // End of file reached
144144
}
145-
*error_code = ferror(input_stream);
146-
fprintf(stderr, "Error: Failed to read from input stream.\n");
145+
*error_code = ferror(input_stream->file);
146+
fossil_io_fprintf(FOSSIL_STDERR, "Error: Failed to read from input stream.\n");
147147
return NULL;
148148
}
149149

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

170-
int fossil_io_fscanf(FILE *input_stream, const char *format, ...) {
170+
int fossil_io_fscanf(fossil_fstream_t *input_stream, const char *format, ...) {
171171
va_list args;
172172
va_start(args, format);
173-
int result = vfscanf(input_stream, format, args);
173+
int result = vfscanf(input_stream->file, format, args);
174174
va_end(args);
175175
return result;
176176
}
177177

178178
int fossil_io_validate_input_buffer(const char *buf, size_t size) {
179179
if (buf == NULL || size == 0) {
180-
fprintf(stderr, "Error: Invalid buffer or size.\n");
180+
fossil_io_fprintf(FOSSIL_STDERR, "Error: Invalid buffer or size.\n");
181181
return 0;
182182
}
183183
return 1;
184184
}
185185

186-
char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream) {
186+
char *fossil_io_gets_utf8(char *buf, size_t size, fossil_fstream_t *input_stream) {
187187
if (!fossil_io_validate_input_buffer(buf, size)) {
188188
return NULL;
189189
}
190190

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

@@ -273,7 +273,17 @@ int fossil_io_validate_is_email(const char *input) {
273273
return 0;
274274
}
275275

276-
return 1;
276+
// Validate the domain against a list of known mailing services
277+
const char *valid_services[] = {"gmail.com", "yahoo.com", "outlook.com", "hotmail.com", "icloud.com"};
278+
size_t num_services = sizeof(valid_services) / sizeof(valid_services[0]);
279+
280+
for (size_t i = 0; i < num_services; i++) {
281+
if (strcmp(at + 1, valid_services[i]) == 0) {
282+
return 1;
283+
}
284+
}
285+
286+
return 0;
277287
}
278288

279289
int fossil_io_validate_is_length(const char *input, size_t max_length) {

0 commit comments

Comments
 (0)