Skip to content

Commit 90d0c8c

Browse files
Merge pull request #5 from dreamer-coding/test_validation
Test validation
2 parents b691b9b + 6f194e0 commit 90d0c8c

File tree

5 files changed

+614
-23
lines changed

5 files changed

+614
-23
lines changed

code/logic/fossil/io/input.h

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define FOSSIL_IO_INPUT_H
1616

1717
#include <stddef.h>
18+
#include <stdarg.h>
1819
#include <stdio.h>
1920

2021
#ifdef __cplusplus
@@ -32,27 +33,128 @@ extern "C" {
3233
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream);
3334

3435
/**
35-
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
36+
* Reads a line from the input stream with error reporting.
37+
*
38+
* @param buf Pointer to the buffer where the line will be stored.
39+
* @param size Maximum number of characters to be read, including the null terminator.
40+
* @param input_stream Pointer to the input stream to read from.
41+
* @param error_code Pointer to an integer to store the error code (e.g., EOF, input error).
42+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
43+
*/
44+
char *fossil_io_gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code);
45+
46+
/**
47+
* Reads formatted input from the standard input stream.
3648
*
37-
* @param buf Pointer to the buffer where the line will be stored.
38-
* @param size Maximum number of characters to be read, including the null terminator.
39-
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
49+
* @param format The format string specifying how the input should be interpreted.
50+
* @param ... Additional arguments for storing the input values.
51+
* @return On success, the number of input items successfully matched and assigned is returned.
52+
* On failure, EOF is returned.
4053
*/
41-
char *fossil_io_gets(char *buf, size_t size);
54+
int fossil_io_scanf(const char *format, ...);
4255

4356
/**
44-
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf',
45-
* while displaying a dialog message to the user.
57+
* Reads formatted input from the specified input stream.
4658
*
47-
* @param buf Pointer to the buffer where the line will be stored.
48-
* @param size Maximum number of characters to be read, including the null terminator.
49-
* @param dialog Dialog message to be displayed to the user.
50-
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
59+
* @param input_stream Pointer to the input stream to read from.
60+
* @param format The format string specifying how the input should be interpreted.
61+
* @param ... Additional arguments for storing the input values.
62+
* @return On success, the number of input items successfully matched and assigned is returned.
63+
* On failure, EOF is returned.
64+
*/
65+
int fossil_io_fscanf(FILE *input_stream, const char *format, ...);
66+
67+
/**
68+
* Validates the input buffer and size before reading.
69+
*
70+
* @param buf Pointer to the buffer where the input will be stored.
71+
* @param size Size of the buffer.
72+
* @return 1 if the buffer and size are valid; 0 otherwise.
5173
*/
52-
char *fossil_io_gets_with_dialog(char *buf, size_t size, const char *dialog);
74+
int fossil_io_validate_input_buffer(const char *buf, size_t size);
75+
76+
/**
77+
* Reads a UTF-8 encoded line from the input stream.
78+
*
79+
* @param buf Pointer to the buffer where the line will be stored.
80+
* @param size Maximum number of characters to be read, including the null terminator.
81+
* @param input_stream Pointer to the input stream to read from.
82+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
83+
*/
84+
char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream);
5385

5486
#ifdef __cplusplus
5587
}
88+
89+
/**
90+
* Namespace for the Fossil Logic I/O library.
91+
*/
92+
namespace fossil {
93+
94+
/**
95+
* Namespace for the I/O utilities.
96+
*/
97+
namespace io {
98+
99+
/**
100+
* Class for handling input operations.
101+
*/
102+
class Input {
103+
public:
104+
/**
105+
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
106+
*
107+
* @param buf Pointer to the buffer where the line will be stored.
108+
* @param size Maximum number of characters to be read, including the null terminator.
109+
* @param input_stream Pointer to the input stream to read from.
110+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
111+
*/
112+
static char *gets_from_stream(char *buf, size_t size, FILE *input_stream) {
113+
return fossil_io_gets_from_stream(buf, size, input_stream);
114+
}
115+
116+
/**
117+
* Reads a line from the input stream with error reporting.
118+
*
119+
* @param buf Pointer to the buffer where the line will be stored.
120+
* @param size Maximum number of characters to be read, including the null terminator.
121+
* @param input_stream Pointer to the input stream to read from.
122+
* @param error_code Pointer to an integer to store the error code (e.g., EOF, input error).
123+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
124+
*/
125+
static char *gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code) {
126+
return fossil_io_gets_from_stream_ex(buf, size, input_stream, error_code);
127+
}
128+
129+
/**
130+
* Validates the input buffer and size before reading.
131+
*
132+
* @param buf Pointer to the buffer where the input will be stored.
133+
* @param size Size of the buffer.
134+
* @return 1 if the buffer and size are valid; 0 otherwise.
135+
*/
136+
static int validate_input_buffer(const char *buf, size_t size) {
137+
return fossil_io_validate_input_buffer(buf, size);
138+
}
139+
140+
/**
141+
* Reads a UTF-8 encoded line from the input stream.
142+
*
143+
* @param buf Pointer to the buffer where the line will be stored.
144+
* @param size Maximum number of characters to be read, including the null terminator.
145+
* @param input_stream Pointer to the input stream to read from.
146+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
147+
*/
148+
static char *gets_utf8(char *buf, size_t size, FILE *input_stream) {
149+
return fossil_io_gets_utf8(buf, size, input_stream);
150+
}
151+
152+
};
153+
154+
}
155+
156+
}
157+
56158
#endif
57159

58160
#endif /* FOSSIL_IO_FRAMEWORK_H */

code/logic/input.c

Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,140 @@
1818
#include <stdlib.h>
1919
#include <string.h>
2020

21+
// Function to trim leading and trailing spaces from a string
22+
void fossil_io_trim(char *str) {
23+
if (str == NULL) return;
24+
25+
// Trim leading spaces
26+
char *start = str;
27+
while (*start == ' ' || *start == '\t' || *start == '\n' || *start == '\r') {
28+
start++;
29+
}
30+
31+
// Move the trimmed string to the beginning
32+
if (start != str) {
33+
memmove(str, start, strlen(start) + 1);
34+
}
35+
36+
// Trim trailing spaces
37+
size_t len = strlen(str);
38+
while (len > 0 && (str[len - 1] == ' ' || str[len - 1] == '\t' || str[len - 1] == '\n' || str[len - 1] == '\r')) {
39+
str[--len] = '\0';
40+
}
41+
}
42+
2143
// Function to get a sanitized line of input from a provided stream (or stdin by default)
2244
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
2345
if (buf == NULL || size == 0 || input_stream == NULL) {
46+
fprintf(stderr, "Error: Invalid buffer or stream\n");
47+
return NULL;
48+
}
49+
50+
// Use fgets to get the input from the stream
51+
if (fgets(buf, size, input_stream) == NULL) {
52+
if (feof(input_stream)) {
53+
return NULL; // End of file reached
54+
}
55+
fprintf(stderr, "Error: Failed to read from input stream\n");
56+
return NULL;
57+
}
58+
59+
// Ensure the string is null-terminated
60+
size_t len = strlen(buf);
61+
if (len > 0 && buf[len - 1] == '\n') {
62+
buf[len - 1] = '\0'; // Remove the newline character
63+
}
64+
65+
// Sanitize the input buffer
66+
fossil_soap_sanitize(buf);
67+
68+
// Trim any leading or trailing whitespace
69+
fossil_io_trim(buf);
70+
71+
return buf;
72+
}
73+
74+
char *fossil_io_gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code) {
75+
if (buf == NULL || size == 0 || input_stream == NULL || error_code == NULL) {
76+
fprintf(stderr, "Error: Invalid buffer, stream, or error code\n");
2477
return NULL;
2578
}
2679

80+
// Use fgets to get the input from the stream
2781
if (fgets(buf, size, input_stream) == NULL) {
82+
if (feof(input_stream)) {
83+
*error_code = EOF;
84+
return NULL; // End of file reached
85+
}
86+
*error_code = ferror(input_stream);
87+
fprintf(stderr, "Error: Failed to read from input stream\n");
2888
return NULL;
2989
}
3090

91+
// Ensure the string is null-terminated
3192
size_t len = strlen(buf);
3293
if (len > 0 && buf[len - 1] == '\n') {
33-
buf[len - 1] = '\0';
94+
buf[len - 1] = '\0'; // Remove the newline character
3495
}
3596

3697
// Sanitize the input buffer
3798
fossil_soap_sanitize(buf);
3899

100+
// Trim any leading or trailing whitespace
101+
fossil_io_trim(buf);
102+
39103
return buf;
40104
}
41105

42-
// Default version that uses stdin
43-
char *fossil_io_gets(char *buf, size_t size) {
44-
return fossil_io_gets_from_stream(buf, size, stdin);
106+
int fossil_io_scanf(const char *format, ...) {
107+
va_list args;
108+
va_start(args, format);
109+
int result = vscanf(format, args);
110+
va_end(args);
111+
return result;
112+
}
113+
114+
int fossil_io_fscanf(FILE *input_stream, const char *format, ...) {
115+
va_list args;
116+
va_start(args, format);
117+
int result = vfscanf(input_stream, format, args);
118+
va_end(args);
119+
return result;
45120
}
46121

47-
// Function to get a sanitized line of input with a dialog prompt
48-
char *fossil_io_gets_with_dialog(char *buf, size_t size, const char *dialog)
49-
{
50-
if (dialog != NULL) {
51-
fossil_io_puts(dialog); // Print the dialog before input
122+
int fossil_io_validate_input_buffer(const char *buf, size_t size) {
123+
if (buf == NULL || size == 0) {
124+
fprintf(stderr, "Error: Invalid buffer or size\n");
125+
return 0;
126+
}
127+
return 1;
128+
}
129+
130+
char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream) {
131+
if (!fossil_io_validate_input_buffer(buf, size)) {
132+
return NULL;
52133
}
53134

54-
return fossil_io_gets(buf, size);
135+
// Use fgets to get the input from the stream
136+
if (fgets(buf, size, input_stream) == NULL) {
137+
if (feof(input_stream)) {
138+
return NULL; // End of file reached
139+
}
140+
fprintf(stderr, "Error: Failed to read from input stream\n");
141+
return NULL;
142+
}
143+
144+
// Ensure the string is null-terminated
145+
size_t len = strlen(buf);
146+
if (len > 0 && buf[len - 1] == '\n') {
147+
buf[len - 1] = '\0'; // Remove the newline character
148+
}
149+
150+
// Sanitize the input buffer
151+
fossil_soap_sanitize(buf);
152+
153+
// Trim any leading or trailing whitespace
154+
fossil_io_trim(buf);
155+
156+
return buf;
55157
}

0 commit comments

Comments
 (0)